This commit is contained in:
Andras Bacsai 2023-06-15 11:23:48 +02:00
parent fc039361b6
commit a489d95ea1
5 changed files with 75 additions and 68 deletions

View File

@ -0,0 +1,67 @@
<?php
namespace App\Actions\Server;
use App\Models\InstanceSettings;
use App\Models\Server;
class UpdateCoolify
{
public Server $server;
public string $latest_version;
public string $current_version;
public function __invoke(bool $force)
{
try {
ray('Running InstanceAutoUpdateJob');
$localhost_name = 'localhost';
if (config('app.env') === 'local') {
$localhost_name = 'testing-local-docker-container';
}
$this->server = Server::where('name', $localhost_name)->firstOrFail();
$this->latest_version = get_latest_version_of_coolify();
$this->current_version = config('version');
ray('latest version:' . $this->latest_version . " current version: " . $this->current_version . ' force: ' . $force);
if ($force) {
$this->update();
} else {
$instance_settings = InstanceSettings::get();
ray($instance_settings);
if (!$instance_settings->is_auto_update_enabled) {
throw new \Exception('Auto update is disabled');
}
if ($this->latest_version === $this->current_version) {
throw new \Exception('Already on latest version');
}
if (version_compare($this->latest_version, $this->current_version, '<')) {
throw new \Exception('Latest version is lower than current version?!');
}
$this->update();
}
return;
} catch (\Exception $e) {
ray('InstanceAutoUpdateJob failed');
ray($e->getMessage());
return;
}
}
private function update()
{
if (config('app.env') === 'local') {
ray('Running update on local docker container');
remote_process([
"sleep 10"
], $this->server);
ray('Update done');
return;
} else {
ray('Running update on production server');
remote_process([
"curl -fsSL https://cdn.coollabs.io/coolify/upgrade.sh -o /data/coolify/source/upgrade.sh",
"bash /data/coolify/source/upgrade.sh $this->latest_version"
], $this->server);
return;
}
}
}

View File

@ -14,12 +14,12 @@ protected function schedule(Schedule $schedule): void
{ {
if (config('app.env') === 'local') { if (config('app.env') === 'local') {
$schedule->command('horizon:snapshot')->everyMinute(); $schedule->command('horizon:snapshot')->everyMinute();
$schedule->job(new InstanceDockerCleanupJob)->everyMinute(); // $schedule->job(new InstanceDockerCleanupJob)->everyMinute();
$schedule->job(new InstanceAutoUpdateJob(true))->everyMinute(); // $schedule->job(new InstanceAutoUpdateJob(true))->everyMinute();
} else { } else {
$schedule->command('horizon:snapshot')->everyFiveMinutes(); $schedule->command('horizon:snapshot')->everyFiveMinutes();
$schedule->job(new InstanceDockerCleanupJob)->everyFiveMinutes(); $schedule->job(new InstanceDockerCleanupJob)->everyFiveMinutes();
$schedule->job(new InstanceAutoUpdateJob)->everyFifteenMinutes(); $schedule->job(new InstanceAutoUpdateJob)->everyTenMinutes();
} }
} }
protected function commands(): void protected function commands(): void

View File

@ -2,8 +2,8 @@
namespace App\Http\Livewire; namespace App\Http\Livewire;
use App\Actions\Server\UpdateCoolify;
use Masmerise\Toaster\Toaster; use Masmerise\Toaster\Toaster;
use App\Jobs\InstanceAutoUpdateJob;
use Livewire\Component; use Livewire\Component;
class Upgrade extends Component class Upgrade extends Component
@ -13,7 +13,7 @@ public function upgrade()
{ {
try { try {
$this->showProgress = true; $this->showProgress = true;
dispatch(new InstanceAutoUpdateJob(force: true)); resolve(UpdateCoolify::class)(true);
Toaster::success('Update started.'); Toaster::success('Update started.');
} catch (\Exception $e) { } catch (\Exception $e) {
return general_error_handler(err: $e, that: $this); return general_error_handler(err: $e, that: $this);

View File

@ -2,6 +2,7 @@
namespace App\Jobs; namespace App\Jobs;
use App\Actions\Server\UpdateCoolify;
use App\Models\InstanceSettings; use App\Models\InstanceSettings;
use App\Models\Server; use App\Models\Server;
use Exception; use Exception;
@ -11,7 +12,6 @@
use Illuminate\Foundation\Bus\Dispatchable; use Illuminate\Foundation\Bus\Dispatchable;
use Illuminate\Queue\InteractsWithQueue; use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Queue\SerializesModels; use Illuminate\Queue\SerializesModels;
use Log;
class InstanceAutoUpdateJob implements ShouldQueue, ShouldBeUnique class InstanceAutoUpdateJob implements ShouldQueue, ShouldBeUnique
{ {
@ -19,70 +19,11 @@ class InstanceAutoUpdateJob implements ShouldQueue, ShouldBeUnique
public $timeout = 120; public $timeout = 120;
public Server $server;
public string $latest_version;
public string $current_version;
public function __construct(private bool $force = false) public function __construct(private bool $force = false)
{ {
} }
public function handle(): void public function handle(): void
{ {
try { resolve(UpdateCoolify::class)($this->force);
ray('Running InstanceAutoUpdateJob');
$localhost_name = 'localhost';
if (config('app.env') === 'local') {
$localhost_name = 'testing-local-docker-container';
}
$this->server = Server::where('name', $localhost_name)->firstOrFail();
$this->latest_version = get_latest_version_of_coolify();
$this->current_version = config('version');
ray('latest version:' . $this->latest_version . " current version: " . $this->current_version . ' force: ' . $this->force);
if ($this->force) {
$this->update();
} else {
$instance_settings = InstanceSettings::get();
ray($instance_settings);
if (!$instance_settings->is_auto_update_enabled) {
throw new \Exception('Auto update is disabled');
}
if ($this->latest_version === $this->current_version) {
throw new \Exception('Already on latest version');
}
if (version_compare($this->latest_version, $this->current_version, '<')) {
throw new \Exception('Latest version is lower than current version?!');
}
$this->update();
}
return;
} catch (\Exception $e) {
ray('InstanceAutoUpdateJob failed');
ray($e->getMessage());
return;
}
}
private function update()
{
if (config('app.env') === 'local') {
ray('Running update on local docker container');
instant_remote_process([
"sleep 10"
], $this->server);
ray('Update done');
return;
} else {
ray('Running update on production server');
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->latest_version"
], $this->server);
return;
}
}
public function failed(Exception $exception)
{
return;
} }
} }

View File

@ -30,9 +30,8 @@ function revive() {
fetch('/api/health') fetch('/api/health')
.then(response => { .then(response => {
if (response.ok) { if (response.ok) {
console.log('Server is back online. Reloading...') Toaster.success('Coolify is back online. Reloading...')
setTimeout(() => { setTimeout(() => {
Toaster.success('Coolify started. Reloading!')
if (checkHealthInterval) clearInterval(checkHealthInterval); if (checkHealthInterval) clearInterval(checkHealthInterval);
window.location.reload(); window.location.reload();
}, 2000) }, 2000)