Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
127 changes: 4 additions & 123 deletions backend/app/Actions/Server/UpdateCoolify.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,128 +2,9 @@

namespace App\Actions\Server;

use App\Models\Server;
use Illuminate\Support\Facades\Http;
use Illuminate\Support\Facades\Log;
use Illuminate\Support\Sleep;
use Lorisleiva\Actions\Concerns\AsAction;

class UpdateCoolify
/**
* @deprecated Use UpdateDevForge. Kept so existing jobs and Livewire keep resolving.
*/
class UpdateCoolify extends UpdateDevForge
{
use AsAction;

public ?Server $server = null;

public ?string $latestVersion = null;

public ?string $currentVersion = null;

public function handle($manual_update = false)
{
if (isDev()) {
Sleep::for(10)->seconds();

return;
}
$settings = instanceSettings();
$this->server = Server::find(0);
if (! $this->server) {
return;
}

// Fetch fresh version from CDN instead of using cache
try {
$response = Http::retry(3, 1000)->timeout(10)
->get(config('constants.coolify.versions_url'));

if ($response->successful()) {
$versions = $response->json();
$this->latestVersion = data_get($versions, 'coolify.v4.version');
} else {
// Fallback to cache if CDN unavailable
$cacheVersion = get_latest_version_of_coolify();

// Validate cache version against current running version
if ($cacheVersion && version_compare($cacheVersion, config('constants.coolify.version'), '<')) {
Log::error('Failed to fetch fresh version from CDN and cache is corrupted/outdated', [
'cached_version' => $cacheVersion,
'current_version' => config('constants.coolify.version'),
]);
throw new \Exception(
'Cannot determine latest version: CDN unavailable and cache version '.
"({$cacheVersion}) is older than running version (".config('constants.coolify.version').')'
);
}

$this->latestVersion = $cacheVersion;
Log::warning('Failed to fetch fresh version from CDN (unsuccessful response), using validated cache', [
'version' => $cacheVersion,
]);
}
} catch (\Throwable $e) {
$cacheVersion = get_latest_version_of_coolify();

// Validate cache version against current running version
if ($cacheVersion && version_compare($cacheVersion, config('constants.coolify.version'), '<')) {
Log::error('Failed to fetch fresh version from CDN and cache is corrupted/outdated', [
'error' => $e->getMessage(),
'cached_version' => $cacheVersion,
'current_version' => config('constants.coolify.version'),
]);
throw new \Exception(
'Cannot determine latest version: CDN unavailable and cache version '.
"({$cacheVersion}) is older than running version (".config('constants.coolify.version').')'
);
}

$this->latestVersion = $cacheVersion;
Log::warning('Failed to fetch fresh version from CDN, using validated cache', [
'error' => $e->getMessage(),
'version' => $cacheVersion,
]);
}

$this->currentVersion = config('constants.coolify.version');
if (! $manual_update) {
if (! $settings->is_auto_update_enabled) {
return;
}
if ($this->latestVersion === $this->currentVersion) {
return;
}
if (version_compare($this->latestVersion, $this->currentVersion, '<')) {
return;
}
}

// ALWAYS check for downgrades (even for manual updates)
if (version_compare($this->latestVersion, $this->currentVersion, '<')) {
Log::error('Downgrade prevented', [
'target_version' => $this->latestVersion,
'current_version' => $this->currentVersion,
'manual_update' => $manual_update,
]);
throw new \Exception(
"Cannot downgrade from {$this->currentVersion} to {$this->latestVersion}. ".
'If you need to downgrade, please do so manually via Docker commands.'
);
}

$this->update();
$settings->new_version_available = false;
$settings->save();
}

private function update()
{
$latestHelperImageVersion = getHelperVersion();
$upgradeScriptUrl = config('constants.coolify.upgrade_script_url');

remote_process([
"mkdir -p /data/coolify/source /data/devforge /tmp 2>/dev/null || true",
"curl -fsSL {$upgradeScriptUrl} -o /data/coolify/source/upgrade.sh 2>/dev/null || curl -fsSL {$upgradeScriptUrl} -o /data/devforge/upgrade.sh 2>/dev/null || curl -fsSL {$upgradeScriptUrl} -o /tmp/upgrade.sh",
"chmod +x /data/coolify/source/upgrade.sh 2>/dev/null || chmod +x /data/devforge/upgrade.sh 2>/dev/null || chmod +x /tmp/upgrade.sh 2>/dev/null || true",
"bash /data/coolify/source/upgrade.sh $this->latestVersion $latestHelperImageVersion 2>/dev/null || bash /data/devforge/upgrade.sh $this->latestVersion $latestHelperImageVersion 2>/dev/null || bash /tmp/upgrade.sh $this->latestVersion $latestHelperImageVersion",
], $this->server);
}
}
173 changes: 173 additions & 0 deletions backend/app/Actions/Server/UpdateDevForge.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,173 @@
<?php

namespace App\Actions\Server;

use App\Models\Server;
use App\Services\DevForge\InstanceUpgradeService;
use Illuminate\Support\Facades\Http;
use Illuminate\Support\Facades\Log;
use Illuminate\Support\Facades\Process;
use Illuminate\Support\Sleep;
use Lorisleiva\Actions\Concerns\AsAction;

