lasthourcloud/bootstrap/helpers/proxy.php

242 lines
8.2 KiB
PHP
Raw Normal View History

2023-05-24 12:26:50 +00:00
<?php
use App\Models\Server;
2023-09-07 11:23:34 +00:00
use App\Models\StandalonePostgresql;
2023-05-24 12:26:50 +00:00
use Symfony\Component\Yaml\Yaml;
function get_proxy_path()
{
2023-07-28 14:42:28 +00:00
$base_path = config('coolify.base_config_path');
$proxy_path = "$base_path/proxy";
return $proxy_path;
}
2023-07-28 14:42:28 +00:00
function generate_default_proxy_configuration(Server $server)
2023-06-22 12:18:17 +00:00
{
2023-07-28 14:42:28 +00:00
$proxy_path = get_proxy_path();
2023-06-22 12:18:17 +00:00
$networks = collect($server->standaloneDockers)->map(function ($docker) {
return $docker['network'];
})->unique();
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,
];
});
$config = [
"version" => "3.8",
"networks" => $array_of_networks->toArray(),
"services" => [
"traefik" => [
"container_name" => "coolify-proxy",
"image" => "traefik:v2.10",
2023-08-21 16:00:12 +00:00
"restart" => RESTART_MODE,
2023-06-22 12:18:17 +00: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 14:42:28 +00:00
"--api.insecure=false",
2023-06-22 12:18:17 +00:00
"--entrypoints.http.address=:80",
"--entrypoints.https.address=:443",
2023-07-07 10:57:22 +00:00
"--entrypoints.http.http.encodequerysemicolons=true",
"--entrypoints.https.http.encodequerysemicolons=true",
2023-06-22 12:18:17 +00:00
"--providers.docker=true",
"--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",
],
"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",
],
],
],
];
if (isDev()) {
2023-06-22 12:18:17 +00:00
$config['services']['traefik']['command'][] = "--log.level=debug";
}
return Yaml::dump($config, 4, 2);
}
2023-06-23 08:33:25 +00:00
function setup_default_redirect_404(string|null $redirect_url, Server $server)
2023-06-22 12:18:17 +00:00
{
2023-07-28 14:42:28 +00:00
ray('called');
$traefik_dynamic_conf_path = get_proxy_path() . "/dynamic";
2023-06-22 12:18:17 +00:00
$traefik_default_redirect_file = "$traefik_dynamic_conf_path/default_redirect_404.yaml";
2023-07-28 14:42:28 +00:00
ray($redirect_url);
2023-06-22 12:18:17 +00:00
if (empty($redirect_url)) {
remote_process([
"rm -f $traefik_default_redirect_file",
], $server);
} else {
$traefik_dynamic_conf = [
'http' =>
2023-08-11 18:48:52 +00:00
[
'routers' =>
2023-06-22 12:18:17 +00:00
[
2023-08-11 18:48:52 +00:00
'catchall' =>
[
'entryPoints' => [
0 => 'http',
1 => 'https',
2023-06-22 12:18:17 +00:00
],
2023-08-11 18:48:52 +00:00
'service' => 'noop',
'rule' => "HostRegexp(`{catchall:.*}`)",
'priority' => 1,
'middlewares' => [
0 => 'redirect-regexp@file',
],
],
],
'services' =>
[
'noop' =>
[
'loadBalancer' =>
2023-06-22 12:18:17 +00:00
[
2023-08-11 18:48:52 +00:00
'servers' =>
[
0 =>
2023-06-22 12:18:17 +00:00
[
2023-08-11 18:48:52 +00:00
'url' => '',
2023-06-22 12:18:17 +00:00
],
2023-08-11 18:48:52 +00:00
],
2023-06-22 12:18:17 +00:00
],
2023-08-11 18:48:52 +00:00
],
],
'middlewares' =>
[
'redirect-regexp' =>
[
'redirectRegex' =>
2023-06-22 12:18:17 +00:00
[
2023-08-11 18:48:52 +00:00
'regex' => '(.*)',
'replacement' => $redirect_url,
'permanent' => false,
2023-06-22 12:18:17 +00:00
],
2023-08-11 18:48:52 +00:00
],
2023-05-24 12:26:50 +00:00
],
2023-08-11 18:48:52 +00:00
],
2023-05-24 12:26:50 +00:00
];
2023-06-22 12:18:17 +00: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);
ray("mkdir -p $traefik_dynamic_conf_path");
remote_process([
"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 12:26:50 +00:00
}
}
}
2023-09-07 11:23:34 +00:00
function startPostgresProxy(StandalonePostgresql $database)
{
$containerName = "{$database->uuid}-proxy";
$configuration_dir = database_proxy_dir($database->uuid);
$nginxconf = <<<EOF
user nginx;
worker_processes auto;
error_log /var/log/nginx/error.log;
events {
worker_connections 1024;
}
stream {
server {
listen $database->public_port;
proxy_pass $database->uuid:5432;
}
}
EOF;
$docker_compose = [
'version' => '3.8',
'services' => [
$containerName => [
'image' => "nginx:stable-alpine",
'container_name' => $containerName,
'restart' => RESTART_MODE,
'volumes' => [
"$configuration_dir/nginx.conf:/etc/nginx/nginx.conf:ro",
],
'ports' => [
"$database->public_port:$database->public_port",
],
'networks' => [
$database->destination->network,
],
'healthcheck' => [
'test' => [
'CMD-SHELL',
'stat /etc/nginx/nginx.conf || exit 1',
],
'interval' => '5s',
'timeout' => '5s',
'retries' => 3,
'start_period' => '1s'
],
]
],
'networks' => [
$database->destination->network => [
'external' => true,
'name' => $database->destination->network,
'attachable' => true,
]
]
];
$dockercompose_base64 = base64_encode(Yaml::dump($docker_compose, 4, 2));
$nginxconf_base64 = base64_encode($nginxconf);
instant_remote_process([
"mkdir -p $configuration_dir",
"echo '{$nginxconf_base64}' | base64 -d > $configuration_dir/nginx.conf",
"echo '{$dockercompose_base64}' | base64 -d > $configuration_dir/docker-compose.yaml",
"docker compose --project-directory {$configuration_dir} up -d >/dev/null",
], $database->destination->server);
}
function stopPostgresProxy(StandalonePostgresql $database)
{
instant_remote_process(["docker rm -f {$database->uuid}-proxy"], $database->destination->server);
}