From 5414accc6ca75f4440c37f74a62c8ad517776263 Mon Sep 17 00:00:00 2001 From: kuaifan Date: Sun, 4 May 2025 00:13:00 +0800 Subject: [PATCH] no message --- app/Http/Controllers/Api/AppsController.php | 27 +++++ app/Module/Docker.php | 111 ++++++++++++++++++++ cmd | 79 +++----------- docker/apps/.gitignore | 2 + routes/web.php | 6 +- 5 files changed, 158 insertions(+), 67 deletions(-) create mode 100755 app/Http/Controllers/Api/AppsController.php create mode 100644 app/Module/Docker.php create mode 100644 docker/apps/.gitignore diff --git a/app/Http/Controllers/Api/AppsController.php b/app/Http/Controllers/Api/AppsController.php new file mode 100755 index 000000000..e67ae1a16 --- /dev/null +++ b/app/Http/Controllers/Api/AppsController.php @@ -0,0 +1,27 @@ + [ + 'PROXY_PORT' => '33062', + ] + ]); + } +} diff --git a/app/Module/Docker.php b/app/Module/Docker.php new file mode 100644 index 000000000..271ac4d69 --- /dev/null +++ b/app/Module/Docker.php @@ -0,0 +1,111 @@ + true]; + + // 查找要修改的服务 + $mainService = null; + $firstService = null; + + foreach ($content['services'] as $name => $service) { + // 记录第一个服务 + if ($firstService === null) { + $firstService = $name; + } + + // 检查是否有labels.main=true的服务 + if (isset($service['labels']['main']) && $service['labels']['main'] === true) { + $mainService = $name; + break; + } + } + + // 确定要修改的服务名称 + $targetService = $mainService ?? $firstService; + + if ($targetService !== null) { + $service = &$content['services'][$targetService]; + + // 更新网络配置 + $service['networks'] = [$networkName]; + + // 处理ports参数 + if (isset($params['ports'])) { + $service['ports'] = $params['ports']; + } + + // 处理volumes参数 + if (isset($params['volumes'])) { + $service['volumes'] = $params['volumes']; + } + + // 处理container_name参数 + if (isset($params['container_name'])) { + $service['container_name'] = $params['container_name']; + } + } + + // 生成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); + } + + // 写回文件 + file_put_contents($savePath ?? $filePath, $yamlContent); + + return true; + } catch (ParseException) { + // YAML解析错误 + return false; + } catch (\Exception) { + // 其他错误 + return false; + } + } +} diff --git a/cmd b/cmd index 57a7b9a0c..240ddbbbb 100755 --- a/cmd +++ b/cmd @@ -226,66 +226,17 @@ run_mysql() { run_exec mariadb "gunzip < /$inputname | mysql -u$username -p$password $database" run_exec php "php artisan migrate" judge "还原数据库" - elif [ "$1" = "open" ]; then - container_name=`docker_name mariadb` - if [ -z "$container_name" ]; then - error "没有找到 mariadb 容器!" - exit 1 - fi - mkdir -p ${cur_path}/docker/mysql/tmp - cat > ${cur_path}/docker/mysql/tmp/${container_name}.conf < /dev/null - judge "开启代理" - elif [ "$1" = "close" ]; then - container_name=`docker_name mariadb` - if [ -z "$container_name" ]; then - error "没有找到 mariadb 容器!" - exit 1 - fi - docker stop ${container_name}-port > /dev/null - docker rm ${container_name}-port > /dev/null - judge "关闭代理" - elif [ "$1" = "rm-port" ]; then - docker rm -f $(docker_name mariadb)-port &> /dev/null fi } +down_by_network() { + local app_id=$(env_get APP_ID) + local network_name="dootask-networks-${app_id}" + for container_id in $(docker ps -q --filter network="$network_name"); do + docker rm -f "$container_id" 1>/dev/null + done +} + https_auto() { restart_nginx="n" if [[ "$(env_get APP_PORT)" != "80" ]]; then @@ -516,8 +467,8 @@ if [ $# -gt 0 ]; then exit 2 ;; esac - run_mysql rm-port - $COMPOSE down + down_by_network + $COMPOSE down --remove-orphans env_set APP_DEBUG "false" rm -rf "./docker/mysql/data" rm -rf "./docker/log/supervisor" @@ -618,10 +569,6 @@ if [ $# -gt 0 ]; then run_mysql backup elif [[ "$1" == "recovery" ]] || [[ "$1" == "r" ]]; then run_mysql recovery - elif [[ "$1" == "agent" ]] || [[ "$1" == "open" ]]; then - run_mysql open - elif [[ "$1" == "unagent" ]] || [[ "$1" == "close" ]]; then - run_mysql close else e="mysql $@" && run_exec mariadb "$e" fi @@ -650,12 +597,12 @@ if [ $# -gt 0 ]; then $COMPOSE start "$@" elif [[ "$1" == "reup" ]]; then shift 1 - run_mysql rm-port + down_by_network $COMPOSE down --remove-orphans - $COMPOSE up -d --remove-orphans + $COMPOSE up -d elif [[ "$1" == "down" ]]; then shift 1 - run_mysql rm-port + down_by_network if [[ $# -eq 0 ]]; then $COMPOSE down --remove-orphans else diff --git a/docker/apps/.gitignore b/docker/apps/.gitignore new file mode 100644 index 000000000..d6b7ef32c --- /dev/null +++ b/docker/apps/.gitignore @@ -0,0 +1,2 @@ +* +!.gitignore diff --git a/routes/web.php b/routes/web.php index 403dfb66b..286040822 100644 --- a/routes/web.php +++ b/routes/web.php @@ -1,8 +1,9 @@ middleware(['webapi'])->group(function () { // 投诉 Route::any('complaint/{method}', ComplaintController::class); Route::any('complaint/{method}/{action}', ComplaintController::class); + // 应用 + Route::any('apps/{method}', AppsController::class); + Route::any('apps/{method}/{action}', AppsController::class); // 测试 Route::any('test/{method}', TestController::class); Route::any('test/{method}/{action}', TestController::class);