lasthourcloud/app/Jobs/InstanceAutoUpdate.php

88 lines
2.8 KiB
PHP
Raw Normal View History

2023-05-22 10:00:59 +00: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;
use Illuminate\Support\Facades\Log;
2023-05-24 12:26:50 +00:00
class InstanceAutoUpdate 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
/**
* Create a new job instance.
*/
public function __construct()
{
$instance_settings = InstanceSettings::get();
if (!$instance_settings->is_auto_update_enabled) {
$this->delete();
}
}
/**
* Execute the job.
*/
public function handle(): void
{
if (config('app.env') === 'local') {
2023-05-24 12:26:50 +00:00
$latest_version = get_latest_version_of_coolify();
2023-05-22 10:00:59 +00:00
$current_version = config('version');
if ($latest_version === $current_version) {
return;
}
if (version_compare($latest_version, $current_version, '<')) {
return;
}
$server = Server::where('ip', 'coolify-testing-host')->first();
if (!$server) {
return;
}
2023-05-24 13:25:08 +00:00
instant_remote_process([
2023-05-22 10:00:59 +00:00
"sleep 2"
], $server);
2023-05-24 13:25:08 +00:00
remote_process([
2023-05-22 10:00:59 +00:00
"sleep 10"
], $server, ActivityTypes::INLINE->value);
} else {
2023-05-24 12:26:50 +00:00
$latest_version = get_latest_version_of_coolify();
2023-05-22 10:00:59 +00:00
$current_version = config('version');
if ($latest_version === $current_version) {
return;
}
if (version_compare($latest_version, $current_version, '<')) {
return;
}
$cdn = "https://coolify-cdn.b-cdn.net/files";
$server = Server::where('ip', 'host.docker.internal')->first();
if (!$server) {
return;
}
2023-05-24 13:25:08 +00:00
instant_remote_process([
2023-05-22 10:00:59 +00:00
"curl -fsSL $cdn/docker-compose.yml -o /data/coolify/source/docker-compose.yml",
"curl -fsSL $cdn/docker-compose.prod.yml -o /data/coolify/source/docker-compose.prod.yml",
"curl -fsSL $cdn/.env.production -o /data/coolify/source/.env.production",
"curl -fsSL $cdn/upgrade.sh -o /data/coolify/source/upgrade.sh",
], $server);
2023-05-24 13:25:08 +00:00
instant_remote_process([
2023-05-22 10:00:59 +00:00
"docker compose -f /data/coolify/source/docker-compose.yml -f /data/coolify/source/docker-compose.prod.yml pull",
], $server);
2023-05-24 13:25:08 +00:00
remote_process([
2023-05-22 10:00:59 +00:00
"bash /data/coolify/source/upgrade.sh $latest_version"
], $server, ActivityTypes::INLINE->value);
}
}
}