coolify/bootstrap/helpers/proxy.php

263 lines
9.9 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\Enums\ProxyTypes;
use App\Models\Application;
2023-05-24 14:26:50 +02:00
use App\Models\Server;
use Symfony\Component\Yaml\Yaml;
function collectProxyDockerNetworksByServer(Server $server)
{
if (! $server->isFunctional()) {
return collect();
}
$proxyType = $server->proxyType();
if (is_null($proxyType) || $proxyType === 'NONE') {
return collect();
}
$networks = instant_remote_process(['docker inspect --format="{{json .NetworkSettings.Networks }}" coolify-proxy'], $server, false);
$networks = collect($networks)->map(function ($network) {
return collect(json_decode($network))->keys();
})->flatten()->unique();
return $networks;
}
function collectDockerNetworksByServer(Server $server)
2023-09-25 09:17:42 +02:00
{
$allNetworks = collect([]);
2023-12-15 15:48:01 +01:00
if ($server->isSwarm()) {
$networks = collect($server->swarmDockers)->map(function ($docker) {
return $docker['network'];
});
} else {
// Standalone networks
$networks = collect($server->standaloneDockers)->map(function ($docker) {
return $docker['network'];
});
}
$allNetworks = $allNetworks->merge($networks);
// Service networks
foreach ($server->services()->get() as $service) {
if ($service->isRunning()) {
$networks->push($service->networks());
}
$allNetworks->push($service->networks());
}
// Docker compose based apps
$docker_compose_apps = $server->dockerComposeBasedApplications();
foreach ($docker_compose_apps as $app) {
if ($app->isRunning()) {
$networks->push($app->uuid);
}
$allNetworks->push($app->uuid);
}
// Docker compose based preview deployments
$docker_compose_previews = $server->dockerComposeBasedPreviewDeployments();
foreach ($docker_compose_previews as $preview) {
if (! $preview->isRunning()) {
continue;
}
$pullRequestId = $preview->pull_request_id;
$applicationId = $preview->application_id;
$application = Application::find($applicationId);
2024-06-10 22:43:34 +02:00
if (! $application) {
continue;
}
$network = "{$application->uuid}-{$pullRequestId}";
$networks->push($network);
$allNetworks->push($network);
}
$networks = collect($networks)->flatten()->unique();
$allNetworks = $allNetworks->flatten()->unique();
2023-12-15 15:48:01 +01:00
if ($server->isSwarm()) {
if ($networks->count() === 0) {
$networks = collect(['coolify-overlay']);
$allNetworks = collect(['coolify-overlay']);
2023-12-15 15:48:01 +01:00
}
} else {
if ($networks->count() === 0) {
$networks = collect(['coolify']);
$allNetworks = collect(['coolify']);
}
}
return [
'networks' => $networks,
'allNetworks' => $allNetworks,
];
}
function connectProxyToNetworks(Server $server)
{
['networks' => $networks] = collectDockerNetworksByServer($server);
if ($server->isSwarm()) {
2023-12-15 15:48:01 +01:00
$commands = $networks->map(function ($network) {
return [
"echo 'Connecting coolify-proxy to $network network...'",
"docker network ls --format '{{.Name}}' | grep '^$network$' >/dev/null || docker network create --driver overlay --attachable $network >/dev/null",
"docker network connect $network coolify-proxy >/dev/null 2>&1 || true",
];
});
} else {
$commands = $networks->map(function ($network) {
return [
"echo 'Connecting coolify-proxy to $network network...'",
"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",
];
});
}
2023-12-15 15:48:01 +01:00
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
{
2024-03-11 15:08:05 +01:00
$proxy_path = $server->proxyPath();
$proxy_type = $server->proxyType();
2023-11-29 10:06:52 +01:00
if ($server->isSwarm()) {
$networks = collect($server->swarmDockers)->map(function ($docker) {
return $docker['network'];
})->unique();
2023-12-18 14:01:25 +01:00
if ($networks->count() === 0) {
$networks = collect(['coolify-overlay']);
}
2023-11-29 10:06:52 +01:00
} else {
$networks = collect($server->standaloneDockers)->map(function ($docker) {
return $docker['network'];
})->unique();
2023-12-18 14:01:25 +01:00
if ($networks->count() === 0) {
$networks = collect(['coolify']);
}
2023-11-29 10:06:52 +01:00
}
2023-12-18 14:01:25 +01:00
2023-06-22 14:18:17 +02:00
$array_of_networks = collect([]);
$networks->map(function ($network) use ($array_of_networks) {
$array_of_networks[$network] = [
2024-06-10 22:43:34 +02:00
'external' => true,
2023-06-22 14:18:17 +02:00
];
});
if ($proxy_type === ProxyTypes::TRAEFIK->value) {
2024-03-11 15:08:05 +01:00
$labels = [
2024-06-10 22:43:34 +02:00
'traefik.enable=true',
'traefik.http.routers.traefik.entrypoints=http',
'traefik.http.routers.traefik.service=api@internal',
'traefik.http.services.traefik.loadbalancer.server.port=8080',
'coolify.managed=true',
2024-03-11 15:08:05 +01:00
];
$config = [
2024-06-10 22:43:34 +02:00
'networks' => $array_of_networks->toArray(),
'services' => [
'traefik' => [
'container_name' => 'coolify-proxy',
2024-08-23 14:21:12 +02:00
'image' => 'traefik:v3.1',
2024-06-10 22:43:34 +02:00
'restart' => RESTART_MODE,
'extra_hosts' => [
'host.docker.internal:host-gateway',
2024-03-11 15:08:05 +01:00
],
2024-06-10 22:43:34 +02:00
'networks' => $networks->toArray(),
'ports' => [
'80:80',
'443:443',
'8080:8080',
2024-03-11 15:08:05 +01:00
],
2024-06-10 22:43:34 +02:00
'healthcheck' => [
'test' => 'wget -qO- http://localhost:80/ping || exit 1',
'interval' => '4s',
'timeout' => '2s',
'retries' => 5,
2024-03-11 15:08:05 +01:00
],
2024-06-10 22:43:34 +02:00
'volumes' => [
'/var/run/docker.sock:/var/run/docker.sock:ro',
2024-03-11 15:08:05 +01:00
"{$proxy_path}:/traefik",
],
2024-06-10 22:43:34 +02:00
'command' => [
'--ping=true',
'--ping.entrypoint=http',
'--api.dashboard=true',
'--api.insecure=false',
'--entrypoints.http.address=:80',
'--entrypoints.https.address=:443',
'--entrypoints.http.http.encodequerysemicolons=true',
'--entryPoints.http.http2.maxConcurrentStreams=50',
'--entrypoints.https.http.encodequerysemicolons=true',
'--entryPoints.https.http2.maxConcurrentStreams=50',
'--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',
2024-03-11 15:08:05 +01:00
],
2024-06-10 22:43:34 +02:00
'labels' => $labels,
2023-06-22 14:18:17 +02:00
],
],
2024-03-11 15:08:05 +01:00
];
if (isDev()) {
// $config['services']['traefik']['command'][] = "--log.level=debug";
2024-06-10 22:43:34 +02:00
$config['services']['traefik']['command'][] = '--accesslog.filepath=/traefik/access.log';
$config['services']['traefik']['command'][] = '--accesslog.bufferingsize=100';
2024-03-11 15:08:05 +01:00
}
if ($server->isSwarm()) {
data_forget($config, 'services.traefik.container_name');
data_forget($config, 'services.traefik.restart');
data_forget($config, 'services.traefik.labels');
2023-11-29 10:06:52 +01:00
2024-06-10 22:43:34 +02:00
$config['services']['traefik']['command'][] = '--providers.docker.swarmMode=true';
2024-03-11 15:08:05 +01:00
$config['services']['traefik']['deploy'] = [
2024-06-10 22:43:34 +02:00
'labels' => $labels,
'placement' => [
'constraints' => [
'node.role==manager',
2024-03-11 15:08:05 +01:00
],
],
];
} else {
2024-06-10 22:43:34 +02:00
$config['services']['traefik']['command'][] = '--providers.docker=true';
2024-03-11 15:08:05 +01:00
}
2024-06-10 22:43:34 +02:00
} elseif ($proxy_type === 'CADDY') {
2024-03-11 15:08:05 +01:00
$config = [
2024-06-10 22:43:34 +02:00
'version' => '3.8',
'networks' => $array_of_networks->toArray(),
'services' => [
'caddy' => [
'container_name' => 'coolify-proxy',
'image' => 'lucaslorentz/caddy-docker-proxy:2.8-alpine',
'restart' => RESTART_MODE,
'extra_hosts' => [
'host.docker.internal:host-gateway',
2024-03-11 15:08:05 +01:00
],
2024-06-10 22:43:34 +02:00
'environment' => [
'CADDY_DOCKER_POLLING_INTERVAL=5s',
'CADDY_DOCKER_CADDYFILE_PATH=/dynamic/Caddyfile',
],
2024-06-10 22:43:34 +02:00
'networks' => $networks->toArray(),
'ports' => [
'80:80',
'443:443',
2024-03-11 15:08:05 +01:00
],
// "healthcheck" => [
// "test" => "wget -qO- http://localhost:80|| exit 1",
// "interval" => "4s",
// "timeout" => "2s",
// "retries" => 5,
// ],
2024-06-10 22:43:34 +02:00
'volumes' => [
'/var/run/docker.sock:/var/run/docker.sock:ro',
2024-03-11 17:17:34 +01:00
"{$proxy_path}/dynamic:/dynamic",
2024-03-11 15:08:05 +01:00
"{$proxy_path}/config:/config",
"{$proxy_path}/data:/data",
],
2023-11-28 18:42:09 +01:00
],
],
];
} else {
2024-03-11 15:08:05 +01:00
return null;
2023-11-28 18:42:09 +01:00
}
2024-03-11 15:08:05 +01:00
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);
2024-06-10 22:43:34 +02:00
2023-09-25 09:17:42 +02:00
return $config;
2023-06-22 14:18:17 +02:00
}