Skip to content

Commit

Permalink
fix: use local versions + service templates and query them every 10 m…
Browse files Browse the repository at this point in the history
…inutes
  • Loading branch information
andrasbacsai committed May 22, 2024
1 parent 3237ca0 commit 10f3d8a
Show file tree
Hide file tree
Showing 11 changed files with 95 additions and 1,149 deletions.
2 changes: 1 addition & 1 deletion app/Console/Commands/ServicesGenerate.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ public function handle()
$serviceTemplatesJson[$name] = $parsed;
}
}
$serviceTemplatesJson = json_encode($serviceTemplatesJson, JSON_PRETTY_PRINT);
$serviceTemplatesJson = json_encode($serviceTemplatesJson);
file_put_contents(base_path('templates/service-templates.json'), $serviceTemplatesJson);
}

Expand Down
4 changes: 3 additions & 1 deletion app/Console/Kernel.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
use App\Jobs\ContainerStatusJob;
use App\Jobs\PullHelperImageJob;
use App\Jobs\PullSentinelImageJob;
use App\Jobs\PullTemplatesAndVersions;
use App\Jobs\ServerStatusJob;
use App\Models\InstanceSettings;
use App\Models\ScheduledDatabaseBackup;
Expand All @@ -29,6 +30,7 @@ protected function schedule(Schedule $schedule): void
// Instance Jobs
$schedule->command('horizon:snapshot')->everyMinute();
$schedule->job(new CleanupInstanceStuffsJob)->everyMinute()->onOneServer();
$schedule->job(new PullTemplatesAndVersions)->everyTenMinutes()->onOneServer();
// $schedule->job(new CheckResaleLicenseJob)->hourly()->onOneServer();
// Server Jobs
$this->check_scheduled_backups($schedule);
Expand All @@ -41,7 +43,7 @@ protected function schedule(Schedule $schedule): void
// Instance Jobs
$schedule->command('horizon:snapshot')->everyFiveMinutes();
$schedule->command('cleanup:unreachable-servers')->daily();

$schedule->job(new PullTemplatesAndVersions)->everyTenMinutes()->onOneServer();
$schedule->job(new CleanupInstanceStuffsJob)->everyTwoMinutes()->onOneServer();
// $schedule->job(new CheckResaleLicenseJob)->hourly()->onOneServer();

