coolify/bootstrap/helpers/services.php

140 lines
4.6 KiB
PHP
Raw Normal View History

2023-09-19 15:51:13 +02:00
<?php
2023-09-27 12:45:53 +02:00
use App\Models\EnvironmentVariable;
2023-09-20 15:42:41 +02:00
use App\Models\Service;
use App\Models\ServiceApplication;
use App\Models\ServiceDatabase;
2023-09-26 14:45:52 +02:00
use Illuminate\Support\Facades\Storage;
2023-09-22 15:29:19 +02:00
use Illuminate\Support\Str;
2023-09-26 14:45:52 +02:00
use Symfony\Component\Yaml\Yaml;
2023-09-19 15:51:13 +02:00
function replaceRegex(?string $name = null)
{
return "/\\\${?{$name}[^}]*}?|\\\${$name}\w+/";
}
function collectRegex(string $name)
{
return "/{$name}\w+/";
}
function replaceVariables($variable)
{
return $variable->replaceFirst('$', '')->replaceFirst('{', '')->replaceLast('}', '');
}
2023-09-21 21:30:13 +02:00
function serviceStatus(Service $service)
{
$foundRunning = false;
$isDegraded = false;
2023-09-26 14:45:52 +02:00
$foundRestaring = false;
2023-09-21 21:30:13 +02:00
$applications = $service->applications;
$databases = $service->databases;
foreach ($applications as $application) {
2023-09-26 14:45:52 +02:00
if ($application->exclude_from_status) {
2023-09-25 15:48:43 +02:00
continue;
}
2023-09-22 15:29:19 +02:00
if (Str::of($application->status)->startsWith('running')) {
2023-09-21 21:30:13 +02:00
$foundRunning = true;
2023-09-26 14:45:52 +02:00
} else if (Str::of($application->status)->startsWith('restarting')) {
$foundRestaring = true;
2023-09-21 21:30:13 +02:00
} else {
$isDegraded = true;
}
}
foreach ($databases as $database) {
2023-09-26 14:45:52 +02:00
if ($database->exclude_from_status) {
2023-09-25 15:48:43 +02:00
continue;
}
2023-09-22 15:29:19 +02:00
if (Str::of($database->status)->startsWith('running')) {
2023-09-21 21:30:13 +02:00
$foundRunning = true;
2023-09-26 14:45:52 +02:00
} else if (Str::of($database->status)->startsWith('restarting')) {
$foundRestaring = true;
2023-09-21 21:30:13 +02:00
} else {
$isDegraded = true;
}
}
2023-09-26 14:45:52 +02:00
if ($foundRestaring) {
return 'degraded';
}
2023-09-21 21:30:13 +02:00
if ($foundRunning && !$isDegraded) {
return 'running';
} else if ($foundRunning && $isDegraded) {
return 'degraded';
2023-09-26 14:45:52 +02:00
} else if (!$foundRunning && !$isDegraded) {
2023-09-21 21:30:13 +02:00
return 'exited';
}
return 'exited';
}
function getFilesystemVolumesFromServer(ServiceApplication|ServiceDatabase $oneService)
{
// TODO: make this async
try {
$workdir = $oneService->service->workdir();
$server = $oneService->service->server;
$fileVolumes = $oneService->fileStorages()->get();
$commands = collect([
2023-09-26 14:45:52 +02:00
"mkdir -p $workdir > /dev/null 2>&1 || true",
"cd "
]);
instant_remote_process($commands, $server);
foreach ($fileVolumes as $fileVolume) {
$path = Str::of(data_get($fileVolume, 'fs_path'));
$content = data_get($fileVolume, 'content');
if ($path->startsWith('.')) {
$path = $path->after('.');
$fileLocation = $workdir . $path;
} else {
$fileLocation = $path;
}
$isFile = instant_remote_process(["test -f $fileLocation && echo OK || echo NOK"], $server);
$isDir = instant_remote_process(["test -d $fileLocation && echo OK || echo NOK"], $server);
if ($isFile == 'OK' && !$fileVolume->is_directory) {
$filesystemContent = instant_remote_process(["cat $fileLocation"], $server);
if (base64_encode($filesystemContent) != base64_encode($content)) {
$fileVolume->content = $filesystemContent;
$fileVolume->save();
}
} else {
if ($isDir == 'OK') {
$fileVolume->content = null;
$fileVolume->is_directory = true;
$fileVolume->save();
} else {
$fileVolume->content = null;
$fileVolume->is_directory = false;
$fileVolume->save();
}
}
}
} catch (\Throwable $e) {
return handleError($e);
}
2023-09-21 21:30:13 +02:00
}
function updateCompose($resource)
{
2023-09-26 14:45:52 +02:00
try {
$name = data_get($resource, 'name');
$dockerComposeRaw = data_get($resource, 'service.docker_compose_raw');
$dockerCompose = Yaml::parse($dockerComposeRaw);
2023-09-27 12:45:53 +02:00
// Switch Image
$image = data_get($resource, 'image');
2023-09-26 14:45:52 +02:00
data_set($dockerCompose, "services.{$name}.image", $image);
2023-09-27 12:45:53 +02:00
// Update FQDN
$variableName = "SERVICE_FQDN_" . Str::of($resource->name)->upper();
2023-09-27 15:48:19 +02:00
ray($variableName);
2023-09-27 12:45:53 +02:00
$generatedEnv = EnvironmentVariable::where('service_id', $resource->service_id)->where('key', $variableName)->first();
if ($generatedEnv) {
2023-09-27 12:45:53 +02:00
$generatedEnv->value = $resource->fqdn;
$generatedEnv->save();
}
2023-09-26 14:45:52 +02:00
$dockerComposeRaw = Yaml::dump($dockerCompose, 10, 2);
$resource->service->docker_compose_raw = $dockerComposeRaw;
$resource->service->save();
} catch (\Throwable $e) {
return handleError($e);
}
}