2023-09-19 13:51:13 +00:00
|
|
|
<?php
|
|
|
|
|
2023-09-20 13:42:41 +00:00
|
|
|
use App\Models\Service;
|
2023-09-25 10:49:55 +00:00
|
|
|
use App\Models\ServiceApplication;
|
|
|
|
use App\Models\ServiceDatabase;
|
2023-09-26 12:45:52 +00:00
|
|
|
use Illuminate\Support\Facades\Storage;
|
2023-09-22 13:29:19 +00:00
|
|
|
use Illuminate\Support\Str;
|
2023-09-26 12:45:52 +00:00
|
|
|
use Symfony\Component\Yaml\Yaml;
|
2023-09-19 13:51:13 +00: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 19:30:13 +00:00
|
|
|
|
|
|
|
function serviceStatus(Service $service)
|
|
|
|
{
|
|
|
|
$foundRunning = false;
|
|
|
|
$isDegraded = false;
|
2023-09-26 12:45:52 +00:00
|
|
|
$foundRestaring = false;
|
2023-09-21 19:30:13 +00:00
|
|
|
$applications = $service->applications;
|
|
|
|
$databases = $service->databases;
|
|
|
|
foreach ($applications as $application) {
|
2023-09-26 12:45:52 +00:00
|
|
|
if ($application->exclude_from_status) {
|
2023-09-25 13:48:43 +00:00
|
|
|
continue;
|
|
|
|
}
|
2023-09-22 13:29:19 +00:00
|
|
|
if (Str::of($application->status)->startsWith('running')) {
|
2023-09-21 19:30:13 +00:00
|
|
|
$foundRunning = true;
|
2023-09-26 12:45:52 +00:00
|
|
|
} else if (Str::of($application->status)->startsWith('restarting')) {
|
|
|
|
$foundRestaring = true;
|
2023-09-21 19:30:13 +00:00
|
|
|
} else {
|
|
|
|
$isDegraded = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
foreach ($databases as $database) {
|
2023-09-26 12:45:52 +00:00
|
|
|
if ($database->exclude_from_status) {
|
2023-09-25 13:48:43 +00:00
|
|
|
continue;
|
|
|
|
}
|
2023-09-22 13:29:19 +00:00
|
|
|
if (Str::of($database->status)->startsWith('running')) {
|
2023-09-21 19:30:13 +00:00
|
|
|
$foundRunning = true;
|
2023-09-26 12:45:52 +00:00
|
|
|
} else if (Str::of($database->status)->startsWith('restarting')) {
|
|
|
|
$foundRestaring = true;
|
2023-09-21 19:30:13 +00:00
|
|
|
} else {
|
|
|
|
$isDegraded = true;
|
|
|
|
}
|
|
|
|
}
|
2023-09-26 12:45:52 +00:00
|
|
|
if ($foundRestaring) {
|
|
|
|
return 'degraded';
|
|
|
|
}
|
2023-09-21 19:30:13 +00:00
|
|
|
if ($foundRunning && !$isDegraded) {
|
|
|
|
return 'running';
|
|
|
|
} else if ($foundRunning && $isDegraded) {
|
|
|
|
return 'degraded';
|
2023-09-26 12:45:52 +00:00
|
|
|
} else if (!$foundRunning && !$isDegraded) {
|
2023-09-21 19:30:13 +00:00
|
|
|
return 'exited';
|
|
|
|
}
|
2023-09-25 10:49:55 +00:00
|
|
|
return 'exited';
|
|
|
|
}
|
|
|
|
function saveFileVolumesHelper(ServiceApplication|ServiceDatabase $oneService)
|
|
|
|
{
|
|
|
|
try {
|
|
|
|
$workdir = $oneService->service->workdir();
|
|
|
|
$server = $oneService->service->server;
|
|
|
|
$applicationFileVolume = $oneService->fileStorages()->get();
|
|
|
|
$commands = collect([
|
2023-09-26 12:45:52 +00:00
|
|
|
"mkdir -p $workdir > /dev/null 2>&1 || true",
|
2023-09-25 10:49:55 +00:00
|
|
|
"cd $workdir"
|
|
|
|
]);
|
|
|
|
foreach ($applicationFileVolume as $fileVolume) {
|
|
|
|
$path = Str::of($fileVolume->fs_path);
|
2023-09-26 12:45:52 +00:00
|
|
|
if ($fileVolume->is_directory) {
|
|
|
|
$commands->push("test -f $path && rm -f $path > /dev/null 2>&1 || true");
|
|
|
|
$commands->push("mkdir -p $path > /dev/null 2>&1 || true");
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
$content = $fileVolume->content;
|
2023-09-25 10:49:55 +00:00
|
|
|
$dir = $path->beforeLast('/');
|
|
|
|
if ($dir->startsWith('.')) {
|
|
|
|
$dir = $dir->after('.');
|
|
|
|
$dir = $workdir . $dir;
|
|
|
|
}
|
|
|
|
$content = base64_encode($content);
|
2023-09-26 12:45:52 +00:00
|
|
|
$commands->push("test -d $path && rm -rf $path > /dev/null 2>&1 || true");
|
|
|
|
$commands->push("mkdir -p $dir > /dev/null 2>&1 || true");
|
2023-09-25 10:49:55 +00:00
|
|
|
$commands->push("echo '$content' | base64 -d > $path");
|
|
|
|
}
|
|
|
|
return instant_remote_process($commands, $server);
|
|
|
|
} catch (\Throwable $e) {
|
|
|
|
return handleError($e);
|
|
|
|
}
|
2023-09-21 19:30:13 +00:00
|
|
|
}
|
2023-09-26 12:45:52 +00:00
|
|
|
function switchImage($resource)
|
|
|
|
{
|
|
|
|
try {
|
|
|
|
$name = data_get($resource, 'name');
|
|
|
|
$dockerComposeRaw = data_get($resource, 'service.docker_compose_raw');
|
|
|
|
$image = data_get($resource, 'image');
|
|
|
|
$dockerCompose = Yaml::parse($dockerComposeRaw);
|
|
|
|
data_set($dockerCompose, "services.{$name}.image", $image);
|
|
|
|
$dockerComposeRaw = Yaml::dump($dockerCompose, 10, 2);
|
|
|
|
$resource->service->docker_compose_raw = $dockerComposeRaw;
|
|
|
|
$resource->service->save();
|
|
|
|
} catch (\Throwable $e) {
|
|
|
|
return handleError($e);
|
|
|
|
}
|
|
|
|
}
|