2023-09-19 13:51:13 +00:00
|
|
|
<?php
|
|
|
|
|
2024-04-15 17:47:17 +00:00
|
|
|
use App\Models\Application;
|
2023-09-27 10:45:53 +00:00
|
|
|
use App\Models\EnvironmentVariable;
|
2023-09-25 10:49:55 +00:00
|
|
|
use App\Models\ServiceApplication;
|
|
|
|
use App\Models\ServiceDatabase;
|
2023-09-22 13:29:19 +00:00
|
|
|
use Illuminate\Support\Str;
|
2024-02-21 11:22:32 +00:00
|
|
|
use Spatie\Url\Url;
|
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)
|
|
|
|
{
|
2024-04-29 10:04:32 +00:00
|
|
|
return $variable->before('}')->replaceFirst('$', '')->replaceFirst('{', '');
|
2023-09-19 13:51:13 +00:00
|
|
|
}
|
2023-09-21 19:30:13 +00:00
|
|
|
|
2024-04-15 17:47:17 +00:00
|
|
|
function getFilesystemVolumesFromServer(ServiceApplication|ServiceDatabase|Application $oneService, bool $isInit = false)
|
2023-09-25 10:49:55 +00:00
|
|
|
{
|
|
|
|
try {
|
2024-04-15 17:47:17 +00:00
|
|
|
if ($oneService->getMorphClass() === 'App\Models\Application') {
|
|
|
|
$workdir = $oneService->workdir();
|
|
|
|
$server = $oneService->destination->server;
|
2024-04-29 10:04:32 +00:00
|
|
|
} else {
|
2024-04-15 17:47:17 +00:00
|
|
|
$workdir = $oneService->service->workdir();
|
|
|
|
$server = $oneService->service->server;
|
|
|
|
}
|
2023-09-29 19:38:11 +00:00
|
|
|
$fileVolumes = $oneService->fileStorages()->get();
|
2023-09-25 10:49:55 +00:00
|
|
|
$commands = collect([
|
2023-09-26 12:45:52 +00:00
|
|
|
"mkdir -p $workdir > /dev/null 2>&1 || true",
|
2024-04-16 18:57:54 +00:00
|
|
|
"cd $workdir"
|
2023-09-25 10:49:55 +00:00
|
|
|
]);
|
2023-09-29 19:38:11 +00:00
|
|
|
instant_remote_process($commands, $server);
|
|
|
|
foreach ($fileVolumes as $fileVolume) {
|
|
|
|
$path = Str::of(data_get($fileVolume, 'fs_path'));
|
|
|
|
$content = data_get($fileVolume, 'content');
|
2023-09-30 13:08:40 +00:00
|
|
|
if ($path->startsWith('.')) {
|
|
|
|
$path = $path->after('.');
|
|
|
|
$fileLocation = $workdir . $path;
|
|
|
|
} else {
|
|
|
|
$fileLocation = $path;
|
|
|
|
}
|
2023-10-04 07:58:39 +00:00
|
|
|
// Exists and is a file
|
2023-09-30 13:08:40 +00:00
|
|
|
$isFile = instant_remote_process(["test -f $fileLocation && echo OK || echo NOK"], $server);
|
2023-10-04 07:58:39 +00:00
|
|
|
// Exists and is a directory
|
2023-09-30 13:08:40 +00:00
|
|
|
$isDir = instant_remote_process(["test -d $fileLocation && echo OK || echo NOK"], $server);
|
2023-10-04 07:58:39 +00:00
|
|
|
|
|
|
|
if ($isFile == 'OK') {
|
|
|
|
// If its a file & exists
|
2023-09-30 13:08:40 +00:00
|
|
|
$filesystemContent = instant_remote_process(["cat $fileLocation"], $server);
|
2023-10-04 07:58:39 +00:00
|
|
|
$fileVolume->content = $filesystemContent;
|
|
|
|
$fileVolume->is_directory = false;
|
|
|
|
$fileVolume->save();
|
|
|
|
} else if ($isDir == 'OK') {
|
|
|
|
// If its a directory & exists
|
|
|
|
$fileVolume->content = null;
|
|
|
|
$fileVolume->is_directory = true;
|
|
|
|
$fileVolume->save();
|
|
|
|
} else if ($isFile == 'NOK' && $isDir == 'NOK' && !$fileVolume->is_directory && $isInit && $content) {
|
|
|
|
// Does not exists (no dir or file), not flagged as directory, is init, has content
|
|
|
|
$fileVolume->content = $content;
|
|
|
|
$fileVolume->is_directory = false;
|
|
|
|
$fileVolume->save();
|
|
|
|
$content = base64_encode($content);
|
|
|
|
$dir = Str::of($fileLocation)->dirname();
|
|
|
|
instant_remote_process([
|
|
|
|
"mkdir -p $dir",
|
2024-04-17 08:49:34 +00:00
|
|
|
"echo '$content' | base64 -d | tee $fileLocation"
|
2023-10-04 07:58:39 +00:00
|
|
|
], $server);
|
|
|
|
} else if ($isFile == 'NOK' && $isDir == 'NOK' && $fileVolume->is_directory && $isInit) {
|
|
|
|
$fileVolume->content = null;
|
|
|
|
$fileVolume->is_directory = true;
|
|
|
|
$fileVolume->save();
|
|
|
|
instant_remote_process(["mkdir -p $fileLocation"], $server);
|
2023-09-25 10:49:55 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
} catch (\Throwable $e) {
|
|
|
|
return handleError($e);
|
|
|
|
}
|
2023-09-21 19:30:13 +00:00
|
|
|
}
|
2024-03-12 14:09:24 +00:00
|
|
|
function updateCompose(ServiceApplication|ServiceDatabase $resource)
|
2023-09-29 19:38:11 +00:00
|
|
|
{
|
2023-09-26 12:45:52 +00:00
|
|
|
try {
|
|
|
|
$name = data_get($resource, 'name');
|
|
|
|
$dockerComposeRaw = data_get($resource, 'service.docker_compose_raw');
|
|
|
|
$dockerCompose = Yaml::parse($dockerComposeRaw);
|
2023-09-27 10:45:53 +00:00
|
|
|
|
|
|
|
// Switch Image
|
2024-04-08 09:17:00 +00:00
|
|
|
$updatedImage = data_get_str($resource, 'image');
|
|
|
|
$currentImage = data_get_str($dockerCompose, "services.{$name}.image");
|
|
|
|
if ($currentImage !== $updatedImage) {
|
|
|
|
data_set($dockerCompose, "services.{$name}.image", $updatedImage->value());
|
|
|
|
$dockerComposeRaw = Yaml::dump($dockerCompose, 10, 2);
|
|
|
|
$resource->service->docker_compose_raw = $dockerComposeRaw;
|
|
|
|
$resource->service->save();
|
|
|
|
$resource->image = $updatedImage;
|
|
|
|
$resource->save();
|
|
|
|
}
|
|
|
|
if ($resource->fqdn) {
|
|
|
|
$resourceFqdns = str($resource->fqdn)->explode(',');
|
|
|
|
if ($resourceFqdns->count() === 1) {
|
|
|
|
$resourceFqdns = $resourceFqdns->first();
|
|
|
|
$variableName = "SERVICE_FQDN_" . Str::of($resource->name)->upper()->replace('-', '');
|
|
|
|
$generatedEnv = EnvironmentVariable::where('service_id', $resource->service_id)->where('key', $variableName)->first();
|
|
|
|
$fqdn = Url::fromString($resourceFqdns);
|
|
|
|
$port = $fqdn->getPort();
|
2024-05-22 13:45:30 +00:00
|
|
|
$path = $fqdn->getPath();
|
2024-04-08 09:17:00 +00:00
|
|
|
$fqdn = $fqdn->getScheme() . '://' . $fqdn->getHost();
|
|
|
|
if ($generatedEnv) {
|
2024-05-22 13:45:30 +00:00
|
|
|
$generatedEnv->value = $fqdn . $path;
|
2024-04-08 09:17:00 +00:00
|
|
|
$generatedEnv->save();
|
|
|
|
}
|
|
|
|
if ($port) {
|
|
|
|
$variableName = $variableName . "_$port";
|
|
|
|
$generatedEnv = EnvironmentVariable::where('service_id', $resource->service_id)->where('key', $variableName)->first();
|
2024-05-22 13:45:30 +00:00
|
|
|
// ray($generatedEnv);
|
2024-04-08 09:17:00 +00:00
|
|
|
if ($generatedEnv) {
|
2024-05-30 19:05:44 +00:00
|
|
|
$generatedEnv->value = $fqdn . $path;
|
2024-04-08 09:17:00 +00:00
|
|
|
$generatedEnv->save();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
$variableName = "SERVICE_URL_" . Str::of($resource->name)->upper()->replace('-', '');
|
|
|
|
$generatedEnv = EnvironmentVariable::where('service_id', $resource->service_id)->where('key', $variableName)->first();
|
|
|
|
$url = Url::fromString($fqdn);
|
|
|
|
$port = $url->getPort();
|
2024-05-22 13:45:30 +00:00
|
|
|
$path = $url->getPath();
|
2024-04-08 09:17:00 +00:00
|
|
|
$url = $url->getHost();
|
|
|
|
if ($generatedEnv) {
|
|
|
|
$url = Str::of($fqdn)->after('://');
|
2024-05-22 13:45:30 +00:00
|
|
|
$generatedEnv->value = $url . $path;
|
2024-04-08 09:17:00 +00:00
|
|
|
$generatedEnv->save();
|
|
|
|
}
|
|
|
|
if ($port) {
|
|
|
|
$variableName = $variableName . "_$port";
|
|
|
|
$generatedEnv = EnvironmentVariable::where('service_id', $resource->service_id)->where('key', $variableName)->first();
|
|
|
|
if ($generatedEnv) {
|
2024-05-22 13:45:30 +00:00
|
|
|
$generatedEnv->value = $url . ':' . $port . $path;
|
2024-04-08 09:17:00 +00:00
|
|
|
$generatedEnv->save();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else if ($resourceFqdns->count() > 1) {
|
|
|
|
foreach ($resourceFqdns as $fqdn) {
|
|
|
|
$host = Url::fromString($fqdn);
|
|
|
|
$port = $host->getPort();
|
|
|
|
$url = $host->getHost();
|
2024-05-22 13:45:30 +00:00
|
|
|
$path = $host->getPath();
|
2024-04-08 09:17:00 +00:00
|
|
|
$host = $host->getScheme() . '://' . $host->getHost();
|
|
|
|
if ($port) {
|
|
|
|
$port_envs = EnvironmentVariable::where('service_id', $resource->service_id)->where('key', 'like', "SERVICE_FQDN_%_$port")->get();
|
|
|
|
foreach ($port_envs as $port_env) {
|
|
|
|
$service_fqdn = str($port_env->key)->beforeLast('_')->after('SERVICE_FQDN_');
|
|
|
|
$env = EnvironmentVariable::where('service_id', $resource->service_id)->where('key', 'SERVICE_FQDN_' . $service_fqdn)->first();
|
|
|
|
if ($env) {
|
2024-05-22 13:45:30 +00:00
|
|
|
$env->value = $host . $path;
|
2024-04-08 09:17:00 +00:00
|
|
|
$env->save();
|
|
|
|
}
|
2024-05-22 13:45:30 +00:00
|
|
|
$port_env->value = $host . ':' . $port . $path;
|
2024-04-08 09:17:00 +00:00
|
|
|
$port_env->save();
|
|
|
|
}
|
|
|
|
$port_envs_url = EnvironmentVariable::where('service_id', $resource->service_id)->where('key', 'like', "SERVICE_URL_%_$port")->get();
|
|
|
|
foreach ($port_envs_url as $port_env_url) {
|
|
|
|
$service_url = str($port_env_url->key)->beforeLast('_')->after('SERVICE_URL_');
|
|
|
|
$env = EnvironmentVariable::where('service_id', $resource->service_id)->where('key', 'SERVICE_URL_' . $service_url)->first();
|
|
|
|
if ($env) {
|
2024-05-22 13:45:30 +00:00
|
|
|
$env->value = $url . $path;
|
2024-04-08 09:17:00 +00:00
|
|
|
$env->save();
|
|
|
|
}
|
2024-05-22 13:45:30 +00:00
|
|
|
$port_env_url->value = $url . ':' . $port . $path;
|
2024-04-08 09:17:00 +00:00
|
|
|
$port_env_url->save();
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
$variableName = "SERVICE_FQDN_" . Str::of($resource->name)->upper()->replace('-', '');
|
|
|
|
$generatedEnv = EnvironmentVariable::where('service_id', $resource->service_id)->where('key', $variableName)->first();
|
|
|
|
$fqdn = Url::fromString($fqdn);
|
2024-05-22 13:45:30 +00:00
|
|
|
$fqdn = $fqdn->getScheme() . '://' . $fqdn->getHost() . $fqdn->getPath();
|
2024-04-08 09:17:00 +00:00
|
|
|
if ($generatedEnv) {
|
|
|
|
$generatedEnv->value = $fqdn;
|
|
|
|
$generatedEnv->save();
|
|
|
|
}
|
|
|
|
$variableName = "SERVICE_URL_" . Str::of($resource->name)->upper()->replace('-', '');
|
|
|
|
$generatedEnv = EnvironmentVariable::where('service_id', $resource->service_id)->where('key', $variableName)->first();
|
|
|
|
$url = Url::fromString($fqdn);
|
2024-05-22 13:45:30 +00:00
|
|
|
$url = $url->getHost() . $url->getPath();
|
2024-04-08 09:17:00 +00:00
|
|
|
if ($generatedEnv) {
|
|
|
|
$url = Str::of($fqdn)->after('://');
|
|
|
|
$generatedEnv->value = $url;
|
|
|
|
$generatedEnv->save();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2023-11-11 20:32:41 +00:00
|
|
|
}
|
2023-10-04 12:40:26 +00:00
|
|
|
}
|
2023-09-26 12:45:52 +00:00
|
|
|
} catch (\Throwable $e) {
|
|
|
|
return handleError($e);
|
|
|
|
}
|
|
|
|
}
|