2023-05-03 08:27:44 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace App\Actions\Proxy;
|
|
|
|
|
2024-03-12 10:22:02 +00:00
|
|
|
use App\Events\ProxyStarted;
|
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;
|
2023-08-08 09:51:36 +00:00
|
|
|
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
|
|
|
|
2023-10-13 12:25:30 +00:00
|
|
|
public function handle(Server $server, bool $async = true): string|Activity
|
2023-05-03 08:27:44 +00:00
|
|
|
{
|
2023-10-13 12:25:30 +00:00
|
|
|
try {
|
|
|
|
$proxyType = $server->proxyType();
|
2024-05-24 10:01:04 +00:00
|
|
|
if (is_null($proxyType) || $proxyType === 'NONE' || $server->proxy->force_stop || $server->isBuildServer()) {
|
2024-03-04 09:42:54 +00:00
|
|
|
return 'OK';
|
|
|
|
}
|
2023-10-13 12:25:30 +00:00
|
|
|
$commands = collect([]);
|
2024-03-11 14:08:05 +00:00
|
|
|
$proxy_path = $server->proxyPath();
|
2023-10-13 12:25:30 +00:00
|
|
|
$configuration = CheckConfiguration::run($server);
|
2024-06-10 20:43:34 +00:00
|
|
|
if (! $configuration) {
|
|
|
|
throw new \Exception('Configuration is not synced');
|
2023-10-13 12:25:30 +00:00
|
|
|
}
|
|
|
|
SaveConfiguration::run($server, $configuration);
|
|
|
|
$docker_compose_yml_base64 = base64_encode($configuration);
|
2024-06-25 08:37:10 +00:00
|
|
|
$server->proxy->last_applied_settings = str($docker_compose_yml_base64)->pipe('md5')->value;
|
2023-09-25 07:17:42 +00:00
|
|
|
$server->save();
|
2023-11-28 17:42:09 +00:00
|
|
|
if ($server->isSwarm()) {
|
|
|
|
$commands = $commands->merge([
|
2024-04-16 18:57:54 +00:00
|
|
|
"mkdir -p $proxy_path/dynamic",
|
|
|
|
"cd $proxy_path",
|
2023-11-28 17:42:09 +00:00
|
|
|
"echo 'Creating required Docker Compose file.'",
|
|
|
|
"echo 'Starting coolify-proxy.'",
|
2024-06-10 20:43:34 +00:00
|
|
|
'docker stack deploy -c docker-compose.yml coolify-proxy',
|
|
|
|
"echo 'Proxy started successfully.'",
|
2023-11-28 17:42:09 +00:00
|
|
|
]);
|
|
|
|
} else {
|
2024-06-10 20:43:34 +00:00
|
|
|
$caddfile = 'import /dynamic/*.caddy';
|
2023-11-28 17:42:09 +00:00
|
|
|
$commands = $commands->merge([
|
2024-04-16 18:57:54 +00:00
|
|
|
"mkdir -p $proxy_path/dynamic",
|
|
|
|
"cd $proxy_path",
|
2024-03-11 16:17:34 +00:00
|
|
|
"echo '$caddfile' > $proxy_path/dynamic/Caddyfile",
|
2023-11-28 17:42:09 +00:00
|
|
|
"echo 'Creating required Docker Compose file.'",
|
|
|
|
"echo 'Pulling docker image.'",
|
|
|
|
'docker compose pull',
|
|
|
|
"echo 'Stopping existing coolify-proxy.'",
|
2024-06-10 20:43:34 +00:00
|
|
|
'docker compose down -v --remove-orphans > /dev/null 2>&1',
|
2023-11-28 17:42:09 +00:00
|
|
|
"echo 'Starting coolify-proxy.'",
|
|
|
|
'docker compose up -d --remove-orphans',
|
2024-06-10 20:43:34 +00:00
|
|
|
"echo 'Proxy started successfully.'",
|
2023-11-28 17:42:09 +00:00
|
|
|
]);
|
|
|
|
$commands = $commands->merge(connectProxyToNetworks($server));
|
|
|
|
}
|
|
|
|
|
2023-10-13 12:25:30 +00:00
|
|
|
if ($async) {
|
2024-03-12 10:22:02 +00:00
|
|
|
$activity = remote_process($commands, $server, callEventOnFinish: 'ProxyStarted', callEventData: $server);
|
2024-06-10 20:43:34 +00:00
|
|
|
|
2023-10-13 12:25:30 +00:00
|
|
|
return $activity;
|
|
|
|
} else {
|
|
|
|
instant_remote_process($commands, $server);
|
|
|
|
$server->proxy->set('status', 'running');
|
|
|
|
$server->proxy->set('type', $proxyType);
|
|
|
|
$server->save();
|
2024-03-12 10:22:02 +00:00
|
|
|
ProxyStarted::dispatch($server);
|
2024-06-10 20:43:34 +00:00
|
|
|
|
2023-10-13 12:25:30 +00:00
|
|
|
return 'OK';
|
|
|
|
}
|
2023-11-28 14:49:24 +00:00
|
|
|
} catch (\Throwable $e) {
|
2023-10-13 12:25:30 +00:00
|
|
|
ray($e);
|
|
|
|
throw $e;
|
2023-09-14 10:45:50 +00:00
|
|
|
}
|
2023-05-03 08:27:44 +00:00
|
|
|
}
|
2023-08-08 09:51:36 +00:00
|
|
|
}
|