From 1a152a55977e832a24ecf4ce423ad6160a4eb095 Mon Sep 17 00:00:00 2001 From: Andras Bacsai Date: Wed, 8 May 2024 19:19:32 +0200 Subject: [PATCH] feat: pull new sentinel image and restart container --- app/Actions/Server/StartSentinel.php | 7 ++-- app/Console/Kernel.php | 2 ++ app/Jobs/PullSentinelImageJob.php | 54 ++++++++++++++++++++++++++++ bootstrap/helpers/shared.php | 12 +++++++ docker-compose.prod.yml | 12 ------- docker-compose.yml | 6 ---- versions.json | 3 ++ 7 files changed, 76 insertions(+), 20 deletions(-) create mode 100644 app/Jobs/PullSentinelImageJob.php diff --git a/app/Actions/Server/StartSentinel.php b/app/Actions/Server/StartSentinel.php index c590aff65..40528dcf5 100644 --- a/app/Actions/Server/StartSentinel.php +++ b/app/Actions/Server/StartSentinel.php @@ -8,8 +8,11 @@ class StartSentinel { use AsAction; - public function handle(Server $server) + public function handle(Server $server, $version = 'latest', bool $restart = false) { - return instant_remote_process(['docker run --rm --pull always -d --name coolify-sentinel -p 127.0.0.1:8888:8888 -v /var/run/docker.sock:/var/run/docker.sock -v /data/coolify/metrics:/var/www/html/storage/app/metrics --pid host --health-cmd "curl --fail http://127.0.0.1:8888/api/health || exit 1" --health-interval 10s --health-retries 3 ghcr.io/coollabsio/sentinel:latest'], $server, false); + if ($restart) { + instant_remote_process(['docker rm -f coolify-sentinel'], $server, false); + } + return instant_remote_process(["docker run --rm --pull always -d --name coolify-sentinel -v /var/run/docker.sock:/var/run/docker.sock -v /data/coolify/metrics:/var/www/html/storage/app/metrics --pid host --health-cmd \"curl --fail http://127.0.0.1:8888/api/health || exit 1\" --health-interval 10s --health-retries 3 ghcr.io/coollabsio/sentinel:$version"], $server, false); } } diff --git a/app/Console/Kernel.php b/app/Console/Kernel.php index 1f7006aaa..efc64ddf7 100644 --- a/app/Console/Kernel.php +++ b/app/Console/Kernel.php @@ -9,6 +9,7 @@ use App\Jobs\InstanceAutoUpdateJob; use App\Jobs\ContainerStatusJob; use App\Jobs\PullHelperImageJob; +use App\Jobs\PullSentinelImageJob; use App\Jobs\ServerStatusJob; use App\Models\InstanceSettings; use App\Models\ScheduledDatabaseBackup; @@ -57,6 +58,7 @@ private function pull_helper_image($schedule) { $servers = Server::all()->where('settings.is_usable', true)->where('settings.is_reachable', true)->where('ip', '!=', '1.2.3.4'); foreach ($servers as $server) { + $schedule->job(new PullSentinelImageJob($server))->everyMinute()->onOneServer(); $schedule->job(new PullHelperImageJob($server))->everyTenMinutes()->onOneServer(); } } diff --git a/app/Jobs/PullSentinelImageJob.php b/app/Jobs/PullSentinelImageJob.php new file mode 100644 index 000000000..8e41adecf --- /dev/null +++ b/app/Jobs/PullSentinelImageJob.php @@ -0,0 +1,54 @@ +server->uuid))]; + } + + public function uniqueId(): string + { + return $this->server->uuid; + } + public function __construct(public Server $server) + { + } + public function handle(): void + { + try { + $version = get_latest_sentinel_version(); + if (!$version) { + ray('Failed to get latest Sentinel version'); + return; + } + $local_version = instant_remote_process(['docker exec coolify-sentinel sh -c "curl http://127.0.0.1:8888/api/version"'], $this->server, '0.0.0'); + if ($version === $local_version) { + ray('Sentinel image is up to date'); + return; + } + ray('Pulling Sentinel image'); + StartSentinel::run($this->server, $version, true); + } catch (\Throwable $e) { + send_internal_notification('PullSentinelImageJob failed with: ' . $e->getMessage()); + ray($e->getMessage()); + throw $e; + } + } +} diff --git a/bootstrap/helpers/shared.php b/bootstrap/helpers/shared.php index 4df95f2c8..b93b0ec3d 100644 --- a/bootstrap/helpers/shared.php +++ b/bootstrap/helpers/shared.php @@ -147,6 +147,18 @@ function get_route_parameters(): array return Route::current()->parameters(); } +function get_latest_sentinel_version(): string +{ + try { + $response = Http::get('https://cdn.coollabs.io/coolify/versions.json'); + $versions = $response->json(); + return data_get($versions, 'coolify.sentinel.version'); + } catch (\Throwable $e) { + //throw $e; + ray($e->getMessage()); + return '0.0.0'; + } +} function get_latest_version_of_coolify(): string { try { diff --git a/docker-compose.prod.yml b/docker-compose.prod.yml index b934d8222..f3dda9748 100644 --- a/docker-compose.prod.yml +++ b/docker-compose.prod.yml @@ -103,18 +103,6 @@ services: condition: service_healthy redis: condition: service_healthy - sentinel: - image: "ghcr.io/coollabsio/sentinel:${LATEST_IMAGE:-latest}" - volumes: - - /var/run/docker.sock:/var/run/docker.sock:ro - - /data/coolify/metrics:/var/www/html/storage/app/metrics - ports: - - "127.0.0.1:8888:8888" - healthcheck: - test: curl --fail http://127.0.0.1:8888/api/health || exit 1 - interval: 10s - retries: 2 - timeout: 5s postgres: volumes: - coolify-db:/var/lib/postgresql/data diff --git a/docker-compose.yml b/docker-compose.yml index f292cc880..8eed44f8c 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -10,12 +10,6 @@ services: depends_on: - postgres - redis - sentinel: - image: "ghcr.io/coollabsio/sentinel:latest" - container_name: coolify-sentinel - restart: always - networks: - - coolify postgres: image: postgres:15-alpine container_name: coolify-db diff --git a/versions.json b/versions.json index 84efec309..bb26a72a8 100644 --- a/versions.json +++ b/versions.json @@ -2,6 +2,9 @@ "coolify": { "v4": { "version": "4.0.0-beta.277" + }, + "sentinel": { + "version": "0.0.1" } } }