diff --git a/app/Actions/Server/UpdateCoolify.php b/app/Actions/Server/UpdateCoolify.php index a945670d4..72ce80b6b 100644 --- a/app/Actions/Server/UpdateCoolify.php +++ b/app/Actions/Server/UpdateCoolify.php @@ -20,7 +20,6 @@ class UpdateCoolify { try { $settings = InstanceSettings::get(); - ray('Running InstanceAutoUpdateJob'); $this->server = Server::find(0); if (! $this->server) { return; @@ -48,7 +47,6 @@ class UpdateCoolify private function update() { if (isDev()) { - ray('Running in dev mode'); remote_process([ 'sleep 10', ], $this->server); diff --git a/app/Console/Kernel.php b/app/Console/Kernel.php index 34761763c..aafda266e 100644 --- a/app/Console/Kernel.php +++ b/app/Console/Kernel.php @@ -14,6 +14,9 @@ use App\Jobs\PullTemplatesFromCDN; use App\Jobs\ScheduledTaskJob; use App\Jobs\ServerCheckJob; use App\Jobs\ServerStatusJob; +use App\Jobs\UpdateCoolifyJob; +use App\Jobs\CheckForUpdatesJob; +use App\Models\InstanceSettings; use App\Models\ScheduledDatabaseBackup; use App\Models\ScheduledTask; use App\Models\Server; @@ -28,11 +31,13 @@ class Kernel extends ConsoleKernel protected function schedule(Schedule $schedule): void { $this->all_servers = Server::all(); + $settings = InstanceSettings::get(); + if (isDev()) { // Instance Jobs $schedule->command('horizon:snapshot')->everyMinute(); $schedule->job(new CleanupInstanceStuffsJob)->everyMinute()->onOneServer(); - $schedule->job(new PullTemplatesFromCDN)->everyTwoHours()->onOneServer(); + $schedule->job(new PullTemplatesFromCDN)->cron($settings->update_check_frequency)->onOneServer(); // Server Jobs $this->check_scheduled_backups($schedule); $this->checkResourcesNew($schedule); @@ -44,10 +49,10 @@ class Kernel extends ConsoleKernel // Instance Jobs $schedule->command('horizon:snapshot')->everyFiveMinutes(); $schedule->command('cleanup:unreachable-servers')->daily(); - $schedule->job(new PullCoolifyImageJob)->everyTenMinutes()->onOneServer(); - $schedule->job(new PullTemplatesFromCDN)->everyThirtyMinutes()->onOneServer(); + $this->scheduleUpdates($schedule); + $schedule->job(new PullCoolifyImageJob)->cron($settings->update_check_frequency)->onOneServer(); + $schedule->job(new PullTemplatesFromCDN)->cron($settings->update_check_frequency)->onOneServer(); $schedule->job(new CleanupInstanceStuffsJob)->everyTwoMinutes()->onOneServer(); - // $schedule->job(new CheckResaleLicenseJob)->hourly()->onOneServer(); // Server Jobs $this->check_scheduled_backups($schedule); @@ -63,12 +68,41 @@ class Kernel extends ConsoleKernel private function pull_images($schedule) { + $settings = InstanceSettings::get(); $servers = $this->all_servers->where('settings.is_usable', true)->where('settings.is_reachable', true)->where('ip', '!=', '1.2.3.4'); foreach ($servers as $server) { if ($server->isSentinelEnabled()) { - $schedule->job(new PullSentinelImageJob($server))->everyFiveMinutes()->onOneServer(); + $schedule->job(new PullSentinelImageJob($server))->cron($settings->update_check_frequency)->onOneServer(); } - $schedule->job(new PullHelperImageJob($server))->everyFiveMinutes()->onOneServer(); + $schedule->job(new PullHelperImageJob($server))->cron($settings->update_check_frequency)->onOneServer(); + } + } + + private function scheduleUpdates($schedule) + { + $settings = InstanceSettings::get(); + + $updateCheckFrequency = $settings->update_check_frequency ?? '0 0 * * *'; + $schedule->job(new CheckForUpdatesJob())->cron($updateCheckFrequency)->onOneServer(); + + if ($settings->is_auto_update_enabled) { + $autoUpdateFrequency = $settings->auto_update_frequency ?? '0 11,23 * * *'; + $schedule->job(new UpdateCoolifyJob())->cron($autoUpdateFrequency)->onOneServer(); + } + } + + private function checkResourcesNew($schedule) + { + if (isCloud()) { + $servers = $this->all_servers->whereNotNull('team.subscription')->where('team.subscription.stripe_trial_already_ended', false)->where('ip', '!=', '1.2.3.4'); + $own = Team::find(0)->servers; + $servers = $servers->merge($own); + } else { + $servers = $this->all_servers->where('ip', '!=', '1.2.3.4'); + } + foreach ($servers as $server) { + $schedule->job(new ServerCheckJob($server))->everyMinute()->onOneServer(); + $schedule->job(new DockerCleanupJob($server))->everyTenMinutes()->onOneServer(); } } @@ -180,4 +214,4 @@ class Kernel extends ConsoleKernel require base_path('routes/console.php'); } -} +} \ No newline at end of file diff --git a/app/Jobs/CheckForUpdatesJob.php b/app/Jobs/CheckForUpdatesJob.php new file mode 100644 index 000000000..e18e37ed6 --- /dev/null +++ b/app/Jobs/CheckForUpdatesJob.php @@ -0,0 +1,44 @@ +get('https://cdn.coollabs.io/coolify/versions.json'); + if ($response->successful()) { + $versions = $response->json(); + $latest_version = $versions['latest']; + $current_version = config('version'); + + if (version_compare($latest_version, $current_version, '>')) { + // New version available + $settings->update(['new_version_available' => true]); + } else { + $settings->update(['new_version_available' => false]); + } + } + } catch (\Throwable $e) { + // Consider implementing a notification to administrators + } + } +} \ No newline at end of file diff --git a/app/Jobs/PullCoolifyImageJob.php b/app/Jobs/PullCoolifyImageJob.php index 253b0b9f0..624dc4414 100644 --- a/app/Jobs/PullCoolifyImageJob.php +++ b/app/Jobs/PullCoolifyImageJob.php @@ -2,6 +2,7 @@ namespace App\Jobs; +use App\Models\InstanceSettings; use App\Models\Server; use Illuminate\Bus\Queueable; use Illuminate\Contracts\Queue\ShouldBeEncrypted; @@ -16,16 +17,13 @@ class PullCoolifyImageJob implements ShouldBeEncrypted, ShouldQueue { use Dispatchable, InteractsWithQueue, Queueable, SerializesModels; - public $timeout = 1000; - - public function __construct() {} - public function handle(): void { try { if (isDev() || isCloud()) { return; } + $settings = InstanceSettings::get(); $server = Server::findOrFail(0); $response = Http::retry(3, 1000)->get('https://cdn.coollabs.io/coolify/versions.json'); if ($response->successful()) { @@ -35,7 +33,6 @@ class PullCoolifyImageJob implements ShouldBeEncrypted, ShouldQueue $latest_version = get_latest_version_of_coolify(); instant_remote_process(["docker pull -q ghcr.io/coollabsio/coolify:{$latest_version}"], $server, false); - $settings = \App\Models\InstanceSettings::get(); $current_version = config('version'); if (! $settings->is_auto_update_enabled) { return; @@ -46,12 +43,8 @@ class PullCoolifyImageJob implements ShouldBeEncrypted, ShouldQueue if (version_compare($latest_version, $current_version, '<')) { return; } - instant_remote_process([ - 'curl -fsSL https://cdn.coollabs.io/coolify/upgrade.sh -o /data/coolify/source/upgrade.sh', - "bash /data/coolify/source/upgrade.sh $latest_version", - ], $server); } catch (\Throwable $e) { throw $e; } } -} +} \ No newline at end of file diff --git a/app/Jobs/UpdateCoolifyJob.php b/app/Jobs/UpdateCoolifyJob.php new file mode 100644 index 000000000..5c8e8e679 --- /dev/null +++ b/app/Jobs/UpdateCoolifyJob.php @@ -0,0 +1,53 @@ +is_auto_update_enabled) { + Log::info('Auto-update is disabled. Skipping update check.'); + return; + } + + if (!$settings->new_version_available) { + Log::info('No new version available. Skipping update.'); + return; + } + + $server = Server::findOrFail(0); + if (!$server) { + Log::error('Server not found. Cannot proceed with update.'); + return; + } + + Log::info('Starting Coolify update process...'); + UpdateCoolify::run(false); // false means it's not a manual update + + $settings->update(['new_version_available' => false]); + Log::info('Coolify update completed successfully.'); + + } catch (\Throwable $e) { + Log::error('UpdateCoolifyJob failed: ' . $e->getMessage()); + // Consider implementing a notification to administrators + } + } +} \ No newline at end of file diff --git a/app/Livewire/Settings/Configuration.php b/app/Livewire/Settings/Configuration.php index 7439e112f..60cd46907 100644 --- a/app/Livewire/Settings/Configuration.php +++ b/app/Livewire/Settings/Configuration.php @@ -5,6 +5,7 @@ namespace App\Livewire\Settings; use App\Models\InstanceSettings as ModelsInstanceSettings; use App\Models\Server; use Livewire\Component; +use Cron\CronExpression; class Configuration extends Component { @@ -20,6 +21,10 @@ class Configuration extends Component public bool $is_api_enabled; + public string $auto_update_frequency; + + public string $update_check_frequency; + protected string $dynamic_config_path = '/data/coolify/proxy/dynamic'; protected Server $server; @@ -32,6 +37,9 @@ class Configuration extends Component 'settings.custom_dns_servers' => 'nullable', 'settings.instance_name' => 'nullable', 'settings.allowed_ips' => 'nullable', + 'settings.is_auto_update_enabled' => 'boolean', + 'auto_update_frequency' => 'nullable|string', + 'update_check_frequency' => 'nullable|string', ]; protected $validationAttributes = [ @@ -41,6 +49,9 @@ class Configuration extends Component 'settings.public_port_max' => 'Public port max', 'settings.custom_dns_servers' => 'Custom DNS servers', 'settings.allowed_ips' => 'Allowed IPs', + 'settings.is_auto_update_enabled' => 'Auto Update Enabled', + 'auto_update_frequency' => 'Auto Update Frequency', + 'update_check_frequency' => 'Update Check Frequency', ]; public function mount() @@ -50,6 +61,8 @@ class Configuration extends Component $this->is_registration_enabled = $this->settings->is_registration_enabled; $this->is_dns_validation_enabled = $this->settings->is_dns_validation_enabled; $this->is_api_enabled = $this->settings->is_api_enabled; + $this->auto_update_frequency = $this->settings->auto_update_frequency; + $this->update_check_frequency = $this->settings->update_check_frequency; } public function instantSave() @@ -59,6 +72,8 @@ class Configuration extends Component $this->settings->is_registration_enabled = $this->is_registration_enabled; $this->settings->is_dns_validation_enabled = $this->is_dns_validation_enabled; $this->settings->is_api_enabled = $this->is_api_enabled; + $this->settings->auto_update_frequency = $this->auto_update_frequency; + $this->settings->update_check_frequency = $this->update_check_frequency; $this->settings->save(); $this->dispatch('success', 'Settings updated!'); } @@ -76,6 +91,16 @@ class Configuration extends Component } $this->validate(); + if ($this->is_auto_update_enabled && !$this->validateCronExpression($this->auto_update_frequency)) { + $this->dispatch('error', 'Invalid Cron / Human expression for Auto Update Frequency.'); + return; + } + + if (!$this->validateCronExpression($this->update_check_frequency)) { + $this->dispatch('error', 'Invalid Cron / Human expression for Update Check Frequency.'); + return; + } + if ($this->settings->is_dns_validation_enabled && $this->settings->fqdn) { if (! validate_dns_entry($this->settings->fqdn, $this->server)) { $this->dispatch('error', "Validating DNS failed.

Make sure you have added the DNS records correctly.

{$this->settings->fqdn}->{$this->server->ip}

Check this documentation for further help."); @@ -99,6 +124,13 @@ class Configuration extends Component $this->settings->allowed_ips = $this->settings->allowed_ips->unique(); $this->settings->allowed_ips = $this->settings->allowed_ips->implode(','); + $this->settings->do_not_track = $this->do_not_track; + $this->settings->is_auto_update_enabled = $this->is_auto_update_enabled; + $this->settings->is_registration_enabled = $this->is_registration_enabled; + $this->settings->is_dns_validation_enabled = $this->is_dns_validation_enabled; + $this->settings->is_api_enabled = $this->is_api_enabled; + $this->settings->auto_update_frequency = $this->auto_update_frequency; + $this->settings->update_check_frequency = $this->update_check_frequency; $this->settings->save(); $this->server->setupDynamicProxyConfiguration(); if (! $error_show) { @@ -108,4 +140,38 @@ class Configuration extends Component return handleError($e, $this); } } -} + + private function validateCronExpression($expression): bool + { + if (empty($expression)) { + return true; + } + $isValid = false; + try { + $cronExpression = new CronExpression($expression); + $isValid = $cronExpression->getNextRunDate() !== false; + } catch (\Exception $e) { + $isValid = false; + } + + if (isset(VALID_CRON_STRINGS[$expression])) { + $isValid = true; + } + + return $isValid; + } + + public function updatedAutoUpdateFrequency() + { + if (!$this->validateCronExpression($this->auto_update_frequency)) { + $this->dispatch('error', 'Invalid Cron / Human expression for Auto Update Frequency.'); + } + } + + public function updatedUpdateCheckFrequency() + { + if (!$this->validateCronExpression($this->update_check_frequency)) { + $this->dispatch('error', 'Invalid Cron / Human expression for Update Check Frequency.'); + } + } +} \ No newline at end of file diff --git a/app/Models/InstanceSettings.php b/app/Models/InstanceSettings.php index bd3c41a1f..5bd421956 100644 --- a/app/Models/InstanceSettings.php +++ b/app/Models/InstanceSettings.php @@ -18,6 +18,9 @@ class InstanceSettings extends Model implements SendsEmail 'resale_license' => 'encrypted', 'smtp_password' => 'encrypted', 'allowed_ip_ranges' => 'array', + 'is_auto_update_enabled' => 'boolean', + 'auto_update_frequency' => 'string', + 'update_check_frequency' => 'string', ]; public function fqdn(): Attribute diff --git a/database/migrations/2024_08_05_142659_add_update_frequency_settings.php b/database/migrations/2024_08_05_142659_add_update_frequency_settings.php new file mode 100644 index 000000000..b24842dcd --- /dev/null +++ b/database/migrations/2024_08_05_142659_add_update_frequency_settings.php @@ -0,0 +1,32 @@ +string('auto_update_frequency')->default('0 0 * * *')->nullable(); + $table->string('update_check_frequency')->default('0 */11 * * *')->nullable(); + $table->boolean('new_version_available')->default(false); + }); + } + + /** + * Reverse the migrations. + */ + public function down(): void + { + Schema::table('instance_settings', function (Blueprint $table) { + $table->dropColumn('update_check_frequency'); + $table->dropColumn('auto_update_frequency'); + $table->dropColumn('new_version_available'); + }); + } +}; diff --git a/resources/views/livewire/settings/configuration.blade.php b/resources/views/livewire/settings/configuration.blade.php index b5fb49d3e..8a6a6e572 100644 --- a/resources/views/livewire/settings/configuration.blade.php +++ b/resources/views/livewire/settings/configuration.blade.php @@ -10,23 +10,25 @@
+

Instance Settings

+

DNS Validation

+
+ +
-
- -
{{--
--}} -
-

API

+ +

API

@@ -42,6 +44,10 @@ id="is_auto_update_enabled" label="Auto Update Coolify" /> @else + @if($is_auto_update_enabled) + + @endif + @endif