Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

v4.0.0-beta.380 #4649

Open
wants to merge 10 commits into
base: main
Choose a base branch
from
3 changes: 3 additions & 0 deletions app/Console/Commands/CloudCleanupSubscriptions.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace App\Console\Commands;

use App\Events\ServerReachabilityChanged;
use App\Models\Team;
use Illuminate\Console\Command;

Expand Down Expand Up @@ -92,6 +93,8 @@ private function disableServers(Team $team)
$server->update([
'ip' => '1.2.3.4',
]);

ServerReachabilityChanged::dispatch($server);
}
}
}
Expand Down
17 changes: 17 additions & 0 deletions app/Events/ServerReachabilityChanged.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php

namespace App\Events;

use App\Models\Server;
use Illuminate\Foundation\Events\Dispatchable;

class ServerReachabilityChanged
{
use Dispatchable;

public function __construct(
public readonly Server $server
) {
$this->server->isReachableChanged();
}
}
7 changes: 1 addition & 6 deletions app/Jobs/ServerCheckJob.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,12 +31,7 @@ public function middleware(): array
return [(new WithoutOverlapping($this->server->uuid))->dontRelease()];
}

public function __construct(public Server $server)
{
if (isDev()) {
$this->handle();
}
}
public function __construct(public Server $server) {}

