lasthourcloud/app/Jobs/InstanceAutoUpdateJob.php

85 lines
2.7 KiB
PHP
Raw Normal View History

2023-05-22 12:00:59 +02:00
<?php
namespace App\Jobs;
use App\Enums\ActivityTypes;
use App\Models\InstanceSettings;
use App\Models\Server;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Queue\SerializesModels;
2023-05-25 14:40:47 +02:00
use Log;
2023-05-22 12:00:59 +02:00
2023-05-25 14:23:49 +02:00
class InstanceAutoUpdateJob implements ShouldQueue
2023-05-22 12:00:59 +02:00
{
2023-05-22 12:47:15 +02:00
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
2023-05-22 12:00:59 +02:00
2023-05-25 14:40:47 +02:00
private string $latest_version;
private string $current_version;
private Server $server;
2023-05-25 21:43:24 +02:00
private string $server_name = 'localhost';
2023-05-25 14:40:47 +02:00
public function __construct(private bool $force = false)
2023-05-22 12:00:59 +02:00
{
2023-05-25 14:40:47 +02:00
if (config('app.env') === 'local') {
$this->server_name = 'coolify-testing-host';
}
2023-05-22 12:00:59 +02:00
$instance_settings = InstanceSettings::get();
2023-05-25 21:49:16 +02:00
$this->server = Server::where('name', $this->server_name)->firstOrFail();
2023-05-25 21:59:29 +02:00
Log::info($this->server);
Log::info('Force: ' . $this->force);
2023-05-25 14:40:47 +02:00
if (!$instance_settings->is_auto_update_enabled || !$this->server) {
return $this->delete();
2023-05-22 12:00:59 +02:00
}
2023-05-25 14:40:47 +02:00
$this->latest_version = get_latest_version_of_coolify();
$this->current_version = config('version');
if (!$this->force) {
2023-05-25 22:03:24 +02:00
Log::info('Checking if update available');
2023-05-25 14:40:47 +02:00
try {
$this->check_if_update_available();
} catch (\Exception $e) {
Log::error($e->getMessage());
return $this->delete();
}
}
}
private function check_if_update_available()
{
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("Already on latest version");
}
}
2023-05-22 12:00:59 +02:00
public function handle(): void
{
2023-05-25 14:40:47 +02:00
try {
if (config('app.env') === 'local') {
instant_remote_process([
"sleep 2"
], $this->server);
remote_process([
"sleep 10"
], $this->server);
} else {
2023-05-25 22:03:24 +02:00
Log::info('Downloading upgrade script');
2023-05-25 14:40:47 +02:00
instant_remote_process([
2023-05-25 21:58:59 +02:00
"curl -fsSL https://coolify-cdn.b-cdn.net/files/upgrade.sh -o /data/coolify/source/upgrade.sh",
2023-05-25 14:40:47 +02:00
], $this->server);
2023-05-25 22:03:24 +02:00
Log::info('Running upgrade script');
2023-05-25 14:40:47 +02:00
remote_process([
2023-05-25 21:58:59 +02:00
"bash /data/coolify/source/upgrade.sh $this->latest_version"
2023-05-25 15:48:26 +02:00
], $this->server);
2023-05-22 12:00:59 +02:00
}
2023-05-25 14:40:47 +02:00
} catch (\Exception $e) {
Log::error($e->getMessage());
2023-05-22 12:00:59 +02:00
}
}
}