fix: use local versions + service templates and query them every 10 minutes

This commit is contained in:
Andras Bacsai 2024-05-22 09:23:17 +02:00
parent 3237ca0d97
commit 10f3d8aa0f
11 changed files with 95 additions and 1149 deletions

View File

@ -40,7 +40,7 @@ class ServicesGenerate extends Command
$serviceTemplatesJson[$name] = $parsed; $serviceTemplatesJson[$name] = $parsed;
} }
} }
$serviceTemplatesJson = json_encode($serviceTemplatesJson, JSON_PRETTY_PRINT); $serviceTemplatesJson = json_encode($serviceTemplatesJson);
file_put_contents(base_path('templates/service-templates.json'), $serviceTemplatesJson); file_put_contents(base_path('templates/service-templates.json'), $serviceTemplatesJson);
} }

View File

@ -10,6 +10,7 @@ use App\Jobs\InstanceAutoUpdateJob;
use App\Jobs\ContainerStatusJob; use App\Jobs\ContainerStatusJob;
use App\Jobs\PullHelperImageJob; use App\Jobs\PullHelperImageJob;
use App\Jobs\PullSentinelImageJob; use App\Jobs\PullSentinelImageJob;
use App\Jobs\PullTemplatesAndVersions;
use App\Jobs\ServerStatusJob; use App\Jobs\ServerStatusJob;
use App\Models\InstanceSettings; use App\Models\InstanceSettings;
use App\Models\ScheduledDatabaseBackup; use App\Models\ScheduledDatabaseBackup;
@ -29,6 +30,7 @@ class Kernel extends ConsoleKernel
// Instance Jobs // Instance Jobs
$schedule->command('horizon:snapshot')->everyMinute(); $schedule->command('horizon:snapshot')->everyMinute();
$schedule->job(new CleanupInstanceStuffsJob)->everyMinute()->onOneServer(); $schedule->job(new CleanupInstanceStuffsJob)->everyMinute()->onOneServer();
$schedule->job(new PullTemplatesAndVersions)->everyTenMinutes()->onOneServer();
// $schedule->job(new CheckResaleLicenseJob)->hourly()->onOneServer(); // $schedule->job(new CheckResaleLicenseJob)->hourly()->onOneServer();
// Server Jobs // Server Jobs
$this->check_scheduled_backups($schedule); $this->check_scheduled_backups($schedule);
@ -41,7 +43,7 @@ class Kernel extends ConsoleKernel
// Instance Jobs // Instance Jobs
$schedule->command('horizon:snapshot')->everyFiveMinutes(); $schedule->command('horizon:snapshot')->everyFiveMinutes();
$schedule->command('cleanup:unreachable-servers')->daily(); $schedule->command('cleanup:unreachable-servers')->daily();
$schedule->job(new PullTemplatesAndVersions)->everyTenMinutes()->onOneServer();
$schedule->job(new CleanupInstanceStuffsJob)->everyTwoMinutes()->onOneServer(); $schedule->job(new CleanupInstanceStuffsJob)->everyTwoMinutes()->onOneServer();
// $schedule->job(new CheckResaleLicenseJob)->hourly()->onOneServer(); // $schedule->job(new CheckResaleLicenseJob)->hourly()->onOneServer();

View File

@ -0,0 +1,55 @@
<?php
namespace App\Jobs;
use App\Models\Server;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldBeEncrypted;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Queue\SerializesModels;
use Illuminate\Support\Facades\File;
use Illuminate\Support\Facades\Http;
class PullTemplatesAndVersions implements ShouldQueue, ShouldBeEncrypted
{
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
public $timeout = 10;
public function __construct()
{
}
public function handle(): void
{
try {
if (!isDev() && !isCloud()) {
ray('PullTemplatesAndVersions versions.json');
$response = Http::retry(3, 1000)->get('https://cdn.coollabs.io/coolify/versions.json');
if ($response->successful()) {
$versions = $response->json();
File::put(base_path('versions.json'), json_encode($versions, JSON_PRETTY_PRINT));
} else {
send_internal_notification('PullTemplatesAndVersions failed with: ' . $response->status() . ' ' . $response->body());
}
}
} catch (\Throwable $e) {
send_internal_notification('PullTemplatesAndVersions failed with: ' . $e->getMessage());
ray($e->getMessage());
}
try {
ray('PullTemplatesAndVersions service-templates');
$response = Http::retry(3, 1000)->get(config('constants.services.official'));
if ($response->successful()) {
$services = $response->json();
File::put(base_path('templates/service-templates.json'), json_encode($services));
} else {
send_internal_notification('PullTemplatesAndVersions failed with: ' . $response->status() . ' ' . $response->body());
}
} catch (\Throwable $e) {
send_internal_notification('PullTemplatesAndVersions failed with: ' . $e->getMessage());
ray($e->getMessage());
}
}
}

View File

