coolify/bootstrap/helpers/proxy.php

347 lines
12 KiB
PHP
Raw Normal View History

2023-05-24 14:26:50 +02:00
<?php
2023-09-25 09:17:42 +02:00
use App\Actions\Proxy\SaveConfiguration;
use App\Models\Application;
2023-12-11 18:06:29 +01:00
use App\Models\InstanceSettings;
2023-05-24 14:26:50 +02:00
use App\Models\Server;
2023-12-11 18:06:29 +01:00
use Spatie\Url\Url;
2023-05-24 14:26:50 +02:00
use Symfony\Component\Yaml\Yaml;
function get_proxy_path()
{
2023-07-28 16:42:28 +02:00
$base_path = config('coolify.base_config_path');
$proxy_path = "$base_path/proxy";
return $proxy_path;
}
2023-09-25 09:17:42 +02:00
function connectProxyToNetworks(Server $server)
{
// Standalone networks
$networks = collect($server->standaloneDockers)->map(function ($docker) {
return $docker['network'];
});
// Service networks
foreach ($server->services()->get() as $service) {
$networks->push($service->networks());
}
// Docker compose based apps
$docker_compose_apps = $server->dockerComposeBasedApplications();
foreach ($docker_compose_apps as $app) {
$networks->push($app->uuid);
}
// Docker compose based preview deployments
$docker_compose_previews = $server->dockerComposeBasedPreviewDeployments();
foreach ($docker_compose_previews as $preview) {
$pullRequestId = $preview->pull_request_id;
$applicationId = $preview->application_id;
$application = Application::find($applicationId);
if (!$application) {
continue;
}
$network = "{$application->uuid}-{$pullRequestId}";
$networks->push($network);
}
$networks = collect($networks)->flatten()->unique();
if ($networks->count() === 0) {
$networks = collect(['coolify']);
}
$commands = $networks->map(function ($network) {
return [
"echo 'Connecting coolify-proxy to $network network...'",
2023-09-25 09:17:42 +02:00
"docker network ls --format '{{.Name}}' | grep '^$network$' >/dev/null || docker network create --attachable $network >/dev/null",
"docker network connect $network coolify-proxy >/dev/null 2>&1 || true",
];
});
return $commands->flatten();
}
2023-07-28 16:42:28 +02:00
function generate_default_proxy_configuration(Server $server)
2023-06-22 14:18:17 +02:00
{
2023-07-28 16:42:28 +02:00
$proxy_path = get_proxy_path();
2023-11-29 10:06:52 +01:00
if ($server->isSwarm()) {
$networks = collect($server->swarmDockers)->map(function ($docker) {
return $docker['network'];
})->unique();
} else {
$networks = collect($server->standaloneDockers)->map(function ($docker) {
return $docker['network'];
})->unique();
}
2023-06-22 14:18:17 +02:00
if ($networks->count() === 0) {
$networks = collect(['coolify']);
}
$array_of_networks = collect([]);
$networks->map(function ($network) use ($array_of_networks) {
$array_of_networks[$network] = [
"external" => true,
];
});
2023-11-29 10:06:52 +01:00
$labels = [
"traefik.enable=true",
"traefik.http.routers.traefik.entrypoints=http",
"traefik.http.routers.traefik.middlewares=traefik-basic-auth@file",
"traefik.http.routers.traefik.service=api@internal",
"traefik.http.services.traefik.loadbalancer.server.port=8080",
// Global Middlewares
"traefik.http.middlewares.redirect-to-https.redirectscheme.scheme=https",
"traefik.http.middlewares.gzip.compress=true",
];
2023-06-22 14:18:17 +02:00
$config = [
"version" => "3.8",
"networks" => $array_of_networks->toArray(),
"services" => [
"traefik" => [
"container_name" => "coolify-proxy",
"image" => "traefik:v2.10",
2023-08-21 18:00:12 +02:00
"restart" => RESTART_MODE,
2023-06-22 14:18:17 +02:00
"extra_hosts" => [
"host.docker.internal:host-gateway",
],
"networks" => $networks->toArray(),
"ports" => [
"80:80",
"443:443",
"8080:8080",
],
"healthcheck" => [
"test" => "wget -qO- http://localhost:80/ping || exit 1",
"interval" => "4s",
"timeout" => "2s",
"retries" => 5,
],
"volumes" => [
"/var/run/docker.sock:/var/run/docker.sock:ro",
"{$proxy_path}:/traefik",
],
"command" => [
"--ping=true",
"--ping.entrypoint=http",
"--api.dashboard=true",
2023-07-28 16:42:28 +02:00
"--api.insecure=false",
2023-06-22 14:18:17 +02:00
"--entrypoints.http.address=:80",
"--entrypoints.https.address=:443",
2023-07-07 12:57:22 +02:00
"--entrypoints.http.http.encodequerysemicolons=true",
"--entrypoints.https.http.encodequerysemicolons=true",
2023-06-22 14:18:17 +02:00
"--providers.docker.exposedbydefault=false",
"--providers.file.directory=/traefik/dynamic/",
"--providers.file.watch=true",
"--certificatesresolvers.letsencrypt.acme.httpchallenge=true",
"--certificatesresolvers.letsencrypt.acme.storage=/traefik/acme.json",
"--certificatesresolvers.letsencrypt.acme.httpchallenge.entrypoint=http",
],
2023-11-29 10:06:52 +01:00
"labels" => $labels,
2023-06-22 14:18:17 +02:00
],
],
];
if (isDev()) {
2023-06-22 14:18:17 +02:00
$config['services']['traefik']['command'][] = "--log.level=debug";
$config['services']['traefik']['command'][] = "--accesslog.filepath=/traefik/access.log";
$config['services']['traefik']['command'][] = "--accesslog.bufferingsize=100";
2023-06-22 14:18:17 +02:00
}
2023-11-28 18:42:09 +01:00
if ($server->isSwarm()) {
2023-11-29 10:06:52 +01:00
data_forget($config, 'services.traefik.container_name');
data_forget($config, 'services.traefik.restart');
data_forget($config, 'services.traefik.labels');
2023-11-28 18:42:09 +01:00
$config['services']['traefik']['command'][] = "--providers.docker.swarmMode=true";
$config['services']['traefik']['deploy'] = [
2023-11-29 10:06:52 +01:00
"labels" => $labels,
2023-11-28 18:42:09 +01:00
"placement" => [
"constraints" => [
"node.role==manager",
],
],
];
} else {
$config['services']['traefik']['command'][] = "--providers.docker=true";
}
2023-11-29 10:06:52 +01:00
$config = Yaml::dump($config, 12, 2);
2023-09-25 09:17:42 +02:00
SaveConfiguration::run($server, $config);
return $config;
2023-06-22 14:18:17 +02:00
}
2023-12-11 18:06:29 +01:00
function setup_dynamic_configuration()
{
$dynamic_config_path = get_proxy_path() . "/dynamic";
$settings = InstanceSettings::get();
$server = Server::findOrFail(0);
$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' =>
[
'routers' =>
[
'coolify-http' =>
[
'entryPoints' => [
0 => 'http',
],
'service' => 'coolify',
'rule' => "Host(`{$host}`)",
],
2023-12-11 18:48:00 +01:00
'coolify-realtime-ws' =>
2023-12-11 18:06:29 +01:00
[
'entryPoints' => [
0 => 'http',
],
'service' => 'coolify-realtime',
2023-12-11 18:48:00 +01:00
'rule' => "Host(`{$host}`) && PathPrefix(`/app`)",
2023-12-11 18:06:29 +01:00
],
],
'services' =>
[
'coolify' =>
[
'loadBalancer' =>
[
'servers' =>
[
0 =>
[
'url' => 'http://coolify:80',
],
],
],
],
'coolify-realtime' =>
[
'loadBalancer' =>
[
'servers' =>
[
0 =>
[
'url' => 'http://coolify-realtime:6001',
],
],
],
],
],
],
];
2023-12-11 18:06:29 +01:00
if ($schema === 'https') {
$traefik_dynamic_conf['http']['routers']['coolify-http']['middlewares'] = [
0 => 'redirect-to-https@docker',
];
2023-12-11 19:30:37 +01:00
2023-12-11 18:06:29 +01:00
$traefik_dynamic_conf['http']['routers']['coolify-https'] = [
'entryPoints' => [
0 => 'https',
],
'service' => 'coolify',
'rule' => "Host(`{$host}`)",
'tls' => [
'certresolver' => 'letsencrypt',
],
];
2023-12-11 18:48:00 +01:00
$traefik_dynamic_conf['http']['routers']['coolify-realtime-wss'] = [
2023-12-11 18:06:29 +01:00
'entryPoints' => [
0 => 'https',
],
'service' => 'coolify-realtime',
2023-12-11 18:48:00 +01:00
'rule' => "Host(`{$host}`) && PathPrefix(`/app`)",
2023-12-11 18:06:29 +01:00
'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);
}
}
}
2023-06-23 10:33:25 +02:00
function setup_default_redirect_404(string|null $redirect_url, Server $server)
2023-06-22 14:18:17 +02:00
{
2023-07-28 16:42:28 +02:00
$traefik_dynamic_conf_path = get_proxy_path() . "/dynamic";
2023-06-22 14:18:17 +02:00
$traefik_default_redirect_file = "$traefik_dynamic_conf_path/default_redirect_404.yaml";
if (empty($redirect_url)) {
2023-09-18 12:18:45 +02:00
instant_remote_process([
2023-09-23 11:53:30 +02:00
"mkdir -p $traefik_dynamic_conf_path",
2023-06-22 14:18:17 +02:00
"rm -f $traefik_default_redirect_file",
], $server);
} else {
$traefik_dynamic_conf = [
'http' =>
2023-08-11 20:48:52 +02:00
[
'routers' =>
2023-06-22 14:18:17 +02:00
[
2023-08-11 20:48:52 +02:00
'catchall' =>
[
'entryPoints' => [
0 => 'http',
1 => 'https',
2023-06-22 14:18:17 +02:00
],
2023-08-11 20:48:52 +02:00
'service' => 'noop',
'rule' => "HostRegexp(`{catchall:.*}`)",
'priority' => 1,
'middlewares' => [
0 => 'redirect-regexp@file',
],
],
],
'services' =>
[
'noop' =>
[
'loadBalancer' =>
2023-06-22 14:18:17 +02:00
[
2023-08-11 20:48:52 +02:00
'servers' =>
[
0 =>
2023-06-22 14:18:17 +02:00
[
2023-08-11 20:48:52 +02:00
'url' => '',
2023-06-22 14:18:17 +02:00
],
2023-08-11 20:48:52 +02:00
],
2023-06-22 14:18:17 +02:00
],
2023-08-11 20:48:52 +02:00
],
],
'middlewares' =>
[
'redirect-regexp' =>
[
'redirectRegex' =>
2023-06-22 14:18:17 +02:00
[
2023-08-11 20:48:52 +02:00
'regex' => '(.*)',
'replacement' => $redirect_url,
'permanent' => false,
2023-06-22 14:18:17 +02:00
],
2023-08-11 20:48:52 +02:00
],
2023-05-24 14:26:50 +02:00
],
2023-08-11 20:48:52 +02:00
],
2023-05-24 14:26:50 +02:00
];
2023-06-22 14:18:17 +02:00
$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);
2023-09-18 12:18:45 +02:00
instant_remote_process([
2023-06-22 14:18:17 +02:00
"mkdir -p $traefik_dynamic_conf_path",
"echo '$base64' | base64 -d > $traefik_default_redirect_file",
], $server);
if (config('app.env') == 'local') {
ray($yaml);
2023-05-24 14:26:50 +02:00
}
}
}