fix: compose env has SERVICE, but not defined for Coolify

This commit is contained in:
Andras Bacsai 2024-03-21 15:36:37 +01:00
parent 93af92743c
commit ca9a2cb13a

View File

@ -1005,61 +1005,63 @@ function parseDockerComposeFile(Service|Application $resource, bool $isNew = fal
'service_id' => $resource->id, 'service_id' => $resource->id,
])->first(); ])->first();
['command' => $command, 'forService' => $forService, 'generatedValue' => $generatedValue, 'port' => $port] = parseEnvVariable($value); ['command' => $command, 'forService' => $forService, 'generatedValue' => $generatedValue, 'port' => $port] = parseEnvVariable($value);
if ($command?->value() === 'FQDN' || $command?->value() === 'URL') { if (!is_null($command)) {
if (Str::lower($forService) === $serviceName) { if ($command?->value() === 'FQDN' || $command?->value() === 'URL') {
$fqdn = generateFqdn($resource->server, $containerName); if (Str::lower($forService) === $serviceName) {
} else { $fqdn = generateFqdn($resource->server, $containerName);
$fqdn = generateFqdn($resource->server, Str::lower($forService) . '-' . $resource->uuid); } else {
} $fqdn = generateFqdn($resource->server, Str::lower($forService) . '-' . $resource->uuid);
if ($port) {
$fqdn = "$fqdn:$port";
}
if ($foundEnv) {
$fqdn = data_get($foundEnv, 'value');
} else {
if ($command->value() === 'URL') {
$fqdn = Str::of($fqdn)->after('://')->value();
} }
EnvironmentVariable::create([ if ($port) {
'key' => $key, $fqdn = "$fqdn:$port";
'value' => $fqdn,
'is_build_time' => false,
'service_id' => $resource->id,
'is_preview' => false,
]);
}
if (!$isDatabase) {
if ($command->value() === 'FQDN' && is_null($savedService->fqdn) && !$foundEnv) {
$savedService->fqdn = $fqdn;
$savedService->save();
} }
// Caddy needs exact port in some cases. if ($foundEnv) {
if ($predefinedPort && !$key->endsWith("_{$predefinedPort}") && $command?->value() === 'FQDN' && $resource->server->proxyType() === 'CADDY') { $fqdn = data_get($foundEnv, 'value');
$env = EnvironmentVariable::where([ } else {
if ($command->value() === 'URL') {
$fqdn = Str::of($fqdn)->after('://')->value();
}
EnvironmentVariable::create([
'key' => $key, 'key' => $key,
'value' => $fqdn,
'is_build_time' => false,
'service_id' => $resource->id, 'service_id' => $resource->id,
])->first(); 'is_preview' => false,
if ($env) { ]);
$env_url = Url::fromString($env->value); }
$env_port = $env_url->getPort(); if (!$isDatabase) {
if ($env_port !== $predefinedPort) { if ($command->value() === 'FQDN' && is_null($savedService->fqdn) && !$foundEnv) {
$env_url = $env_url->withPort($predefinedPort); $savedService->fqdn = $fqdn;
$savedService->fqdn = $env_url->__toString(); $savedService->save();
$savedService->save(); }
// Caddy needs exact port in some cases.
if ($predefinedPort && !$key->endsWith("_{$predefinedPort}") && $command?->value() === 'FQDN' && $resource->server->proxyType() === 'CADDY') {
$env = EnvironmentVariable::where([
'key' => $key,
'service_id' => $resource->id,
])->first();
if ($env) {
$env_url = Url::fromString($env->value);
$env_port = $env_url->getPort();
if ($env_port !== $predefinedPort) {
$env_url = $env_url->withPort($predefinedPort);
$savedService->fqdn = $env_url->__toString();
$savedService->save();
}
} }
} }
} }
} } else {
} else { $generatedValue = generateEnvValue($command, $resource);
$generatedValue = generateEnvValue($command, $resource); if (!$foundEnv) {
if (!$foundEnv) { EnvironmentVariable::create([
EnvironmentVariable::create([ 'key' => $key,
'key' => $key, 'value' => $generatedValue,
'value' => $generatedValue, 'is_build_time' => false,
'is_build_time' => false, 'service_id' => $resource->id,
'service_id' => $resource->id, 'is_preview' => false,
'is_preview' => false, ]);
]); }
} }
} }
} else { } else {
@ -1471,39 +1473,41 @@ function parseDockerComposeFile(Service|Application $resource, bool $isNew = fal
'application_id' => $resource->id, 'application_id' => $resource->id,
])->first(); ])->first();
['command' => $command, 'forService' => $forService, 'generatedValue' => $generatedValue, 'port' => $port] = parseEnvVariable($value); ['command' => $command, 'forService' => $forService, 'generatedValue' => $generatedValue, 'port' => $port] = parseEnvVariable($value);
if ($command?->value() === 'FQDN' || $command?->value() === 'URL') { if (!is_null($command)) {
if (Str::lower($forService) === $serviceName) { if ($command?->value() === 'FQDN' || $command?->value() === 'URL') {
$fqdn = generateFqdn($server, $containerName); if (Str::lower($forService) === $serviceName) {
} else { $fqdn = generateFqdn($server, $containerName);
$fqdn = generateFqdn($server, Str::lower($forService) . '-' . $resource->uuid); } else {
} $fqdn = generateFqdn($server, Str::lower($forService) . '-' . $resource->uuid);
if ($port) { }
$fqdn = "$fqdn:$port"; if ($port) {
} $fqdn = "$fqdn:$port";
if ($foundEnv) { }
$fqdn = data_get($foundEnv, 'value'); if ($foundEnv) {
} else { $fqdn = data_get($foundEnv, 'value');
if ($command->value() === 'URL') { } else {
$fqdn = Str::of($fqdn)->after('://')->value(); if ($command?->value() === 'URL') {
$fqdn = Str::of($fqdn)->after('://')->value();
}
EnvironmentVariable::create([
'key' => $key,
'value' => $fqdn,
'is_build_time' => false,
'application_id' => $resource->id,
'is_preview' => false,
]);
}
} else {
$generatedValue = generateEnvValue($command);
if (!$foundEnv) {
EnvironmentVariable::create([
'key' => $key,
'value' => $generatedValue,
'is_build_time' => false,
'application_id' => $resource->id,
'is_preview' => false,
]);
} }
EnvironmentVariable::create([
'key' => $key,
'value' => $fqdn,
'is_build_time' => false,
'application_id' => $resource->id,
'is_preview' => false,
]);
}
} else {
$generatedValue = generateEnvValue($command);
if (!$foundEnv) {
EnvironmentVariable::create([
'key' => $key,
'value' => $generatedValue,
'is_build_time' => false,
'application_id' => $resource->id,
'is_preview' => false,
]);
} }
} }
} else { } else {
@ -1655,29 +1659,30 @@ function parseEnvVariable(Str|string $value)
$forService = null; $forService = null;
$generatedValue = null; $generatedValue = null;
$port = null; $port = null;
if ($value->startsWith('SERVICE')) {
if ($count === 2) { if ($count === 2) {
if ($value->startsWith('SERVICE_FQDN') || $value->startsWith('SERVICE_URL')) { if ($value->startsWith('SERVICE_FQDN') || $value->startsWith('SERVICE_URL')) {
// SERVICE_FQDN_UMAMI // SERVICE_FQDN_UMAMI
$command = $value->after('SERVICE_')->beforeLast('_'); $command = $value->after('SERVICE_')->beforeLast('_');
$forService = $value->afterLast('_'); $forService = $value->afterLast('_');
} else { } else {
// SERVICE_BASE64_UMAMI // SERVICE_BASE64_UMAMI
$command = $value->after('SERVICE_')->beforeLast('_'); $command = $value->after('SERVICE_')->beforeLast('_');
} }
} }
if ($count === 3) { if ($count === 3) {
if ($value->startsWith('SERVICE_FQDN') || $value->startsWith('SERVICE_URL')) { if ($value->startsWith('SERVICE_FQDN') || $value->startsWith('SERVICE_URL')) {
// SERVICE_FQDN_UMAMI_1000 // SERVICE_FQDN_UMAMI_1000
$command = $value->after('SERVICE_')->before('_'); $command = $value->after('SERVICE_')->before('_');
$forService = $value->after('SERVICE_')->after('_')->before('_'); $forService = $value->after('SERVICE_')->after('_')->before('_');
$port = $value->afterLast('_'); $port = $value->afterLast('_');
if (filter_var($port, FILTER_VALIDATE_INT) === false) { if (filter_var($port, FILTER_VALIDATE_INT) === false) {
$port = null; $port = null;
}
} else {
// SERVICE_BASE64_64_UMAMI
$command = $value->after('SERVICE_')->beforeLast('_');
} }
} else {
// SERVICE_BASE64_64_UMAMI
$command = $value->after('SERVICE_')->beforeLast('_');
} }
} }
return [ return [