Skip to content

Commit d816863

Browse files
committed
Fix: Pull helper image if not available otherwise s3 backup upload fails
1 parent 3eb909d commit d816863

File tree

1 file changed

+49
-1
lines changed

1 file changed

+49
-1
lines changed

app/Jobs/DatabaseBackupJob.php

Lines changed: 49 additions & 1 deletion
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,7 +494,16 @@ 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+
// Ensure helper image is available before using it
499+
$this->ensureHelperImageAvailable();
500+
501+
$settings = InstanceSettings::get();
502+
$helperImage = config('coolify.helper_image');
503+
$helperImageTag = $settings->helper_version;
504+
$fullImageName = "{$helperImage}:{$helperImageTag}";
505+
506+
$commands[] = "docker run -d --network {$network} --name backup-of-{$this->backup->uuid} --rm -v $this->backup_location:$this->backup_location:ro {$fullImageName}";
497507
$commands[] = "docker exec backup-of-{$this->backup->uuid} mc config host add temporary {$endpoint} $key $secret";
498508
$commands[] = "docker exec backup-of-{$this->backup->uuid} mc cp $this->backup_location temporary/$bucket{$this->backup_dir}/";
499509
instant_remote_process($commands, $this->server);
@@ -507,4 +517,42 @@ private function upload_to_s3(): void
507517
instant_remote_process([$command], $this->server);
508518
}
509519
}
520+
521+
private function ensureHelperImageAvailable(): void
522+
{
523+
$settings = InstanceSettings::get();
524+
ray($settings);
525+
526+
$helperImage = config('coolify.helper_image');
527+
ray('Helper Image:', $helperImage);
528+
529+
$helperImageTag = $settings->helper_version;
530+
ray('Helper Image Tag:', $helperImageTag);
531+
532+
$fullImageName = "{$helperImage}:{$helperImageTag}";
533+
ray('Full Image Name:', $fullImageName);
534+
535+
$imageExists = instant_remote_process(["docker image inspect {$fullImageName}"], $this->server, false);
536+
ray('Image Exists:', $imageExists);
537+
538+
if (empty($imageExists)) {
539+
$this->add_to_backup_output("Helper image not found. Pulling {$fullImageName}.");
540+
ray('Helper image not found. Attempting to pull.');
541+
try {
542+
$pullResult = instant_remote_process(["docker pull {$fullImageName}"], $this->server);
543+
ray('Pull Result:', $pullResult);
544+
$this->add_to_backup_output("Helper image pulled successfully.");
545+
ray('Helper image pulled successfully.');
546+
} catch (\Exception $e) {
547+
$errorMessage = "Failed to pull helper image: " . $e->getMessage();
548+
ray('Error:', $errorMessage);
549+
$this->add_to_backup_output($errorMessage);
550+
throw new \RuntimeException($errorMessage);
551+
}
552+
} else {
553+
$message = "Helper image {$fullImageName} is available.";
554+
ray($message);
555+
$this->add_to_backup_output($message);
556+
}
557+
}
510558
}

0 commit comments

Comments
 (0)