mirror of
https://github.com/kuaifan/dootask.git
synced 2025-12-10 18:02:55 +00:00
no message
This commit is contained in:
parent
c415ace453
commit
5414accc6c
27
app/Http/Controllers/Api/AppsController.php
Executable file
27
app/Http/Controllers/Api/AppsController.php
Executable file
@ -0,0 +1,27 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Controllers\Api;
|
||||
|
||||
|
||||
use App\Module\Docker;
|
||||
|
||||
/**
|
||||
* @apiDefine apps
|
||||
*
|
||||
* 应用相关接口
|
||||
*/
|
||||
class AppsController extends AbstractController
|
||||
{
|
||||
|
||||
public function test()
|
||||
{
|
||||
$dirPath = base_path('docker/apps/MysqlExposePort');
|
||||
$filePath = $dirPath . '/docker-compose.yml';
|
||||
$savePath = $dirPath . '/docker-compose-local.yml';
|
||||
return Docker::generateComposeYml($filePath, $savePath, [
|
||||
'config' => [
|
||||
'PROXY_PORT' => '33062',
|
||||
]
|
||||
]);
|
||||
}
|
||||
}
|
||||
111
app/Module/Docker.php
Normal file
111
app/Module/Docker.php
Normal file
@ -0,0 +1,111 @@
|
||||
<?php
|
||||
|
||||
namespace App\Module;
|
||||
|
||||
use Symfony\Component\Yaml\Yaml;
|
||||
use Symfony\Component\Yaml\Exception\ParseException;
|
||||
|
||||
class Docker
|
||||
{
|
||||
/**
|
||||
* 生成docker-compose.yml文件配置
|
||||
*
|
||||
* @param string $filePath docker-compose.yml文件路径
|
||||
* @param string|null $savePath 保存文件路径,为空则覆盖原文件
|
||||
* @param array $params 可选参数,可包含ports、volumes、container_name和config配置
|
||||
* @return bool 是否生成成功
|
||||
*/
|
||||
public static function generateComposeYml(string $filePath, ?string $savePath = null, array $params = []): bool
|
||||
{
|
||||
// 应用名称
|
||||
$appName = basename(dirname($filePath));
|
||||
$appName = preg_replace('/(?<!^)([A-Z])/', '-$1', $appName);
|
||||
$appName = 'dootask-app-' . strtolower($appName);
|
||||
|
||||
// 网络名称
|
||||
$networkName = 'dootask-networks-' . env('APP_ID');
|
||||
|
||||
try {
|
||||
// 解析YAML文件
|
||||
$content = Yaml::parseFile($filePath);
|
||||
|
||||
// 确保services部分存在
|
||||
if (!isset($content['services'])) {
|
||||
return false;
|
||||
}
|
||||
|
||||
// 设置服务名称
|
||||
$content['name'] = $appName;
|
||||
|
||||
// 删除所有现有网络配置
|
||||
unset($content['networks']);
|
||||
|
||||
// 添加网络配置
|
||||
$content['networks'][$networkName] = ['external' => 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;
|
||||
}
|
||||
}
|
||||
}
|
||||
79
cmd
79
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 <<EOF
|
||||
user nginx;
|
||||
worker_processes auto;
|
||||
error_log /var/log/nginx/error.log notice;
|
||||
pid /var/run/nginx.pid;
|
||||
events {
|
||||
worker_connections 1024;
|
||||
}
|
||||
stream {
|
||||
upstream mysql {
|
||||
server ${container_name}:3306 max_fails=1 fail_timeout=30s;
|
||||
}
|
||||
server {
|
||||
listen 3306;
|
||||
proxy_pass mysql;
|
||||
proxy_connect_timeout 5s;
|
||||
}
|
||||
}
|
||||
EOF
|
||||
default_value="$(env_get DB_PORT_OPEN)"
|
||||
if [ -n "$default_value" ]; then
|
||||
read_tip="请输入代理端口 (3300-65500, 默认: ${default_value}): "
|
||||
else
|
||||
read_tip="请输入代理端口 (3300-65500): "
|
||||
fi
|
||||
read -rp "$read_tip" inputport
|
||||
inputport=${inputport:-$default_value}
|
||||
if [ $inputport -lt 3300 ] || [ $inputport -gt 65500 ]; then
|
||||
error "端口范围不正确!"
|
||||
exit 1
|
||||
fi
|
||||
env_set DB_PORT_OPEN $inputport
|
||||
run_mysql rm-port
|
||||
docker run --name ${container_name}-port \
|
||||
--network dootask-networks-$(env_get APP_ID) \
|
||||
-p ${inputport}:3306 \
|
||||
-v ${cur_path}/docker/mysql/tmp/${container_name}.conf:/etc/nginx/nginx.conf \
|
||||
-d nginx:alpine > /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
|
||||
|
||||
2
docker/apps/.gitignore
vendored
Normal file
2
docker/apps/.gitignore
vendored
Normal file
@ -0,0 +1,2 @@
|
||||
*
|
||||
!.gitignore
|
||||
@ -1,8 +1,9 @@
|
||||
<?php
|
||||
|
||||
use App\Http\Controllers\Api\TestController;
|
||||
use Illuminate\Support\Facades\Route;
|
||||
use App\Http\Controllers\IndexController;
|
||||
use App\Http\Controllers\Api\AppsController;
|
||||
use App\Http\Controllers\Api\TestController;
|
||||
use App\Http\Controllers\Api\FileController;
|
||||
use App\Http\Controllers\Api\UsersController;
|
||||
use App\Http\Controllers\Api\DialogController;
|
||||
@ -56,6 +57,9 @@ Route::prefix('api')->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);
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user