no message

This commit is contained in:
kuaifan 2025-05-04 22:40:38 +08:00
parent 5414accc6c
commit 5a0d1ac0c0
3 changed files with 54 additions and 12 deletions

View File

@ -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);
}
}

View File

@ -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']);
}
}

View File

@ -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}"