feat caddy dynamic configurations
This commit is contained in:
parent
f24063cfea
commit
9bdad6bb67
@ -37,8 +37,10 @@ class StartProxy
|
|||||||
"echo 'Proxy started successfully.'"
|
"echo 'Proxy started successfully.'"
|
||||||
]);
|
]);
|
||||||
} else {
|
} else {
|
||||||
|
$caddfile = "import /dynamic/*.caddy";
|
||||||
$commands = $commands->merge([
|
$commands = $commands->merge([
|
||||||
"mkdir -p $proxy_path/dynamic && cd $proxy_path",
|
"mkdir -p $proxy_path/dynamic && cd $proxy_path",
|
||||||
|
"echo '$caddfile' > $proxy_path/dynamic/Caddyfile",
|
||||||
"echo 'Creating required Docker Compose file.'",
|
"echo 'Creating required Docker Compose file.'",
|
||||||
"echo 'Pulling docker image.'",
|
"echo 'Pulling docker image.'",
|
||||||
'docker compose pull',
|
'docker compose pull',
|
||||||
|
@ -35,7 +35,8 @@ class Init extends Command
|
|||||||
$this->call('cleanup:queue');
|
$this->call('cleanup:queue');
|
||||||
$this->call('cleanup:stucked-resources');
|
$this->call('cleanup:stucked-resources');
|
||||||
try {
|
try {
|
||||||
setup_dynamic_configuration();
|
$server = Server::find(0)->first();
|
||||||
|
$server->setupDynamicProxyConfiguration();
|
||||||
} catch (\Throwable $e) {
|
} catch (\Throwable $e) {
|
||||||
echo "Could not setup dynamic configuration: {$e->getMessage()}\n";
|
echo "Could not setup dynamic configuration: {$e->getMessage()}\n";
|
||||||
}
|
}
|
||||||
|
@ -2,12 +2,9 @@
|
|||||||
|
|
||||||
namespace App\Livewire\Settings;
|
namespace App\Livewire\Settings;
|
||||||
|
|
||||||
use App\Jobs\ContainerStatusJob;
|
|
||||||
use App\Models\InstanceSettings as ModelsInstanceSettings;
|
use App\Models\InstanceSettings as ModelsInstanceSettings;
|
||||||
use App\Models\Server;
|
use App\Models\Server;
|
||||||
use Livewire\Component;
|
use Livewire\Component;
|
||||||
use Spatie\Url\Url;
|
|
||||||
use Symfony\Component\Yaml\Yaml;
|
|
||||||
|
|
||||||
class Configuration extends Component
|
class Configuration extends Component
|
||||||
{
|
{
|
||||||
@ -84,7 +81,6 @@ class Configuration extends Component
|
|||||||
|
|
||||||
private function setup_instance_fqdn()
|
private function setup_instance_fqdn()
|
||||||
{
|
{
|
||||||
setup_dynamic_configuration();
|
$this->server->setupDynamicProxyConfiguration();
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -15,6 +15,8 @@ use Spatie\SchemalessAttributes\Casts\SchemalessAttributes;
|
|||||||
use Spatie\SchemalessAttributes\SchemalessAttributesTrait;
|
use Spatie\SchemalessAttributes\SchemalessAttributesTrait;
|
||||||
use Illuminate\Support\Str;
|
use Illuminate\Support\Str;
|
||||||
use Illuminate\Support\Stringable;
|
use Illuminate\Support\Stringable;
|
||||||
|
use Spatie\Url\Url;
|
||||||
|
use Symfony\Component\Yaml\Yaml;
|
||||||
|
|
||||||
class Server extends BaseModel
|
class Server extends BaseModel
|
||||||
{
|
{
|
||||||
@ -118,7 +120,156 @@ class Server extends BaseModel
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
public function proxyPath() {
|
public function setupDynamicProxyConfiguration()
|
||||||
|
{
|
||||||
|
$settings = InstanceSettings::get();
|
||||||
|
$dynamic_config_path = $this->proxyPath() . "/dynamic";
|
||||||
|
if ($this) {
|
||||||
|
if ($this->proxyType() === 'TRAEFIK_V2') {
|
||||||
|
$file = "$dynamic_config_path/coolify.yaml";
|
||||||
|
if (empty($settings->fqdn)) {
|
||||||
|
instant_remote_process([
|
||||||
|
"rm -f $file",
|
||||||
|
], $this);
|
||||||
|
} else {
|
||||||
|
$url = Url::fromString($settings->fqdn);
|
||||||
|
$host = $url->getHost();
|
||||||
|
$schema = $url->getScheme();
|
||||||
|
$traefik_dynamic_conf = [
|
||||||
|
'http' =>
|
||||||
|
[
|
||||||
|
'middlewares' => [
|
||||||
|
'redirect-to-https' => [
|
||||||
|
'redirectscheme' => [
|
||||||
|
'scheme' => 'https',
|
||||||
|
],
|
||||||
|
],
|
||||||
|
'gzip' => [
|
||||||
|
'compress' => true,
|
||||||
|
],
|
||||||
|
],
|
||||||
|
'routers' =>
|
||||||
|
[
|
||||||
|
'coolify-http' =>
|
||||||
|
[
|
||||||
|
'middlewares' => [
|
||||||
|
0 => 'gzip',
|
||||||
|
],
|
||||||
|
'entryPoints' => [
|
||||||
|
0 => 'http',
|
||||||
|
],
|
||||||
|
'service' => 'coolify',
|
||||||
|
'rule' => "Host(`{$host}`)",
|
||||||
|
],
|
||||||
|
'coolify-realtime-ws' =>
|
||||||
|
[
|
||||||
|
'entryPoints' => [
|
||||||
|
0 => 'http',
|
||||||
|
],
|
||||||
|
'service' => 'coolify-realtime',
|
||||||
|
'rule' => "Host(`{$host}`) && PathPrefix(`/app`)",
|
||||||
|
],
|
||||||
|
],
|
||||||
|
'services' =>
|
||||||
|
[
|
||||||
|
'coolify' =>
|
||||||
|
[
|
||||||
|
'loadBalancer' =>
|
||||||
|
[
|
||||||
|
'servers' =>
|
||||||
|
[
|
||||||
|
0 =>
|
||||||
|
[
|
||||||
|
'url' => 'http://coolify:80',
|
||||||
|
],
|
||||||
|
],
|
||||||
|
],
|
||||||
|
],
|
||||||
|
'coolify-realtime' =>
|
||||||
|
[
|
||||||
|
'loadBalancer' =>
|
||||||
|
[
|
||||||
|
'servers' =>
|
||||||
|
[
|
||||||
|
0 =>
|
||||||
|
[
|
||||||
|
'url' => 'http://coolify-realtime:6001',
|
||||||
|
],
|
||||||
|
],
|
||||||
|
],
|
||||||
|
],
|
||||||
|
],
|
||||||
|
],
|
||||||
|
];
|
||||||
|
|
||||||
|
if ($schema === 'https') {
|
||||||
|
$traefik_dynamic_conf['http']['routers']['coolify-http']['middlewares'] = [
|
||||||
|
0 => 'redirect-to-https',
|
||||||
|
];
|
||||||
|
|
||||||
|
$traefik_dynamic_conf['http']['routers']['coolify-https'] = [
|
||||||
|
'entryPoints' => [
|
||||||
|
0 => 'https',
|
||||||
|
],
|
||||||
|
'service' => 'coolify',
|
||||||
|
'rule' => "Host(`{$host}`)",
|
||||||
|
'tls' => [
|
||||||
|
'certresolver' => 'letsencrypt',
|
||||||
|
],
|
||||||
|
];
|
||||||
|
$traefik_dynamic_conf['http']['routers']['coolify-realtime-wss'] = [
|
||||||
|
'entryPoints' => [
|
||||||
|
0 => 'https',
|
||||||
|
],
|
||||||
|
'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') {
|
||||||
|
$file = "$dynamic_config_path/coolify.caddy";
|
||||||
|
if (empty($settings->fqdn)) {
|
||||||
|
instant_remote_process([
|
||||||
|
"rm -f $file",
|
||||||
|
"docker exec coolify-proxy caddy reload --config /dynamic/Caddyfile",
|
||||||
|
], $this);
|
||||||
|
} else {
|
||||||
|
$url = Url::fromString($settings->fqdn);
|
||||||
|
$host = $url->getHost();
|
||||||
|
$schema = $url->getScheme();
|
||||||
|
$caddy_file = "
|
||||||
|
$schema://$host {
|
||||||
|
reverse_proxy coolify:80
|
||||||
|
}";
|
||||||
|
$base64 = base64_encode($caddy_file);
|
||||||
|
instant_remote_process([
|
||||||
|
"echo '$base64' | base64 -d > $file",
|
||||||
|
"docker exec coolify-proxy caddy reload --config /dynamic/Caddyfile",
|
||||||
|
], $this);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
public function proxyPath()
|
||||||
|
{
|
||||||
$base_path = config('coolify.base_config_path');
|
$base_path = config('coolify.base_config_path');
|
||||||
$proxyType = $this->proxyType();
|
$proxyType = $this->proxyType();
|
||||||
$proxy_path = "$base_path/proxy";
|
$proxy_path = "$base_path/proxy";
|
||||||
|
@ -188,6 +188,7 @@ function generate_default_proxy_configuration(Server $server)
|
|||||||
],
|
],
|
||||||
"environment" => [
|
"environment" => [
|
||||||
"CADDY_DOCKER_POLLING_INTERVAL=5s",
|
"CADDY_DOCKER_POLLING_INTERVAL=5s",
|
||||||
|
"CADDY_DOCKER_CADDYFILE_PATH=/dynamic/Caddyfile",
|
||||||
],
|
],
|
||||||
"networks" => $networks->toArray(),
|
"networks" => $networks->toArray(),
|
||||||
"ports" => [
|
"ports" => [
|
||||||
@ -202,6 +203,7 @@ function generate_default_proxy_configuration(Server $server)
|
|||||||
// ],
|
// ],
|
||||||
"volumes" => [
|
"volumes" => [
|
||||||
"/var/run/docker.sock:/var/run/docker.sock:ro",
|
"/var/run/docker.sock:/var/run/docker.sock:ro",
|
||||||
|
"{$proxy_path}/dynamic:/dynamic",
|
||||||
"{$proxy_path}/config:/config",
|
"{$proxy_path}/config:/config",
|
||||||
"{$proxy_path}/data:/data",
|
"{$proxy_path}/data:/data",
|
||||||
],
|
],
|
||||||
@ -216,132 +218,7 @@ function generate_default_proxy_configuration(Server $server)
|
|||||||
SaveConfiguration::run($server, $config);
|
SaveConfiguration::run($server, $config);
|
||||||
return $config;
|
return $config;
|
||||||
}
|
}
|
||||||
function setup_dynamic_configuration()
|
|
||||||
{
|
|
||||||
$settings = InstanceSettings::get();
|
|
||||||
$server = Server::find(0);
|
|
||||||
$dynamic_config_path = $server->proxyPath() . "/dynamic";
|
|
||||||
if ($server) {
|
|
||||||
$file = "$dynamic_config_path/coolify.yaml";
|
|
||||||
if (empty($settings->fqdn)) {
|
|
||||||
instant_remote_process([
|
|
||||||
"rm -f $file",
|
|
||||||
], $server);
|
|
||||||
} else {
|
|
||||||
$url = Url::fromString($settings->fqdn);
|
|
||||||
$host = $url->getHost();
|
|
||||||
$schema = $url->getScheme();
|
|
||||||
$traefik_dynamic_conf = [
|
|
||||||
'http' =>
|
|
||||||
[
|
|
||||||
'middlewares' => [
|
|
||||||
'redirect-to-https' => [
|
|
||||||
'redirectscheme' => [
|
|
||||||
'scheme' => 'https',
|
|
||||||
],
|
|
||||||
],
|
|
||||||
'gzip' => [
|
|
||||||
'compress' => true,
|
|
||||||
],
|
|
||||||
],
|
|
||||||
'routers' =>
|
|
||||||
[
|
|
||||||
'coolify-http' =>
|
|
||||||
[
|
|
||||||
'middlewares' => [
|
|
||||||
0 => 'gzip',
|
|
||||||
],
|
|
||||||
'entryPoints' => [
|
|
||||||
0 => 'http',
|
|
||||||
],
|
|
||||||
'service' => 'coolify',
|
|
||||||
'rule' => "Host(`{$host}`)",
|
|
||||||
],
|
|
||||||
'coolify-realtime-ws' =>
|
|
||||||
[
|
|
||||||
'entryPoints' => [
|
|
||||||
0 => 'http',
|
|
||||||
],
|
|
||||||
'service' => 'coolify-realtime',
|
|
||||||
'rule' => "Host(`{$host}`) && PathPrefix(`/app`)",
|
|
||||||
],
|
|
||||||
],
|
|
||||||
'services' =>
|
|
||||||
[
|
|
||||||
'coolify' =>
|
|
||||||
[
|
|
||||||
'loadBalancer' =>
|
|
||||||
[
|
|
||||||
'servers' =>
|
|
||||||
[
|
|
||||||
0 =>
|
|
||||||
[
|
|
||||||
'url' => 'http://coolify:80',
|
|
||||||
],
|
|
||||||
],
|
|
||||||
],
|
|
||||||
],
|
|
||||||
'coolify-realtime' =>
|
|
||||||
[
|
|
||||||
'loadBalancer' =>
|
|
||||||
[
|
|
||||||
'servers' =>
|
|
||||||
[
|
|
||||||
0 =>
|
|
||||||
[
|
|
||||||
'url' => 'http://coolify-realtime:6001',
|
|
||||||
],
|
|
||||||
],
|
|
||||||
],
|
|
||||||
],
|
|
||||||
],
|
|
||||||
],
|
|
||||||
];
|
|
||||||
|
|
||||||
if ($schema === 'https') {
|
|
||||||
$traefik_dynamic_conf['http']['routers']['coolify-http']['middlewares'] = [
|
|
||||||
0 => 'redirect-to-https',
|
|
||||||
];
|
|
||||||
|
|
||||||
$traefik_dynamic_conf['http']['routers']['coolify-https'] = [
|
|
||||||
'entryPoints' => [
|
|
||||||
0 => 'https',
|
|
||||||
],
|
|
||||||
'service' => 'coolify',
|
|
||||||
'rule' => "Host(`{$host}`)",
|
|
||||||
'tls' => [
|
|
||||||
'certresolver' => 'letsencrypt',
|
|
||||||
],
|
|
||||||
];
|
|
||||||
$traefik_dynamic_conf['http']['routers']['coolify-realtime-wss'] = [
|
|
||||||
'entryPoints' => [
|
|
||||||
0 => 'https',
|
|
||||||
],
|
|
||||||
'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",
|
|
||||||
], $server);
|
|
||||||
|
|
||||||
if (config('app.env') == 'local') {
|
|
||||||
// ray($yaml);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
function setup_default_redirect_404(string|null $redirect_url, Server $server)
|
function setup_default_redirect_404(string|null $redirect_url, Server $server)
|
||||||
{
|
{
|
||||||
$traefik_dynamic_conf_path = $server->proxyPath() . "/dynamic";
|
$traefik_dynamic_conf_path = $server->proxyPath() . "/dynamic";
|
||||||
|
Loading…
x
Reference in New Issue
Block a user