fix: proxy switch

This commit is contained in:
Andras Bacsai 2024-03-12 12:30:40 +01:00
parent bcc61b0d8b
commit 1835a91467
6 changed files with 133 additions and 134 deletions

View File

@ -114,7 +114,6 @@ class RunRemoteProcess
} }
if ($this->call_event_on_finish) { if ($this->call_event_on_finish) {
try { try {
ray($this->call_event_data);
if ($this->call_event_data) { if ($this->call_event_data) {
event(resolve("App\\Events\\$this->call_event_on_finish", [ event(resolve("App\\Events\\$this->call_event_on_finish", [
"data" => $this->call_event_data, "data" => $this->call_event_data,

View File

@ -2,7 +2,6 @@
namespace App\Events; namespace App\Events;
use App\Models\Server;
use Illuminate\Broadcasting\InteractsWithSockets; use Illuminate\Broadcasting\InteractsWithSockets;
use Illuminate\Foundation\Events\Dispatchable; use Illuminate\Foundation\Events\Dispatchable;
use Illuminate\Queue\SerializesModels; use Illuminate\Queue\SerializesModels;
@ -10,7 +9,8 @@ use Illuminate\Queue\SerializesModels;
class ProxyStarted class ProxyStarted
{ {
use Dispatchable, InteractsWithSockets, SerializesModels; use Dispatchable, InteractsWithSockets, SerializesModels;
public function __construct(public Server $server) public function __construct(public $data)
{ {
} }
} }

View File

@ -14,7 +14,7 @@ class ProxyStartedNotification
public function handle(ProxyStarted $event): void public function handle(ProxyStarted $event): void
{ {
$this->server = data_get($event, 'server'); $this->server = data_get($event, 'data');
$this->server->setupDefault404Redirect(); $this->server->setupDefault404Redirect();
$this->server->setupDynamicProxyConfiguration(); $this->server->setupDynamicProxyConfiguration();
} }

View File

@ -11,7 +11,15 @@ class DynamicConfigurations extends Component
public ?Server $server = null; public ?Server $server = null;
public $parameters = []; public $parameters = [];
public Collection $contents; public Collection $contents;
protected $listeners = ['loadDynamicConfigurations', 'refresh' => '$refresh']; public function getListeners()
{
$teamId = auth()->user()->currentTeam()->id;
return [
"echo-private:team.{$teamId},ProxyStatusChanged" => 'loadDynamicConfigurations',
'loadDynamicConfigurations',
'refresh' => '$refresh'
];
}
protected $rules = [ protected $rules = [
'contents.*' => 'nullable|string', 'contents.*' => 'nullable|string',
]; ];
@ -24,6 +32,7 @@ class DynamicConfigurations extends Component
$files = $files->sort(); $files = $files->sort();
if ($files->contains('coolify.yaml')) { if ($files->contains('coolify.yaml')) {
$files = $files->filter(fn ($file) => $file !== 'coolify.yaml')->prepend('coolify.yaml'); $files = $files->filter(fn ($file) => $file !== 'coolify.yaml')->prepend('coolify.yaml');
$files = $files->filter(fn ($file) => $file !== 'Caddyfile')->prepend('Caddyfile');
} }
$contents = collect([]); $contents = collect([]);
foreach ($files as $file) { foreach ($files as $file) {
@ -31,6 +40,7 @@ class DynamicConfigurations extends Component
$contents[$without_extension] = instant_remote_process(["cat {$proxy_path}/dynamic/{$file}"], $this->server); $contents[$without_extension] = instant_remote_process(["cat {$proxy_path}/dynamic/{$file}"], $this->server);
} }
$this->contents = $contents; $this->contents = $contents;
$this->dispatch('refresh');
} }
public function mount() public function mount()
{ {

View File

@ -75,12 +75,7 @@ class Configuration extends Component
$this->settings->save(); $this->settings->save();
$this->server = Server::findOrFail(0); $this->server = Server::findOrFail(0);
$this->setup_instance_fqdn(); $this->server->setupDynamicProxyConfiguration();
$this->dispatch('success', 'Instance settings updated successfully!'); $this->dispatch('success', 'Instance settings updated successfully!');
} }
private function setup_instance_fqdn()
{
$this->server->setupDynamicProxyConfiguration();
}
} }

View File

@ -3,7 +3,6 @@
namespace App\Models; namespace App\Models;
use App\Actions\Server\InstallDocker; use App\Actions\Server\InstallDocker;
use App\Enums\ProxyStatus;
use App\Enums\ProxyTypes; use App\Enums\ProxyTypes;
use App\Notifications\Server\Revived; use App\Notifications\Server\Revived;
use App\Notifications\Server\Unreachable; use App\Notifications\Server\Unreachable;
@ -125,7 +124,6 @@ class Server extends BaseModel
$dynamic_conf_path = $this->proxyPath() . "/dynamic"; $dynamic_conf_path = $this->proxyPath() . "/dynamic";
$proxy_type = $this->proxyType(); $proxy_type = $this->proxyType();
$redirect_url = $this->proxy->redirect_url; $redirect_url = $this->proxy->redirect_url;
if ($proxy_type === 'TRAEFIK_V2') { if ($proxy_type === 'TRAEFIK_V2') {
$default_redirect_file = "$dynamic_conf_path/default_redirect_404.yaml"; $default_redirect_file = "$dynamic_conf_path/default_redirect_404.yaml";
} else if ($proxy_type === 'CADDY') { } else if ($proxy_type === 'CADDY') {
@ -199,7 +197,6 @@ class Server extends BaseModel
$conf = ":80, :443 { $conf = ":80, :443 {
redir $redirect_url redir $redirect_url
}"; }";
ray($conf);
$conf = $conf =
"# This file is automatically generated by Coolify.\n" . "# This file is automatically generated by Coolify.\n" .
"# Do not edit it manually (only if you know what are you doing).\n\n" . "# Do not edit it manually (only if you know what are you doing).\n\n" .
@ -224,150 +221,148 @@ ray($conf);
{ {
$settings = InstanceSettings::get(); $settings = InstanceSettings::get();
$dynamic_config_path = $this->proxyPath() . "/dynamic"; $dynamic_config_path = $this->proxyPath() . "/dynamic";
if ($this) { if ($this->proxyType() === 'TRAEFIK_V2') {
if ($this->proxyType() === 'TRAEFIK_V2') { $file = "$dynamic_config_path/coolify.yaml";
$file = "$dynamic_config_path/coolify.yaml"; if (empty($settings->fqdn)) {
if (empty($settings->fqdn)) { instant_remote_process([
instant_remote_process([ "rm -f $file",
"rm -f $file", ], $this);
], $this); } else {
} else { $url = Url::fromString($settings->fqdn);
$url = Url::fromString($settings->fqdn); $host = $url->getHost();
$host = $url->getHost(); $schema = $url->getScheme();
$schema = $url->getScheme(); $traefik_dynamic_conf = [
$traefik_dynamic_conf = [ 'http' =>
'http' => [
'middlewares' => [
'redirect-to-https' => [
'redirectscheme' => [
'scheme' => 'https',
],
],
'gzip' => [
'compress' => true,
],
],
'routers' =>
[ [
'middlewares' => [ 'coolify-http' =>
'redirect-to-https' => [
'redirectscheme' => [
'scheme' => 'https',
],
],
'gzip' => [
'compress' => true,
],
],
'routers' =>
[ [
'coolify-http' => 'middlewares' => [
[ 0 => 'gzip',
'middlewares' => [
0 => 'gzip',
],
'entryPoints' => [
0 => 'http',
],
'service' => 'coolify',
'rule' => "Host(`{$host}`)",
], ],
'coolify-realtime-ws' => 'entryPoints' => [
[ 0 => 'http',
'entryPoints' => [
0 => 'http',
],
'service' => 'coolify-realtime',
'rule' => "Host(`{$host}`) && PathPrefix(`/app`)",
], ],
'service' => 'coolify',
'rule' => "Host(`{$host}`)",
], ],
'services' => 'coolify-realtime-ws' =>
[ [
'coolify' => 'entryPoints' => [
0 => 'http',
],
'service' => 'coolify-realtime',
'rule' => "Host(`{$host}`) && PathPrefix(`/app`)",
],
],
'services' =>
[
'coolify' =>
[
'loadBalancer' =>
[ [
'loadBalancer' => 'servers' =>
[ [
'servers' => 0 =>
[ [
0 => 'url' => 'http://coolify:80',
[
'url' => 'http://coolify:80',
],
], ],
], ],
], ],
'coolify-realtime' => ],
'coolify-realtime' =>
[
'loadBalancer' =>
[ [
'loadBalancer' => 'servers' =>
[ [
'servers' => 0 =>
[ [
0 => 'url' => 'http://coolify-realtime:6001',
[
'url' => 'http://coolify-realtime:6001',
],
], ],
], ],
], ],
], ],
], ],
],
];
if ($schema === 'https') {
$traefik_dynamic_conf['http']['routers']['coolify-http']['middlewares'] = [
0 => 'redirect-to-https',
]; ];
if ($schema === 'https') { $traefik_dynamic_conf['http']['routers']['coolify-https'] = [
$traefik_dynamic_conf['http']['routers']['coolify-http']['middlewares'] = [ 'entryPoints' => [
0 => 'redirect-to-https', 0 => 'https',
]; ],
'service' => 'coolify',
$traefik_dynamic_conf['http']['routers']['coolify-https'] = [ 'rule' => "Host(`{$host}`)",
'entryPoints' => [ 'tls' => [
0 => 'https', 'certresolver' => 'letsencrypt',
], ],
'service' => 'coolify', ];
'rule' => "Host(`{$host}`)", $traefik_dynamic_conf['http']['routers']['coolify-realtime-wss'] = [
'tls' => [ 'entryPoints' => [
'certresolver' => 'letsencrypt', 0 => 'https',
], ],
]; 'service' => 'coolify-realtime',
$traefik_dynamic_conf['http']['routers']['coolify-realtime-wss'] = [ 'rule' => "Host(`{$host}`) && PathPrefix(`/app`)",
'entryPoints' => [ 'tls' => [
0 => 'https', 'certresolver' => 'letsencrypt',
], ],
'service' => 'coolify-realtime', ];
'rule' => "Host(`{$host}`) && PathPrefix(`/app`)",
'tls' => [
'certresolver' => 'letsencrypt',
],
];
}
$yaml = Yaml::dump($traefik_dynamic_conf, 12, 2);
$yaml =
"# This file is automatically generated by Coolify.\n" .
"# Do not edit it manually (only if you know what are you doing).\n\n" .
$yaml;
$base64 = base64_encode($yaml);
instant_remote_process([
"mkdir -p $dynamic_config_path",
"echo '$base64' | base64 -d > $file",
], $this);
if (config('app.env') == 'local') {
// ray($yaml);
}
} }
} else if ($this->proxyType() === 'CADDY') { $yaml = Yaml::dump($traefik_dynamic_conf, 12, 2);
$file = "$dynamic_config_path/coolify.caddy"; $yaml =
if (empty($settings->fqdn)) { "# This file is automatically generated by Coolify.\n" .
instant_remote_process([ "# Do not edit it manually (only if you know what are you doing).\n\n" .
"rm -f $file", $yaml;
], $this);
$this->reloadCaddy(); $base64 = base64_encode($yaml);
} else { instant_remote_process([
$url = Url::fromString($settings->fqdn); "mkdir -p $dynamic_config_path",
$host = $url->getHost(); "echo '$base64' | base64 -d > $file",
$schema = $url->getScheme(); ], $this);
$caddy_file = "
if (config('app.env') == 'local') {
// ray($yaml);
}
}
} else if ($this->proxyType() === 'CADDY') {
$file = "$dynamic_config_path/coolify.caddy";
if (empty($settings->fqdn)) {
instant_remote_process([
"rm -f $file",
], $this);
$this->reloadCaddy();
} else {
$url = Url::fromString($settings->fqdn);
$host = $url->getHost();
$schema = $url->getScheme();
$caddy_file = "
$schema://$host { $schema://$host {
handle /app/* { handle /app/* {
reverse_proxy coolify-realtime:6001 reverse_proxy coolify-realtime:6001
} }
reverse_proxy coolify:80 reverse_proxy coolify:80
}"; }";
$base64 = base64_encode($caddy_file); $base64 = base64_encode($caddy_file);
instant_remote_process([ instant_remote_process([
"echo '$base64' | base64 -d > $file", "echo '$base64' | base64 -d > $file",
], $this); ], $this);
$this->reloadCaddy(); $this->reloadCaddy();
}
} }
} }
} }
@ -396,16 +391,16 @@ $schema://$host {
} }
public function proxyType() public function proxyType()
{ {
$proxyType = $this->proxy->get('type'); // $proxyType = $this->proxy->get('type');
if ($proxyType === ProxyTypes::NONE->value) { // if ($proxyType === ProxyTypes::NONE->value) {
return $proxyType; // return $proxyType;
} // }
// if (is_null($proxyType)) { // if (is_null($proxyType)) {
// $this->proxy->type = ProxyTypes::TRAEFIK_V2->value; // $this->proxy->type = ProxyTypes::TRAEFIK_V2->value;
// $this->proxy->status = ProxyStatus::EXITED->value; // $this->proxy->status = ProxyStatus::EXITED->value;
// $this->save(); // $this->save();
// } // }
return $this->proxy->get('type'); return data_get($this->proxy, 'type.type');
} }
public function scopeWithProxy(): Builder public function scopeWithProxy(): Builder
{ {