coolify/app/Actions/Server/UpdateCoolify.php

67 lines
2.2 KiB
PHP
Raw Normal View History

2023-06-15 11:23:48 +02:00
<?php
namespace App\Actions\Server;
2023-10-12 08:56:29 +02:00
use Lorisleiva\Actions\Concerns\AsAction;
2023-06-15 11:23:48 +02:00
use App\Models\InstanceSettings;
use App\Models\Server;
2024-05-28 15:05:18 +02:00
use Illuminate\Support\Facades\Log;
2023-06-15 11:23:48 +02:00
class UpdateCoolify
{
2023-10-12 08:56:29 +02:00
use AsAction;
public ?Server $server = null;
public ?string $latestVersion = null;
public ?string $currentVersion = null;
2023-06-15 11:23:48 +02:00
2024-05-28 15:05:18 +02:00
public function handle()
2023-06-15 11:23:48 +02:00
{
try {
2023-06-23 13:13:02 +02:00
$settings = InstanceSettings::get();
2023-06-15 11:23:48 +02:00
ray('Running InstanceAutoUpdateJob');
2023-12-13 11:55:08 +01:00
$this->server = Server::find(0);
2023-08-28 18:02:31 +02:00
if (!$this->server) {
return;
}
CleanupDocker::run($this->server, false);
$this->latestVersion = get_latest_version_of_coolify();
$this->currentVersion = config('version');
2024-05-28 15:05:18 +02:00
if (!$settings->is_auto_update_enabled) {
2024-05-30 19:35:44 +02:00
Log::debug('Auto update is disabled');
return;
2023-06-15 11:23:48 +02:00
}
2024-05-28 15:05:18 +02:00
if ($this->latestVersion === $this->currentVersion) {
2024-05-30 19:35:44 +02:00
Log::debug('Already on latest version');
return;
2024-05-28 15:05:18 +02:00
}
if (version_compare($this->latestVersion, $this->currentVersion, '<')) {
2024-05-30 19:35:44 +02:00
Log::debug('Latest version is lower than current version?!');
return;
2024-05-28 15:05:18 +02:00
}
Log::info("Updating from {$this->currentVersion} -> {$this->latestVersion}");
$this->update();
2023-09-11 17:36:30 +02:00
} catch (\Throwable $e) {
2023-06-15 11:23:48 +02:00
ray('InstanceAutoUpdateJob failed');
2023-09-11 17:36:30 +02:00
ray($e->getMessage());
send_internal_notification('InstanceAutoUpdateJob failed: ' . $e->getMessage());
throw $e;
2023-06-15 11:23:48 +02:00
}
}
2023-06-15 11:23:48 +02:00
private function update()
{
if (isDev()) {
2024-05-28 15:05:18 +02:00
instant_remote_process([
"sleep 10"
], $this->server);
2023-06-15 11:23:48 +02:00
return;
}
2024-05-28 15:05:18 +02:00
instant_remote_process([
"curl -fsSL https://cdn.coollabs.io/coolify/upgrade.sh -o /data/coolify/source/upgrade.sh",
"bash /data/coolify/source/upgrade.sh $this->latestVersion"
], $this->server);
send_internal_notification("Instance updated from {$this->currentVersion} -> {$this->latestVersion}");
return;
2023-06-15 11:23:48 +02:00
}
}