From e8892b3d29290b69b796a6f2bddeab84d323b392 Mon Sep 17 00:00:00 2001 From: Andras Bacsai <5845193+andrasbacsai@users.noreply.github.com> Date: Mon, 4 Aug 2025 10:46:59 +0200 Subject: [PATCH] feat(core): finally fqdn is fqdn and url is url. haha --- app/Livewire/Project/Application/General.php | 18 +- .../Project/Application/PreviewsCompose.php | 2 +- app/Livewire/Project/CloneMe.php | 2 +- app/Livewire/Project/Resource/Create.php | 2 +- .../Project/Shared/ResourceOperations.php | 2 +- app/Models/Application.php | 18 +- app/Models/Service.php | 6 +- bootstrap/helpers/docker.php | 8 +- bootstrap/helpers/parsers.php | 235 +- bootstrap/helpers/shared.php | 1914 +++++++++-------- 10 files changed, 1159 insertions(+), 1048 deletions(-) diff --git a/app/Livewire/Project/Application/General.php b/app/Livewire/Project/Application/General.php index 2eadbaa0e..e7515b23f 100644 --- a/app/Livewire/Project/Application/General.php +++ b/app/Livewire/Project/Application/General.php @@ -227,7 +227,19 @@ class General extends Component return; } - $this->application->parse(); + + // Refresh parsedServiceDomains to reflect any changes in docker_compose_domains + $this->application->refresh(); + $this->parsedServiceDomains = $this->application->docker_compose_domains ? json_decode($this->application->docker_compose_domains, true) : []; + ray($this->parsedServiceDomains); + // Convert service names with dots to use underscores for HTML form binding + $sanitizedDomains = []; + foreach ($this->parsedServiceDomains as $serviceName => $domain) { + $sanitizedKey = str($serviceName)->slug('_')->toString(); + $sanitizedDomains[$sanitizedKey] = $domain; + } + $this->parsedServiceDomains = $sanitizedDomains; + $showToast && $this->dispatch('success', 'Docker compose file loaded.'); $this->dispatch('compose_loaded'); $this->dispatch('refreshStorages'); @@ -245,7 +257,7 @@ class General extends Component public function generateDomain(string $serviceName) { $uuid = new Cuid2; - $domain = generateFqdn($this->application->destination->server, $uuid); + $domain = generateUrl(server: $this->application->destination->server, random: $uuid); $sanitizedKey = str($serviceName)->slug('_')->toString(); $this->parsedServiceDomains[$sanitizedKey]['domain'] = $domain; @@ -315,7 +327,7 @@ class General extends Component { $server = data_get($this->application, 'destination.server'); if ($server) { - $fqdn = generateFqdn($server, $this->application->uuid); + $fqdn = generateFqdn(server: $server, random: $this->application->uuid, parserVersion: $this->application->compose_parsing_version); $this->application->fqdn = $fqdn; $this->application->save(); $this->resetDefaultLabels(); diff --git a/app/Livewire/Project/Application/PreviewsCompose.php b/app/Livewire/Project/Application/PreviewsCompose.php index 334d96cad..5938b2944 100644 --- a/app/Livewire/Project/Application/PreviewsCompose.php +++ b/app/Livewire/Project/Application/PreviewsCompose.php @@ -48,7 +48,7 @@ class PreviewsCompose extends Component $random = new Cuid2; // Generate a unique domain like main app services do - $generated_fqdn = generateFqdn($server, $random); + $generated_fqdn = generateFqdn(server: $server, random: $random, parserVersion: $this->preview->application->compose_parsing_version); $preview_fqdn = str_replace('{{random}}', $random, $template); $preview_fqdn = str_replace('{{domain}}', str($generated_fqdn)->after('://'), $preview_fqdn); diff --git a/app/Livewire/Project/CloneMe.php b/app/Livewire/Project/CloneMe.php index a5d80a11a..57f4a0c68 100644 --- a/app/Livewire/Project/CloneMe.php +++ b/app/Livewire/Project/CloneMe.php @@ -129,7 +129,7 @@ class CloneMe extends Component $uuid = (string) new Cuid2; $url = $application->fqdn; if ($this->server->proxyType() !== 'NONE' && $applicationSettings->is_container_label_readonly_enabled === true) { - $url = generateFqdn($this->server, $uuid); + $url = generateFqdn(server: $this->server, random: $uuid, parserVersion: $application->compose_parsing_version); } $newApplication = $application->replicate([ diff --git a/app/Livewire/Project/Resource/Create.php b/app/Livewire/Project/Resource/Create.php index d8e397e59..e7cff4f29 100644 --- a/app/Livewire/Project/Resource/Create.php +++ b/app/Livewire/Project/Resource/Create.php @@ -102,7 +102,7 @@ class Create extends Component } }); } - $service->parse(isNew: true, isOneClick: true); + $service->parse(isNew: true); return redirect()->route('project.service.configuration', [ 'service_uuid' => $service->uuid, diff --git a/app/Livewire/Project/Shared/ResourceOperations.php b/app/Livewire/Project/Shared/ResourceOperations.php index c8916bf19..853dbe57a 100644 --- a/app/Livewire/Project/Shared/ResourceOperations.php +++ b/app/Livewire/Project/Shared/ResourceOperations.php @@ -61,7 +61,7 @@ class ResourceOperations extends Component $url = $this->resource->fqdn; if ($server->proxyType() !== 'NONE' && $applicationSettings->is_container_label_readonly_enabled === true) { - $url = generateFqdn($server, $uuid); + $url = generateFqdn(server: $server, random: $uuid, parserVersion: $this->resource->compose_parsing_version); } $new_resource = $this->resource->replicate([ diff --git a/app/Models/Application.php b/app/Models/Application.php index ed97454d2..f74ed89d1 100644 --- a/app/Models/Application.php +++ b/app/Models/Application.php @@ -111,7 +111,7 @@ class Application extends BaseModel { use HasConfiguration, HasFactory, SoftDeletes; - private static $parserVersion = '4'; + private static $parserVersion = '5'; protected $guarded = []; @@ -1442,7 +1442,21 @@ class Application extends BaseModel $parsedServices = $this->parse(); if ($this->docker_compose_domains) { $json = collect(json_decode($this->docker_compose_domains)); - $names = collect(data_get($parsedServices, 'services'))->keys()->toArray(); + foreach ($json as $key => $value) { + if (str($key)->contains('-')) { + $key = str($key)->replace('-', '_'); + } + $json->put((string) $key, $value); + } + $services = collect(data_get($parsedServices, 'services', [])); + foreach ($services as $name => $service) { + if (str($name)->contains('-')) { + $replacedName = str($name)->replace('-', '_'); + $services->put((string) $replacedName, $service); + $services->forget((string) $name); + } + } + $names = collect($services)->keys()->toArray(); $jsonNames = $json->keys()->toArray(); $diff = array_diff($jsonNames, $names); $json = $json->filter(function ($value, $key) use ($diff) { diff --git a/app/Models/Service.php b/app/Models/Service.php index 4bef46642..ed3241f46 100644 --- a/app/Models/Service.php +++ b/app/Models/Service.php @@ -42,7 +42,7 @@ class Service extends BaseModel { use HasFactory, SoftDeletes; - private static $parserVersion = '4'; + private static $parserVersion = '5'; protected $guarded = []; @@ -1274,10 +1274,10 @@ class Service extends BaseModel instant_remote_process($commands, $this->server); } - public function parse(bool $isNew = false, bool $isOneClick = false): Collection + public function parse(bool $isNew = false): Collection { if ((int) $this->compose_parsing_version >= 3) { - return serviceParser($this, isOneClick: $isOneClick); + return serviceParser($this); } elseif ($this->docker_compose_raw) { return parseDockerComposeFile($this, $isNew); } else { diff --git a/bootstrap/helpers/docker.php b/bootstrap/helpers/docker.php index 739f98f22..1737ca714 100644 --- a/bootstrap/helpers/docker.php +++ b/bootstrap/helpers/docker.php @@ -256,12 +256,12 @@ function generateServiceSpecificFqdns(ServiceApplication|Application $resource) if (str($MINIO_BROWSER_REDIRECT_URL->value ?? '')->isEmpty()) { $MINIO_BROWSER_REDIRECT_URL->update([ - 'value' => generateFqdn($server, 'console-'.$uuid, true), + 'value' => generateFqdn(server: $server, random: 'console-'.$uuid, parserVersion: $resource->compose_parsing_version, forceHttps: true), ]); } if (str($MINIO_SERVER_URL->value ?? '')->isEmpty()) { $MINIO_SERVER_URL->update([ - 'value' => generateFqdn($server, 'minio-'.$uuid, true), + 'value' => generateFqdn(server: $server, random: 'minio-'.$uuid, parserVersion: $resource->compose_parsing_version, forceHttps: true), ]); } $payload = collect([ @@ -279,12 +279,12 @@ function generateServiceSpecificFqdns(ServiceApplication|Application $resource) if (str($LOGTO_ENDPOINT->value ?? '')->isEmpty()) { $LOGTO_ENDPOINT->update([ - 'value' => generateFqdn($server, 'logto-'.$uuid), + 'value' => generateFqdn(server: $server, random: 'logto-'.$uuid, parserVersion: $resource->compose_parsing_version), ]); } if (str($LOGTO_ADMIN_ENDPOINT->value ?? '')->isEmpty()) { $LOGTO_ADMIN_ENDPOINT->update([ - 'value' => generateFqdn($server, 'logto-admin-'.$uuid), + 'value' => generateFqdn(server: $server, random: 'logto-admin-'.$uuid, parserVersion: $resource->compose_parsing_version), ]); } $payload = collect([ diff --git a/bootstrap/helpers/parsers.php b/bootstrap/helpers/parsers.php index 649de731d..e5e0cbe86 100644 --- a/bootstrap/helpers/parsers.php +++ b/bootstrap/helpers/parsers.php @@ -16,7 +16,7 @@ use Spatie\Url\Url; use Symfony\Component\Yaml\Yaml; use Visus\Cuid2\Cuid2; -function applicationParser(Application $resource, int $pull_request_id = 0, ?int $preview_id = null, bool $isOneClick = false): Collection +function applicationParser(Application $resource, int $pull_request_id = 0, ?int $preview_id = null): Collection { $uuid = data_get($resource, 'uuid'); $compose = data_get($resource, 'docker_compose_raw'); @@ -100,6 +100,7 @@ function applicationParser(Application $resource, int $pull_request_id = 0, ?int } } // Get magic environments where we need to preset the FQDN + // for example SERVICE_FQDN_APP_3000 (without a value) if ($key->startsWith('SERVICE_FQDN_')) { // SERVICE_FQDN_APP or SERVICE_FQDN_APP_3000 if (substr_count(str($key)->value(), '_') === 3) { @@ -111,7 +112,7 @@ function applicationParser(Application $resource, int $pull_request_id = 0, ?int } $fqdn = $resource->fqdn; if (blank($resource->fqdn)) { - $fqdn = generateFqdn($server, "$uuid"); + $fqdn = generateFqdn(server: $server, random: "$uuid", parserVersion: $resource->compose_parsing_version); } if ($value && get_class($value) === \Illuminate\Support\Stringable::class && $value->startsWith('/')) { @@ -160,44 +161,58 @@ function applicationParser(Application $resource, int $pull_request_id = 0, ?int $allMagicEnvironments = $allMagicEnvironments->merge($magicEnvironments); if ($magicEnvironments->count() > 0) { + // Generate Coolify environment variables foreach ($magicEnvironments as $key => $value) { $key = str($key); $value = replaceVariables($value); $command = parseCommandFromMagicEnvVariable($key); if ($command->value() === 'FQDN') { - if ($isOneClick) { - $fqdnFor = $key->after('SERVICE_FQDN_')->lower()->value(); - if (str($fqdnFor)->contains('_')) { - $fqdnFor = str($fqdnFor)->before('_'); - } - $fqdn = generateFqdn($server, "$fqdnFor-$uuid"); - $resource->environment_variables()->firstOrCreate([ - 'key' => $key->value(), - 'resourceable_type' => get_class($resource), - 'resourceable_id' => $resource->id, - ], [ - 'value' => $fqdn, - 'is_build_time' => false, - 'is_preview' => false, + $fqdnFor = $key->after('SERVICE_FQDN_')->lower()->value(); + if (str($fqdnFor)->contains('-')) { + $fqdnFor = str($fqdnFor)->replace('-', '_'); + } + $fqdn = generateFqdn(server: $server, random: "$fqdnFor-$uuid", parserVersion: $resource->compose_parsing_version); + $url = generateUrl(server: $server, random: "$fqdnFor-$uuid"); + $resource->environment_variables()->firstOrCreate([ + 'key' => $key->value(), + 'resourceable_type' => get_class($resource), + 'resourceable_id' => $resource->id, + ], [ + 'value' => $fqdn, + 'is_build_time' => false, + 'is_preview' => false, + ]); + if ($resource->build_pack === 'dockercompose') { + $domains = collect(json_decode(data_get($resource, 'docker_compose_domains'))) ?? collect([]); + // Put URL in the domains array instead of FQDN + $domains->put((string) $fqdnFor, [ + 'domain' => $url, ]); + $resource->docker_compose_domains = $domains->toJson(); + $resource->save(); } } elseif ($command->value() === 'URL') { - if ($isOneClick) { - $fqdnFor = $key->after('SERVICE_URL_')->lower()->value(); - if (str($fqdnFor)->contains('_')) { - $fqdnFor = str($fqdnFor)->before('_'); - } - $fqdn = generateFqdn($server, "$fqdnFor-$uuid"); - $fqdn = str($fqdn)->replace('http://', '')->replace('https://', ''); - $resource->environment_variables()->firstOrCreate([ - 'key' => $key->value(), - 'resourceable_type' => get_class($resource), - 'resourceable_id' => $resource->id, - ], [ - 'value' => $fqdn, - 'is_build_time' => false, - 'is_preview' => false, + $urlFor = $key->after('SERVICE_URL_')->lower()->value(); + if (str($urlFor)->contains('-')) { + $urlFor = str($urlFor)->replace('-', '_'); + } + $url = generateUrl(server: $server, random: "$urlFor-$uuid"); + $resource->environment_variables()->firstOrCreate([ + 'key' => $key->value(), + 'resourceable_type' => get_class($resource), + 'resourceable_id' => $resource->id, + ], [ + 'value' => $url, + 'is_build_time' => false, + 'is_preview' => false, + ]); + if ($resource->build_pack === 'dockercompose') { + $domains = collect(json_decode(data_get($resource, 'docker_compose_domains'))) ?? collect([]); + $domains->put((string) $urlFor, [ + 'domain' => $url, ]); + $resource->docker_compose_domains = $domains->toJson(); + $resource->save(); } } else { $value = generateEnvValue($command, $resource); @@ -599,7 +614,6 @@ function applicationParser(Application $resource, int $pull_request_id = 0, ?int } else { $domains = collect(json_decode($resource->docker_compose_domains)) ?? collect([]); } - ray($domains); $fqdns = data_get($domains, "$serviceName.domain"); // Generate SERVICE_FQDN & SERVICE_URL for dockercompose if ($resource->build_pack === 'dockercompose') { @@ -849,7 +863,7 @@ function applicationParser(Application $resource, int $pull_request_id = 0, ?int return $topLevel; } -function serviceParser(Service $resource, bool $isOneClick = false): Collection +function serviceParser(Service $resource): Collection { $uuid = data_get($resource, 'uuid'); $compose = data_get($resource, 'docker_compose_raw'); @@ -977,49 +991,86 @@ function serviceParser(Service $resource, bool $isOneClick = false): Collection } } // Get magic environments where we need to preset the FQDN - if ($key->startsWith('SERVICE_FQDN_')) { + if ($key->startsWith('SERVICE_FQDN_') || $key->startsWith('SERVICE_URL_')) { // SERVICE_FQDN_APP or SERVICE_FQDN_APP_3000 if (substr_count(str($key)->value(), '_') === 3) { - $fqdnFor = $key->after('SERVICE_FQDN_')->beforeLast('_')->lower()->value(); + if ($key->startsWith('SERVICE_FQDN_')) { + $urlFor = null; + $fqdnFor = $key->after('SERVICE_FQDN_')->beforeLast('_')->lower()->value(); + } + if ($key->startsWith('SERVICE_URL_')) { + $fqdnFor = null; + $urlFor = $key->after('SERVICE_URL_')->beforeLast('_')->lower()->value(); + } $port = $key->afterLast('_')->value(); } else { - $fqdnFor = $key->after('SERVICE_FQDN_')->lower()->value(); + if ($key->startsWith('SERVICE_FQDN_')) { + $urlFor = null; + $fqdnFor = $key->after('SERVICE_FQDN_')->lower()->value(); + } + if ($key->startsWith('SERVICE_URL_')) { + $fqdnFor = null; + $urlFor = $key->after('SERVICE_URL_')->lower()->value(); + } $port = null; } - if ($isOneClick) { - if (blank($savedService->fqdn)) { - if ($fqdnFor) { - $fqdn = generateFqdn($server, "$fqdnFor-$uuid"); - } else { - $fqdn = generateFqdn($server, "{$savedService->name}-$uuid"); - } + // if ($isOneClick) { + if (blank($savedService->fqdn)) { + if ($fqdnFor) { + $fqdn = generateFqdn(server: $server, random: "$fqdnFor-$uuid", parserVersion: $resource->compose_parsing_version); } else { - $fqdn = str($savedService->fqdn)->after('://')->before(':')->prepend(str($savedService->fqdn)->before('://')->append('://'))->value(); + $fqdn = generateFqdn(server: $server, random: "{$savedService->name}-$uuid", parserVersion: $resource->compose_parsing_version); + } + if ($urlFor) { + $url = generateUrl($server, "$urlFor-$uuid"); + } else { + $url = generateUrl($server, "{$savedService->name}-$uuid"); } - } else { - // For services which are not one-click, if no explicit FQDN is set, leave SERVICE_FQDN_ variables empty - if (blank($savedService->fqdn)) { - $fqdn = ''; - } else { - $fqdn = str($savedService->fqdn)->after('://')->before(':')->prepend(str($savedService->fqdn)->before('://')->append('://'))->value(); - } + $fqdn = str($savedService->fqdn)->after('://')->before(':')->prepend(str($savedService->fqdn)->before('://')->append('://'))->value(); + $url = str($savedService->fqdn)->after('://')->before(':')->prepend(str($savedService->fqdn)->before('://')->append('://'))->value(); } + + // } else { + // // For services which are not one-click, if no explicit FQDN is set, leave SERVICE_FQDN_ variables empty + // if (blank($savedService->fqdn)) { + // $fqdn = ''; + // } else { + // $fqdn = str($savedService->fqdn)->after('://')->before(':')->prepend(str($savedService->fqdn)->before('://')->append('://'))->value(); + // } + // } if ($value && get_class($value) === \Illuminate\Support\Stringable::class && $value->startsWith('/')) { $path = $value->value(); if ($path !== '/') { $fqdn = "$fqdn$path"; + $url = "$url$path"; } } $fqdnWithPort = $fqdn; - if ($port) { + $urlWithPort = $url; + if ($fqdn && $port) { $fqdnWithPort = "$fqdn:$port"; } + if ($url && $port) { + $urlWithPort = "$url:$port"; + } + ray("urlWithPort: $urlWithPort, fqdnWithPort: $fqdnWithPort", "parserVersion: $resource->compose_parsing_version", 'isVersionGreaterThan4207: '.version_compare('4.0.0-beta.420.7', config('constants.coolify.version'), '>=')); if (is_null($savedService->fqdn)) { - $savedService->fqdn = $fqdnWithPort; + if ((int) $resource->compose_parsing_version >= 5 && version_compare(config('constants.coolify.version'), '4.0.0-beta.420.7', '>=')) { + if ($fqdnFor) { + ray("setting fqdn(fqdnWithPort) to $fqdnWithPort"); + $savedService->fqdn = $fqdnWithPort; + } + if ($urlFor) { + ray("setting fqdn(urlWithPort) to $urlWithPort"); + $savedService->fqdn = $urlWithPort; + } + } else { + ray("setting fqdn(fqdnWithPort) old parser version to $fqdnWithPort"); + $savedService->fqdn = $fqdnWithPort; + } $savedService->save(); } - if (substr_count(str($key)->value(), '_') === 2) { $resource->environment_variables()->updateOrCreate([ 'key' => $key->value(), @@ -1030,6 +1081,15 @@ function serviceParser(Service $resource, bool $isOneClick = false): Collection 'is_build_time' => false, 'is_preview' => false, ]); + $resource->environment_variables()->updateOrCreate([ + 'key' => $key->value(), + 'resourceable_type' => get_class($resource), + 'resourceable_id' => $resource->id, + ], [ + 'value' => $url, + 'is_build_time' => false, + 'is_preview' => false, + ]); } if (substr_count(str($key)->value(), '_') === 3) { $newKey = str($key)->beforeLast('_'); @@ -1042,6 +1102,15 @@ function serviceParser(Service $resource, bool $isOneClick = false): Collection 'is_build_time' => false, 'is_preview' => false, ]); + $resource->environment_variables()->updateOrCreate([ + 'key' => $newKey->value(), + 'resourceable_type' => get_class($resource), + 'resourceable_id' => $resource->id, + ], [ + 'value' => $url, + 'is_build_time' => false, + 'is_preview' => false, + ]); } } } @@ -1053,40 +1122,36 @@ function serviceParser(Service $resource, bool $isOneClick = false): Collection $value = replaceVariables($value); $command = parseCommandFromMagicEnvVariable($key); if ($command->value() === 'FQDN') { - if ($isOneClick) { - $fqdnFor = $key->after('SERVICE_FQDN_')->lower()->value(); - if (str($fqdnFor)->contains('_')) { - $fqdnFor = str($fqdnFor)->before('_'); - } - $fqdn = generateFqdn($server, "$fqdnFor-$uuid"); - $resource->environment_variables()->firstOrCreate([ - 'key' => $key->value(), - 'resourceable_type' => get_class($resource), - 'resourceable_id' => $resource->id, - ], [ - 'value' => $fqdn, - 'is_build_time' => false, - 'is_preview' => false, - ]); + $fqdnFor = $key->after('SERVICE_FQDN_')->lower()->value(); + if (str($fqdnFor)->contains('_')) { + $fqdnFor = str($fqdnFor)->before('_'); } + $fqdn = generateFqdn(server: $server, random: "$fqdnFor-$uuid", parserVersion: $resource->compose_parsing_version); + $resource->environment_variables()->firstOrCreate([ + 'key' => $key->value(), + 'resourceable_type' => get_class($resource), + 'resourceable_id' => $resource->id, + ], [ + 'value' => $fqdn, + 'is_build_time' => false, + 'is_preview' => false, + ]); } elseif ($command->value() === 'URL') { - if ($isOneClick) { - $fqdnFor = $key->after('SERVICE_URL_')->lower()->value(); - if (str($fqdnFor)->contains('_')) { - $fqdnFor = str($fqdnFor)->before('_'); - } - $fqdn = generateFqdn($server, "$fqdnFor-$uuid"); - $fqdn = str($fqdn)->replace('http://', '')->replace('https://', ''); - $resource->environment_variables()->firstOrCreate([ - 'key' => $key->value(), - 'resourceable_type' => get_class($resource), - 'resourceable_id' => $resource->id, - ], [ - 'value' => $fqdn, - 'is_build_time' => false, - 'is_preview' => false, - ]); + $fqdnFor = $key->after('SERVICE_URL_')->lower()->value(); + if (str($fqdnFor)->contains('_')) { + $fqdnFor = str($fqdnFor)->before('_'); } + $fqdn = generateFqdn(server: $server, random: "$fqdnFor-$uuid", parserVersion: $resource->compose_parsing_version); + $fqdn = str($fqdn)->replace('http://', '')->replace('https://', ''); + $resource->environment_variables()->firstOrCreate([ + 'key' => $key->value(), + 'resourceable_type' => get_class($resource), + 'resourceable_id' => $resource->id, + ], [ + 'value' => $fqdn, + 'is_build_time' => false, + 'is_preview' => false, + ]); } else { $value = generateEnvValue($command, $resource); $resource->environment_variables()->firstOrCreate([ diff --git a/bootstrap/helpers/shared.php b/bootstrap/helpers/shared.php index 1e9b95288..624e0b0d1 100644 --- a/bootstrap/helpers/shared.php +++ b/bootstrap/helpers/shared.php @@ -402,7 +402,7 @@ function data_get_str($data, $key, $default = null): Stringable return str($str); } -function generateFqdn(Server $server, string $random, bool $forceHttps = false): string +function generateUrl(Server $server, string $random, bool $forceHttps = false): string { $wildcard = data_get($server, 'settings.wildcard_domain'); if (is_null($wildcard) || $wildcard === '') { @@ -418,6 +418,26 @@ function generateFqdn(Server $server, string $random, bool $forceHttps = false): return "$scheme://{$random}.$host$path"; } +function generateFqdn(Server $server, string $random, bool $forceHttps = false, int $parserVersion = 4): string +{ + $wildcard = data_get($server, 'settings.wildcard_domain'); + if (is_null($wildcard) || $wildcard === '') { + $wildcard = sslip($server); + } + $url = Url::fromString($wildcard); + $host = $url->getHost(); + $path = $url->getPath() === '/' ? '' : $url->getPath(); + $scheme = $url->getScheme(); + if ($forceHttps) { + $scheme = 'https'; + } + + if ($parserVersion >= 5 && version_compare(config('constants.coolify.version'), '4.0.0-beta.420.7', '>=')) { + return "{$random}.$host$path"; + } + + return "$scheme://{$random}.$host$path"; +} function sslip(Server $server) { if (isDev() && $server->id === 0) { @@ -2918,1008 +2938,1008 @@ function parseDockerComposeFile(Service|Application $resource, bool $isNew = fal } } -function newParser(Application|Service $resource, int $pull_request_id = 0, ?int $preview_id = null, bool $isOneClick = false): Collection -{ - $isApplication = $resource instanceof Application; - $isService = $resource instanceof Service; - if ($isApplication) { - return applicationParser($resource, $pull_request_id, $preview_id); - } - if ($isService) { - return serviceParser($resource, $isOneClick); - } +// function newParser(Application|Service $resource, int $pull_request_id = 0, ?int $preview_id = null): Collection +// { +// $isApplication = $resource instanceof Application; +// $isService = $resource instanceof Service; +// if ($isApplication) { +// return applicationParser($resource, $pull_request_id, $preview_id); +// } +// if ($isService) { +// return serviceParser($resource); +// } - return collect([]); +// return collect([]); - $uuid = data_get($resource, 'uuid'); - $compose = data_get($resource, 'docker_compose_raw'); - if (! $compose) { - return collect([]); - } +// $uuid = data_get($resource, 'uuid'); +// $compose = data_get($resource, 'docker_compose_raw'); +// if (! $compose) { +// return collect([]); +// } - if ($isApplication) { - $pullRequestId = $pull_request_id; - $isPullRequest = $pullRequestId == 0 ? false : true; - $server = data_get($resource, 'destination.server'); - $fileStorages = $resource->fileStorages(); - } elseif ($isService) { - $server = data_get($resource, 'server'); - $allServices = get_service_templates(); - } else { - return collect([]); - } +// if ($isApplication) { +// $pullRequestId = $pull_request_id; +// $isPullRequest = $pullRequestId == 0 ? false : true; +// $server = data_get($resource, 'destination.server'); +// $fileStorages = $resource->fileStorages(); +// } elseif ($isService) { +// $server = data_get($resource, 'server'); +// $allServices = get_service_templates(); +// } else { +// return collect([]); +// } - try { - $yaml = Yaml::parse($compose); - } catch (\Exception) { - return collect([]); - } - $services = data_get($yaml, 'services', collect([])); - $topLevel = collect([ - 'volumes' => collect(data_get($yaml, 'volumes', [])), - 'networks' => collect(data_get($yaml, 'networks', [])), - 'configs' => collect(data_get($yaml, 'configs', [])), - 'secrets' => collect(data_get($yaml, 'secrets', [])), - ]); - // If there are predefined volumes, make sure they are not null - if ($topLevel->get('volumes')->count() > 0) { - $temp = collect([]); - foreach ($topLevel['volumes'] as $volumeName => $volume) { - if (is_null($volume)) { - continue; - } - $temp->put($volumeName, $volume); - } - $topLevel['volumes'] = $temp; - } - // Get the base docker network - $baseNetwork = collect([$uuid]); - if ($isApplication && $isPullRequest) { - $baseNetwork = collect(["{$uuid}-{$pullRequestId}"]); - } +// try { +// $yaml = Yaml::parse($compose); +// } catch (\Exception) { +// return collect([]); +// } +// $services = data_get($yaml, 'services', collect([])); +// $topLevel = collect([ +// 'volumes' => collect(data_get($yaml, 'volumes', [])), +// 'networks' => collect(data_get($yaml, 'networks', [])), +// 'configs' => collect(data_get($yaml, 'configs', [])), +// 'secrets' => collect(data_get($yaml, 'secrets', [])), +// ]); +// // If there are predefined volumes, make sure they are not null +// if ($topLevel->get('volumes')->count() > 0) { +// $temp = collect([]); +// foreach ($topLevel['volumes'] as $volumeName => $volume) { +// if (is_null($volume)) { +// continue; +// } +// $temp->put($volumeName, $volume); +// } +// $topLevel['volumes'] = $temp; +// } +// // Get the base docker network +// $baseNetwork = collect([$uuid]); +// if ($isApplication && $isPullRequest) { +// $baseNetwork = collect(["{$uuid}-{$pullRequestId}"]); +// } - $parsedServices = collect([]); +// $parsedServices = collect([]); - $allMagicEnvironments = collect([]); - foreach ($services as $serviceName => $service) { - $predefinedPort = null; - $magicEnvironments = collect([]); - $image = data_get_str($service, 'image'); - $environment = collect(data_get($service, 'environment', [])); - $buildArgs = collect(data_get($service, 'build.args', [])); - $environment = $environment->merge($buildArgs); - $isDatabase = isDatabaseImage($image, $service); +// $allMagicEnvironments = collect([]); +// foreach ($services as $serviceName => $service) { +// $predefinedPort = null; +// $magicEnvironments = collect([]); +// $image = data_get_str($service, 'image'); +// $environment = collect(data_get($service, 'environment', [])); +// $buildArgs = collect(data_get($service, 'build.args', [])); +// $environment = $environment->merge($buildArgs); +// $isDatabase = isDatabaseImage($image, $service); - if ($isService) { - $containerName = "$serviceName-{$resource->uuid}"; +// if ($isService) { +// $containerName = "$serviceName-{$resource->uuid}"; - if ($serviceName === 'registry') { - $tempServiceName = 'docker-registry'; - } else { - $tempServiceName = $serviceName; - } - if (str(data_get($service, 'image'))->contains('glitchtip')) { - $tempServiceName = 'glitchtip'; - } - if ($serviceName === 'supabase-kong') { - $tempServiceName = 'supabase'; - } - $serviceDefinition = data_get($allServices, $tempServiceName); - $predefinedPort = data_get($serviceDefinition, 'port'); - if ($serviceName === 'plausible') { - $predefinedPort = '8000'; - } - if ($isDatabase) { - $applicationFound = ServiceApplication::where('name', $serviceName)->where('service_id', $resource->id)->first(); - if ($applicationFound) { - $savedService = $applicationFound; - } else { - $savedService = ServiceDatabase::firstOrCreate([ - 'name' => $serviceName, - 'service_id' => $resource->id, - ]); - } - } else { - $savedService = ServiceApplication::firstOrCreate([ - 'name' => $serviceName, - 'service_id' => $resource->id, - ], [ - 'is_gzip_enabled' => true, - ]); - } - // Check if image changed - if ($savedService->image !== $image) { - $savedService->image = $image; - $savedService->save(); - } - // Pocketbase does not need gzip for SSE. - if (str($savedService->image)->contains('pocketbase') && $savedService->is_gzip_enabled) { - $savedService->is_gzip_enabled = false; - $savedService->save(); - } - } +// if ($serviceName === 'registry') { +// $tempServiceName = 'docker-registry'; +// } else { +// $tempServiceName = $serviceName; +// } +// if (str(data_get($service, 'image'))->contains('glitchtip')) { +// $tempServiceName = 'glitchtip'; +// } +// if ($serviceName === 'supabase-kong') { +// $tempServiceName = 'supabase'; +// } +// $serviceDefinition = data_get($allServices, $tempServiceName); +// $predefinedPort = data_get($serviceDefinition, 'port'); +// if ($serviceName === 'plausible') { +// $predefinedPort = '8000'; +// } +// if ($isDatabase) { +// $applicationFound = ServiceApplication::where('name', $serviceName)->where('service_id', $resource->id)->first(); +// if ($applicationFound) { +// $savedService = $applicationFound; +// } else { +// $savedService = ServiceDatabase::firstOrCreate([ +// 'name' => $serviceName, +// 'service_id' => $resource->id, +// ]); +// } +// } else { +// $savedService = ServiceApplication::firstOrCreate([ +// 'name' => $serviceName, +// 'service_id' => $resource->id, +// ], [ +// 'is_gzip_enabled' => true, +// ]); +// } +// // Check if image changed +// if ($savedService->image !== $image) { +// $savedService->image = $image; +// $savedService->save(); +// } +// // Pocketbase does not need gzip for SSE. +// if (str($savedService->image)->contains('pocketbase') && $savedService->is_gzip_enabled) { +// $savedService->is_gzip_enabled = false; +// $savedService->save(); +// } +// } - $environment = collect(data_get($service, 'environment', [])); - $buildArgs = collect(data_get($service, 'build.args', [])); - $environment = $environment->merge($buildArgs); +// $environment = collect(data_get($service, 'environment', [])); +// $buildArgs = collect(data_get($service, 'build.args', [])); +// $environment = $environment->merge($buildArgs); - // convert environment variables to one format - $environment = convertToKeyValueCollection($environment); +// // convert environment variables to one format +// $environment = convertToKeyValueCollection($environment); - // Add Coolify defined environments - $allEnvironments = $resource->environment_variables()->get(['key', 'value']); +// // Add Coolify defined environments +// $allEnvironments = $resource->environment_variables()->get(['key', 'value']); - $allEnvironments = $allEnvironments->mapWithKeys(function ($item) { - return [$item['key'] => $item['value']]; - }); - // filter and add magic environments - foreach ($environment as $key => $value) { - // Get all SERVICE_ variables from keys and values - $key = str($key); - $value = str($value); - $regex = '/\$(\{?([a-zA-Z_\x80-\xff][a-zA-Z0-9_\x80-\xff]*)\}?)/'; - preg_match_all($regex, $value, $valueMatches); - if (count($valueMatches[1]) > 0) { - foreach ($valueMatches[1] as $match) { - $match = replaceVariables($match); - if ($match->startsWith('SERVICE_')) { - if ($magicEnvironments->has($match->value())) { - continue; - } - $magicEnvironments->put($match->value(), ''); - } - } - } - // Get magic environments where we need to preset the FQDN - if ($key->startsWith('SERVICE_FQDN_')) { - // SERVICE_FQDN_APP or SERVICE_FQDN_APP_3000 - if (substr_count(str($key)->value(), '_') === 3) { - $fqdnFor = $key->after('SERVICE_FQDN_')->beforeLast('_')->lower()->value(); - $port = $key->afterLast('_')->value(); - } else { - $fqdnFor = $key->after('SERVICE_FQDN_')->lower()->value(); - $port = null; - } - if ($isApplication) { - $fqdn = $resource->fqdn; - if (blank($resource->fqdn)) { - $fqdn = generateFqdn($server, "$uuid"); - } - } elseif ($isService) { - if ($isOneClick) { - if (blank($savedService->fqdn)) { - if ($fqdnFor) { - $fqdn = generateFqdn($server, "$fqdnFor-$uuid"); - } else { - $fqdn = generateFqdn($server, "{$savedService->name}-$uuid"); - } - } else { - $fqdn = str($savedService->fqdn)->after('://')->before(':')->prepend(str($savedService->fqdn)->before('://')->append('://'))->value(); - } +// $allEnvironments = $allEnvironments->mapWithKeys(function ($item) { +// return [$item['key'] => $item['value']]; +// }); +// // filter and add magic environments +// foreach ($environment as $key => $value) { +// // Get all SERVICE_ variables from keys and values +// $key = str($key); +// $value = str($value); +// $regex = '/\$(\{?([a-zA-Z_\x80-\xff][a-zA-Z0-9_\x80-\xff]*)\}?)/'; +// preg_match_all($regex, $value, $valueMatches); +// if (count($valueMatches[1]) > 0) { +// foreach ($valueMatches[1] as $match) { +// $match = replaceVariables($match); +// if ($match->startsWith('SERVICE_')) { +// if ($magicEnvironments->has($match->value())) { +// continue; +// } +// $magicEnvironments->put($match->value(), ''); +// } +// } +// } +// // Get magic environments where we need to preset the FQDN +// if ($key->startsWith('SERVICE_FQDN_')) { +// // SERVICE_FQDN_APP or SERVICE_FQDN_APP_3000 +// if (substr_count(str($key)->value(), '_') === 3) { +// $fqdnFor = $key->after('SERVICE_FQDN_')->beforeLast('_')->lower()->value(); +// $port = $key->afterLast('_')->value(); +// } else { +// $fqdnFor = $key->after('SERVICE_FQDN_')->lower()->value(); +// $port = null; +// } +// if ($isApplication) { +// $fqdn = $resource->fqdn; +// if (blank($resource->fqdn)) { +// $fqdn = generateFqdn($server, "$uuid"); +// } +// } elseif ($isService) { +// if ($isOneClick) { +// if (blank($savedService->fqdn)) { +// if ($fqdnFor) { +// $fqdn = generateFqdn($server, "$fqdnFor-$uuid"); +// } else { +// $fqdn = generateFqdn($server, "{$savedService->name}-$uuid"); +// } +// } else { +// $fqdn = str($savedService->fqdn)->after('://')->before(':')->prepend(str($savedService->fqdn)->before('://')->append('://'))->value(); +// } - } else { - // For services which are not one-click, if no explicit FQDN is set, leave SERVICE_FQDN_ variables empty - if (blank($savedService->fqdn)) { - $fqdn = ''; - } else { - $fqdn = str($savedService->fqdn)->after('://')->before(':')->prepend(str($savedService->fqdn)->before('://')->append('://'))->value(); - } - ray($fqdn); - } - } +// } else { +// // For services which are not one-click, if no explicit FQDN is set, leave SERVICE_FQDN_ variables empty +// if (blank($savedService->fqdn)) { +// $fqdn = ''; +// } else { +// $fqdn = str($savedService->fqdn)->after('://')->before(':')->prepend(str($savedService->fqdn)->before('://')->append('://'))->value(); +// } +// ray($fqdn); +// } +// } - if ($value && get_class($value) === \Illuminate\Support\Stringable::class && $value->startsWith('/')) { - $path = $value->value(); - if ($path !== '/') { - $fqdn = "$fqdn$path"; - } - } - $fqdnWithPort = $fqdn; - if ($port) { - $fqdnWithPort = "$fqdn:$port"; - } - if ($isApplication && is_null($resource->fqdn)) { - data_forget($resource, 'environment_variables'); - data_forget($resource, 'environment_variables_preview'); - $resource->fqdn = $fqdnWithPort; - $resource->save(); - } elseif ($isService && is_null($savedService->fqdn)) { - $savedService->fqdn = $fqdnWithPort; - $savedService->save(); - } +// if ($value && get_class($value) === \Illuminate\Support\Stringable::class && $value->startsWith('/')) { +// $path = $value->value(); +// if ($path !== '/') { +// $fqdn = "$fqdn$path"; +// } +// } +// $fqdnWithPort = $fqdn; +// if ($port) { +// $fqdnWithPort = "$fqdn:$port"; +// } +// if ($isApplication && is_null($resource->fqdn)) { +// data_forget($resource, 'environment_variables'); +// data_forget($resource, 'environment_variables_preview'); +// $resource->fqdn = $fqdnWithPort; +// $resource->save(); +// } elseif ($isService && is_null($savedService->fqdn)) { +// $savedService->fqdn = $fqdnWithPort; +// $savedService->save(); +// } - if (substr_count(str($key)->value(), '_') === 2) { - $resource->environment_variables()->updateOrCreate([ - 'key' => $key->value(), - 'resourceable_type' => get_class($resource), - 'resourceable_id' => $resource->id, - ], [ - 'value' => $fqdn, - 'is_build_time' => false, - 'is_preview' => false, - ]); - } - if (substr_count(str($key)->value(), '_') === 3) { - $newKey = str($key)->beforeLast('_'); - $resource->environment_variables()->updateOrCreate([ - 'key' => $newKey->value(), - 'resourceable_type' => get_class($resource), - 'resourceable_id' => $resource->id, - ], [ - 'value' => $fqdn, - 'is_build_time' => false, - 'is_preview' => false, - ]); - } - } - } +// if (substr_count(str($key)->value(), '_') === 2) { +// $resource->environment_variables()->updateOrCreate([ +// 'key' => $key->value(), +// 'resourceable_type' => get_class($resource), +// 'resourceable_id' => $resource->id, +// ], [ +// 'value' => $fqdn, +// 'is_build_time' => false, +// 'is_preview' => false, +// ]); +// } +// if (substr_count(str($key)->value(), '_') === 3) { +// $newKey = str($key)->beforeLast('_'); +// $resource->environment_variables()->updateOrCreate([ +// 'key' => $newKey->value(), +// 'resourceable_type' => get_class($resource), +// 'resourceable_id' => $resource->id, +// ], [ +// 'value' => $fqdn, +// 'is_build_time' => false, +// 'is_preview' => false, +// ]); +// } +// } +// } - $allMagicEnvironments = $allMagicEnvironments->merge($magicEnvironments); - if ($magicEnvironments->count() > 0) { - foreach ($magicEnvironments as $key => $value) { - $key = str($key); - $value = replaceVariables($value); - $command = parseCommandFromMagicEnvVariable($key); - if ($command->value() === 'FQDN') { - if ($isOneClick) { - $fqdnFor = $key->after('SERVICE_FQDN_')->lower()->value(); - if (str($fqdnFor)->contains('_')) { - $fqdnFor = str($fqdnFor)->before('_'); - } - $fqdn = generateFqdn($server, "$fqdnFor-$uuid"); - $resource->environment_variables()->firstOrCreate([ - 'key' => $key->value(), - 'resourceable_type' => get_class($resource), - 'resourceable_id' => $resource->id, - ], [ - 'value' => $fqdn, - 'is_build_time' => false, - 'is_preview' => false, - ]); - } - } elseif ($command->value() === 'URL') { - if ($isOneClick) { - $fqdnFor = $key->after('SERVICE_URL_')->lower()->value(); - if (str($fqdnFor)->contains('_')) { - $fqdnFor = str($fqdnFor)->before('_'); - } - $fqdn = generateFqdn($server, "$fqdnFor-$uuid"); - $fqdn = str($fqdn)->replace('http://', '')->replace('https://', ''); - $resource->environment_variables()->firstOrCreate([ - 'key' => $key->value(), - 'resourceable_type' => get_class($resource), - 'resourceable_id' => $resource->id, - ], [ - 'value' => $fqdn, - 'is_build_time' => false, - 'is_preview' => false, - ]); - } - } else { - $value = generateEnvValue($command, $resource); - $resource->environment_variables()->firstOrCreate([ - 'key' => $key->value(), - 'resourceable_type' => get_class($resource), - 'resourceable_id' => $resource->id, - ], [ - 'value' => $value, - 'is_build_time' => false, - 'is_preview' => false, - ]); - } - } - } - } +// $allMagicEnvironments = $allMagicEnvironments->merge($magicEnvironments); +// if ($magicEnvironments->count() > 0) { +// foreach ($magicEnvironments as $key => $value) { +// $key = str($key); +// $value = replaceVariables($value); +// $command = parseCommandFromMagicEnvVariable($key); +// if ($command->value() === 'FQDN') { +// if ($isOneClick) { +// $fqdnFor = $key->after('SERVICE_FQDN_')->lower()->value(); +// if (str($fqdnFor)->contains('_')) { +// $fqdnFor = str($fqdnFor)->before('_'); +// } +// $fqdn = generateFqdn($server, "$fqdnFor-$uuid"); +// $resource->environment_variables()->firstOrCreate([ +// 'key' => $key->value(), +// 'resourceable_type' => get_class($resource), +// 'resourceable_id' => $resource->id, +// ], [ +// 'value' => $fqdn, +// 'is_build_time' => false, +// 'is_preview' => false, +// ]); +// } +// } elseif ($command->value() === 'URL') { +// if ($isOneClick) { +// $fqdnFor = $key->after('SERVICE_URL_')->lower()->value(); +// if (str($fqdnFor)->contains('_')) { +// $fqdnFor = str($fqdnFor)->before('_'); +// } +// $fqdn = generateFqdn($server, "$fqdnFor-$uuid"); +// $fqdn = str($fqdn)->replace('http://', '')->replace('https://', ''); +// $resource->environment_variables()->firstOrCreate([ +// 'key' => $key->value(), +// 'resourceable_type' => get_class($resource), +// 'resourceable_id' => $resource->id, +// ], [ +// 'value' => $fqdn, +// 'is_build_time' => false, +// 'is_preview' => false, +// ]); +// } +// } else { +// $value = generateEnvValue($command, $resource); +// $resource->environment_variables()->firstOrCreate([ +// 'key' => $key->value(), +// 'resourceable_type' => get_class($resource), +// 'resourceable_id' => $resource->id, +// ], [ +// 'value' => $value, +// 'is_build_time' => false, +// 'is_preview' => false, +// ]); +// } +// } +// } +// } - $serviceAppsLogDrainEnabledMap = collect([]); - if ($resource instanceof Service) { - $serviceAppsLogDrainEnabledMap = $resource->applications()->get()->keyBy('name')->map(function ($app) { - return $app->isLogDrainEnabled(); - }); - } +// $serviceAppsLogDrainEnabledMap = collect([]); +// if ($resource instanceof Service) { +// $serviceAppsLogDrainEnabledMap = $resource->applications()->get()->keyBy('name')->map(function ($app) { +// return $app->isLogDrainEnabled(); +// }); +// } - // Parse the rest of the services - foreach ($services as $serviceName => $service) { - $image = data_get_str($service, 'image'); - $restart = data_get_str($service, 'restart', RESTART_MODE); - $logging = data_get($service, 'logging'); +// // Parse the rest of the services +// foreach ($services as $serviceName => $service) { +// $image = data_get_str($service, 'image'); +// $restart = data_get_str($service, 'restart', RESTART_MODE); +// $logging = data_get($service, 'logging'); - if ($server->isLogDrainEnabled()) { - if ($resource instanceof Application && $resource->isLogDrainEnabled()) { - $logging = generate_fluentd_configuration(); - } - if ($resource instanceof Service && $serviceAppsLogDrainEnabledMap->get($serviceName)) { - $logging = generate_fluentd_configuration(); - } - } - $volumes = collect(data_get($service, 'volumes', [])); - $networks = collect(data_get($service, 'networks', [])); - $use_network_mode = data_get($service, 'network_mode') !== null; - $depends_on = collect(data_get($service, 'depends_on', [])); - $labels = collect(data_get($service, 'labels', [])); - if ($labels->count() > 0) { - if (isAssociativeArray($labels)) { - $newLabels = collect([]); - $labels->each(function ($value, $key) use ($newLabels) { - $newLabels->push("$key=$value"); - }); - $labels = $newLabels; - } - } - $environment = collect(data_get($service, 'environment', [])); - $ports = collect(data_get($service, 'ports', [])); - $buildArgs = collect(data_get($service, 'build.args', [])); - $environment = $environment->merge($buildArgs); +// if ($server->isLogDrainEnabled()) { +// if ($resource instanceof Application && $resource->isLogDrainEnabled()) { +// $logging = generate_fluentd_configuration(); +// } +// if ($resource instanceof Service && $serviceAppsLogDrainEnabledMap->get($serviceName)) { +// $logging = generate_fluentd_configuration(); +// } +// } +// $volumes = collect(data_get($service, 'volumes', [])); +// $networks = collect(data_get($service, 'networks', [])); +// $use_network_mode = data_get($service, 'network_mode') !== null; +// $depends_on = collect(data_get($service, 'depends_on', [])); +// $labels = collect(data_get($service, 'labels', [])); +// if ($labels->count() > 0) { +// if (isAssociativeArray($labels)) { +// $newLabels = collect([]); +// $labels->each(function ($value, $key) use ($newLabels) { +// $newLabels->push("$key=$value"); +// }); +// $labels = $newLabels; +// } +// } +// $environment = collect(data_get($service, 'environment', [])); +// $ports = collect(data_get($service, 'ports', [])); +// $buildArgs = collect(data_get($service, 'build.args', [])); +// $environment = $environment->merge($buildArgs); - $environment = convertToKeyValueCollection($environment); - $coolifyEnvironments = collect([]); +// $environment = convertToKeyValueCollection($environment); +// $coolifyEnvironments = collect([]); - $isDatabase = isDatabaseImage($image, $service); - $volumesParsed = collect([]); +// $isDatabase = isDatabaseImage($image, $service); +// $volumesParsed = collect([]); - if ($isApplication) { - $baseName = generateApplicationContainerName( - application: $resource, - pull_request_id: $pullRequestId - ); - $containerName = "$serviceName-$baseName"; - $predefinedPort = null; - } elseif ($isService) { - $containerName = "$serviceName-{$resource->uuid}"; +// if ($isApplication) { +// $baseName = generateApplicationContainerName( +// application: $resource, +// pull_request_id: $pullRequestId +// ); +// $containerName = "$serviceName-$baseName"; +// $predefinedPort = null; +// } elseif ($isService) { +// $containerName = "$serviceName-{$resource->uuid}"; - if ($serviceName === 'registry') { - $tempServiceName = 'docker-registry'; - } else { - $tempServiceName = $serviceName; - } - if (str(data_get($service, 'image'))->contains('glitchtip')) { - $tempServiceName = 'glitchtip'; - } - if ($serviceName === 'supabase-kong') { - $tempServiceName = 'supabase'; - } - $serviceDefinition = data_get($allServices, $tempServiceName); - $predefinedPort = data_get($serviceDefinition, 'port'); - if ($serviceName === 'plausible') { - $predefinedPort = '8000'; - } +// if ($serviceName === 'registry') { +// $tempServiceName = 'docker-registry'; +// } else { +// $tempServiceName = $serviceName; +// } +// if (str(data_get($service, 'image'))->contains('glitchtip')) { +// $tempServiceName = 'glitchtip'; +// } +// if ($serviceName === 'supabase-kong') { +// $tempServiceName = 'supabase'; +// } +// $serviceDefinition = data_get($allServices, $tempServiceName); +// $predefinedPort = data_get($serviceDefinition, 'port'); +// if ($serviceName === 'plausible') { +// $predefinedPort = '8000'; +// } - if ($isDatabase) { - $applicationFound = ServiceApplication::where('name', $serviceName)->where('image', $image)->where('service_id', $resource->id)->first(); - if ($applicationFound) { - $savedService = $applicationFound; - } else { - $savedService = ServiceDatabase::firstOrCreate([ - 'name' => $serviceName, - 'image' => $image, - 'service_id' => $resource->id, - ]); - } - } else { - $savedService = ServiceApplication::firstOrCreate([ - 'name' => $serviceName, - 'image' => $image, - 'service_id' => $resource->id, - ]); - } - $fileStorages = $savedService->fileStorages(); - if ($savedService->image !== $image) { - $savedService->image = $image; - $savedService->save(); - } - } +// if ($isDatabase) { +// $applicationFound = ServiceApplication::where('name', $serviceName)->where('image', $image)->where('service_id', $resource->id)->first(); +// if ($applicationFound) { +// $savedService = $applicationFound; +// } else { +// $savedService = ServiceDatabase::firstOrCreate([ +// 'name' => $serviceName, +// 'image' => $image, +// 'service_id' => $resource->id, +// ]); +// } +// } else { +// $savedService = ServiceApplication::firstOrCreate([ +// 'name' => $serviceName, +// 'image' => $image, +// 'service_id' => $resource->id, +// ]); +// } +// $fileStorages = $savedService->fileStorages(); +// if ($savedService->image !== $image) { +// $savedService->image = $image; +// $savedService->save(); +// } +// } - $originalResource = $isApplication ? $resource : $savedService; +// $originalResource = $isApplication ? $resource : $savedService; - if ($volumes->count() > 0) { - foreach ($volumes as $index => $volume) { - $type = null; - $source = null; - $target = null; - $content = null; - $isDirectory = false; - if (is_string($volume)) { - $source = str($volume)->before(':'); - $target = str($volume)->after(':')->beforeLast(':'); - $foundConfig = $fileStorages->whereMountPath($target)->first(); - if (sourceIsLocal($source)) { - $type = str('bind'); - if ($foundConfig) { - $contentNotNull_temp = data_get($foundConfig, 'content'); - if ($contentNotNull_temp) { - $content = $contentNotNull_temp; - } - $isDirectory = data_get($foundConfig, 'is_directory'); - } else { - // By default, we cannot determine if the bind is a directory or not, so we set it to directory - $isDirectory = true; - } - } else { - $type = str('volume'); - } - } elseif (is_array($volume)) { - $type = data_get_str($volume, 'type'); - $source = data_get_str($volume, 'source'); - $target = data_get_str($volume, 'target'); - $content = data_get($volume, 'content'); - $isDirectory = (bool) data_get($volume, 'isDirectory', null) || (bool) data_get($volume, 'is_directory', null); +// if ($volumes->count() > 0) { +// foreach ($volumes as $index => $volume) { +// $type = null; +// $source = null; +// $target = null; +// $content = null; +// $isDirectory = false; +// if (is_string($volume)) { +// $source = str($volume)->before(':'); +// $target = str($volume)->after(':')->beforeLast(':'); +// $foundConfig = $fileStorages->whereMountPath($target)->first(); +// if (sourceIsLocal($source)) { +// $type = str('bind'); +// if ($foundConfig) { +// $contentNotNull_temp = data_get($foundConfig, 'content'); +// if ($contentNotNull_temp) { +// $content = $contentNotNull_temp; +// } +// $isDirectory = data_get($foundConfig, 'is_directory'); +// } else { +// // By default, we cannot determine if the bind is a directory or not, so we set it to directory +// $isDirectory = true; +// } +// } else { +// $type = str('volume'); +// } +// } elseif (is_array($volume)) { +// $type = data_get_str($volume, 'type'); +// $source = data_get_str($volume, 'source'); +// $target = data_get_str($volume, 'target'); +// $content = data_get($volume, 'content'); +// $isDirectory = (bool) data_get($volume, 'isDirectory', null) || (bool) data_get($volume, 'is_directory', null); - $foundConfig = $fileStorages->whereMountPath($target)->first(); - if ($foundConfig) { - $contentNotNull_temp = data_get($foundConfig, 'content'); - if ($contentNotNull_temp) { - $content = $contentNotNull_temp; - } - $isDirectory = data_get($foundConfig, 'is_directory'); - } else { - // if isDirectory is not set (or false) & content is also not set, we assume it is a directory - if ((is_null($isDirectory) || ! $isDirectory) && is_null($content)) { - $isDirectory = true; - } - } - } - if ($type->value() === 'bind') { - if ($source->value() === '/var/run/docker.sock') { - $volume = $source->value().':'.$target->value(); - } elseif ($source->value() === '/tmp' || $source->value() === '/tmp/') { - $volume = $source->value().':'.$target->value(); - } else { - if ((int) $resource->compose_parsing_version >= 4) { - if ($isApplication) { - $mainDirectory = str(base_configuration_dir().'/applications/'.$uuid); - } elseif ($isService) { - $mainDirectory = str(base_configuration_dir().'/services/'.$uuid); - } - } else { - $mainDirectory = str(base_configuration_dir().'/applications/'.$uuid); - } - $source = replaceLocalSource($source, $mainDirectory); - if ($isApplication && $isPullRequest) { - $source = $source."-pr-$pullRequestId"; - } - LocalFileVolume::updateOrCreate( - [ - 'mount_path' => $target, - 'resource_id' => $originalResource->id, - 'resource_type' => get_class($originalResource), - ], - [ - 'fs_path' => $source, - 'mount_path' => $target, - 'content' => $content, - 'is_directory' => $isDirectory, - 'resource_id' => $originalResource->id, - 'resource_type' => get_class($originalResource), - ] - ); - if (isDev()) { - if ((int) $resource->compose_parsing_version >= 4) { - if ($isApplication) { - $source = $source->replace($mainDirectory, '/var/lib/docker/volumes/coolify_dev_coolify_data/_data/applications/'.$uuid); - } elseif ($isService) { - $source = $source->replace($mainDirectory, '/var/lib/docker/volumes/coolify_dev_coolify_data/_data/services/'.$uuid); - } - } else { - $source = $source->replace($mainDirectory, '/var/lib/docker/volumes/coolify_dev_coolify_data/_data/applications/'.$uuid); - } - } - $volume = "$source:$target"; - } - } elseif ($type->value() === 'volume') { - if ($topLevel->get('volumes')->has($source->value())) { - $temp = $topLevel->get('volumes')->get($source->value()); - if (data_get($temp, 'driver_opts.type') === 'cifs') { - continue; - } - if (data_get($temp, 'driver_opts.type') === 'nfs') { - continue; - } - } - $slugWithoutUuid = Str::slug($source, '-'); - $name = "{$uuid}_{$slugWithoutUuid}"; +// $foundConfig = $fileStorages->whereMountPath($target)->first(); +// if ($foundConfig) { +// $contentNotNull_temp = data_get($foundConfig, 'content'); +// if ($contentNotNull_temp) { +// $content = $contentNotNull_temp; +// } +// $isDirectory = data_get($foundConfig, 'is_directory'); +// } else { +// // if isDirectory is not set (or false) & content is also not set, we assume it is a directory +// if ((is_null($isDirectory) || ! $isDirectory) && is_null($content)) { +// $isDirectory = true; +// } +// } +// } +// if ($type->value() === 'bind') { +// if ($source->value() === '/var/run/docker.sock') { +// $volume = $source->value().':'.$target->value(); +// } elseif ($source->value() === '/tmp' || $source->value() === '/tmp/') { +// $volume = $source->value().':'.$target->value(); +// } else { +// if ((int) $resource->compose_parsing_version >= 4) { +// if ($isApplication) { +// $mainDirectory = str(base_configuration_dir().'/applications/'.$uuid); +// } elseif ($isService) { +// $mainDirectory = str(base_configuration_dir().'/services/'.$uuid); +// } +// } else { +// $mainDirectory = str(base_configuration_dir().'/applications/'.$uuid); +// } +// $source = replaceLocalSource($source, $mainDirectory); +// if ($isApplication && $isPullRequest) { +// $source = $source."-pr-$pullRequestId"; +// } +// LocalFileVolume::updateOrCreate( +// [ +// 'mount_path' => $target, +// 'resource_id' => $originalResource->id, +// 'resource_type' => get_class($originalResource), +// ], +// [ +// 'fs_path' => $source, +// 'mount_path' => $target, +// 'content' => $content, +// 'is_directory' => $isDirectory, +// 'resource_id' => $originalResource->id, +// 'resource_type' => get_class($originalResource), +// ] +// ); +// if (isDev()) { +// if ((int) $resource->compose_parsing_version >= 4) { +// if ($isApplication) { +// $source = $source->replace($mainDirectory, '/var/lib/docker/volumes/coolify_dev_coolify_data/_data/applications/'.$uuid); +// } elseif ($isService) { +// $source = $source->replace($mainDirectory, '/var/lib/docker/volumes/coolify_dev_coolify_data/_data/services/'.$uuid); +// } +// } else { +// $source = $source->replace($mainDirectory, '/var/lib/docker/volumes/coolify_dev_coolify_data/_data/applications/'.$uuid); +// } +// } +// $volume = "$source:$target"; +// } +// } elseif ($type->value() === 'volume') { +// if ($topLevel->get('volumes')->has($source->value())) { +// $temp = $topLevel->get('volumes')->get($source->value()); +// if (data_get($temp, 'driver_opts.type') === 'cifs') { +// continue; +// } +// if (data_get($temp, 'driver_opts.type') === 'nfs') { +// continue; +// } +// } +// $slugWithoutUuid = Str::slug($source, '-'); +// $name = "{$uuid}_{$slugWithoutUuid}"; - if ($isApplication && $isPullRequest) { - $name = "{$name}-pr-$pullRequestId"; - } - if (is_string($volume)) { - $source = str($volume)->before(':'); - $target = str($volume)->after(':')->beforeLast(':'); - $source = $name; - $volume = "$source:$target"; - } elseif (is_array($volume)) { - data_set($volume, 'source', $name); - } - $topLevel->get('volumes')->put($name, [ - 'name' => $name, - ]); - LocalPersistentVolume::updateOrCreate( - [ - 'name' => $name, - 'resource_id' => $originalResource->id, - 'resource_type' => get_class($originalResource), - ], - [ - 'name' => $name, - 'mount_path' => $target, - 'resource_id' => $originalResource->id, - 'resource_type' => get_class($originalResource), - ] - ); - } - dispatch(new ServerFilesFromServerJob($originalResource)); - $volumesParsed->put($index, $volume); - } - } +// if ($isApplication && $isPullRequest) { +// $name = "{$name}-pr-$pullRequestId"; +// } +// if (is_string($volume)) { +// $source = str($volume)->before(':'); +// $target = str($volume)->after(':')->beforeLast(':'); +// $source = $name; +// $volume = "$source:$target"; +// } elseif (is_array($volume)) { +// data_set($volume, 'source', $name); +// } +// $topLevel->get('volumes')->put($name, [ +// 'name' => $name, +// ]); +// LocalPersistentVolume::updateOrCreate( +// [ +// 'name' => $name, +// 'resource_id' => $originalResource->id, +// 'resource_type' => get_class($originalResource), +// ], +// [ +// 'name' => $name, +// 'mount_path' => $target, +// 'resource_id' => $originalResource->id, +// 'resource_type' => get_class($originalResource), +// ] +// ); +// } +// dispatch(new ServerFilesFromServerJob($originalResource)); +// $volumesParsed->put($index, $volume); +// } +// } - if ($depends_on?->count() > 0) { - if ($isApplication && $isPullRequest) { - $newDependsOn = collect([]); - $depends_on->each(function ($dependency, $condition) use ($pullRequestId, $newDependsOn) { - if (is_numeric($condition)) { - $dependency = "$dependency-pr-$pullRequestId"; +// if ($depends_on?->count() > 0) { +// if ($isApplication && $isPullRequest) { +// $newDependsOn = collect([]); +// $depends_on->each(function ($dependency, $condition) use ($pullRequestId, $newDependsOn) { +// if (is_numeric($condition)) { +// $dependency = "$dependency-pr-$pullRequestId"; - $newDependsOn->put($condition, $dependency); - } else { - $condition = "$condition-pr-$pullRequestId"; - $newDependsOn->put($condition, $dependency); - } - }); - $depends_on = $newDependsOn; - } - } - if (! $use_network_mode) { - if ($topLevel->get('networks')?->count() > 0) { - foreach ($topLevel->get('networks') as $networkName => $network) { - if ($networkName === 'default') { - continue; - } - // ignore aliases - if ($network['aliases'] ?? false) { - continue; - } - $networkExists = $networks->contains(function ($value, $key) use ($networkName) { - return $value == $networkName || $key == $networkName; - }); - if (! $networkExists) { - $networks->put($networkName, null); - } - } - } - $baseNetworkExists = $networks->contains(function ($value, $_) use ($baseNetwork) { - return $value == $baseNetwork; - }); - if (! $baseNetworkExists) { - foreach ($baseNetwork as $network) { - $topLevel->get('networks')->put($network, [ - 'name' => $network, - 'external' => true, - ]); - } - } - } +// $newDependsOn->put($condition, $dependency); +// } else { +// $condition = "$condition-pr-$pullRequestId"; +// $newDependsOn->put($condition, $dependency); +// } +// }); +// $depends_on = $newDependsOn; +// } +// } +// if (! $use_network_mode) { +// if ($topLevel->get('networks')?->count() > 0) { +// foreach ($topLevel->get('networks') as $networkName => $network) { +// if ($networkName === 'default') { +// continue; +// } +// // ignore aliases +// if ($network['aliases'] ?? false) { +// continue; +// } +// $networkExists = $networks->contains(function ($value, $key) use ($networkName) { +// return $value == $networkName || $key == $networkName; +// }); +// if (! $networkExists) { +// $networks->put($networkName, null); +// } +// } +// } +// $baseNetworkExists = $networks->contains(function ($value, $_) use ($baseNetwork) { +// return $value == $baseNetwork; +// }); +// if (! $baseNetworkExists) { +// foreach ($baseNetwork as $network) { +// $topLevel->get('networks')->put($network, [ +// 'name' => $network, +// 'external' => true, +// ]); +// } +// } +// } - // Collect/create/update ports - $collectedPorts = collect([]); - if ($ports->count() > 0) { - foreach ($ports as $sport) { - if (is_string($sport) || is_numeric($sport)) { - $collectedPorts->push($sport); - } - if (is_array($sport)) { - $target = data_get($sport, 'target'); - $published = data_get($sport, 'published'); - $protocol = data_get($sport, 'protocol'); - $collectedPorts->push("$target:$published/$protocol"); - } - } - } - if ($isService) { - $originalResource->ports = $collectedPorts->implode(','); - $originalResource->save(); - } +// // Collect/create/update ports +// $collectedPorts = collect([]); +// if ($ports->count() > 0) { +// foreach ($ports as $sport) { +// if (is_string($sport) || is_numeric($sport)) { +// $collectedPorts->push($sport); +// } +// if (is_array($sport)) { +// $target = data_get($sport, 'target'); +// $published = data_get($sport, 'published'); +// $protocol = data_get($sport, 'protocol'); +// $collectedPorts->push("$target:$published/$protocol"); +// } +// } +// } +// if ($isService) { +// $originalResource->ports = $collectedPorts->implode(','); +// $originalResource->save(); +// } - $networks_temp = collect(); +// $networks_temp = collect(); - if (! $use_network_mode) { - foreach ($networks as $key => $network) { - if (gettype($network) === 'string') { - // networks: - // - appwrite - $networks_temp->put($network, null); - } elseif (gettype($network) === 'array') { - // networks: - // default: - // ipv4_address: 192.168.203.254 - $networks_temp->put($key, $network); - } - } - foreach ($baseNetwork as $key => $network) { - $networks_temp->put($network, null); - } +// if (! $use_network_mode) { +// foreach ($networks as $key => $network) { +// if (gettype($network) === 'string') { +// // networks: +// // - appwrite +// $networks_temp->put($network, null); +// } elseif (gettype($network) === 'array') { +// // networks: +// // default: +// // ipv4_address: 192.168.203.254 +// $networks_temp->put($key, $network); +// } +// } +// foreach ($baseNetwork as $key => $network) { +// $networks_temp->put($network, null); +// } - if ($isApplication) { - if (data_get($resource, 'settings.connect_to_docker_network')) { - $network = $resource->destination->network; - $networks_temp->put($network, null); - $topLevel->get('networks')->put($network, [ - 'name' => $network, - 'external' => true, - ]); - } - } - } +// if ($isApplication) { +// if (data_get($resource, 'settings.connect_to_docker_network')) { +// $network = $resource->destination->network; +// $networks_temp->put($network, null); +// $topLevel->get('networks')->put($network, [ +// 'name' => $network, +// 'external' => true, +// ]); +// } +// } +// } - $normalEnvironments = $environment->diffKeys($allMagicEnvironments); - $normalEnvironments = $normalEnvironments->filter(function ($value, $key) { - return ! str($value)->startsWith('SERVICE_'); - }); - foreach ($normalEnvironments as $key => $value) { - $key = str($key); - $value = str($value); - $originalValue = $value; - $parsedValue = replaceVariables($value); - if ($value->startsWith('$SERVICE_')) { - $resource->environment_variables()->firstOrCreate([ - 'key' => $key, - 'resourceable_type' => get_class($resource), - 'resourceable_id' => $resource->id, - ], [ - 'value' => $value, - 'is_build_time' => false, - 'is_preview' => false, - ]); +// $normalEnvironments = $environment->diffKeys($allMagicEnvironments); +// $normalEnvironments = $normalEnvironments->filter(function ($value, $key) { +// return ! str($value)->startsWith('SERVICE_'); +// }); +// foreach ($normalEnvironments as $key => $value) { +// $key = str($key); +// $value = str($value); +// $originalValue = $value; +// $parsedValue = replaceVariables($value); +// if ($value->startsWith('$SERVICE_')) { +// $resource->environment_variables()->firstOrCreate([ +// 'key' => $key, +// 'resourceable_type' => get_class($resource), +// 'resourceable_id' => $resource->id, +// ], [ +// 'value' => $value, +// 'is_build_time' => false, +// 'is_preview' => false, +// ]); - continue; - } - if (! $value->startsWith('$')) { - continue; - } - if ($key->value() === $parsedValue->value()) { - $value = null; - $resource->environment_variables()->firstOrCreate([ - 'key' => $key, - 'resourceable_type' => get_class($resource), - 'resourceable_id' => $resource->id, - ], [ - 'value' => $value, - 'is_build_time' => false, - 'is_preview' => false, - ]); - } else { - if ($value->startsWith('$')) { - $isRequired = false; - if ($value->contains(':-')) { - $value = replaceVariables($value); - $key = $value->before(':'); - $value = $value->after(':-'); - } elseif ($value->contains('-')) { - $value = replaceVariables($value); +// continue; +// } +// if (! $value->startsWith('$')) { +// continue; +// } +// if ($key->value() === $parsedValue->value()) { +// $value = null; +// $resource->environment_variables()->firstOrCreate([ +// 'key' => $key, +// 'resourceable_type' => get_class($resource), +// 'resourceable_id' => $resource->id, +// ], [ +// 'value' => $value, +// 'is_build_time' => false, +// 'is_preview' => false, +// ]); +// } else { +// if ($value->startsWith('$')) { +// $isRequired = false; +// if ($value->contains(':-')) { +// $value = replaceVariables($value); +// $key = $value->before(':'); +// $value = $value->after(':-'); +// } elseif ($value->contains('-')) { +// $value = replaceVariables($value); - $key = $value->before('-'); - $value = $value->after('-'); - } elseif ($value->contains(':?')) { - $value = replaceVariables($value); +// $key = $value->before('-'); +// $value = $value->after('-'); +// } elseif ($value->contains(':?')) { +// $value = replaceVariables($value); - $key = $value->before(':'); - $value = $value->after(':?'); - $isRequired = true; - } elseif ($value->contains('?')) { - $value = replaceVariables($value); +// $key = $value->before(':'); +// $value = $value->after(':?'); +// $isRequired = true; +// } elseif ($value->contains('?')) { +// $value = replaceVariables($value); - $key = $value->before('?'); - $value = $value->after('?'); - $isRequired = true; - } - if ($originalValue->value() === $value->value()) { - // This means the variable does not have a default value, so it needs to be created in Coolify - $parsedKeyValue = replaceVariables($value); - $resource->environment_variables()->firstOrCreate([ - 'key' => $parsedKeyValue, - 'resourceable_type' => get_class($resource), - 'resourceable_id' => $resource->id, - ], [ - 'is_build_time' => false, - 'is_preview' => false, - 'is_required' => $isRequired, - ]); - // Add the variable to the environment so it will be shown in the deployable compose file - // $environment[$parsedKeyValue->value()] = $resource->environment_variables()->where('key', $parsedKeyValue)->where('resourceable_type', get_class($resource))->where('resourceable_id', $resource->id)->first()->real_value; - $environment[$parsedKeyValue->value()] = $value; +// $key = $value->before('?'); +// $value = $value->after('?'); +// $isRequired = true; +// } +// if ($originalValue->value() === $value->value()) { +// // This means the variable does not have a default value, so it needs to be created in Coolify +// $parsedKeyValue = replaceVariables($value); +// $resource->environment_variables()->firstOrCreate([ +// 'key' => $parsedKeyValue, +// 'resourceable_type' => get_class($resource), +// 'resourceable_id' => $resource->id, +// ], [ +// 'is_build_time' => false, +// 'is_preview' => false, +// 'is_required' => $isRequired, +// ]); +// // Add the variable to the environment so it will be shown in the deployable compose file +// // $environment[$parsedKeyValue->value()] = $resource->environment_variables()->where('key', $parsedKeyValue)->where('resourceable_type', get_class($resource))->where('resourceable_id', $resource->id)->first()->real_value; +// $environment[$parsedKeyValue->value()] = $value; - continue; - } - $resource->environment_variables()->firstOrCreate([ - 'key' => $key, - 'resourceable_type' => get_class($resource), - 'resourceable_id' => $resource->id, - ], [ - 'value' => $value, - 'is_build_time' => false, - 'is_preview' => false, - 'is_required' => $isRequired, - ]); - } - } - } - if ($isApplication) { - $branch = $originalResource->git_branch; - if ($pullRequestId !== 0) { - $branch = "pull/{$pullRequestId}/head"; - } - if ($originalResource->environment_variables->where('key', 'COOLIFY_BRANCH')->isEmpty()) { - $coolifyEnvironments->put('COOLIFY_BRANCH', "\"{$branch}\""); - } - } +// continue; +// } +// $resource->environment_variables()->firstOrCreate([ +// 'key' => $key, +// 'resourceable_type' => get_class($resource), +// 'resourceable_id' => $resource->id, +// ], [ +// 'value' => $value, +// 'is_build_time' => false, +// 'is_preview' => false, +// 'is_required' => $isRequired, +// ]); +// } +// } +// } +// if ($isApplication) { +// $branch = $originalResource->git_branch; +// if ($pullRequestId !== 0) { +// $branch = "pull/{$pullRequestId}/head"; +// } +// if ($originalResource->environment_variables->where('key', 'COOLIFY_BRANCH')->isEmpty()) { +// $coolifyEnvironments->put('COOLIFY_BRANCH', "\"{$branch}\""); +// } +// } - // Add COOLIFY_RESOURCE_UUID to environment - if ($resource->environment_variables->where('key', 'COOLIFY_RESOURCE_UUID')->isEmpty()) { - $coolifyEnvironments->put('COOLIFY_RESOURCE_UUID', "{$resource->uuid}"); - } +// // Add COOLIFY_RESOURCE_UUID to environment +// if ($resource->environment_variables->where('key', 'COOLIFY_RESOURCE_UUID')->isEmpty()) { +// $coolifyEnvironments->put('COOLIFY_RESOURCE_UUID', "{$resource->uuid}"); +// } - // Add COOLIFY_CONTAINER_NAME to environment - if ($resource->environment_variables->where('key', 'COOLIFY_CONTAINER_NAME')->isEmpty()) { - $coolifyEnvironments->put('COOLIFY_CONTAINER_NAME', "{$containerName}"); - } +// // Add COOLIFY_CONTAINER_NAME to environment +// if ($resource->environment_variables->where('key', 'COOLIFY_CONTAINER_NAME')->isEmpty()) { +// $coolifyEnvironments->put('COOLIFY_CONTAINER_NAME', "{$containerName}"); +// } - if ($isApplication) { - if ($isPullRequest) { - $preview = $resource->previews()->find($preview_id); - $domains = collect(json_decode(data_get($preview, 'docker_compose_domains'))) ?? collect([]); - } else { - $domains = collect(json_decode($resource->docker_compose_domains)) ?? collect([]); - } - $fqdns = data_get($domains, "$serviceName.domain"); - // Generate SERVICE_FQDN & SERVICE_URL for dockercompose - if ($resource->build_pack === 'dockercompose') { +// if ($isApplication) { +// if ($isPullRequest) { +// $preview = $resource->previews()->find($preview_id); +// $domains = collect(json_decode(data_get($preview, 'docker_compose_domains'))) ?? collect([]); +// } else { +// $domains = collect(json_decode($resource->docker_compose_domains)) ?? collect([]); +// } +// $fqdns = data_get($domains, "$serviceName.domain"); +// // Generate SERVICE_FQDN & SERVICE_URL for dockercompose +// if ($resource->build_pack === 'dockercompose') { - foreach ($domains as $forServiceName => $domain) { - $parsedDomain = data_get($domain, 'domain'); - if (filled($parsedDomain)) { - $parsedDomain = str($parsedDomain)->explode(',')->first(); - $coolifyUrl = Url::fromString($parsedDomain); - $coolifyScheme = $coolifyUrl->getScheme(); - $coolifyFqdn = $coolifyUrl->getHost(); - $coolifyUrl = $coolifyUrl->withScheme($coolifyScheme)->withHost($coolifyFqdn)->withPort(null); - $coolifyEnvironments->put('SERVICE_URL_'.str($forServiceName)->upper()->replace('-', '_'), $coolifyUrl->__toString()); - $coolifyEnvironments->put('SERVICE_FQDN_'.str($forServiceName)->upper()->replace('-', '_'), $coolifyFqdn); - } - } - } - // If the domain is set, we need to generate the FQDNs for the preview - if (filled($fqdns)) { - $fqdns = str($fqdns)->explode(','); - if ($isPullRequest) { - $preview = $resource->previews()->find($preview_id); - $docker_compose_domains = collect(json_decode(data_get($preview, 'docker_compose_domains'))); - if ($docker_compose_domains->count() > 0) { - $found_fqdn = data_get($docker_compose_domains, "$serviceName.domain"); - if ($found_fqdn) { - $fqdns = collect($found_fqdn); - } else { - $fqdns = collect([]); - } - } else { - $fqdns = $fqdns->map(function ($fqdn) use ($pullRequestId, $resource) { - $preview = ApplicationPreview::findPreviewByApplicationAndPullId($resource->id, $pullRequestId); - $url = Url::fromString($fqdn); - $template = $resource->preview_url_template; - $host = $url->getHost(); - $schema = $url->getScheme(); - $random = new Cuid2; - $preview_fqdn = str_replace('{{random}}', $random, $template); - $preview_fqdn = str_replace('{{domain}}', $host, $preview_fqdn); - $preview_fqdn = str_replace('{{pr_id}}', $pullRequestId, $preview_fqdn); - $preview_fqdn = "$schema://$preview_fqdn"; - $preview->fqdn = $preview_fqdn; - $preview->save(); +// foreach ($domains as $forServiceName => $domain) { +// $parsedDomain = data_get($domain, 'domain'); +// if (filled($parsedDomain)) { +// $parsedDomain = str($parsedDomain)->explode(',')->first(); +// $coolifyUrl = Url::fromString($parsedDomain); +// $coolifyScheme = $coolifyUrl->getScheme(); +// $coolifyFqdn = $coolifyUrl->getHost(); +// $coolifyUrl = $coolifyUrl->withScheme($coolifyScheme)->withHost($coolifyFqdn)->withPort(null); +// $coolifyEnvironments->put('SERVICE_URL_'.str($forServiceName)->upper()->replace('-', '_'), $coolifyUrl->__toString()); +// $coolifyEnvironments->put('SERVICE_FQDN_'.str($forServiceName)->upper()->replace('-', '_'), $coolifyFqdn); +// } +// } +// } +// // If the domain is set, we need to generate the FQDNs for the preview +// if (filled($fqdns)) { +// $fqdns = str($fqdns)->explode(','); +// if ($isPullRequest) { +// $preview = $resource->previews()->find($preview_id); +// $docker_compose_domains = collect(json_decode(data_get($preview, 'docker_compose_domains'))); +// if ($docker_compose_domains->count() > 0) { +// $found_fqdn = data_get($docker_compose_domains, "$serviceName.domain"); +// if ($found_fqdn) { +// $fqdns = collect($found_fqdn); +// } else { +// $fqdns = collect([]); +// } +// } else { +// $fqdns = $fqdns->map(function ($fqdn) use ($pullRequestId, $resource) { +// $preview = ApplicationPreview::findPreviewByApplicationAndPullId($resource->id, $pullRequestId); +// $url = Url::fromString($fqdn); +// $template = $resource->preview_url_template; +// $host = $url->getHost(); +// $schema = $url->getScheme(); +// $random = new Cuid2; +// $preview_fqdn = str_replace('{{random}}', $random, $template); +// $preview_fqdn = str_replace('{{domain}}', $host, $preview_fqdn); +// $preview_fqdn = str_replace('{{pr_id}}', $pullRequestId, $preview_fqdn); +// $preview_fqdn = "$schema://$preview_fqdn"; +// $preview->fqdn = $preview_fqdn; +// $preview->save(); - return $preview_fqdn; - }); - } - } - } - $defaultLabels = defaultLabels( - id: $resource->id, - name: $containerName, - projectName: $resource->project()->name, - resourceName: $resource->name, - pull_request_id: $pullRequestId, - type: 'application', - environment: $resource->environment->name, - ); +// return $preview_fqdn; +// }); +// } +// } +// } +// $defaultLabels = defaultLabels( +// id: $resource->id, +// name: $containerName, +// projectName: $resource->project()->name, +// resourceName: $resource->name, +// pull_request_id: $pullRequestId, +// type: 'application', +// environment: $resource->environment->name, +// ); - } elseif ($isService) { - if ($savedService->serviceType()) { - $fqdns = generateServiceSpecificFqdns($savedService); - } else { - $fqdns = collect(data_get($savedService, 'fqdns'))->filter(); - } - $defaultLabels = defaultLabels( - id: $resource->id, - name: $containerName, - projectName: $resource->project()->name, - resourceName: $resource->name, - type: 'service', - subType: $isDatabase ? 'database' : 'application', - subId: $savedService->id, - subName: $savedService->human_name ?? $savedService->name, - environment: $resource->environment->name, - ); - } - // Add COOLIFY_FQDN & COOLIFY_URL to environment - if (! $isDatabase && $fqdns instanceof Collection && $fqdns->count() > 0) { - $fqdnsWithoutPort = $fqdns->map(function ($fqdn) { - return str($fqdn)->after('://')->before(':')->prepend(str($fqdn)->before('://')->append('://')); - }); - $coolifyEnvironments->put('COOLIFY_URL', $fqdnsWithoutPort->implode(',')); +// } elseif ($isService) { +// if ($savedService->serviceType()) { +// $fqdns = generateServiceSpecificFqdns($savedService); +// } else { +// $fqdns = collect(data_get($savedService, 'fqdns'))->filter(); +// } +// $defaultLabels = defaultLabels( +// id: $resource->id, +// name: $containerName, +// projectName: $resource->project()->name, +// resourceName: $resource->name, +// type: 'service', +// subType: $isDatabase ? 'database' : 'application', +// subId: $savedService->id, +// subName: $savedService->human_name ?? $savedService->name, +// environment: $resource->environment->name, +// ); +// } +// // Add COOLIFY_FQDN & COOLIFY_URL to environment +// if (! $isDatabase && $fqdns instanceof Collection && $fqdns->count() > 0) { +// $fqdnsWithoutPort = $fqdns->map(function ($fqdn) { +// return str($fqdn)->after('://')->before(':')->prepend(str($fqdn)->before('://')->append('://')); +// }); +// $coolifyEnvironments->put('COOLIFY_URL', $fqdnsWithoutPort->implode(',')); - $urls = $fqdns->map(function ($fqdn) { - return str($fqdn)->replace('http://', '')->replace('https://', '')->before(':'); - }); - $coolifyEnvironments->put('COOLIFY_FQDN', $urls->implode(',')); - } - add_coolify_default_environment_variables($resource, $coolifyEnvironments, $resource->environment_variables); - if ($environment->count() > 0) { - $environment = $environment->filter(function ($value, $key) { - return ! str($key)->startsWith('SERVICE_FQDN_'); - })->map(function ($value, $key) use ($resource) { - // if value is empty, set it to null so if you set the environment variable in the .env file (Coolify's UI), it will used - if (str($value)->isEmpty()) { - if ($resource->environment_variables()->where('key', $key)->exists()) { - $value = $resource->environment_variables()->where('key', $key)->first()->value; - } else { - $value = null; - } - } +// $urls = $fqdns->map(function ($fqdn) { +// return str($fqdn)->replace('http://', '')->replace('https://', '')->before(':'); +// }); +// $coolifyEnvironments->put('COOLIFY_FQDN', $urls->implode(',')); +// } +// add_coolify_default_environment_variables($resource, $coolifyEnvironments, $resource->environment_variables); +// if ($environment->count() > 0) { +// $environment = $environment->filter(function ($value, $key) { +// return ! str($key)->startsWith('SERVICE_FQDN_'); +// })->map(function ($value, $key) use ($resource) { +// // if value is empty, set it to null so if you set the environment variable in the .env file (Coolify's UI), it will used +// if (str($value)->isEmpty()) { +// if ($resource->environment_variables()->where('key', $key)->exists()) { +// $value = $resource->environment_variables()->where('key', $key)->first()->value; +// } else { +// $value = null; +// } +// } - return $value; - }); - } - $serviceLabels = $labels->merge($defaultLabels); - if ($serviceLabels->count() > 0) { - if ($isApplication) { - $isContainerLabelEscapeEnabled = data_get($resource, 'settings.is_container_label_escape_enabled'); - } else { - $isContainerLabelEscapeEnabled = data_get($resource, 'is_container_label_escape_enabled'); - } - if ($isContainerLabelEscapeEnabled) { - $serviceLabels = $serviceLabels->map(function ($value, $key) { - return escapeDollarSign($value); - }); - } - } - if (! $isDatabase && $fqdns instanceof Collection && $fqdns->count() > 0) { - if ($isApplication) { - $shouldGenerateLabelsExactly = $resource->destination->server->settings->generate_exact_labels; - $uuid = $resource->uuid; - $network = data_get($resource, 'destination.network'); - if ($isPullRequest) { - $uuid = "{$resource->uuid}-{$pullRequestId}"; - } - if ($isPullRequest) { - $network = "{$resource->destination->network}-{$pullRequestId}"; - } - } else { - $shouldGenerateLabelsExactly = $resource->server->settings->generate_exact_labels; - $uuid = $resource->uuid; - $network = data_get($resource, 'destination.network'); - } - if ($shouldGenerateLabelsExactly) { - switch ($server->proxyType()) { - case ProxyTypes::TRAEFIK->value: - $serviceLabels = $serviceLabels->merge(fqdnLabelsForTraefik( - uuid: $uuid, - domains: $fqdns, - is_force_https_enabled: true, - serviceLabels: $serviceLabels, - is_gzip_enabled: $originalResource->isGzipEnabled(), - is_stripprefix_enabled: $originalResource->isStripprefixEnabled(), - service_name: $serviceName, - image: $image - )); - break; - case ProxyTypes::CADDY->value: - $serviceLabels = $serviceLabels->merge(fqdnLabelsForCaddy( - network: $network, - uuid: $uuid, - domains: $fqdns, - is_force_https_enabled: true, - serviceLabels: $serviceLabels, - is_gzip_enabled: $originalResource->isGzipEnabled(), - is_stripprefix_enabled: $originalResource->isStripprefixEnabled(), - service_name: $serviceName, - image: $image, - predefinedPort: $predefinedPort - )); - break; - } - } else { - $serviceLabels = $serviceLabels->merge(fqdnLabelsForTraefik( - uuid: $uuid, - domains: $fqdns, - is_force_https_enabled: true, - serviceLabels: $serviceLabels, - is_gzip_enabled: $originalResource->isGzipEnabled(), - is_stripprefix_enabled: $originalResource->isStripprefixEnabled(), - service_name: $serviceName, - image: $image - )); - $serviceLabels = $serviceLabels->merge(fqdnLabelsForCaddy( - network: $network, - uuid: $uuid, - domains: $fqdns, - is_force_https_enabled: true, - serviceLabels: $serviceLabels, - is_gzip_enabled: $originalResource->isGzipEnabled(), - is_stripprefix_enabled: $originalResource->isStripprefixEnabled(), - service_name: $serviceName, - image: $image, - predefinedPort: $predefinedPort - )); - } - } - if ($isService) { - if (data_get($service, 'restart') === 'no' || data_get($service, 'exclude_from_hc')) { - $savedService->update(['exclude_from_status' => true]); - } - } - data_forget($service, 'volumes.*.content'); - data_forget($service, 'volumes.*.isDirectory'); - data_forget($service, 'volumes.*.is_directory'); - data_forget($service, 'exclude_from_hc'); +// return $value; +// }); +// } +// $serviceLabels = $labels->merge($defaultLabels); +// if ($serviceLabels->count() > 0) { +// if ($isApplication) { +// $isContainerLabelEscapeEnabled = data_get($resource, 'settings.is_container_label_escape_enabled'); +// } else { +// $isContainerLabelEscapeEnabled = data_get($resource, 'is_container_label_escape_enabled'); +// } +// if ($isContainerLabelEscapeEnabled) { +// $serviceLabels = $serviceLabels->map(function ($value, $key) { +// return escapeDollarSign($value); +// }); +// } +// } +// if (! $isDatabase && $fqdns instanceof Collection && $fqdns->count() > 0) { +// if ($isApplication) { +// $shouldGenerateLabelsExactly = $resource->destination->server->settings->generate_exact_labels; +// $uuid = $resource->uuid; +// $network = data_get($resource, 'destination.network'); +// if ($isPullRequest) { +// $uuid = "{$resource->uuid}-{$pullRequestId}"; +// } +// if ($isPullRequest) { +// $network = "{$resource->destination->network}-{$pullRequestId}"; +// } +// } else { +// $shouldGenerateLabelsExactly = $resource->server->settings->generate_exact_labels; +// $uuid = $resource->uuid; +// $network = data_get($resource, 'destination.network'); +// } +// if ($shouldGenerateLabelsExactly) { +// switch ($server->proxyType()) { +// case ProxyTypes::TRAEFIK->value: +// $serviceLabels = $serviceLabels->merge(fqdnLabelsForTraefik( +// uuid: $uuid, +// domains: $fqdns, +// is_force_https_enabled: true, +// serviceLabels: $serviceLabels, +// is_gzip_enabled: $originalResource->isGzipEnabled(), +// is_stripprefix_enabled: $originalResource->isStripprefixEnabled(), +// service_name: $serviceName, +// image: $image +// )); +// break; +// case ProxyTypes::CADDY->value: +// $serviceLabels = $serviceLabels->merge(fqdnLabelsForCaddy( +// network: $network, +// uuid: $uuid, +// domains: $fqdns, +// is_force_https_enabled: true, +// serviceLabels: $serviceLabels, +// is_gzip_enabled: $originalResource->isGzipEnabled(), +// is_stripprefix_enabled: $originalResource->isStripprefixEnabled(), +// service_name: $serviceName, +// image: $image, +// predefinedPort: $predefinedPort +// )); +// break; +// } +// } else { +// $serviceLabels = $serviceLabels->merge(fqdnLabelsForTraefik( +// uuid: $uuid, +// domains: $fqdns, +// is_force_https_enabled: true, +// serviceLabels: $serviceLabels, +// is_gzip_enabled: $originalResource->isGzipEnabled(), +// is_stripprefix_enabled: $originalResource->isStripprefixEnabled(), +// service_name: $serviceName, +// image: $image +// )); +// $serviceLabels = $serviceLabels->merge(fqdnLabelsForCaddy( +// network: $network, +// uuid: $uuid, +// domains: $fqdns, +// is_force_https_enabled: true, +// serviceLabels: $serviceLabels, +// is_gzip_enabled: $originalResource->isGzipEnabled(), +// is_stripprefix_enabled: $originalResource->isStripprefixEnabled(), +// service_name: $serviceName, +// image: $image, +// predefinedPort: $predefinedPort +// )); +// } +// } +// if ($isService) { +// if (data_get($service, 'restart') === 'no' || data_get($service, 'exclude_from_hc')) { +// $savedService->update(['exclude_from_status' => true]); +// } +// } +// data_forget($service, 'volumes.*.content'); +// data_forget($service, 'volumes.*.isDirectory'); +// data_forget($service, 'volumes.*.is_directory'); +// data_forget($service, 'exclude_from_hc'); - $volumesParsed = $volumesParsed->map(function ($volume) { - data_forget($volume, 'content'); - data_forget($volume, 'is_directory'); - data_forget($volume, 'isDirectory'); +// $volumesParsed = $volumesParsed->map(function ($volume) { +// data_forget($volume, 'content'); +// data_forget($volume, 'is_directory'); +// data_forget($volume, 'isDirectory'); - return $volume; - }); +// return $volume; +// }); - $payload = collect($service)->merge([ - 'container_name' => $containerName, - 'restart' => $restart->value(), - 'labels' => $serviceLabels, - ]); - if (! $use_network_mode) { - $payload['networks'] = $networks_temp; - } - if ($ports->count() > 0) { - $payload['ports'] = $ports; - } - if ($volumesParsed->count() > 0) { - $payload['volumes'] = $volumesParsed; - } - if ($environment->count() > 0 || $coolifyEnvironments->count() > 0) { - $payload['environment'] = $environment->merge($coolifyEnvironments); - } - if ($logging) { - $payload['logging'] = $logging; - } - if ($depends_on->count() > 0) { - $payload['depends_on'] = $depends_on; - } - if ($isApplication && $isPullRequest) { - $serviceName = "{$serviceName}-pr-{$pullRequestId}"; - } +// $payload = collect($service)->merge([ +// 'container_name' => $containerName, +// 'restart' => $restart->value(), +// 'labels' => $serviceLabels, +// ]); +// if (! $use_network_mode) { +// $payload['networks'] = $networks_temp; +// } +// if ($ports->count() > 0) { +// $payload['ports'] = $ports; +// } +// if ($volumesParsed->count() > 0) { +// $payload['volumes'] = $volumesParsed; +// } +// if ($environment->count() > 0 || $coolifyEnvironments->count() > 0) { +// $payload['environment'] = $environment->merge($coolifyEnvironments); +// } +// if ($logging) { +// $payload['logging'] = $logging; +// } +// if ($depends_on->count() > 0) { +// $payload['depends_on'] = $depends_on; +// } +// if ($isApplication && $isPullRequest) { +// $serviceName = "{$serviceName}-pr-{$pullRequestId}"; +// } - $parsedServices->put($serviceName, $payload); - } - $topLevel->put('services', $parsedServices); +// $parsedServices->put($serviceName, $payload); +// } +// $topLevel->put('services', $parsedServices); - $customOrder = ['services', 'volumes', 'networks', 'configs', 'secrets']; +// $customOrder = ['services', 'volumes', 'networks', 'configs', 'secrets']; - $topLevel = $topLevel->sortBy(function ($value, $key) use ($customOrder) { - return array_search($key, $customOrder); - }); +// $topLevel = $topLevel->sortBy(function ($value, $key) use ($customOrder) { +// return array_search($key, $customOrder); +// }); - $resource->docker_compose = Yaml::dump(convertToArray($topLevel), 10, 2); - data_forget($resource, 'environment_variables'); - data_forget($resource, 'environment_variables_preview'); - $resource->save(); +// $resource->docker_compose = Yaml::dump(convertToArray($topLevel), 10, 2); +// data_forget($resource, 'environment_variables'); +// data_forget($resource, 'environment_variables_preview'); +// $resource->save(); - return $topLevel; -} +// return $topLevel; +// } function generate_fluentd_configuration(): array {