Skip to content

Commit bb1454d

Browse files
authored
Merge pull request coollabsio#3353 from coollabsio/fix-helper-pull-s3-backup
Fix: Pull helper image if not available otherwise s3 backup upload fails
2 parents 3eb909d + a882d3c commit bb1454d

File tree

1 file changed

+42
-2
lines changed

1 file changed

+42
-2
lines changed

app/Jobs/DatabaseBackupJob.php

Lines changed: 42 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
use Illuminate\Queue\Middleware\WithoutOverlapping;
2626
use Illuminate\Queue\SerializesModels;
2727
use Illuminate\Support\Str;
28+
use App\Models\InstanceSettings;
2829

2930
class DatabaseBackupJob implements ShouldBeEncrypted, ShouldQueue
3031
{
@@ -493,12 +494,15 @@ private function upload_to_s3(): void
493494
} else {
494495
$network = $this->database->destination->network;
495496
}
496-
$commands[] = "docker run -d --network {$network} --name backup-of-{$this->backup->uuid} --rm -v $this->backup_location:$this->backup_location:ro ghcr.io/coollabsio/coolify-helper";
497+
498+
$this->ensureHelperImageAvailable();
499+
500+
$fullImageName = $this->getFullImageName();
501+
$commands[] = "docker run -d --network {$network} --name backup-of-{$this->backup->uuid} --rm -v $this->backup_location:$this->backup_location:ro {$fullImageName}";
497502
$commands[] = "docker exec backup-of-{$this->backup->uuid} mc config host add temporary {$endpoint} $key $secret";
498503
$commands[] = "docker exec backup-of-{$this->backup->uuid} mc cp $this->backup_location temporary/$bucket{$this->backup_dir}/";
499504
instant_remote_process($commands, $this->server);
500505
$this->add_to_backup_output('Uploaded to S3.');
501-
ray('Uploaded to S3. '.$this->backup_location.' to s3://'.$bucket.$this->backup_dir);
502506
} catch (\Throwable $e) {
503507
$this->add_to_backup_output($e->getMessage());
504508
throw $e;
@@ -507,4 +511,40 @@ private function upload_to_s3(): void
507511
instant_remote_process([$command], $this->server);
508512
}
509513
}
514+
515+
private function ensureHelperImageAvailable(): void
516+
{
517+
$fullImageName = $this->getFullImageName();
518+
519+
$imageExists = $this->checkImageExists($fullImageName);
520+
521+
if (!$imageExists) {
522+
$this->pullHelperImage($fullImageName);
523+
}
524+
}
525+
526+
private function checkImageExists(string $fullImageName): bool
527+
{
528+
$result = instant_remote_process(["docker image inspect {$fullImageName} >/dev/null 2>&1 && echo 'exists' || echo 'not exists'"], $this->server, false);
529+
return trim($result) === 'exists';
530+
}
531+
532+
private function pullHelperImage(string $fullImageName): void
533+
{
534+
try {
535+
instant_remote_process(["docker pull {$fullImageName}"], $this->server);
536+
} catch (\Exception $e) {
537+
$errorMessage = "Failed to pull helper image: " . $e->getMessage();
538+
$this->add_to_backup_output($errorMessage);
539+
throw new \RuntimeException($errorMessage);
540+
}
541+
}
542+
543+
private function getFullImageName(): string
544+
{
545+
$settings = InstanceSettings::get();
546+
$helperImage = config('coolify.helper_image');
547+
$latestVersion = $settings->helper_version;
548+
return "{$helperImage}:{$latestVersion}";
549+
}
510550
}

0 commit comments

Comments
 (0)