refactor(services): simplify environment variable updates by using updateOrCreate and add cleanup for removed FQDNs

This commit is contained in:
Andras Bacsai
2025-07-18 20:24:53 +02:00
parent 9ed77e5eef
commit 5e693eb4b5

View File

@@ -120,69 +120,53 @@ function updateCompose(ServiceApplication|ServiceDatabase $resource)
if ($resourceFqdns->count() === 1) { if ($resourceFqdns->count() === 1) {
$resourceFqdns = $resourceFqdns->first(); $resourceFqdns = $resourceFqdns->first();
$variableName = 'SERVICE_FQDN_'.str($resource->name)->upper()->replace('-', '_'); $variableName = 'SERVICE_FQDN_'.str($resource->name)->upper()->replace('-', '_');
$generatedEnv = EnvironmentVariable::where('resourceable_type', Service::class)
->where('resourceable_id', $resource->service_id)
->where('key', $variableName)
->first();
$fqdn = Url::fromString($resourceFqdns); $fqdn = Url::fromString($resourceFqdns);
$port = $fqdn->getPort(); $port = $fqdn->getPort();
$path = $fqdn->getPath(); $path = $fqdn->getPath();
$fqdn = $fqdn->getScheme().'://'.$fqdn->getHost(); $fqdn = $fqdn->getScheme().'://'.$fqdn->getHost();
if ($generatedEnv) { $fqdnValue = ($path === '/') ? $fqdn : $fqdn.$path;
if ($path === '/') { EnvironmentVariable::updateOrCreate([
$generatedEnv->value = $fqdn; 'resourceable_type' => Service::class,
} else { 'resourceable_id' => $resource->service_id,
$generatedEnv->value = $fqdn.$path; 'key' => $variableName,
} ], [
$generatedEnv->save(); 'value' => $fqdnValue,
} ]);
if ($port) { if ($port) {
$variableName = $variableName."_$port"; $variableName = $variableName."_$port";
$generatedEnv = EnvironmentVariable::where('resourceable_type', Service::class) EnvironmentVariable::updateOrCreate([
->where('resourceable_id', $resource->service_id) 'resourceable_type' => Service::class,
->where('key', $variableName) 'resourceable_id' => $resource->service_id,
->first(); 'key' => $variableName,
if ($generatedEnv) { ], [
if ($path === '/') { 'value' => $fqdnValue,
$generatedEnv->value = $fqdn; ]);
} else {
$generatedEnv->value = $fqdn.$path;
}
$generatedEnv->save();
}
} }
$variableName = 'SERVICE_URL_'.str($resource->name)->upper()->replace('-', '_'); $variableName = 'SERVICE_URL_'.str($resource->name)->upper()->replace('-', '_');
$generatedEnv = EnvironmentVariable::where('resourceable_type', Service::class)
->where('resourceable_id', $resource->service_id)
->where('key', $variableName)
->first();
$url = Url::fromString($fqdn); $url = Url::fromString($fqdn);
$port = $url->getPort(); $port = $url->getPort();
$path = $url->getPath(); $path = $url->getPath();
$url = $url->getHost(); $url = $url->getHost();
if ($generatedEnv) { $urlValue = str($fqdn)->after('://');
$url = str($fqdn)->after('://'); if ($path !== '/') {
if ($path === '/') { $urlValue = $urlValue.$path;
$generatedEnv->value = $url;
} else {
$generatedEnv->value = $url.$path;
}
$generatedEnv->save();
} }
EnvironmentVariable::updateOrCreate([
'resourceable_type' => Service::class,
'resourceable_id' => $resource->service_id,
'key' => $variableName,
], [
'value' => $urlValue,
]);
if ($port) { if ($port) {
$variableName = $variableName."_$port"; $variableName = $variableName."_$port";
$generatedEnv = EnvironmentVariable::where('resourceable_type', Service::class) EnvironmentVariable::updateOrCreate([
->where('resourceable_id', $resource->service_id) 'resourceable_type' => Service::class,
->where('key', $variableName) 'resourceable_id' => $resource->service_id,
->first(); 'key' => $variableName,
if ($generatedEnv) { ], [
if ($path === '/') { 'value' => $urlValue,
$generatedEnv->value = $url; ]);
} else {
$generatedEnv->value = $url.$path;
}
$generatedEnv->save();
}
} }
} elseif ($resourceFqdns->count() > 1) { } elseif ($resourceFqdns->count() > 1) {
foreach ($resourceFqdns as $fqdn) { foreach ($resourceFqdns as $fqdn) {
@@ -269,6 +253,17 @@ function updateCompose(ServiceApplication|ServiceDatabase $resource)
} }
} }
} }
} else {
// If FQDN is removed, delete the corresponding environment variables
$serviceName = str($resource->name)->upper()->replace('-', '_');
EnvironmentVariable::where('resourceable_type', Service::class)
->where('resourceable_id', $resource->service_id)
->where('key', 'LIKE', "SERVICE_FQDN_{$serviceName}%")
->delete();
EnvironmentVariable::where('resourceable_type', Service::class)
->where('resourceable_id', $resource->service_id)
->where('key', 'LIKE', "SERVICE_URL_{$serviceName}%")
->delete();
} }
} catch (\Throwable $e) { } catch (\Throwable $e) {
return handleError($e); return handleError($e);