lasthourcloud/app/Actions/Proxy/StartProxy.php

63 lines
2.8 KiB
PHP
Raw Normal View History

2023-05-03 08:27:44 +00:00
<?php
namespace App\Actions\Proxy;
use App\Models\Server;
2023-05-15 11:45:37 +00:00
use Illuminate\Support\Str;
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-14 10:45:50 +00:00
public function __invoke(Server $server, bool $async = true): Activity|string
2023-05-03 08:27:44 +00:00
{
2023-07-28 14:42:28 +00:00
$proxy_path = get_proxy_path();
2023-05-15 11:45:37 +00:00
$networks = collect($server->standaloneDockers)->map(function ($docker) {
return $docker['network'];
})->unique();
if ($networks->count() === 0) {
2023-05-15 13:13:34 +00:00
$networks = collect(['coolify']);
2023-05-15 11:45:37 +00:00
}
2023-05-15 13:13:34 +00:00
$create_networks_command = $networks->map(function ($network) {
2023-05-15 11:45:37 +00:00
return "docker network ls --format '{{.Name}}' | grep '^$network$' >/dev/null 2>&1 || docker network create --attachable $network > /dev/null 2>&1";
});
2023-07-28 14:42:28 +00:00
$configuration = resolve(CheckConfigurationSync::class)($server);
2023-05-15 11:45:37 +00:00
$docker_compose_yml_base64 = base64_encode($configuration);
2023-06-20 18:19:31 +00:00
$server->proxy->last_applied_settings = Str::of($docker_compose_yml_base64)->pipe('md5')->value;
2023-05-15 11:45:37 +00:00
$server->save();
2023-09-14 10:45:50 +00:00
$commands = [
"command -v lsof >/dev/null || echo '####### Installing lsof...'",
"command -v lsof >/dev/null || apt-get update",
"command -v lsof >/dev/null || apt install -y lsof",
"command -v lsof >/dev/null || command -v fuser >/dev/null || apt install -y psmisc",
"echo '####### Creating required Docker networks...'",
2023-05-15 11:45:37 +00:00
...$create_networks_command,
"cd $proxy_path",
"echo '####### Creating Docker Compose file...'",
"echo '####### Pulling docker image...'",
2023-09-09 11:52:40 +00:00
'docker compose pull',
2023-09-11 20:29:34 +00:00
"echo '####### Stopping existing coolify-proxy...'",
2023-05-15 11:45:37 +00:00
'docker compose down -v --remove-orphans',
"command -v lsof >/dev/null && lsof -nt -i:80 | xargs -r kill -9",
"command -v lsof >/dev/null && lsof -nt -i:443 | xargs -r kill -9",
"command -v fuser >/dev/null && fuser -k 80/tcp",
"command -v fuser >/dev/null && fuser -k 443/tcp",
"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...'",
2023-09-11 20:29:34 +00: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",
"echo '####### Starting coolify-proxy...'",
2023-05-03 12:05:31 +00:00
'docker compose up -d --remove-orphans',
"echo '####### Proxy installed successfully...'"
2023-09-14 10:45:50 +00:00
];
if (!$async) {
instant_remote_process($commands, $server);
return 'OK';
} else {
$activity = remote_process($commands, $server);
return $activity;
}
2023-05-03 08:27:44 +00:00
}
}