public function handle()
{
Expand Down
6 changes: 3 additions & 3 deletions app/Livewire/Notifications/Email.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,8 @@ class Email extends Component
#[Validate(['nullable', 'numeric', 'min:1', 'max:65535'])]
public ?int $smtpPort = null;

#[Validate(['nullable', 'string', 'in:tls,ssl,none'])]
public ?string $smtpEncryption = 'tls';
#[Validate(['nullable', 'string', 'in:starttls,tls,none'])]
public ?string $smtpEncryption = null;

#[Validate(['nullable', 'string'])]
public ?string $smtpUsername = null;
Expand Down Expand Up @@ -235,7 +235,7 @@ public function submitSmtp()
'smtpFromName' => 'required|string',
'smtpHost' => 'required|string',
'smtpPort' => 'required|numeric',
'smtpEncryption' => 'required|string|in:tls,ssl,none',
'smtpEncryption' => 'required|string|in:starttls,tls,none',
'smtpUsername' => 'nullable|string',
'smtpPassword' => 'nullable|string',
'smtpTimeout' => 'nullable|numeric',
Expand Down
2 changes: 2 additions & 0 deletions app/Livewire/Server/Show.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use App\Actions\Server\StartSentinel;
use App\Actions\Server\StopSentinel;
use App\Events\ServerReachabilityChanged;
use App\Models\Server;
use Livewire\Attributes\Computed;
use Livewire\Attributes\Validate;
Expand Down Expand Up @@ -202,6 +203,7 @@ public function checkLocalhostConnection()
$this->server->settings->is_reachable = $this->isReachable = true;
$this->server->settings->is_usable = $this->isUsable = true;
$this->server->settings->save();
ServerReachabilityChanged::dispatch($this->server);
$this->dispatch('proxyStatusUpdated');
} else {
$this->dispatch('error', 'Server is not reachable.', 'Please validate your configuration and connection.<br><br>Check this <a target="_blank" class="underline" href="https://coolify.io/docs/knowledge-base/server/openssh">documentation</a> for further help. <br><br>Error: '.$error);
Expand Down
6 changes: 3 additions & 3 deletions app/Livewire/SettingsEmail.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,8 @@ class SettingsEmail extends Component
#[Validate(['nullable', 'numeric', 'min:1', 'max:65535'])]
public ?int $smtpPort = null;

#[Validate(['nullable', 'string', 'in:tls,ssl,none'])]
public ?string $smtpEncryption = 'tls';
#[Validate(['nullable', 'string', 'in:starttls,tls,none'])]
public ?string $smtpEncryption = null;

#[Validate(['nullable', 'string'])]
public ?string $smtpUsername = null;
Expand Down Expand Up @@ -142,7 +142,7 @@ public function submitSmtp()
'smtpFromName' => 'required|string',
'smtpHost' => 'required|string',
'smtpPort' => 'required|numeric',
'smtpEncryption' => 'required|string|in:tls,ssl,none',
'smtpEncryption' => 'required|string|in:starttls,tls,none',
'smtpUsername' => 'nullable|string',
'smtpPassword' => 'nullable|string',
'smtpTimeout' => 'nullable|numeric',
Expand Down
65 changes: 59 additions & 6 deletions app/Models/Server.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
use App\Actions\Server\InstallDocker;
use App\Actions\Server\StartSentinel;
use App\Enums\ProxyTypes;
use App\Events\ServerReachabilityChanged;
use App\Jobs\CheckAndStartSentinelJob;
use App\Notifications\Server\Reachable;
use App\Notifications\Server\Unreachable;
Expand Down Expand Up @@ -346,7 +347,7 @@ public function setupDynamicProxyConfiguration()
'loadBalancer' => [
'servers' => [
0 => [
'url' => 'http://coolify:80',
'url' => 'http://coolify:8080',
],
],
],
Expand Down Expand Up @@ -444,7 +445,7 @@ public function setupDynamicProxyConfiguration()
handle /terminal/ws {
reverse_proxy coolify-realtime:6002
}
reverse_proxy coolify:80
reverse_proxy coolify:8080
}";
$base64 = base64_encode($caddy_file);
instant_remote_process([
Expand Down Expand Up @@ -1024,14 +1025,63 @@ public function isReachableChanged()
$this->refresh();
$unreachableNotificationSent = (bool) $this->unreachable_notification_sent;
$isReachable = (bool) $this->settings->is_reachable;
// If the server is reachable, send the reachable notification if it was sent before

\Log::debug('Server reachability check', [
'server_id' => $this->id,
'is_reachable' => $isReachable,
'notification_sent' => $unreachableNotificationSent,
'unreachable_count' => $this->unreachable_count,
]);

if ($isReachable === true) {
$this->unreachable_count = 0;
$this->save();

if ($unreachableNotificationSent === true) {
\Log::debug('Server is now reachable, sending notification', [
'server_id' => $this->id,
]);
$this->sendReachableNotification();
}
} else {
// If the server is unreachable, send the unreachable notification if it was not sent before
if ($unreachableNotificationSent === false) {

return;
}

$this->increment('unreachable_count');
\Log::debug('Incremented unreachable count', [
'server_id' => $this->id,
'new_count' => $this->unreachable_count,
]);

if ($this->unreachable_count === 1) {
$this->settings->is_reachable = true;
$this->settings->save();
\Log::debug('First unreachable attempt, marking as reachable', [
'server_id' => $this->id,
]);

return;
}

if ($this->unreachable_count >= 2 && ! $unreachableNotificationSent) {
$failedChecks = 0;
for ($i = 0; $i < 3; $i++) {
$status = $this->serverStatus();
\Log::debug('Additional reachability check', [
'server_id' => $this->id,
'attempt' => $i + 1,
'status' => $status,
]);
sleep(5);
if (! $status) {
$failedChecks++;
}
}

if ($failedChecks === 3 && ! $unreachableNotificationSent) {
\Log::debug('Server confirmed unreachable after 3 attempts, sending notification', [
'server_id' => $this->id,
]);
$this->sendUnreachableNotification();
}
}
Expand Down Expand Up @@ -1065,6 +1115,7 @@ public function validateConnection(bool $justCheckingNewKey = false)
if ($this->settings->is_reachable === false) {
$this->settings->is_reachable = true;
$this->settings->save();
ServerReachabilityChanged::dispatch($this);
}

return ['uptime' => true, 'error' => null];
Expand All @@ -1075,6 +1126,7 @@ public function validateConnection(bool $justCheckingNewKey = false)
if ($this->settings->is_reachable === true) {
$this->settings->is_reachable = false;
$this->settings->save();
ServerReachabilityChanged::dispatch($this);
}

return ['uptime' => false, 'error' => $e->getMessage()];
Expand Down Expand Up @@ -1165,6 +1217,7 @@ public function validateDockerEngineVersion()
$this->settings->is_reachable = true;
$this->settings->is_usable = true;
$this->settings->save();
ServerReachabilityChanged::dispatch($this);

return true;
}
Expand Down
3 changes: 0 additions & 3 deletions app/Models/ServerSetting.php
Original file line number Diff line number Diff line change
Expand Up @@ -85,9 +85,6 @@ protected static function booted()
) {
$settings->server->restartSentinel();
}
if ($settings->isDirty('is_reachable')) {
$settings->server->isReachableChanged();
}
});
}

Expand Down
2 changes: 2 additions & 0 deletions app/Models/Team.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace App\Models;

use App\Events\ServerReachabilityChanged;
use App\Notifications\Channels\SendsDiscord;
use App\Notifications\Channels\SendsEmail;
use App\Notifications\Channels\SendsPushover;
Expand Down Expand Up @@ -202,6 +203,7 @@ public function subscriptionEnded()
'is_usable' => false,
'is_reachable' => false,
]);
ServerReachabilityChanged::dispatch($server);
}
}

