coolify/app/Actions/Proxy/StartProxy.php

67 lines
2.9 KiB
PHP
Raw Normal View History

2023-05-03 10:27:44 +02:00
<?php
namespace App\Actions\Proxy;
use App\Enums\ProxyTypes;
2023-05-03 10:27:44 +02:00
use App\Models\Server;
2023-05-15 13:45:37 +02:00
use Illuminate\Support\Str;
2023-09-18 12:29:50 +02:00
use Lorisleiva\Actions\Concerns\AsAction;
use Spatie\Activitylog\Models\Activity;
2023-05-03 10:27:44 +02:00
2023-07-14 13:38:24 +02:00
class StartProxy
2023-05-03 10:27:44 +02:00
{
2023-09-18 12:29:50 +02:00
use AsAction;
public function handle(Server $server, bool $async = true): Activity|string
2023-05-03 10:27:44 +02:00
{
$commands = collect([]);
2023-09-21 17:48:31 +02:00
$proxyType = $server->proxyType();
if ($proxyType === ProxyTypes::NONE->value) {
return 'OK';
}
2023-07-28 16:42:28 +02:00
$proxy_path = get_proxy_path();
2023-09-18 12:18:45 +02:00
$configuration = CheckConfiguration::run($server);
if (!$configuration) {
throw new \Exception("Configuration is not synced");
}
2023-09-25 09:17:42 +02:00
SaveConfiguration::run($server, $configuration);
2023-05-15 13:45:37 +02:00
$docker_compose_yml_base64 = base64_encode($configuration);
2023-06-20 20:19:31 +02:00
$server->proxy->last_applied_settings = Str::of($docker_compose_yml_base64)->pipe('md5')->value;
2023-05-15 13:45:37 +02:00
$server->save();
$commands = $commands->merge([
2023-09-23 12:21:56 +02:00
"apt-get update > /dev/null 2>&1 || true",
2023-09-24 10:55:15 +02:00
"command -v lsof >/dev/null || echo '####### Installing lsof.'",
"command -v lsof >/dev/null || apt install -y lsof",
"command -v lsof >/dev/null || command -v fuser >/dev/null || apt install -y psmisc",
2023-09-23 11:53:30 +02:00
"mkdir -p $proxy_path && cd $proxy_path",
2023-09-24 10:55:15 +02:00
"echo '####### Creating Docker Compose file.'",
"echo '####### Pulling docker image.'",
2023-09-23 12:21:56 +02:00
'docker compose pull',
2023-09-24 10:55:15 +02:00
"echo '####### Stopping existing coolify-proxy.'",
2023-09-23 12:21:56 +02:00
"docker compose down -v --remove-orphans > /dev/null 2>&1",
"command -v fuser >/dev/null || command -v lsof >/dev/null || echo '####### Could not kill existing processes listening on port 80 & 443. Please stop the process holding these ports...'",
"command -v lsof >/dev/null && lsof -nt -i:80 | xargs -r kill -9 || true",
"command -v lsof >/dev/null && lsof -nt -i:443 | xargs -r kill -9 || true",
"command -v fuser >/dev/null && fuser -k 80/tcp || true",
"command -v fuser >/dev/null && fuser -k 443/tcp || true",
2023-09-11 22:29:34 +02:00
"systemctl disable nginx > /dev/null 2>&1 || true",
"systemctl disable apache2 > /dev/null 2>&1 || true",
"systemctl disable apache > /dev/null 2>&1 || true",
2023-09-24 10:55:15 +02:00
"echo '####### Starting coolify-proxy.'",
2023-09-23 12:21:56 +02:00
'docker compose up -d --remove-orphans',
2023-09-24 10:55:15 +02:00
"echo '####### Proxy installed successfully.'"
]);
$commands = $commands->merge(connectProxyToNetworks($server));
2023-09-25 09:17:42 +02:00
if ($async) {
2023-09-14 12:45:50 +02:00
$activity = remote_process($commands, $server);
return $activity;
2023-09-25 09:17:42 +02:00
} else {
instant_remote_process($commands, $server);
$server->proxy->set('status', 'running');
$server->proxy->set('type', $proxyType);
$server->save();
return 'OK';
2023-09-14 12:45:50 +02:00
}
2023-05-03 10:27:44 +02:00
}
}