lasthourcloud/app/Actions/Proxy/StartProxy.php

67 lines
2.5 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;
2023-09-18 10:29:50 +00:00
use Lorisleiva\Actions\Concerns\AsAction;
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;
public function handle(Server $server, bool $async = true): string|Activity
2023-05-03 08:27:44 +00:00
{
try {
2023-11-28 17:42:09 +00:00
$proxyType = $server->proxyType();
$commands = collect([]);
$proxy_path = get_proxy_path();
$configuration = CheckConfiguration::run($server);
if (!$configuration) {
throw new \Exception("Configuration is not synced");
}
SaveConfiguration::run($server, $configuration);
$docker_compose_yml_base64 = base64_encode($configuration);
$server->proxy->last_applied_settings = Str::of($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([
"mkdir -p $proxy_path && cd $proxy_path",
"echo 'Creating required Docker Compose file.'",
"echo 'Starting coolify-proxy.'",
2023-11-29 09:06:52 +00:00
"cd $proxy_path && docker stack deploy -c docker-compose.yml coolify-proxy",
2023-11-28 17:42:09 +00:00
"echo 'Proxy started successfully.'"
]);
} else {
$commands = $commands->merge([
"mkdir -p $proxy_path && cd $proxy_path",
"echo 'Creating required Docker Compose file.'",
"echo 'Pulling docker image.'",
'docker compose pull',
"echo 'Stopping existing coolify-proxy.'",
"docker compose down -v --remove-orphans > /dev/null 2>&1",
"echo 'Starting coolify-proxy.'",
'docker compose up -d --remove-orphans',
"echo 'Proxy started successfully.'"
]);
$commands = $commands->merge(connectProxyToNetworks($server));
}
if ($async) {
$activity = remote_process($commands, $server);
return $activity;
} else {
instant_remote_process($commands, $server);
$server->proxy->set('status', 'running');
$server->proxy->set('type', $proxyType);
$server->save();
return 'OK';
}
2023-11-28 14:49:24 +00:00
} catch (\Throwable $e) {
ray($e);
throw $e;
2023-09-14 10:45:50 +00:00
}
2023-05-03 08:27:44 +00:00
}
}