2023-06-15 09:23:48 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace App\Actions\Server;
|
|
|
|
|
|
|
|
use App\Models\InstanceSettings;
|
|
|
|
use App\Models\Server;
|
|
|
|
|
|
|
|
class UpdateCoolify
|
|
|
|
{
|
2023-09-01 08:11:00 +00:00
|
|
|
public ?Server $server = null;
|
|
|
|
public ?string $latestVersion = null;
|
|
|
|
public ?string $currentVersion = null;
|
2023-06-15 09:23:48 +00:00
|
|
|
|
|
|
|
public function __invoke(bool $force)
|
|
|
|
{
|
|
|
|
try {
|
2023-06-23 11:13:02 +00:00
|
|
|
$settings = InstanceSettings::get();
|
2023-06-15 09:23:48 +00:00
|
|
|
ray('Running InstanceAutoUpdateJob');
|
|
|
|
$localhost_name = 'localhost';
|
2023-08-28 16:02:31 +00:00
|
|
|
$this->server = Server::where('name', $localhost_name)->first();
|
|
|
|
if (!$this->server) {
|
|
|
|
return;
|
|
|
|
}
|
2023-09-01 08:11:00 +00:00
|
|
|
$this->latestVersion = get_latest_version_of_coolify();
|
|
|
|
$this->currentVersion = config('version');
|
|
|
|
ray('latest version:' . $this->latestVersion . " current version: " . $this->currentVersion . ' force: ' . $force);
|
2023-06-23 11:13:02 +00:00
|
|
|
if ($settings->next_channel) {
|
|
|
|
ray('next channel enabled');
|
2023-09-01 08:11:00 +00:00
|
|
|
$this->latestVersion = 'next';
|
2023-06-23 11:13:02 +00:00
|
|
|
}
|
2023-06-15 09:23:48 +00:00
|
|
|
if ($force) {
|
|
|
|
$this->update();
|
|
|
|
} else {
|
2023-06-23 11:13:02 +00:00
|
|
|
if (!$settings->is_auto_update_enabled) {
|
2023-08-24 18:51:14 +00:00
|
|
|
return 'Auto update is disabled';
|
2023-06-15 09:23:48 +00:00
|
|
|
}
|
2023-09-01 08:11:00 +00:00
|
|
|
if ($this->latestVersion === $this->currentVersion) {
|
2023-08-24 18:51:14 +00:00
|
|
|
return 'Already on latest version';
|
2023-06-15 09:23:48 +00:00
|
|
|
}
|
2023-09-01 08:11:00 +00:00
|
|
|
if (version_compare($this->latestVersion, $this->currentVersion, '<')) {
|
2023-08-24 18:51:14 +00:00
|
|
|
return 'Latest version is lower than current version?!';
|
2023-06-15 09:23:48 +00:00
|
|
|
}
|
|
|
|
$this->update();
|
|
|
|
}
|
2023-09-01 08:11:00 +00:00
|
|
|
send_internal_notification('InstanceAutoUpdateJob done to version: ' . $this->latestVersion . ' from version: ' . $this->currentVersion);
|
2023-09-11 15:36:30 +00:00
|
|
|
} catch (\Throwable $e) {
|
2023-06-15 09:23:48 +00:00
|
|
|
ray('InstanceAutoUpdateJob failed');
|
2023-09-11 15:36:30 +00:00
|
|
|
ray($e->getMessage());
|
|
|
|
send_internal_notification('InstanceAutoUpdateJob failed: ' . $e->getMessage());
|
|
|
|
throw $e;
|
2023-06-15 09:23:48 +00:00
|
|
|
}
|
|
|
|
}
|
2023-08-08 09:51:36 +00:00
|
|
|
|
2023-06-15 09:23:48 +00:00
|
|
|
private function update()
|
|
|
|
{
|
2023-08-27 13:23:47 +00:00
|
|
|
if (isDev()) {
|
2023-09-01 08:11:00 +00:00
|
|
|
ray("Running update on local docker container. Updating to $this->latestVersion");
|
2023-06-15 09:23:48 +00:00
|
|
|
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",
|
2023-09-01 08:11:00 +00:00
|
|
|
"bash /data/coolify/source/upgrade.sh $this->latestVersion"
|
2023-06-15 09:23:48 +00:00
|
|
|
], $this->server);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|