@ -10,7 +10,7 @@ class Compose extends Component
public string $base64 = ''; public string $base64 = '';
public $services; public $services;
public function mount() { public function mount() {
$this->services = getServiceTemplates(); $this->services = get_service_templates();
} }
public function setService(string $selected) { public function setService(string $selected) {
$this->base64 = data_get($this->services, $selected . '.compose'); $this->base64 = data_get($this->services, $selected . '.compose');

View File

@ -91,7 +91,7 @@ class Select extends Component
}); });
} else { } else {
$this->search = null; $this->search = null;
$this->allServices = getServiceTemplates(); $this->allServices = get_service_templates();
$this->services = $this->allServices->filter(function ($service, $key) { $this->services = $this->allServices->filter(function ($service, $key) {
return str_contains(strtolower($key), strtolower($this->search)); return str_contains(strtolower($key), strtolower($this->search));
}); });

View File

@ -25,7 +25,7 @@ class Create extends Component
return redirect()->route('dashboard'); return redirect()->route('dashboard');
} }
if (isset($type) && isset($destination_uuid) && isset($server_id)) { if (isset($type) && isset($destination_uuid) && isset($server_id)) {
$services = getServiceTemplates(); $services = get_service_templates();
if (in_array($type, DATABASE_TYPES)) { if (in_array($type, DATABASE_TYPES)) {
if ($type->value() === "postgresql") { if ($type->value() === "postgresql") {

View File

@ -667,7 +667,7 @@ class Service extends BaseModel
} }
public function documentation() public function documentation()
{ {
$services = getServiceTemplates(); $services = get_service_templates();
$service = data_get($services, str($this->name)->beforeLast('-')->value, []); $service = data_get($services, str($this->name)->beforeLast('-')->value, []);
return data_get($service, 'documentation', config('constants.docs.base_url')); return data_get($service, 'documentation', config('constants.docs.base_url'));
} }

View File

@ -165,9 +165,12 @@ function get_latest_sentinel_version(): string
function get_latest_version_of_coolify(): string function get_latest_version_of_coolify(): string
{ {
try { try {
$response = Http::get('https://cdn.coollabs.io/coolify/versions.json'); $versions = File::get(base_path('versions.json'));
$versions = $response->json(); $versions = json_decode($versions, true);
return data_get($versions, 'coolify.v4.version'); return data_get($versions, 'coolify.v4.version');
// $response = Http::get('https://cdn.coollabs.io/coolify/versions.json');
// $versions = $response->json();
// return data_get($versions, 'coolify.v4.version');
} catch (\Throwable $e) { } catch (\Throwable $e) {
//throw $e; //throw $e;
ray($e->getMessage()); ray($e->getMessage());
@ -462,24 +465,25 @@ function sslip(Server $server)
return "http://{$server->ip}.sslip.io"; return "http://{$server->ip}.sslip.io";
} }
function getServiceTemplates() function get_service_templates()
{ {
if (isDev()) { // if (isDev()) {
$services = File::get(base_path('templates/service-templates.json')); // $services = File::get(base_path('templates/service-templates.json'));
$services = collect(json_decode($services))->sortKeys(); // $services = collect(json_decode($services))->sortKeys();
} else { // } else {
try { // try {
$response = Http::retry(3, 50)->get(config('constants.services.official')); // $response = Http::retry(3, 50)->get(config('constants.services.official'));
if ($response->failed()) { // if ($response->failed()) {
return collect([]); // return collect([]);
} // }
$services = $response->json(); // $services = $response->json();
$services = collect($services)->sortKeys(); // $services = collect($services)->sortKeys();
} catch (\Throwable $e) { // } catch (\Throwable $e) {
$services = collect([]); // $services = collect([]);
} // }
} // }
return $services; $services = File::get(base_path('templates/service-templates.json'));
return collect(json_decode($services))->sortKeys();
} }
function getResourceByUuid(string $uuid, ?int $teamId = null) function getResourceByUuid(string $uuid, ?int $teamId = null)
@ -649,7 +653,7 @@ function parseDockerComposeFile(Service|Application $resource, bool $isNew = fal
} catch (\Exception $e) { } catch (\Exception $e) {
throw new \Exception($e->getMessage()); throw new \Exception($e->getMessage());
} }
$allServices = getServiceTemplates(); $allServices = get_service_templates();
$topLevelVolumes = collect(data_get($yaml, 'volumes', [])); $topLevelVolumes = collect(data_get($yaml, 'volumes', []));
$topLevelNetworks = collect(data_get($yaml, 'networks', [])); $topLevelNetworks = collect(data_get($yaml, 'networks', []));
$services = data_get($yaml, 'services'); $services = data_get($yaml, 'services');

View File

@ -21,8 +21,8 @@ return [
], ],
'services' => [ 'services' => [
// Temporary disabled until cache is implemented // Temporary disabled until cache is implemented
// 'official' => 'https://cdn.coollabs.io/coolify/service-templates.json', 'official' => 'https://cdn.coollabs.io/coolify/service-templates.json',
'official' => 'https://raw.githubusercontent.com/coollabsio/coolify/main/templates/service-templates.json', // 'official' => 'https://raw.githubusercontent.com/coollabsio/coolify/main/templates/service-templates.json',
], ],
'limits' => [ 'limits' => [
'trial_period' => 0, 'trial_period' => 0,

View File

@ -307,7 +307,6 @@
@if (isCloud() && isInstanceAdmin()) @if (isCloud() && isInstanceAdmin())
<li> <li>
<a title="Admin" class="menu-item" href="/admin"> <a title="Admin" class="menu-item" href="/admin">
<svg class="text-pink-600 icon" viewBox="0 0 256 256" <svg class="text-pink-600 icon" viewBox="0 0 256 256"
xmlns="http://www.w3.org/2000/svg"> xmlns="http://www.w3.org/2000/svg">
@ -320,9 +319,11 @@
@endif @endif
<div class="flex-1"></div> <div class="flex-1"></div>
@if (isInstanceAdmin() && !isCloud()) @if (isInstanceAdmin() && !isCloud())
<li> @persist('upgrade')
<livewire:upgrade /> <li>
</li> <livewire:upgrade />
</li>
@endpersist
@endif @endif
<li> <li>
<a title="Onboarding" <a title="Onboarding"

File diff suppressed because one or more lines are too long