This commit is contained in:
Andras Bacsai
2023-06-06 08:43:01 +02:00
parent 63a2e69cc6
commit 2014183e88
6 changed files with 60 additions and 49 deletions

View File

@@ -10,21 +10,18 @@ use Illuminate\Foundation\Console\Kernel as ConsoleKernel;
class Kernel extends ConsoleKernel
{
/**
* Define the application's command schedule.
*/
protected function schedule(Schedule $schedule): void
{
$schedule->command('horizon:snapshot')->everyFiveMinutes();
$schedule->job(new InstanceDockerCleanupJob)->everyFiveMinutes();
$schedule->job(new InstanceAutoUpdateJob)->everyFifteenMinutes();
// $schedule->job(new InstanceProxyCheckJob)->everyMinute();
if (config('app.env') === 'local') {
$schedule->command('horizon:snapshot')->everyMinute();
$schedule->job(new InstanceDockerCleanupJob)->everyMinute();
$schedule->job(new InstanceAutoUpdateJob(true))->everyMinute();
} else {
$schedule->command('horizon:snapshot')->everyFiveMinutes();
$schedule->job(new InstanceDockerCleanupJob)->everyFiveMinutes();
$schedule->job(new InstanceAutoUpdateJob)->everyFifteenMinutes();
}
}
/**
* Register the commands for the application.
*/
protected function commands(): void
{
$this->load(__DIR__ . '/Commands');

View File

@@ -18,6 +18,11 @@ class InstanceAutoUpdateJob implements ShouldQueue, ShouldBeUnique
public $tries = 1;
public $timeout = 120;
public Server $server;
public string $latest_version;
public string $current_version;
public function uniqueId(): int
{
return 1;
@@ -25,47 +30,56 @@ class InstanceAutoUpdateJob implements ShouldQueue, ShouldBeUnique
public function __construct(private bool $force = false)
{
}
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://coolify-cdn.b-cdn.net/files/upgrade.sh -o /data/coolify/source/upgrade.sh",
"bash /data/coolify/source/upgrade.sh $this->latest_version"
], $this->server);
return;
}
}
public function handle(): void
{
try {
ray('Running InstanceAutoUpdateJob');
$localhost_name = 'localhost';
if (config('app.env') === 'local') {
$localhost_name = 'testing-local-docker-container';
}
$server = Server::where('name', $localhost_name)->firstOrFail();
$latest_version = get_latest_version_of_coolify();
$current_version = config('version');
if (config('app.env') === 'local') {
instant_remote_process([
"sleep 10"
], $server);
return;
$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 {
if (!$this->force) {
$instance_settings = InstanceSettings::get();
if (!$instance_settings->is_auto_update_enabled) {
$this->fail('Auto update is disabled');
return;
}
if ($latest_version === $current_version) {
$this->fail("Already on latest version");
return;
}
if (version_compare($latest_version, $current_version, '<')) {
$this->fail("Latest version is lower than current version?!");
return;
}
$instance_settings = InstanceSettings::get();
ray($instance_settings);
if (!$instance_settings->is_auto_update_enabled) {
throw new \Exception('Auto update is disabled');
}
instant_remote_process([
"curl -fsSL https://coolify-cdn.b-cdn.net/files/upgrade.sh -o /data/coolify/source/upgrade.sh",
"bash /data/coolify/source/upgrade.sh $latest_version"
], $server);
return;
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) {
Log::error($e->getMessage());
ray('InstanceAutoUpdateJob failed');
ray($e->getMessage());
$this->fail($e->getMessage());
return;
}