From 5a0d1ac0c0b302fe42903367f9a1f021cbe33d18 Mon Sep 17 00:00:00 2001 From: kuaifan Date: Sun, 4 May 2025 22:40:38 +0800 Subject: [PATCH] no message --- app/Http/Controllers/Api/AppsController.php | 14 ++++++-- app/Module/{Docker.php => Apps.php} | 40 ++++++++++++++++----- docker-compose.yml | 12 +++++++ 3 files changed, 54 insertions(+), 12 deletions(-) rename app/Module/{Docker.php => Apps.php} (71%) diff --git a/app/Http/Controllers/Api/AppsController.php b/app/Http/Controllers/Api/AppsController.php index e67ae1a16..758e1c73d 100755 --- a/app/Http/Controllers/Api/AppsController.php +++ b/app/Http/Controllers/Api/AppsController.php @@ -3,7 +3,8 @@ namespace App\Http\Controllers\Api; -use App\Module\Docker; +use App\Module\Apps; +use App\Module\Base; /** * @apiDefine apps @@ -15,13 +16,20 @@ class AppsController extends AbstractController public function test() { - $dirPath = base_path('docker/apps/MysqlExposePort'); + $appName = 'MysqlExposePort'; + $dirPath = base_path('docker/apps/' . $appName); $filePath = $dirPath . '/docker-compose.yml'; $savePath = $dirPath . '/docker-compose-local.yml'; - return Docker::generateComposeYml($filePath, $savePath, [ + + $res = Apps::generateDockerComposeYml($filePath, $savePath, [ 'config' => [ 'PROXY_PORT' => '33062', ] ]); + if (!$res) { + return Base::retError('生成docker-compose.yml失败'); + } + + return Apps::dockerComposeUp($appName); } } diff --git a/app/Module/Docker.php b/app/Module/Apps.php similarity index 71% rename from app/Module/Docker.php rename to app/Module/Apps.php index 271ac4d69..63bedcc86 100644 --- a/app/Module/Docker.php +++ b/app/Module/Apps.php @@ -5,7 +5,7 @@ namespace App\Module; use Symfony\Component\Yaml\Yaml; use Symfony\Component\Yaml\Exception\ParseException; -class Docker +class Apps { /** * 生成docker-compose.yml文件配置 @@ -15,7 +15,7 @@ class Docker * @param array $params 可选参数,可包含ports、volumes、container_name和config配置 * @return bool 是否生成成功 */ - public static function generateComposeYml(string $filePath, ?string $savePath = null, array $params = []): bool + public static function generateDockerComposeYml(string $filePath, ?string $savePath = null, array $params = []): bool { // 应用名称 $appName = basename(dirname($filePath)); @@ -88,13 +88,11 @@ class Docker // 生成YAML内容 $yamlContent = Yaml::dump($content, 4, 2); - // 替换${xxx}格式变量 - if (isset($params['config'])) { - $yamlContent = preg_replace_callback('/\$\{(.*?)\}/', function($matches) use ($params) { - $varName = $matches[1]; - return $params['config'][$varName] ?? $matches[0]; - }, $yamlContent); - } + // 替换${XXX}格式变量 + $yamlContent = preg_replace_callback('/\$\{(.*?)\}/', function($matches) use ($params) { + $varName = $matches[1]; + return $params['config'][$varName] ?? $varName; + }, $yamlContent); // 写回文件 file_put_contents($savePath ?? $filePath, $yamlContent); @@ -108,4 +106,28 @@ class Docker return false; } } + + /** + * 执行docker-compose up|down命令 + * @param string $appName + * @param string $command + * @return array + */ + public static function dockerComposeUp(string $appName, string $command = 'up'): array + { + $url = "http://host.docker.internal:" . env("APPS_PORT") . "/apps/{$command}/{$appName}"; + $extra = [ + 'Content-Type' => 'application/json', + 'Authorization' => 'Bearer ' . env('APP_KEY'), + ]; + $res = Ihttp::ihttp_request($url, [], $extra); + if (Base::isError($res)) { + return Base::retError("请求错误", $res); + } + $resData = Base::json2array($res['data']); + if ($resData['code'] != 200) { + return Base::retError("请求失败", $resData); + } + return Base::retSuccess("success", $resData['data']); + } } diff --git a/docker-compose.yml b/docker-compose.yml index a8e825883..525701749 100755 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -237,6 +237,18 @@ services: ipv4_address: "${APP_IPPR}.15" restart: unless-stopped + apps: + container_name: "dootask-apps-${APP_ID}" + privileged: true + build: + context: ../dooso + dockerfile: Dockerfile-cli + volumes: + - ./:/var/www + - /var/run/docker.sock:/var/run/docker.sock + network_mode: host + restart: unless-stopped + networks: extnetwork: name: "dootask-networks-${APP_ID}"