feat: predefined server variables (COOLIFY_SERVER_NAME, COOLIFY_SERVER_UUID)

These are not visible on shared env page but user can use these variables like they use the COOLIFY_RESOURCE_UUID
This commit is contained in:
ShadowArcanist 2025-12-24 13:58:50 +01:00
parent 81009c29cf
commit 5ed308dcf0
7 changed files with 62 additions and 8 deletions

View file

@ -24,6 +24,10 @@ public function saveKey($data)
try { try {
$this->authorize('update', $this->server); $this->authorize('update', $this->server);
if (in_array($data['key'], ['COOLIFY_SERVER_UUID', 'COOLIFY_SERVER_NAME'])) {
throw new \Exception('Cannot create predefined variable.');
}
$found = $this->server->environment_variables()->where('key', $data['key'])->first(); $found = $this->server->environment_variables()->where('key', $data['key'])->first();
if ($found) { if ($found) {
throw new \Exception('Variable already exists.'); throw new \Exception('Variable already exists.');
@ -64,7 +68,7 @@ public function switch()
public function getDevView() public function getDevView()
{ {
$this->variables = $this->formatEnvironmentVariables($this->server->environment_variables->sortBy('key')); $this->variables = $this->formatEnvironmentVariables($this->server->environment_variables->whereNotIn('key', ['COOLIFY_SERVER_UUID', 'COOLIFY_SERVER_NAME'])->sortBy('key'));
} }
private function formatEnvironmentVariables($variables) private function formatEnvironmentVariables($variables)
@ -115,13 +119,19 @@ private function handleBulkSubmit()
private function deleteRemovedVariables($variables) private function deleteRemovedVariables($variables)
{ {
$variablesToDelete = $this->server->environment_variables()->whereNotIn('key', array_keys($variables))->get(); $variablesToDelete = $this->server->environment_variables()
->whereNotIn('key', array_keys($variables))
->whereNotIn('key', ['COOLIFY_SERVER_UUID', 'COOLIFY_SERVER_NAME'])
->get();
if ($variablesToDelete->isEmpty()) { if ($variablesToDelete->isEmpty()) {
return 0; return 0;
} }
$this->server->environment_variables()->whereNotIn('key', array_keys($variables))->delete(); $this->server->environment_variables()
->whereNotIn('key', array_keys($variables))
->whereNotIn('key', ['COOLIFY_SERVER_UUID', 'COOLIFY_SERVER_NAME'])
->delete();
return $variablesToDelete->count(); return $variablesToDelete->count();
} }
@ -130,6 +140,10 @@ private function updateOrCreateVariables($variables)
{ {
$count = 0; $count = 0;
foreach ($variables as $key => $value) { foreach ($variables as $key => $value) {
// Skip predefined variables
if (in_array($key, ['COOLIFY_SERVER_UUID', 'COOLIFY_SERVER_NAME'])) {
continue;
}
$found = $this->server->environment_variables()->where('key', $key)->first(); $found = $this->server->environment_variables()->where('key', $key)->first();
if ($found) { if ($found) {

View file

@ -56,7 +56,7 @@ public function isEmpty()
public function environment_variables() public function environment_variables()
{ {
return $this->hasMany(SharedEnvironmentVariable::class); return $this->hasMany(SharedEnvironmentVariable::class)->where('type', 'environment');
} }
public function applications() public function applications()

View file

@ -73,7 +73,7 @@ protected static function booted()
public function environment_variables() public function environment_variables()
{ {
return $this->hasMany(SharedEnvironmentVariable::class); return $this->hasMany(SharedEnvironmentVariable::class)->where('type', 'project');
} }
public function environments() public function environments()

View file

@ -19,6 +19,7 @@
use App\Traits\HasSafeStringAttribute; use App\Traits\HasSafeStringAttribute;
use Illuminate\Database\Eloquent\Builder; use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Casts\Attribute; use Illuminate\Database\Eloquent\Casts\Attribute;
use Illuminate\Database\Eloquent\Relations\HasMany;
use Illuminate\Database\Eloquent\Factories\HasFactory; use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\SoftDeletes; use Illuminate\Database\Eloquent\SoftDeletes;
use Illuminate\Support\Carbon; use Illuminate\Support\Carbon;
@ -168,9 +169,25 @@ protected static function booted()
$standaloneDocker->saveQuietly(); $standaloneDocker->saveQuietly();
} }
} }
if (! isset($server->proxy->redirect_enabled)) { if (! isset($server->proxy->redirect_enabled)) {
$server->proxy->redirect_enabled = true; $server->proxy->redirect_enabled = true;
} }
// Create predefined server shared variables
SharedEnvironmentVariable::create([
'key' => 'COOLIFY_SERVER_UUID',
'value' => $server->uuid,
'type' => 'server',
'server_id' => $server->id,
'team_id' => $server->team_id,
]);
SharedEnvironmentVariable::create([
'key' => 'COOLIFY_SERVER_NAME',
'value' => $server->name,
'type' => 'server',
'server_id' => $server->id,
'team_id' => $server->team_id,
]);
}); });
static::retrieved(function ($server) { static::retrieved(function ($server) {
if (! isset($server->proxy->redirect_enabled)) { if (! isset($server->proxy->redirect_enabled)) {

View file

@ -214,7 +214,7 @@ public function subscriptionEnded()
public function environment_variables() public function environment_variables()
{ {
return $this->hasMany(SharedEnvironmentVariable::class)->whereNull('project_id')->whereNull('environment_id'); return $this->hasMany(SharedEnvironmentVariable::class)->where('type', 'team');
} }
public function members() public function members()

View file

@ -2,6 +2,7 @@
namespace Database\Seeders; namespace Database\Seeders;
use App\Models\Server;
use App\Models\SharedEnvironmentVariable; use App\Models\SharedEnvironmentVariable;
use Illuminate\Database\Seeder; use Illuminate\Database\Seeder;
@ -32,5 +33,27 @@ public function run(): void
'project_id' => 1, 'project_id' => 1,
'team_id' => 0, 'team_id' => 0,
]); ]);
// Add predefined server variables to all existing servers
$servers = \App\Models\Server::all();
foreach ($servers as $server) {
SharedEnvironmentVariable::firstOrCreate([
'key' => 'COOLIFY_SERVER_UUID',
'type' => 'server',
'server_id' => $server->id,
'team_id' => $server->team_id,
], [
'value' => $server->uuid,
]);
SharedEnvironmentVariable::firstOrCreate([
'key' => 'COOLIFY_SERVER_NAME',
'type' => 'server',
'server_id' => $server->id,
'team_id' => $server->team_id,
], [
'value' => $server->name,
]);
}
} }
} }

View file

@ -19,7 +19,7 @@
</div> </div>
@if ($view === 'normal') @if ($view === 'normal')
<div class="flex flex-col gap-2"> <div class="flex flex-col gap-2">
@forelse ($server->environment_variables->sort()->sortBy('key') as $env) @forelse ($server->environment_variables->whereNotIn('key', ['COOLIFY_SERVER_UUID', 'COOLIFY_SERVER_NAME'])->sort()->sortBy('key') as $env)
<livewire:project.shared.environment-variable.show wire:key="environment-{{ $env->id }}" <livewire:project.shared.environment-variable.show wire:key="environment-{{ $env->id }}"
:env="$env" type="server" /> :env="$env" type="server" />
@empty @empty