Files
coolify/app/Traits/ExecutesDatabaseStartCommands.php
033f0c84da v4.7 (#12478)
Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
Co-authored-by: peaklabs-dev <122374094+peaklabs-dev@users.noreply.github.com>
Co-authored-by: Aditya Tripathi <aditya@climactic.co>
2026-11-06 14:25:02 +01:00

71 lines
2.7 KiB
PHP

<?php
namespace App\Traits;
use App\Jobs\ScanResourceImagesJob;
use App\Services\DatabaseStartCommandExecutor;
use App\Services\ImageScan\ImageStartPin;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Collection;
use Spatie\Activitylog\Models\Activity;
trait ExecutesDatabaseStartCommands
{
private function executeDatabaseStartCommands(array $commands, Model $database, ?Activity $activity = null): Activity
{
if ($activity) {
$activity = app(DatabaseStartCommandExecutor::class)->execute($commands, $database, $activity);
} else {
$activity = remote_process($commands, $database->destination->server, callEventOnFinish: 'DatabaseStatusChanged', queue: deployment_queue());
}
ScanResourceImagesJob::dispatchAfterStart($database);
return $activity;
}
/**
* The image pull of the start: the scanned build of a "Pull latest" security update when one waits for this
* database (the compose file keeps the tag), else a pull of the configured image.
*
* @return list<string>
*/
private function pullImageCommands(string $composeFile): array
{
$prefix = serverDockerConfigPrefix($this->database->destination->server);
$pins = ImageStartPin::for(ImageStartPin::take($this->database), [(string) $this->database->image]);
return $pins === []
? [$prefix."docker compose -f {$composeFile} pull"]
: ImageStartPin::commands($pins, $prefix);
}
/**
* Whether a `KEY=value` list already defines the exact key (not just a key or value containing it).
*
* @param Collection<int, string> $environmentVariables
*/
private function hasEnvironmentVariable(Collection $environmentVariables, string $key): bool
{
return $environmentVariables->contains(fn ($variable) => str($variable)->before('=')->value() === $key);
}
/**
* Adds the Coolify variables that applications and services also get. A key that the user
* defines wins.
*
* @param Collection<int, string> $environmentVariables
*/
private function addCoolifyEnvironmentVariables(Collection $environmentVariables): void
{
$definedKeys = $environmentVariables->map(fn ($variable) => str($variable)->before('=')->value());
$coolifyVariables = collect([
'COOLIFY_RESOURCE_UUID' => $this->database->uuid,
'COOLIFY_CONTAINER_NAME' => $this->database->uuid,
])->except($definedKeys->all())->merge(coolifyDefaultEnvironmentVariables($this->database, $definedKeys));
foreach ($coolifyVariables as $key => $value) {
$environmentVariables->push($key.'='.escapeDollarSign($value));
}
}
}