class UpdateDevForge
{
use AsAction;

public ?Server $server = null;

public ?string $latestVersion = null;

public ?string $currentVersion = null;

public function handle($manual_update = false)
{
if (isDev()) {
Sleep::for(10)->seconds();

return;
}
$settings = instanceSettings();
$this->server = Server::find(0);
if (! $this->server) {
return;
}

// Fetch fresh version from CDN instead of using cache
try {
$response = Http::retry(3, 1000)->timeout(10)
->get(config('constants.devforge.versions_url'));

if ($response->successful()) {
$versions = $response->json();
$this->latestVersion = data_get($versions, 'devforge.version')
?? data_get($versions, 'coolify.v4.version');
} else {
// Fallback to cache if CDN unavailable
$cacheVersion = get_latest_version_of_coolify();

// Validate cache version against current running version
if ($cacheVersion && version_compare($cacheVersion, config('constants.devforge.version'), '<')) {
Log::error('Failed to fetch fresh version from CDN and cache is corrupted/outdated', [
'cached_version' => $cacheVersion,
'current_version' => config('constants.devforge.version'),
]);
throw new \Exception(
'Cannot determine latest version: CDN unavailable and cache version '.
"({$cacheVersion}) is older than running version (".config('constants.devforge.version').')'
);
}

$this->latestVersion = $cacheVersion;
Log::warning('Failed to fetch fresh version from CDN (unsuccessful response), using validated cache', [
'version' => $cacheVersion,
]);
}
} catch (\Throwable $e) {
$cacheVersion = get_latest_version_of_coolify();

// Validate cache version against current running version
if ($cacheVersion && version_compare($cacheVersion, config('constants.devforge.version'), '<')) {
Log::error('Failed to fetch fresh version from CDN and cache is corrupted/outdated', [
'error' => $e->getMessage(),
'cached_version' => $cacheVersion,
'current_version' => config('constants.devforge.version'),
]);
throw new \Exception(
'Cannot determine latest version: CDN unavailable and cache version '.
"({$cacheVersion}) is older than running version (".config('constants.devforge.version').')'
);
}

$this->latestVersion = $cacheVersion;
Log::warning('Failed to fetch fresh version from CDN, using validated cache', [
'error' => $e->getMessage(),
'version' => $cacheVersion,
]);
}

$this->currentVersion = config('constants.devforge.version');
if (! $manual_update) {
if (! $settings->is_auto_update_enabled) {
return;
}
if ($this->latestVersion === $this->currentVersion) {
return;
}
if (version_compare($this->latestVersion, $this->currentVersion, '<')) {
return;
}
}

// ALWAYS check for downgrades (even for manual updates)
if (version_compare($this->latestVersion, $this->currentVersion, '<')) {
Log::error('Downgrade prevented', [
'target_version' => $this->latestVersion,
'current_version' => $this->currentVersion,
'manual_update' => $manual_update,
]);
throw new \Exception(
"Cannot downgrade from {$this->currentVersion} to {$this->latestVersion}. ".
'If you need to downgrade, please do so manually via Docker commands.'
);
}

$this->update();
$settings->new_version_available = false;
$settings->save();
}

private function update()
{
$latestHelperImageVersion = getHelperVersion();
$upgradeScriptUrl = config('constants.devforge.upgrade_script_url');

$commands = [
'mkdir -p /data/coolify/source /data/devforge /tmp 2>/dev/null || true',
"curl -fsSL {$upgradeScriptUrl} -o /data/coolify/source/upgrade.sh 2>/dev/null || curl -fsSL {$upgradeScriptUrl} -o /data/devforge/upgrade.sh 2>/dev/null || curl -fsSL {$upgradeScriptUrl} -o /tmp/upgrade.sh",
'chmod +x /data/coolify/source/upgrade.sh 2>/dev/null || chmod +x /data/devforge/upgrade.sh 2>/dev/null || chmod +x /tmp/upgrade.sh 2>/dev/null || true',
"bash /data/coolify/source/upgrade.sh $this->latestVersion $latestHelperImageVersion 2>/dev/null || bash /data/devforge/upgrade.sh $this->latestVersion $latestHelperImageVersion 2>/dev/null || bash /tmp/upgrade.sh $this->latestVersion $latestHelperImageVersion",
];

$reachable = (bool) data_get($this->server->settings, 'is_reachable', false);
$canLocal = InstanceUpgradeService::canRunLocalDockerUpgrade();

// CasaOS / Zima: SSH to host.docker.internal is often broken — prefer docker.sock.
if ($this->server->isLocalhost() && $canLocal && ! $reachable) {
Log::info('Starting DevForge upgrade via local docker.sock (SSH unreachable)');
$this->updateViaLocalProcess($commands);

return;
}

try {
remote_process($commands, $this->server);
} catch (\Throwable $e) {
if ($this->server->isLocalhost() && $canLocal) {
Log::warning('SSH upgrade failed, falling back to local docker.sock', [
'error' => $e->getMessage(),
]);
$this->updateViaLocalProcess($commands);

return;
}

throw $e;
}
}

/**
* @param list<string> $commands
*/
private function updateViaLocalProcess(array $commands): void
{
$script = implode("\n", $commands);
$result = Process::timeout(600)->run(['bash', '-lc', $script]);

if ($result->failed()) {
$detail = trim($result->errorOutput() ?: $result->output() ?: 'erreur inconnue');

throw new \Exception('Mise à jour locale échouée : '.$detail);
}
}
}
14 changes: 14 additions & 0 deletions backend/app/Jobs/DevForgeTask.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?php

namespace App\Jobs;

/**
* @deprecated Use App\Jobs\RemoteProcessJob instead.
*
* This class is kept for backwards compatibility with existing queued jobs.
* New code should use RemoteProcessJob directly.
*/
class DevForgeTask extends RemoteProcessJob
{
//
}
Loading