coolify/app/Livewire/Upgrade.php

124 lines
3.1 KiB
PHP
Raw Permalink Normal View History

2023-04-28 13:08:48 +00:00
<?php
2023-12-07 18:06:32 +00:00
namespace App\Livewire;
2023-04-28 13:08:48 +00:00
2023-06-15 09:23:48 +00:00
use App\Actions\Server\UpdateCoolify;
use App\Models\InstanceSettings;
use App\Models\Server;
2023-04-28 13:08:48 +00:00
use Livewire\Component;
2023-06-15 08:48:13 +00:00
class Upgrade extends Component
2023-04-28 13:08:48 +00:00
{
public bool $updateInProgress = false;
2024-06-10 20:43:34 +00:00
2023-06-15 09:39:15 +00:00
public bool $isUpgradeAvailable = false;
2024-06-10 20:43:34 +00:00
2023-06-16 10:35:40 +00:00
public string $latestVersion = '';
2023-06-15 09:39:15 +00:00
public string $currentVersion = '';
public bool $devMode = false;
protected $listeners = ['updateAvailable' => 'checkUpdate'];
public function mount()
{
$this->currentVersion = config('constants.coolify.version');
$this->devMode = isDev();
}
2023-06-15 09:39:15 +00:00
public function checkUpdate()
{
try {
2024-09-05 08:15:22 +00:00
$this->latestVersion = get_latest_version_of_coolify();
$this->currentVersion = config('constants.coolify.version');
2024-09-05 08:15:22 +00:00
$this->isUpgradeAvailable = data_get(InstanceSettings::get(), 'new_version_available', false);
if (isDev()) {
$this->isUpgradeAvailable = true;
}
} catch (\Throwable $e) {
return handleError($e, $this);
}
2023-06-15 09:39:15 +00:00
}
2023-04-28 13:08:48 +00:00
public function upgrade()
{
2023-05-25 20:33:47 +00:00
try {
if ($this->updateInProgress) {
2023-06-16 19:16:30 +00:00
return;
}
$this->updateInProgress = true;
UpdateCoolify::run(manual_update: true);
2023-09-11 15:36:30 +00:00
} catch (\Throwable $e) {
return handleError($e, $this);
2023-05-25 20:33:47 +00:00
}
2023-04-28 13:08:48 +00:00
}
public function getUpgradeStatus(): array
{
// Only root team members can view upgrade status
if (auth()->user()?->currentTeam()?->id !== 0) {
return ['status' => 'none'];
}
$server = Server::find(0);
if (! $server) {
return ['status' => 'none'];
}
$statusFile = '/data/coolify/source/.upgrade-status';
try {
$content = instant_remote_process(
["cat {$statusFile} 2>/dev/null || echo ''"],
$server,
false
);
$content = trim($content ?? '');
} catch (\Throwable $e) {
return ['status' => 'none'];
}
if (empty($content)) {
return ['status' => 'none'];
}
$parts = explode('|', $content);
if (count($parts) < 3) {
return ['status' => 'none'];
}
[$step, $message, $timestamp] = $parts;
// Check if status is stale (older than 10 minutes)
try {
$statusTime = new \DateTime($timestamp);
$now = new \DateTime;
$diffMinutes = ($now->getTimestamp() - $statusTime->getTimestamp()) / 60;
if ($diffMinutes > 10) {
return ['status' => 'none'];
}
} catch (\Throwable $e) {
return ['status' => 'none'];
}
if ($step === 'error') {
return [
'status' => 'error',
'step' => 0,
'message' => $message,
];
}
$stepInt = (int) $step;
$status = $stepInt >= 6 ? 'complete' : 'in_progress';
return [
'status' => $status,
'step' => $stepInt,
'message' => $message,
];
}
}