lasthourcloud/bootstrap/helpers/services.php

77 lines
2.2 KiB
PHP
Raw Normal View History

2023-09-19 13:51:13 +00:00
<?php
2023-09-20 13:42:41 +00:00
use App\Models\Service;
use App\Models\ServiceApplication;
use App\Models\ServiceDatabase;
2023-09-22 13:29:19 +00:00
use Illuminate\Support\Str;
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;
$applications = $service->applications;
$databases = $service->databases;
foreach ($applications as $application) {
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;
} else {
$isDegraded = true;
}
}
foreach ($databases as $database) {
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;
} else {
$isDegraded = true;
}
}
if ($foundRunning && !$isDegraded) {
return 'running';
} else if ($foundRunning && $isDegraded) {
return 'degraded';
} else if (!$foundRunning && $isDegraded) {
return 'exited';
}
return 'exited';
}
function saveFileVolumesHelper(ServiceApplication|ServiceDatabase $oneService)
{
try {
$workdir = $oneService->service->workdir();
$server = $oneService->service->server;
$applicationFileVolume = $oneService->fileStorages()->get();
$commands = collect([
"mkdir -p $workdir",
"cd $workdir"
]);
foreach ($applicationFileVolume as $fileVolume) {
$content = $fileVolume->content;
$path = Str::of($fileVolume->fs_path);
$dir = $path->beforeLast('/');
if ($dir->startsWith('.')) {
$dir = $dir->after('.');
$dir = $workdir . $dir;
}
$content = base64_encode($content);
$commands->push("mkdir -p $dir");
$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
}