From d7af57a95ebb10339c28ac086d3d040d21bcbaea Mon Sep 17 00:00:00 2001 From: Andras Bacsai Date: Thu, 26 Oct 2023 11:38:37 +0200 Subject: [PATCH] fix: custom labels only should have non-coolify labels fix: pull helper image every 10 minutes instead of every deployment --- app/Console/Kernel.php | 16 ++++++++++ app/Jobs/ApplicationDeploymentJob.php | 13 ++++---- app/Jobs/PullHelperImageJob.php | 45 +++++++++++++++++++++++++++ bootstrap/helpers/docker.php | 2 -- 4 files changed, 68 insertions(+), 8 deletions(-) create mode 100644 app/Jobs/PullHelperImageJob.php diff --git a/app/Console/Kernel.php b/app/Console/Kernel.php index 8bbab0081..4aa0fc339 100644 --- a/app/Console/Kernel.php +++ b/app/Console/Kernel.php @@ -8,6 +8,7 @@ use App\Jobs\DatabaseBackupJob; use App\Jobs\DockerCleanupJob; use App\Jobs\InstanceAutoUpdateJob; use App\Jobs\ContainerStatusJob; +use App\Jobs\PullHelperImageJob; use App\Models\InstanceSettings; use App\Models\ScheduledDatabaseBackup; use App\Models\Server; @@ -19,20 +20,35 @@ class Kernel extends ConsoleKernel protected function schedule(Schedule $schedule): void { if (isDev()) { + // Instance Jobs $schedule->command('horizon:snapshot')->everyMinute(); $schedule->job(new CleanupInstanceStuffsJob)->everyMinute()->onOneServer(); + + // Server Jobs $this->check_scheduled_backups($schedule); $this->check_resources($schedule); $this->cleanup_servers($schedule); $this->check_scheduled_backups($schedule); + $this->pull_helper_image($schedule); } else { + // Instance Jobs $schedule->command('horizon:snapshot')->everyFiveMinutes(); $schedule->job(new CleanupInstanceStuffsJob)->everyTwoMinutes()->onOneServer(); $schedule->job(new CheckResaleLicenseJob)->hourly()->onOneServer(); + + // Server Jobs $this->instance_auto_update($schedule); $this->check_scheduled_backups($schedule); $this->check_resources($schedule); $this->cleanup_servers($schedule); + $this->pull_helper_image($schedule); + } + } + private function pull_helper_image($schedule) + { + $servers = Server::all()->where('settings.is_usable', true)->where('settings.is_reachable', true); + foreach ($servers as $server) { + $schedule->job(new PullHelperImageJob($server))->everyTenMinutes()->onOneServer(); } } private function cleanup_servers($schedule) diff --git a/app/Jobs/ApplicationDeploymentJob.php b/app/Jobs/ApplicationDeploymentJob.php index 6017f1f1d..962ea362b 100644 --- a/app/Jobs/ApplicationDeploymentJob.php +++ b/app/Jobs/ApplicationDeploymentJob.php @@ -484,17 +484,16 @@ class ApplicationDeploymentJob implements ShouldQueue, ShouldBeEncrypted private function prepare_builder_image() { - $pull = "--pull=always"; $helperImage = config('coolify.helper_image'); if ($this->dockerConfigFileExists === 'OK') { - $runCommand = "docker run {$pull} -d --network {$this->destination->network} -v /:/host --name {$this->deployment_uuid} --rm -v {$this->serverUserHomeDir}/.docker/config.json:/root/.docker/config.json:ro -v /var/run/docker.sock:/var/run/docker.sock {$helperImage}"; + $runCommand = "docker run -d --network {$this->destination->network} -v /:/host --name {$this->deployment_uuid} --rm -v {$this->serverUserHomeDir}/.docker/config.json:/root/.docker/config.json:ro -v /var/run/docker.sock:/var/run/docker.sock {$helperImage}"; } else { - $runCommand = "docker run {$pull} -d --network {$this->destination->network} -v /:/host --name {$this->deployment_uuid} --rm -v /var/run/docker.sock:/var/run/docker.sock {$helperImage}"; + $runCommand = "docker run -d --network {$this->destination->network} -v /:/host --name {$this->deployment_uuid} --rm -v /var/run/docker.sock:/var/run/docker.sock {$helperImage}"; } $this->execute_remote_command( [ - "echo -n 'Pulling helper image from $helperImage.'", + "echo -n 'Preparing container with helper image: $helperImage.'", ], [ $runCommand, @@ -676,10 +675,12 @@ class ApplicationDeploymentJob implements ShouldQueue, ShouldBeEncrypted $volume_names = $this->generate_local_persistent_volumes_only_volume_names(); $environment_variables = $this->generate_environment_variables($ports); - $labels = generateLabelsApplication($this->application, $this->preview); if (data_get($this->application, 'custom_labels')) { - $labels = str($this->application->custom_labels)->explode(',')->toArray(); + $labels = collect(str($this->application->custom_labels)->explode(',')->toArray()); + } else { + $labels = collect(generateLabelsApplication($this->application, $this->preview)); } + $labels = $labels->merge(defaultLabels($this->application->id, $this->application->uuid, $this->pull_request_id))->toArray(); $docker_compose = [ 'version' => '3.8', 'services' => [ diff --git a/app/Jobs/PullHelperImageJob.php b/app/Jobs/PullHelperImageJob.php new file mode 100644 index 000000000..b61a0f295 --- /dev/null +++ b/app/Jobs/PullHelperImageJob.php @@ -0,0 +1,45 @@ +server->uuid))->dontRelease()]; + } + + public function uniqueId(): string + { + return $this->server->uuid; + } + public function __construct(public Server $server) + { + } + public function handle(): void + { + try { + $helperImage = config('coolify.helper_image'); + ray("Pulling {$helperImage}"); + instant_remote_process(["docker pull -q {$helperImage}"], $this->server); + ray('PullHelperImageJob done'); + } catch (\Throwable $e) { + send_internal_notification('PullHelperImageJob failed with: ' . $e->getMessage()); + ray($e->getMessage()); + throw $e; + } + } +} diff --git a/bootstrap/helpers/docker.php b/bootstrap/helpers/docker.php index 179abecb9..a61bb177c 100644 --- a/bootstrap/helpers/docker.php +++ b/bootstrap/helpers/docker.php @@ -212,13 +212,11 @@ function generateLabelsApplication(Application $application, ?ApplicationPreview $onlyPort = $ports[0]; } $pull_request_id = data_get($preview, 'pull_request_id', 0); - // $container_name = generateApplicationContainerName($application, $pull_request_id); $appId = $application->id; if ($pull_request_id !== 0 && $pull_request_id !== null) { $appId = $appId . '-pr-' . $pull_request_id; } $labels = collect([]); - $labels = $labels->merge(defaultLabels($appId, $application->uuid, $pull_request_id)); if ($application->fqdn) { if ($pull_request_id !== 0) { $domains = Str::of(data_get($preview, 'fqdn'))->explode(',');