lasthourcloud/app/Actions/Proxy/StartProxy.php

51 lines
1.7 KiB
PHP
Raw Normal View History

2023-05-03 08:27:44 +00:00
<?php
namespace App\Actions\Proxy;
2023-06-16 14:15:44 +00:00
use App\Enums\ProxyStatus;
use App\Enums\ProxyTypes;
2023-05-03 08:27:44 +00:00
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-05-12 18:15:36 +00:00
public function __invoke(Server $server): Activity
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-07-28 14:42:28 +00:00
2023-05-24 13:25:08 +00:00
$activity = remote_process([
2023-06-02 13:15:12 +00:00
"echo 'Creating required Docker networks...'",
2023-05-15 11:45:37 +00:00
...$create_networks_command,
"cd $proxy_path",
2023-06-02 13:15:12 +00:00
"echo 'Creating Docker Compose file...'",
2023-05-15 11:45:37 +00:00
"echo 'Pulling docker image...'",
2023-09-09 11:52:40 +00:00
'docker compose pull',
2023-07-28 12:44:26 +00:00
"echo 'Stopping existing proxy...'",
2023-05-15 11:45:37 +00:00
'docker compose down -v --remove-orphans',
2023-07-28 12:44:26 +00:00
"lsof -nt -i:80 | xargs -r kill -9",
"lsof -nt -i:443 | xargs -r kill -9",
"echo 'Starting proxy...'",
2023-05-03 12:05:31 +00:00
'docker compose up -d --remove-orphans',
2023-05-15 11:45:37 +00:00
"echo 'Proxy installed successfully...'"
2023-05-25 13:48:26 +00:00
], $server);
2023-05-03 08:27:44 +00:00
return $activity;
}
}