coolify/app/Actions/Proxy/StartProxy.php

83 lines
3.2 KiB
PHP
Raw Normal View History

2023-05-03 08:27:44 +00:00
<?php
namespace App\Actions\Proxy;
2024-12-06 12:39:00 +00:00
use App\Enums\ProxyTypes;
use App\Events\ProxyStatusChanged;
use App\Events\ProxyStatusChangedUI;
2023-05-03 08:27:44 +00:00
use App\Models\Server;
2023-09-18 10:29:50 +00:00
use Lorisleiva\Actions\Concerns\AsAction;
use Spatie\Activitylog\Models\Activity;
2023-05-03 08:27:44 +00:00
2023-07-14 11:38:24 +00:00
class StartProxy
2023-05-03 08:27:44 +00:00
{
2023-09-18 10:29:50 +00:00
use AsAction;
2024-06-10 20:43:34 +00:00
public function handle(Server $server, bool $async = true, bool $force = false): string|Activity
2023-05-03 08:27:44 +00:00
{
2024-10-28 13:37:00 +00:00
$proxyType = $server->proxyType();
if ((is_null($proxyType) || $proxyType === 'NONE' || $server->proxy->force_stop || $server->isBuildServer()) && $force === false) {
return 'OK';
}
$commands = collect([]);
$proxy_path = $server->proxyPath();
$configuration = CheckConfiguration::run($server);
if (! $configuration) {
throw new \Exception('Configuration is not synced');
}
SaveConfiguration::run($server, $configuration);
$docker_compose_yml_base64 = base64_encode($configuration);
$server->proxy->last_applied_settings = str($docker_compose_yml_base64)->pipe('md5')->value();
$server->save();
if ($server->isSwarmManager()) {
2024-10-28 13:37:00 +00:00
$commands = $commands->merge([
"mkdir -p $proxy_path/dynamic",
"cd $proxy_path",
"echo 'Creating required Docker Compose file.'",
"echo 'Starting coolify-proxy.'",
'docker stack deploy --detach=true -c docker-compose.yml coolify-proxy',
2024-10-28 13:37:00 +00:00
"echo 'Successfully started coolify-proxy.'",
]);
} else {
2024-12-06 12:39:00 +00:00
if (isDev()) {
if ($proxyType === ProxyTypes::CADDY->value) {
$proxy_path = '/data/coolify/proxy/caddy';
}
}
$caddyfile = 'import /dynamic/*.caddy';
2024-10-28 13:37:00 +00:00
$commands = $commands->merge([
"mkdir -p $proxy_path/dynamic",
"cd $proxy_path",
2024-12-06 12:39:00 +00:00
"echo '$caddyfile' > $proxy_path/dynamic/Caddyfile",
2024-10-28 13:37:00 +00:00
"echo 'Creating required Docker Compose file.'",
"echo 'Pulling docker image.'",
'docker compose pull',
'if docker ps -a --format "{{.Names}}" | grep -q "^coolify-proxy$"; then',
" echo 'Stopping and removing existing coolify-proxy.'",
' docker rm -f coolify-proxy || true',
" echo 'Successfully stopped and removed existing coolify-proxy.'",
'fi',
"echo 'Starting coolify-proxy.'",
'docker compose up -d --wait --remove-orphans',
2024-10-28 13:37:00 +00:00
"echo 'Successfully started coolify-proxy.'",
]);
$commands = $commands->merge(connectProxyToNetworks($server));
}
$server->proxy->set('status', 'starting');
$server->save();
ProxyStatusChangedUI::dispatch($server->team_id);
2023-11-28 17:42:09 +00:00
2024-10-28 13:37:00 +00:00
if ($async) {
return remote_process($commands, $server, callEventOnFinish: 'ProxyStatusChanged', callEventData: $server->id);
2024-10-28 13:37:00 +00:00
} else {
instant_remote_process($commands, $server);
$server->proxy->set('type', $proxyType);
$server->save();
ProxyStatusChanged::dispatch($server->id);
2024-06-10 20:43:34 +00:00
2024-10-28 13:37:00 +00:00
return 'OK';
2023-09-14 10:45:50 +00:00
}
2023-05-03 08:27:44 +00:00
}
}