Expand Down
11 changes: 9 additions & 2 deletions app/Notifications/Channels/EmailChannel.php
Original file line number Diff line number Diff line change
Expand Up @@ -66,17 +66,24 @@ private function bootConfigs($notifiable): void
}

if ($emailSettings->smtp_enabled) {
$encryption = match (strtolower($emailSettings->smtp_encryption)) {
'starttls' => null,
'tls' => 'tls',
'none' => null,
default => null,
};

config()->set('mail.default', 'smtp');
config()->set('mail.mailers.smtp', [
'transport' => 'smtp',
'host' => $emailSettings->smtp_host,
'port' => $emailSettings->smtp_port,
'encryption' => $emailSettings->smtp_encryption === 'none' ? null : $emailSettings->smtp_encryption,
'encryption' => $encryption,
'username' => $emailSettings->smtp_username,
'password' => $emailSettings->smtp_password,
'timeout' => $emailSettings->smtp_timeout,
'local_domain' => null,
'auto_tls' => $emailSettings->smtp_encryption === 'none' ? '0' : '',
'auto_tls' => $emailSettings->smtp_encryption === 'none' ? '0' : '', // If encryption is "none", it will not try to upgrade to TLS via StartTLS to make sure it is unencrypted.
]);
}
}
Expand Down
11 changes: 10 additions & 1 deletion bootstrap/helpers/notifications.php
Original file line number Diff line number Diff line change
Expand Up @@ -67,17 +67,26 @@ function set_transanctional_email_settings(?InstanceSettings $settings = null):

return 'resend';
}

$encryption = match (strtolower(data_get($settings, 'smtp_encryption'))) {
'starttls' => null,
'tls' => 'tls',
'none' => null,
default => null,
};

if (data_get($settings, 'smtp_enabled')) {
config()->set('mail.default', 'smtp');
config()->set('mail.mailers.smtp', [
'transport' => 'smtp',
'host' => data_get($settings, 'smtp_host'),
'port' => data_get($settings, 'smtp_port'),
'encryption' => data_get($settings, 'smtp_encryption'),
'encryption' => $encryption,
'username' => data_get($settings, 'smtp_username'),
'password' => data_get($settings, 'smtp_password'),
'timeout' => data_get($settings, 'smtp_timeout'),
'local_domain' => null,
'auto_tls' => data_get($settings, 'smtp_encryption') === 'none' ? '0' : '', // If encryption is "none", it will not try to upgrade to TLS via StartTLS to make sure it is unencrypted.
]);

return 'smtp';
Expand Down
2 changes: 1 addition & 1 deletion config/constants.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

return [
'coolify' => [
'version' => '4.0.0-beta.379',
'version' => '4.0.0-beta.380',
'self_hosted' => env('SELF_HOSTED', true),
'autoupdate' => env('AUTOUPDATE'),
'base_config_path' => env('BASE_CONFIG_PATH', '/data/coolify'),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,15 @@
*/
public function up(): void
{
if (DB::table('instance_settings')->exists()) {
Schema::table('instance_settings', function (Blueprint $table) {
$table->text('smtp_from_address')->nullable()->change();
$table->text('smtp_from_name')->nullable()->change();
$table->text('smtp_recipients')->nullable()->change();
$table->text('smtp_host')->nullable()->change();
$table->text('smtp_username')->nullable()->change();
});
Schema::table('instance_settings', function (Blueprint $table) {
$table->text('smtp_from_address')->nullable()->change();
$table->text('smtp_from_name')->nullable()->change();
$table->text('smtp_recipients')->nullable()->change();
$table->text('smtp_host')->nullable()->change();
$table->text('smtp_username')->nullable()->change();
});

if (DB::table('instance_settings')->exists()) {
$settings = DB::table('instance_settings')->get();
foreach ($settings as $setting) {
try {
Expand All @@ -45,11 +45,11 @@ public function up(): void
public function down(): void
{
Schema::table('instance_settings', function (Blueprint $table) {
$table->text('smtp_from_address')->nullable()->change();
$table->text('smtp_from_name')->nullable()->change();
$table->text('smtp_recipients')->nullable()->change();
$table->text('smtp_host')->nullable()->change();
$table->text('smtp_username')->nullable()->change();
$table->string('smtp_from_address')->nullable()->change();
$table->string('smtp_from_name')->nullable()->change();
$table->string('smtp_recipients')->nullable()->change();
$table->string('smtp_host')->nullable()->change();
$table->string('smtp_username')->nullable()->change();
});

if (DB::table('instance_settings')->exists()) {
Expand Down
Loading
Loading