Expand Down
55 changes: 55 additions & 0 deletions app/Jobs/PullTemplatesAndVersions.php
Original file line number Diff line number Diff line change
@@ -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());
}
}
}
2 changes: 1 addition & 1 deletion app/Livewire/Dev/Compose.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ class Compose extends Component
public string $base64 = '';
public $services;
public function mount() {
$this->services = getServiceTemplates();
$this->services = get_service_templates();
}
public function setService(string $selected) {
$this->base64 = data_get($this->services, $selected . '.compose');
Expand Down
2 changes: 1 addition & 1 deletion app/Livewire/Project/New/Select.php
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ public function loadServices(bool $force = false)
});
} else {
$this->search = null;
$this->allServices = getServiceTemplates();
$this->allServices = get_service_templates();
$this->services = $this->allServices->filter(function ($service, $key) {
return str_contains(strtolower($key), strtolower($this->search));
});
Expand Down
2 changes: 1 addition & 1 deletion app/Livewire/Project/Resource/Create.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ public function mount()
return redirect()->route('dashboard');
}
if (isset($type) && isset($destination_uuid) && isset($server_id)) {
$services = getServiceTemplates();
$services = get_service_templates();

if (in_array($type, DATABASE_TYPES)) {
if ($type->value() === "postgresql") {
Expand Down
2 changes: 1 addition & 1 deletion app/Models/Service.php
Original file line number Diff line number Diff line change
Expand Up @@ -667,7 +667,7 @@ public function failedTaskLink($task_uuid)
}
public function documentation()
{
$services = getServiceTemplates();
$services = get_service_templates();
$service = data_get($services, str($this->name)->beforeLast('-')->value, []);
return data_get($service, 'documentation', config('constants.docs.base_url'));
}
Expand Down
44 changes: 24 additions & 20 deletions bootstrap/helpers/shared.php
Original file line number Diff line number Diff line change
Expand Up @@ -165,9 +165,12 @@ function get_latest_sentinel_version(): string
function get_latest_version_of_coolify(): string
{
try {
$response = Http::get('https://cdn.coollabs.io/coolify/versions.json');
$versions = $response->json();
$versions = File::get(base_path('versions.json'));
$versions = json_decode($versions, true);
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) {
//throw $e;
ray($e->getMessage());
Expand Down Expand Up @@ -462,24 +465,25 @@ function sslip(Server $server)
return "http://{$server->ip}.sslip.io";
}

function getServiceTemplates()
function get_service_templates()
{
if (isDev()) {
$services = File::get(base_path('templates/service-templates.json'));
$services = collect(json_decode($services))->sortKeys();
} else {
try {
$response = Http::retry(3, 50)->get(config('constants.services.official'));
if ($response->failed()) {
return collect([]);
}
$services = $response->json();
$services = collect($services)->sortKeys();
} catch (\Throwable $e) {
$services = collect([]);
}
}
return $services;
// if (isDev()) {
// $services = File::get(base_path('templates/service-templates.json'));
// $services = collect(json_decode($services))->sortKeys();
// } else {
// try {
// $response = Http::retry(3, 50)->get(config('constants.services.official'));
// if ($response->failed()) {
// return collect([]);
// }
// $services = $response->json();
// $services = collect($services)->sortKeys();
// } catch (\Throwable $e) {
// $services = collect([]);
// }
// }
$services = File::get(base_path('templates/service-templates.json'));
return collect(json_decode($services))->sortKeys();
}

function getResourceByUuid(string $uuid, ?int $teamId = null)
Expand Down Expand Up @@ -649,7 +653,7 @@ function parseDockerComposeFile(Service|Application $resource, bool $isNew = fal
} catch (\Exception $e) {
throw new \Exception($e->getMessage());
}
$allServices = getServiceTemplates();
$allServices = get_service_templates();
$topLevelVolumes = collect(data_get($yaml, 'volumes', []));
$topLevelNetworks = collect(data_get($yaml, 'networks', []));
$services = data_get($yaml, 'services');
Expand Down
4 changes: 2 additions & 2 deletions config/constants.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@
],
'services' => [
// Temporary disabled until cache is implemented
// 'official' => 'https://cdn.coollabs.io/coolify/service-templates.json',
'official' => 'https://raw.githubusercontent.com/coollabsio/coolify/main/templates/service-templates.json',
'official' => 'https://cdn.coollabs.io/coolify/service-templates.json',
// 'official' => 'https://raw.githubusercontent.com/coollabsio/coolify/main/templates/service-templates.json',
],
'limits' => [
'trial_period' => 0,
Expand Down
9 changes: 5 additions & 4 deletions resources/views/components/navbar.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -307,7 +307,6 @@ class="{{ request()->is('settings*') ? 'menu-item-active menu-item' : 'menu-item

@if (isCloud() && isInstanceAdmin())
<li>

<a title="Admin" class="menu-item" href="/admin">
<svg class="text-pink-600 icon" viewBox="0 0 256 256"
xmlns="http://www.w3.org/2000/svg">
Expand All @@ -320,9 +319,11 @@ class="{{ request()->is('settings*') ? 'menu-item-active menu-item' : 'menu-item
@endif
<div class="flex-1"></div>
@if (isInstanceAdmin() && !isCloud())
<li>
<livewire:upgrade />
</li>
@persist('upgrade')
<li>
<livewire:upgrade />
</li>
@endpersist
@endif
<li>
<a title="Onboarding"
Expand Down
1,118 changes: 1 addition & 1,117 deletions templates/service-templates.json

Large diffs are not rendered by default.

0 comments on commit 10f3d8a

Please sign in to comment.