2023-09-21 15:48:31 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
|
|
namespace App\Actions\Service;
|
|
|
|
|
|
|
|
|
|
use App\Models\Service;
|
2024-06-10 20:43:34 +00:00
|
|
|
use Lorisleiva\Actions\Concerns\AsAction;
|
2023-10-02 14:57:55 +00:00
|
|
|
use Symfony\Component\Yaml\Yaml;
|
2023-09-21 15:48:31 +00:00
|
|
|
|
|
|
|
|
class StartService
|
|
|
|
|
{
|
|
|
|
|
use AsAction;
|
2024-06-10 20:43:34 +00:00
|
|
|
|
2024-11-22 10:16:01 +00:00
|
|
|
public string $jobQueue = 'high';
|
|
|
|
|
|
2025-02-04 13:34:34 +00:00
|
|
|
public function handle(Service $service, bool $pullLatestImages = false, bool $stopBeforeStart = false)
|
2023-09-21 15:48:31 +00:00
|
|
|
{
|
2025-02-04 13:34:34 +00:00
|
|
|
$service->parse();
|
|
|
|
|
if ($stopBeforeStart) {
|
|
|
|
|
StopService::run(service: $service, dockerCleanup: false);
|
|
|
|
|
}
|
2023-09-25 10:49:55 +00:00
|
|
|
$service->saveComposeConfigs();
|
2025-07-03 13:08:13 +00:00
|
|
|
$service->isConfigurationChanged(save: true);
|
2025-11-10 13:40:03 +00:00
|
|
|
$workdir = $service->workdir();
|
|
|
|
|
// $commands[] = "cd {$workdir}";
|
|
|
|
|
$commands[] = "echo 'Saved configuration files to {$workdir}.'";
|
|
|
|
|
// Ensure .env exists in the correct directory before docker compose tries to load it
|
2025-11-07 14:19:57 +00:00
|
|
|
// This is defensive programming - saveComposeConfigs() already creates it,
|
|
|
|
|
// but we guarantee it here in case of any edge cases or manual deployments
|
2025-11-10 13:40:03 +00:00
|
|
|
$commands[] = "touch {$workdir}/.env";
|
2025-02-04 13:34:34 +00:00
|
|
|
if ($pullLatestImages) {
|
|
|
|
|
$commands[] = "echo 'Pulling images.'";
|
2025-11-10 13:40:03 +00:00
|
|
|
$commands[] = "docker compose --project-directory {$workdir} pull";
|
2025-02-04 13:34:34 +00:00
|
|
|
}
|
2024-09-16 14:39:16 +00:00
|
|
|
if ($service->networks()->count() > 0) {
|
2024-09-09 21:46:29 +00:00
|
|
|
$commands[] = "echo 'Creating Docker network.'";
|
|
|
|
|
$commands[] = "docker network inspect $service->uuid >/dev/null 2>&1 || docker network create --attachable $service->uuid";
|
|
|
|
|
}
|
2024-06-10 20:43:34 +00:00
|
|
|
$commands[] = 'echo Starting service.';
|
2025-11-10 13:40:03 +00:00
|
|
|
$commands[] = "docker compose --project-directory {$workdir} -f {$workdir}/docker-compose.yml --project-name {$service->uuid} up -d --remove-orphans --force-recreate --build";
|
2023-11-28 11:05:04 +00:00
|
|
|
$commands[] = "docker network connect $service->uuid coolify-proxy >/dev/null 2>&1 || true";
|
2024-01-21 13:30:03 +00:00
|
|
|
if (data_get($service, 'connect_to_docker_network')) {
|
|
|
|
|
$compose = data_get($service, 'docker_compose', []);
|
|
|
|
|
$network = $service->destination->network;
|
|
|
|
|
$serviceNames = data_get(Yaml::parse($compose), 'services', []);
|
|
|
|
|
foreach ($serviceNames as $serviceName => $serviceConfig) {
|
2024-09-16 14:39:16 +00:00
|
|
|
$commands[] = "docker network connect --alias {$serviceName}-{$service->uuid} $network {$serviceName}-{$service->uuid} >/dev/null 2>&1 || true";
|
2024-01-21 13:30:03 +00:00
|
|
|
}
|
2023-10-02 14:57:55 +00:00
|
|
|
}
|
2024-06-10 20:43:34 +00:00
|
|
|
|
2025-06-24 10:52:22 +00:00
|
|
|
return remote_process($commands, $service->server, type_uuid: $service->uuid, callEventOnFinish: 'ServiceStatusChanged');
|
2023-09-21 15:48:31 +00:00
|
|
|
}
|
|
|
|
|
}
|