no message

This commit is contained in:
kuaifan 2025-05-05 08:25:21 +08:00
parent cc9f346d49
commit 23a68370b4
3 changed files with 46 additions and 45 deletions

View File

@ -22,6 +22,6 @@ class AppsController extends AbstractController
public function down() public function down()
{ {
$appName = 'MysqlExposePort'; $appName = 'MysqlExposePort';
return Apps::dockerComposeUp($appName, 'down'); return Apps::dockerComposeDown($appName);
} }
} }

View File

@ -10,13 +10,13 @@ use Symfony\Component\Yaml\Exception\ParseException;
class Apps class Apps
{ {
/** /**
* 执行docker-compose up|down命令 * 执行docker-compose up命令
* @param string $appName * @param string $appName
* @param string $command
* @param string $version * @param string $version
* @param string $command
* @return array * @return array
*/ */
public static function dockerComposeUp(string $appName, string $command = 'up', string $version = 'latest'): array public static function dockerComposeUp(string $appName, string $version = 'latest', string $command = 'up'): array
{ {
// 获取版本信息 // 获取版本信息
$versions = self::getAvailableVersions($appName); $versions = self::getAvailableVersions($appName);
@ -37,9 +37,7 @@ class Apps
file_put_contents($versionInfo['base_dir'] . '/latest', $versionInfo['version']); file_put_contents($versionInfo['base_dir'] . '/latest', $versionInfo['version']);
// 生成docker-compose.yml文件 // 生成docker-compose.yml文件
$filePath = $versionInfo['compose_file']; $result = self::generateDockerComposeYml($versionInfo['compose_file'], [
$savePath = $versionInfo['path'] . '/docker-compose.doo.yml';
$result = self::generateDockerComposeYml($filePath, $savePath, [
'PROXY_PORT' => '33062', // todo 参数自定义 'PROXY_PORT' => '33062', // todo 参数自定义
]); ]);
if (!$result) { if (!$result) {
@ -67,24 +65,41 @@ class Apps
return Base::retSuccess("success", $resData['data']); return Base::retSuccess("success", $resData['data']);
} }
/**
* 执行docker-compose down命令
* @param string $appName
* @param string $version
* @return array
*/
public static function dockerComposeDown(string $appName, string $version = 'latest'): array
{
return self::dockerComposeUp($appName, $version, 'down');
}
/** /**
* 生成docker-compose.yml文件配置 * 生成docker-compose.yml文件配置
* *
* @param string $filePath docker-compose.yml文件路径 * @param string $filePath docker-compose.yml文件路径
* @param string|null $savePath 保存文件路径,为空则覆盖原文件
* @param array $params 可选参数替换docker-compose.yml中的${XXX}变量 * @param array $params 可选参数替换docker-compose.yml中的${XXX}变量
* @return bool 是否生成成功 * @return bool 是否生成成功
*/ */
private static function generateDockerComposeYml(string $filePath, ?string $savePath = null, array $params = []): bool private static function generateDockerComposeYml(string $filePath, array $params = []): bool
{ {
// 应用名称 // 应用名称
$appName = basename(dirname($filePath)); $appName = basename(dirname($filePath, 2));
$serviceName = preg_replace('/(?<!^)([A-Z])/', '-$1', $appName);
$serviceName = 'dootask-app-' . strtolower($serviceName); // 服务名称
$serviceName = 'dootask-app-' . strtolower(preg_replace('/(?<!^)([A-Z])/', '-$1', $appName));
// 网络名称 // 网络名称
$networkName = 'dootask-networks-' . env('APP_ID'); $networkName = 'dootask-networks-' . env('APP_ID');
// 主机路径
$hostPwd = '${HOST_PWD}/docker/apps/' . $appName . '/' . basename(dirname($filePath));
// 保存路径
$savePath = dirname($filePath) . '/docker-compose.doo.yml';
try { try {
// 解析YAML文件 // 解析YAML文件
$content = Yaml::parseFile($filePath); $content = Yaml::parseFile($filePath);
@ -109,7 +124,7 @@ class Apps
// 处理现有的volumes配置 // 处理现有的volumes配置
if (isset($service['volumes'])) { if (isset($service['volumes'])) {
$service['volumes'] = Volumes::processVolumeConfigurations($service['volumes'], $appName); $service['volumes'] = Volumes::processVolumeConfigurations($service['volumes'], $hostPwd);
} }
} }
@ -121,8 +136,8 @@ class Apps
return $params[$matches[1]] ?? $matches[0]; return $params[$matches[1]] ?? $matches[0];
}, $yamlContent); }, $yamlContent);
// 写回文件 // 保存文件
file_put_contents($savePath ?? $filePath, $yamlContent); file_put_contents($savePath, $yamlContent);
return true; return true;
} catch (ParseException) { } catch (ParseException) {

View File

@ -8,18 +8,18 @@ class Volumes
* 处理卷挂载配置,将相对路径转换为绝对路径 * 处理卷挂载配置,将相对路径转换为绝对路径
* *
* @param array $volumes 原始卷挂载配置 * @param array $volumes 原始卷挂载配置
* @param string $appName 应用名称 * @param string $hostPwd 主机路径
* @return array 处理后的卷挂载配置 * @return array 处理后的卷挂载配置
*/ */
public static function processVolumeConfigurations(array $volumes, string $appName): array public static function processVolumeConfigurations(array $volumes, string $hostPwd): array
{ {
return array_map(function ($volume) use ($appName) { return array_map(function ($volume) use ($hostPwd) {
// 短语法格式:字符串形式如 "./src:/app" // 短语法格式:字符串形式如 "./src:/app"
if (is_string($volume)) { if (is_string($volume)) {
return self::processShortSyntaxVolume($volume, $appName); return self::processShortSyntaxVolume($volume, $hostPwd);
} // 长语法格式:数组形式包含 source 键 } // 长语法格式:数组形式包含 source 键
elseif (is_array($volume) && isset($volume['source'])) { elseif (is_array($volume) && isset($volume['source'])) {
return self::processLongSyntaxVolume($volume, $appName); return self::processLongSyntaxVolume($volume, $hostPwd);
} }
// 其他格式保持不变 // 其他格式保持不变
return $volume; return $volume;
@ -30,15 +30,15 @@ class Volumes
* 处理短语法格式的卷挂载 * 处理短语法格式的卷挂载
* *
* @param string $volume 原始卷配置字符串 * @param string $volume 原始卷配置字符串
* @param string $appName 应用名称 * @param string $hostPwd 主机路径
* @return string 处理后的卷配置字符串 * @return string 处理后的卷配置字符串
*/ */
private static function processShortSyntaxVolume(string $volume, string $appName): string private static function processShortSyntaxVolume(string $volume, string $hostPwd): string
{ {
$parts = explode(':', $volume, 3); $parts = explode(':', $volume, 3);
$sourcePath = $parts[0]; $sourcePath = $parts[0];
$newSourcePath = self::convertRelativePathToAbsolute($sourcePath, $appName); $newSourcePath = self::convertRelativePathToAbsolute($sourcePath, $hostPwd);
// 如果路径已更改,重建挂载配置 // 如果路径已更改,重建挂载配置
if ($newSourcePath !== $sourcePath) { if ($newSourcePath !== $sourcePath) {
@ -53,12 +53,12 @@ class Volumes
* 处理长语法格式的卷挂载 * 处理长语法格式的卷挂载
* *
* @param array $volume 原始卷配置数组 * @param array $volume 原始卷配置数组
* @param string $appName 应用名称 * @param string $hostPwd 主机路径
* @return array 处理后的卷配置数组 * @return array 处理后的卷配置数组
*/ */
private static function processLongSyntaxVolume(array $volume, string $appName): array private static function processLongSyntaxVolume(array $volume, string $hostPwd): array
{ {
$newSourcePath = self::convertRelativePathToAbsolute($volume['source'], $appName); $newSourcePath = self::convertRelativePathToAbsolute($volume['source'], $hostPwd);
// 如果路径已更改更新source // 如果路径已更改更新source
if ($newSourcePath !== $volume['source']) { if ($newSourcePath !== $volume['source']) {
@ -68,25 +68,14 @@ class Volumes
return $volume; return $volume;
} }
/**
* 获取应用的基础路径
*
* @param string $appName 应用名称
* @return string 应用的基础路径
*/
private static function getBaseAppPath(string $appName): string
{
return '${HOST_PWD}/docker/apps/' . $appName;
}
/** /**
* 将相对路径转换为绝对路径 * 将相对路径转换为绝对路径
* *
* @param string $path 原始路径 * @param string $path 原始路径
* @param string $appName 应用名称 * @param string $hostPwd 主机路径
* @return string 处理后的路径 * @return string 处理后的路径
*/ */
private static function convertRelativePathToAbsolute(string $path, string $appName): string private static function convertRelativePathToAbsolute(string $path, string $hostPwd): string
{ {
// 判断是否为相对路径Docker卷挂载有以下几种情况 // 判断是否为相对路径Docker卷挂载有以下几种情况
// 1. 环境变量路径:包含${PWD}的路径,如 "${PWD}/data:/app/data" // 1. 环境变量路径:包含${PWD}的路径,如 "${PWD}/data:/app/data"
@ -100,24 +89,21 @@ class Volumes
// 注Docker Compose在解析路径时相对路径是相对于docker-compose.yml文件所在目录的 // 注Docker Compose在解析路径时相对路径是相对于docker-compose.yml文件所在目录的
// 我们需要将这些相对路径转换为绝对路径,以确保在不同环境中能正确挂载目录 // 我们需要将这些相对路径转换为绝对路径,以确保在不同环境中能正确挂载目录
// 获取基础应用路径
$baseAppPath = self::getBaseAppPath($appName);
// 处理${PWD}路径 // 处理${PWD}路径
if (str_contains($path, '${PWD}')) { if (str_contains($path, '${PWD}')) {
// 将${PWD}替换为我们的标准路径格式 // 将${PWD}替换为我们的标准路径格式
return str_replace('${PWD}', $baseAppPath, $path); return str_replace('${PWD}', $hostPwd, $path);
} }
// 处理./或../开头的显式相对路径 // 处理./或../开头的显式相对路径
if (str_starts_with($path, './') || str_starts_with($path, '../')) { if (str_starts_with($path, './') || str_starts_with($path, '../')) {
$cleanPath = ltrim($path, './'); $cleanPath = ltrim($path, './');
return $baseAppPath . '/' . $cleanPath; return $hostPwd . '/' . $cleanPath;
} }
// 处理不以/开头的路径,且要么包含/(明确是路径而非命名卷) // 处理不以/开头的路径,且要么包含/(明确是路径而非命名卷)
if (!str_starts_with($path, '/') && str_contains($path, '/')) { if (!str_starts_with($path, '/') && str_contains($path, '/')) {
return $baseAppPath . '/' . $path; return $hostPwd . '/' . $path;
} }
// 命名卷或绝对路径,保持不变 // 命名卷或绝对路径,保持不变