lasthourcloud/app/Jobs/InstanceAutoUpdateJob.php

93 lines
3.3 KiB
PHP
Raw Normal View History

2023-05-22 10:00:59 +00:00
<?php
namespace App\Jobs;
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 12:40:47 +00:00
use Log;
2023-05-22 10:00:59 +00:00
2023-05-25 12:23:49 +00:00
class InstanceAutoUpdateJob implements ShouldQueue
2023-05-22 10:00:59 +00:00
{
2023-05-22 10:47:15 +00:00
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
2023-05-22 10:00:59 +00:00
2023-05-25 12:40:47 +00:00
private string $latest_version;
private string $current_version;
private Server $server;
2023-05-25 20:33:47 +00:00
public function __construct(private bool $force = false, Server|null $server = null)
2023-05-22 10:00:59 +00:00
{
2023-05-25 20:39:33 +00:00
Log::info("InstanceAutoUpdateJob");
2023-05-25 20:33:47 +00:00
if (is_null($server)) {
2023-05-25 20:39:33 +00:00
Log::info("InstanceAutoUpdateJob: server is null");
2023-05-25 20:33:47 +00:00
if (config('app.env') === 'local') {
$server_name = 'testing-local-docker-container';
} else {
$server_name = 'localhost';
}
$this->server = Server::where('name', $server_name)->first();
2023-05-25 20:39:33 +00:00
Log::info("InstanceAutoUpdateJob: server is " . $this->server->name);
2023-05-25 20:33:47 +00:00
} else {
$this->server = $server;
2023-05-25 12:40:47 +00:00
}
2023-05-22 10:00:59 +00:00
$instance_settings = InstanceSettings::get();
2023-05-25 12:40:47 +00:00
$this->latest_version = get_latest_version_of_coolify();
$this->current_version = config('version');
2023-05-25 20:39:33 +00:00
Log::info("InstanceAutoUpdateJob: latest version is " . $this->latest_version);
Log::info("InstanceAutoUpdateJob: current version is " . $this->current_version);
2023-05-25 12:40:47 +00:00
if (!$this->force) {
2023-05-25 20:33:47 +00:00
if (!$instance_settings->is_auto_update_enabled) {
2023-05-25 20:10:21 +00:00
return $this->delete();
}
2023-05-25 12:40:47 +00: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) {
2023-05-25 20:39:33 +00:00
Log::info("InstanceAutoUpdateJob: Already on latest version");
2023-05-25 12:40:47 +00:00
throw new \Exception("Already on latest version");
}
if (version_compare($this->latest_version, $this->current_version, '<')) {
2023-05-25 20:39:33 +00:00
Log::info("InstanceAutoUpdateJob: Already on latest version");
2023-05-25 12:40:47 +00:00
throw new \Exception("Already on latest version");
}
}
2023-05-22 10:00:59 +00:00
public function handle(): void
{
2023-05-25 12:40:47 +00:00
try {
2023-05-25 20:39:33 +00:00
Log::info('InstanceAutoUpdateJob: Starting update process');
2023-05-25 12:40:47 +00:00
if (config('app.env') === 'local') {
instant_remote_process([
"sleep 2"
], $this->server);
remote_process([
"sleep 10"
], $this->server);
} else {
2023-05-25 20:39:33 +00:00
Log::info('InstanceAutoUpdateJob: Downloading upgrade script');
2023-05-25 12:40:47 +00:00
instant_remote_process([
2023-05-25 19:58:59 +00:00
"curl -fsSL https://coolify-cdn.b-cdn.net/files/upgrade.sh -o /data/coolify/source/upgrade.sh",
2023-05-25 12:40:47 +00:00
], $this->server);
2023-05-25 20:39:33 +00:00
Log::info('InstanceAutoUpdateJob: Running upgrade script');
2023-05-25 12:40:47 +00:00
remote_process([
2023-05-25 19:58:59 +00:00
"bash /data/coolify/source/upgrade.sh $this->latest_version"
2023-05-25 13:48:26 +00:00
], $this->server);
2023-05-22 10:00:59 +00:00
}
2023-05-25 12:40:47 +00:00
} catch (\Exception $e) {
Log::error($e->getMessage());
2023-05-22 10:00:59 +00:00
}
}
}