coolify/app/Actions/Proxy/StartProxy.php

57 lines
2.0 KiB
PHP
Raw Normal View History

2023-05-03 10:27:44 +02:00
<?php
namespace App\Actions\Proxy;
2023-06-16 16:15:44 +02:00
use App\Enums\ProxyStatus;
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;
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-05-12 20:15:36 +02:00
public function __invoke(Server $server): Activity
2023-05-03 10:27:44 +02:00
{
// TODO: check for other proxies
2023-06-20 20:19:31 +02:00
if (is_null(data_get($server, 'proxy.type'))) {
$server->proxy->type = ProxyTypes::TRAEFIK_V2->value;
$server->proxy->status = ProxyStatus::EXITED->value;
2023-06-16 16:15:44 +02:00
$server->save();
}
2023-07-28 16:42:28 +02:00
$proxy_path = get_proxy_path();
2023-05-15 13:45:37 +02:00
$networks = collect($server->standaloneDockers)->map(function ($docker) {
return $docker['network'];
})->unique();
if ($networks->count() === 0) {
2023-05-15 15:13:34 +02:00
$networks = collect(['coolify']);
2023-05-15 13:45:37 +02:00
}
2023-05-15 15:13:34 +02:00
$create_networks_command = $networks->map(function ($network) {
2023-05-15 13:45:37 +02: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 16:42:28 +02:00
$configuration = resolve(CheckConfigurationSync::class)($server);
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();
2023-07-28 16:42:28 +02:00
2023-05-24 15:25:08 +02:00
$activity = remote_process([
2023-06-02 15:15:12 +02:00
"echo 'Creating required Docker networks...'",
2023-05-15 13:45:37 +02:00
...$create_networks_command,
"cd $proxy_path",
2023-06-02 15:15:12 +02:00
"echo 'Creating Docker Compose file...'",
2023-05-15 13:45:37 +02:00
"echo 'Pulling docker image...'",
'docker compose pull -q',
2023-07-28 14:44:26 +02:00
"echo 'Stopping existing proxy...'",
2023-05-15 13:45:37 +02:00
'docker compose down -v --remove-orphans',
2023-07-28 14:44:26 +02:00
"lsof -nt -i:80 | xargs -r kill -9",
"lsof -nt -i:443 | xargs -r kill -9",
"echo 'Starting proxy...'",
2023-05-03 14:05:31 +02:00
'docker compose up -d --remove-orphans',
2023-05-15 13:45:37 +02:00
"echo 'Proxy installed successfully...'"
2023-05-25 15:48:26 +02:00
], $server);
2023-05-03 10:27:44 +02:00
return $activity;
}
}