coolify/bootstrap/helpers/docker.php

819 lines
33 KiB
PHP
Raw Normal View History

2023-05-24 14:26:50 +02:00
<?php
2024-07-17 21:06:56 +02:00
use App\Enums\ProxyTypes;
2023-08-21 18:00:12 +02:00
use App\Models\Application;
2023-09-19 15:51:13 +02:00
use App\Models\ApplicationPreview;
2023-05-24 14:26:50 +02:00
use App\Models\Server;
2023-11-24 15:48:23 +01:00
use App\Models\ServiceApplication;
2023-05-24 14:26:50 +02:00
use Illuminate\Support\Collection;
2023-08-17 13:14:46 +02:00
use Illuminate\Support\Str;
2023-09-19 15:51:13 +02:00
use Spatie\Url\Url;
use Visus\Cuid2\Cuid2;
2023-05-24 14:26:50 +02:00
2024-02-27 09:01:19 +01:00
function getCurrentApplicationContainerStatus(Server $server, int $id, ?int $pullRequestId = null, ?bool $includePullrequests = false): Collection
2023-09-19 15:51:13 +02:00
{
$containers = collect([]);
2024-08-23 14:21:12 +02:00
if (! $server->isSwarm()) {
2023-12-20 14:11:50 +01:00
$containers = instant_remote_process(["docker ps -a --filter='label=coolify.applicationId={$id}' --format '{{json .}}' "], $server);
$containers = format_docker_command_output_to_json($containers);
2024-02-27 09:01:19 +01:00
$containers = $containers->map(function ($container) use ($pullRequestId, $includePullrequests) {
2023-12-20 14:11:50 +01:00
$labels = data_get($container, 'Labels');
2024-08-23 14:21:12 +02:00
if (! str($labels)->contains('coolify.pullRequestId=')) {
data_set($container, 'Labels', $labels.",coolify.pullRequestId={$pullRequestId}");
2024-06-10 22:43:34 +02:00
2023-12-20 14:11:50 +01:00
return $container;
}
2024-02-27 09:01:19 +01:00
if ($includePullrequests) {
return $container;
}
2023-12-20 14:11:50 +01:00
if (str($labels)->contains("coolify.pullRequestId=$pullRequestId")) {
return $container;
}
2024-06-10 22:43:34 +02:00
2023-12-20 14:11:50 +01:00
return null;
});
$containers = $containers->filter();
2024-06-10 22:43:34 +02:00
2023-12-20 14:11:50 +01:00
return $containers;
}
2024-06-10 22:43:34 +02:00
return $containers;
2023-08-21 18:00:12 +02:00
}
2023-05-24 14:26:50 +02:00
function format_docker_command_output_to_json($rawOutput): Collection
{
$outputLines = explode(PHP_EOL, $rawOutput);
2023-08-21 18:00:12 +02:00
if (count($outputLines) === 1) {
$outputLines = collect($outputLines[0]);
} else {
$outputLines = collect($outputLines);
}
2024-06-10 22:43:34 +02:00
try {
return $outputLines
2024-08-23 14:21:12 +02:00
->reject(fn ($line) => empty($line))
->map(fn ($outputLine) => json_decode($outputLine, true, flags: JSON_THROW_ON_ERROR));
} catch (\Throwable $e) {
return collect([]);
}
2023-05-24 14:26:50 +02:00
}
2024-08-23 14:21:12 +02:00
function format_docker_labels_to_json(string|array $rawOutput): Collection
2023-05-24 14:26:50 +02:00
{
2023-09-12 15:47:30 +02:00
if (is_array($rawOutput)) {
return collect($rawOutput);
}
2023-05-24 14:26:50 +02:00
$outputLines = explode(PHP_EOL, $rawOutput);
return collect($outputLines)
2024-08-23 14:21:12 +02:00
->reject(fn ($line) => empty($line))
2023-05-24 14:26:50 +02:00
->map(function ($outputLine) {
$outputArray = explode(',', $outputLine);
2024-06-10 22:43:34 +02:00
2023-05-24 14:26:50 +02:00
return collect($outputArray)
->map(function ($outputLine) {
return explode('=', $outputLine);
})
->mapWithKeys(function ($outputLine) {
return [$outputLine[0] => $outputLine[1]];
});
})[0];
}
2023-08-11 16:13:53 +02:00
function format_docker_envs_to_json($rawOutput)
{
try {
$outputLines = json_decode($rawOutput, true, flags: JSON_THROW_ON_ERROR);
2024-06-10 22:43:34 +02:00
2023-08-11 16:13:53 +02:00
return collect(data_get($outputLines[0], 'Config.Env', []))->mapWithKeys(function ($env) {
$env = explode('=', $env);
2024-06-10 22:43:34 +02:00
2023-08-11 16:13:53 +02:00
return [$env[0] => $env[1]];
});
2023-09-11 17:36:30 +02:00
} catch (\Throwable $e) {
2023-08-11 16:13:53 +02:00
return collect([]);
}
}
2023-09-19 15:51:13 +02:00
function checkMinimumDockerEngineVersion($dockerVersion)
{
$majorDockerVersion = str($dockerVersion)->before('.')->value();
if ($majorDockerVersion <= 22) {
$dockerVersion = null;
}
2024-06-10 22:43:34 +02:00
return $dockerVersion;
}
function executeInDocker(string $containerId, string $command)
{
return "docker exec {$containerId} bash -c '{$command}'";
// return "docker exec {$this->deployment_uuid} bash -c '{$command} |& tee -a /proc/1/fd/1; [ \$PIPESTATUS -eq 0 ] || exit \$PIPESTATUS'";
}
2023-08-11 16:13:53 +02:00
2023-08-21 18:00:12 +02:00
function getContainerStatus(Server $server, string $container_id, bool $all_data = false, bool $throwError = false)
2023-05-24 14:26:50 +02:00
{
2023-11-29 14:59:06 +01:00
if ($server->isSwarm()) {
$container = instant_remote_process(["docker service ls --filter 'name={$container_id}' --format '{{json .}}' "], $server, $throwError);
} else {
$container = instant_remote_process(["docker inspect --format '{{json .}}' {$container_id}"], $server, $throwError);
}
2024-08-23 14:21:12 +02:00
if (! $container) {
2023-05-24 14:26:50 +02:00
return 'exited';
}
$container = format_docker_command_output_to_json($container);
2023-06-02 15:15:12 +02:00
if ($all_data) {
2023-08-24 21:42:47 +02:00
return $container[0];
2023-06-02 15:15:12 +02:00
}
2023-11-29 14:59:06 +01:00
if ($server->isSwarm()) {
$replicas = data_get($container[0], 'Replicas');
$replicas = explode('/', $replicas);
2024-06-10 22:43:34 +02:00
$active = (int) $replicas[0];
$total = (int) $replicas[1];
2023-11-29 14:59:06 +01:00
if ($active === $total) {
return 'running';
} else {
return 'starting';
}
} else {
return data_get($container[0], 'State.Status', 'exited');
}
2023-05-24 14:26:50 +02:00
}
2023-05-30 15:52:17 +02:00
function generateApplicationContainerName(Application $application, $pull_request_id = 0)
2023-05-30 15:52:17 +02:00
{
2024-08-23 14:21:12 +02:00
// TODO: refactor generateApplicationContainerName, we do not need $application and $pull_request_id
$consistent_container_name = $application->settings->is_consistent_container_name_enabled;
2023-09-01 16:07:46 +02:00
$now = now()->format('Hisu');
if ($pull_request_id !== 0 && $pull_request_id !== null) {
2024-08-23 14:21:12 +02:00
return $application->uuid.'-pr-'.$pull_request_id;
2023-05-30 15:52:17 +02:00
} else {
if ($consistent_container_name) {
return $application->uuid;
}
2024-06-10 22:43:34 +02:00
2024-08-23 14:21:12 +02:00
return $application->uuid.'-'.$now;
2023-05-30 15:52:17 +02:00
}
}
2024-06-10 22:43:34 +02:00
function get_port_from_dockerfile($dockerfile): ?int
2023-08-11 22:41:47 +02:00
{
2023-08-17 13:14:46 +02:00
$dockerfile_array = explode("\n", $dockerfile);
$found_exposed_port = null;
foreach ($dockerfile_array as $line) {
$line_str = str($line)->trim();
2023-08-17 13:14:46 +02:00
if ($line_str->startsWith('EXPOSE')) {
$found_exposed_port = $line_str->replace('EXPOSE', '')->trim();
break;
}
}
if ($found_exposed_port) {
2024-06-10 22:43:34 +02:00
return (int) $found_exposed_port->value();
2023-08-11 22:41:47 +02:00
}
2024-06-10 22:43:34 +02:00
return null;
2023-08-11 22:41:47 +02:00
}
2023-09-19 15:51:13 +02:00
2023-09-25 15:48:43 +02:00
function defaultLabels($id, $name, $pull_request_id = 0, string $type = 'application', $subType = null, $subId = null)
2023-09-20 15:42:41 +02:00
{
$labels = collect([]);
$labels->push('coolify.managed=true');
2024-08-23 14:21:12 +02:00
$labels->push('coolify.version='.config('version'));
$labels->push('coolify.'.$type.'Id='.$id);
2023-09-21 17:48:31 +02:00
$labels->push("coolify.type=$type");
2024-08-23 14:21:12 +02:00
$labels->push('coolify.name='.$name);
$labels->push('coolify.pullRequestId='.$pull_request_id);
2023-09-25 15:48:43 +02:00
if ($type === 'service') {
2024-08-23 14:21:12 +02:00
$subId && $labels->push('coolify.service.subId='.$subId);
$subType && $labels->push('coolify.service.subType='.$subType);
2023-09-25 15:48:43 +02:00
}
2024-06-10 22:43:34 +02:00
2023-09-20 15:42:41 +02:00
return $labels;
}
2024-08-23 14:21:12 +02:00
function generateServiceSpecificFqdns(ServiceApplication|Application $resource)
{
2023-11-24 15:48:23 +01:00
if ($resource->getMorphClass() === 'App\Models\ServiceApplication') {
2024-05-24 17:29:38 +02:00
$uuid = data_get($resource, 'uuid');
$server = data_get($resource, 'service.server');
$environment_variables = data_get($resource, 'service.environment_variables');
2023-11-24 15:48:23 +01:00
$type = $resource->serviceType();
2024-06-10 22:43:34 +02:00
} elseif ($resource->getMorphClass() === 'App\Models\Application') {
2024-05-24 17:29:38 +02:00
$uuid = data_get($resource, 'uuid');
$server = data_get($resource, 'destination.server');
$environment_variables = data_get($resource, 'environment_variables');
2023-11-24 15:48:23 +01:00
$type = $resource->serviceType();
}
if (is_null($server) || is_null($type)) {
return collect([]);
}
2023-11-24 15:48:23 +01:00
$variables = collect($environment_variables);
$payload = collect([]);
switch ($type) {
2023-11-24 15:48:23 +01:00
case $type?->contains('minio'):
$MINIO_BROWSER_REDIRECT_URL = $variables->where('key', 'MINIO_BROWSER_REDIRECT_URL')->first();
$MINIO_SERVER_URL = $variables->where('key', 'MINIO_SERVER_URL')->first();
if (is_null($MINIO_BROWSER_REDIRECT_URL) || is_null($MINIO_SERVER_URL)) {
return $payload;
}
if (is_null($MINIO_BROWSER_REDIRECT_URL?->value)) {
$MINIO_BROWSER_REDIRECT_URL?->update([
2024-08-23 14:21:12 +02:00
'value' => generateFqdn($server, 'console-'.$uuid),
]);
}
if (is_null($MINIO_SERVER_URL?->value)) {
$MINIO_SERVER_URL?->update([
2024-08-23 14:21:12 +02:00
'value' => generateFqdn($server, 'minio-'.$uuid),
]);
}
$payload = collect([
2024-08-23 14:21:12 +02:00
$MINIO_BROWSER_REDIRECT_URL->value.':9001',
$MINIO_SERVER_URL->value.':9000',
]);
break;
case $type?->contains('logto'):
$LOGTO_ENDPOINT = $variables->where('key', 'LOGTO_ENDPOINT')->first();
$LOGTO_ADMIN_ENDPOINT = $variables->where('key', 'LOGTO_ADMIN_ENDPOINT')->first();
if (is_null($LOGTO_ENDPOINT) || is_null($LOGTO_ADMIN_ENDPOINT)) {
return $payload;
}
if (is_null($LOGTO_ENDPOINT?->value)) {
$LOGTO_ENDPOINT?->update([
2024-08-23 14:21:12 +02:00
'value' => generateFqdn($server, 'logto-'.$uuid),
]);
}
if (is_null($LOGTO_ADMIN_ENDPOINT?->value)) {
$LOGTO_ADMIN_ENDPOINT?->update([
2024-08-23 14:21:12 +02:00
'value' => generateFqdn($server, 'logto-admin-'.$uuid),
]);
}
$payload = collect([
2024-08-23 14:21:12 +02:00
$LOGTO_ENDPOINT->value.':3001',
$LOGTO_ADMIN_ENDPOINT->value.':3002',
]);
break;
}
2024-06-10 22:43:34 +02:00
return $payload;
}
function fqdnLabelsForCaddy(string $network, string $uuid, Collection $domains, bool $is_force_https_enabled = false, $onlyPort = null, ?Collection $serviceLabels = null, ?bool $is_gzip_enabled = true, ?bool $is_stripprefix_enabled = true, ?string $service_name = null, ?string $image = null, string $redirect_direction = 'both')
2024-03-11 15:08:05 +01:00
{
$labels = collect([]);
if ($serviceLabels) {
$labels->push("caddy_ingress_network={$uuid}");
} else {
$labels->push("caddy_ingress_network={$network}");
}
2024-03-11 15:08:05 +01:00
foreach ($domains as $loop => $domain) {
$loop = $loop;
$url = Url::fromString($domain);
$host = $url->getHost();
$path = $url->getPath();
$host_without_www = str($host)->replace('www.', '');
2024-03-11 15:08:05 +01:00
$schema = $url->getScheme();
$port = $url->getPort();
2024-08-23 14:21:12 +02:00
if (is_null($port) && ! is_null($onlyPort)) {
2024-03-11 15:08:05 +01:00
$port = $onlyPort;
}
$labels->push("caddy_{$loop}={$schema}://{$host}");
$labels->push("caddy_{$loop}.header=-Server");
$labels->push("caddy_{$loop}.try_files={path} /index.html /index.php");
2024-03-11 15:08:05 +01:00
2024-03-13 09:27:42 +01:00
if ($port) {
$labels->push("caddy_{$loop}.handle_path.{$loop}_reverse_proxy={{upstreams $port}}");
2024-03-11 15:08:05 +01:00
} else {
2024-03-13 09:27:42 +01:00
$labels->push("caddy_{$loop}.handle_path.{$loop}_reverse_proxy={{upstreams}}");
2024-03-11 15:08:05 +01:00
}
2024-03-13 09:27:42 +01:00
$labels->push("caddy_{$loop}.handle_path={$path}*");
2024-03-11 15:08:05 +01:00
if ($is_gzip_enabled) {
$labels->push("caddy_{$loop}.encode=zstd gzip");
}
2024-08-23 14:21:12 +02:00
if ($redirect_direction === 'www' && ! str($host)->startsWith('www.')) {
$labels->push("caddy_{$loop}.redir={$schema}://www.{$host}{uri}");
}
if ($redirect_direction === 'non-www' && str($host)->startsWith('www.')) {
$labels->push("caddy_{$loop}.redir={$schema}://{$host_without_www}{uri}");
}
2024-03-11 15:08:05 +01:00
if (isDev()) {
// $labels->push("caddy_{$loop}.tls=internal");
}
}
2024-06-10 22:43:34 +02:00
2024-03-11 15:08:05 +01:00
return $labels->sort();
}
function fqdnLabelsForTraefik(string $uuid, Collection $domains, bool $is_force_https_enabled = false, $onlyPort = null, ?Collection $serviceLabels = null, ?bool $is_gzip_enabled = true, ?bool $is_stripprefix_enabled = true, ?string $service_name = null, bool $generate_unique_uuid = false, ?string $image = null, string $redirect_direction = 'both')
2023-09-20 15:42:41 +02:00
{
$labels = collect([]);
$labels->push('traefik.enable=true');
2024-06-10 22:43:34 +02:00
$labels->push('traefik.http.middlewares.gzip.compress=true');
$labels->push('traefik.http.middlewares.redirect-to-https.redirectscheme.scheme=https');
$basic_auth = false;
$basic_auth_middleware = null;
$redirect = false;
$redirect_middleware = null;
if ($serviceLabels) {
$basic_auth = $serviceLabels->contains(function ($value) {
return str_contains($value, 'basicauth');
});
if ($basic_auth) {
$basic_auth_middleware = $serviceLabels
->map(function ($item) {
if (preg_match('/traefik\.http\.middlewares\.(.*?)\.basicauth\.users/', $item, $matches)) {
return $matches[1];
}
})
->filter()
->first();
}
$redirect = $serviceLabels->contains(function ($value) {
return str_contains($value, 'redirectregex');
});
if ($redirect) {
$redirect_middleware = $serviceLabels
->map(function ($item) {
if (preg_match('/traefik\.http\.middlewares\.(.*?)\.redirectregex\.regex/', $item, $matches)) {
return $matches[1];
}
})
->filter()
->first();
}
}
2023-10-18 14:14:40 +02:00
foreach ($domains as $loop => $domain) {
2023-12-28 17:53:47 +01:00
try {
if ($generate_unique_uuid) {
2024-07-25 13:31:59 +02:00
$uuid = new Cuid2;
}
2023-12-28 17:53:47 +01:00
$url = Url::fromString($domain);
$host = $url->getHost();
$path = $url->getPath();
$schema = $url->getScheme();
$port = $url->getPort();
2024-08-23 14:21:12 +02:00
if (is_null($port) && ! is_null($onlyPort)) {
2023-12-28 17:53:47 +01:00
$port = $onlyPort;
}
$http_label = "http-{$loop}-{$uuid}";
$https_label = "https-{$loop}-{$uuid}";
if ($service_name) {
$http_label = "http-{$loop}-{$uuid}-{$service_name}";
$https_label = "https-{$loop}-{$uuid}-{$service_name}";
}
2024-05-22 21:10:37 +02:00
if (str($image)->contains('ghost')) {
$labels->push("traefik.http.middlewares.redir-ghost.redirectregex.regex=^{$path}/(.*)");
2024-06-10 22:43:34 +02:00
$labels->push('traefik.http.middlewares.redir-ghost.redirectregex.replacement=/$1');
2024-05-22 21:10:37 +02:00
}
$to_www_name = "{$loop}-{$uuid}-to-www";
$to_non_www_name = "{$loop}-{$uuid}-to-non-www";
$redirect_to_non_www = [
"traefik.http.middlewares.{$to_non_www_name}.redirectregex.regex=^(http|https)://www\.(.+)",
"traefik.http.middlewares.{$to_non_www_name}.redirectregex.replacement=\${1}://\${2}",
2024-06-11 11:36:42 +02:00
"traefik.http.middlewares.{$to_non_www_name}.redirectregex.permanent=false",
];
$redirect_to_www = [
"traefik.http.middlewares.{$to_www_name}.redirectregex.regex=^(http|https)://(?:www\.)?(.+)",
"traefik.http.middlewares.{$to_www_name}.redirectregex.replacement=\${1}://www.\${2}",
2024-06-11 11:36:42 +02:00
"traefik.http.middlewares.{$to_www_name}.redirectregex.permanent=false",
];
2023-12-28 17:53:47 +01:00
if ($schema === 'https') {
// Set labels for https
$labels->push("traefik.http.routers.{$https_label}.rule=Host(`{$host}`) && PathPrefix(`{$path}`)");
$labels->push("traefik.http.routers.{$https_label}.entryPoints=https");
if ($port) {
$labels->push("traefik.http.routers.{$https_label}.service={$https_label}");
$labels->push("traefik.http.services.{$https_label}.loadbalancer.server.port=$port");
}
if ($path !== '/') {
2024-05-22 21:10:37 +02:00
$middlewares = collect([]);
2024-08-23 14:21:12 +02:00
if ($is_stripprefix_enabled && ! str($image)->contains('ghost')) {
$labels->push("traefik.http.middlewares.{$https_label}-stripprefix.stripprefix.prefixes={$path}");
2024-05-22 21:10:37 +02:00
$middlewares->push("{$https_label}-stripprefix");
}
if ($is_gzip_enabled) {
$middlewares->push('gzip');
}
if ($basic_auth && $basic_auth_middleware) {
$middlewares->push($basic_auth_middleware);
}
if ($redirect && $redirect_middleware) {
$middlewares->push($redirect_middleware);
}
2024-05-22 21:10:37 +02:00
if (str($image)->contains('ghost')) {
$middlewares->push('redir-ghost');
}
if ($redirect_direction === 'non-www' && str($host)->startsWith('www.')) {
$labels = $labels->merge($redirect_to_non_www);
$middlewares->push($to_non_www_name);
}
2024-08-23 14:21:12 +02:00
if ($redirect_direction === 'www' && ! str($host)->startsWith('www.')) {
$labels = $labels->merge($redirect_to_www);
$middlewares->push($to_www_name);
}
if ($middlewares->isNotEmpty()) {
$middlewares = $middlewares->join(',');
$labels->push("traefik.http.routers.{$https_label}.middlewares={$middlewares}");
}
2024-01-15 12:12:34 +01:00
} else {
$middlewares = collect([]);
if ($is_gzip_enabled) {
$middlewares->push('gzip');
}
if ($basic_auth && $basic_auth_middleware) {
$middlewares->push($basic_auth_middleware);
}
if ($redirect && $redirect_middleware) {
$middlewares->push($redirect_middleware);
}
2024-05-22 21:10:37 +02:00
if (str($image)->contains('ghost')) {
$middlewares->push('redir-ghost');
}
if ($redirect_direction === 'non-www' && str($host)->startsWith('www.')) {
$labels = $labels->merge($redirect_to_non_www);
$middlewares->push($to_non_www_name);
}
2024-08-23 14:21:12 +02:00
if ($redirect_direction === 'www' && ! str($host)->startsWith('www.')) {
$labels = $labels->merge($redirect_to_www);
$middlewares->push($to_www_name);
}
if ($middlewares->isNotEmpty()) {
$middlewares = $middlewares->join(',');
$labels->push("traefik.http.routers.{$https_label}.middlewares={$middlewares}");
}
2023-12-28 17:53:47 +01:00
}
$labels->push("traefik.http.routers.{$https_label}.tls=true");
$labels->push("traefik.http.routers.{$https_label}.tls.certresolver=letsencrypt");
// Set labels for http (redirect to https)
$labels->push("traefik.http.routers.{$http_label}.rule=Host(`{$host}`) && PathPrefix(`{$path}`)");
$labels->push("traefik.http.routers.{$http_label}.entryPoints=http");
2024-01-09 12:29:45 +01:00
if ($port) {
$labels->push("traefik.http.services.{$http_label}.loadbalancer.server.port=$port");
$labels->push("traefik.http.routers.{$http_label}.service={$http_label}");
2024-01-09 12:29:45 +01:00
}
2023-12-28 17:53:47 +01:00
if ($is_force_https_enabled) {
$labels->push("traefik.http.routers.{$http_label}.middlewares=redirect-to-https");
}
} else {
// Set labels for http
$labels->push("traefik.http.routers.{$http_label}.rule=Host(`{$host}`) && PathPrefix(`{$path}`)");
$labels->push("traefik.http.routers.{$http_label}.entryPoints=http");
if ($port) {
$labels->push("traefik.http.services.{$http_label}.loadbalancer.server.port=$port");
$labels->push("traefik.http.routers.{$http_label}.service={$http_label}");
2023-12-28 17:53:47 +01:00
}
if ($path !== '/') {
2024-05-22 21:10:37 +02:00
$middlewares = collect([]);
2024-08-23 14:21:12 +02:00
if ($is_stripprefix_enabled && ! str($image)->contains('ghost')) {
$labels->push("traefik.http.middlewares.{$http_label}-stripprefix.stripprefix.prefixes={$path}");
$middlewares->push("{$http_label}-stripprefix");
}
if ($is_gzip_enabled) {
$middlewares->push('gzip');
}
if ($basic_auth && $basic_auth_middleware) {
$middlewares->push($basic_auth_middleware);
}
if ($redirect && $redirect_middleware) {
$middlewares->push($redirect_middleware);
}
2024-05-22 21:10:37 +02:00
if (str($image)->contains('ghost')) {
$middlewares->push('redir-ghost');
}
if ($redirect_direction === 'non-www' && str($host)->startsWith('www.')) {
$labels = $labels->merge($redirect_to_non_www);
$middlewares->push($to_non_www_name);
}
2024-08-23 14:21:12 +02:00
if ($redirect_direction === 'www' && ! str($host)->startsWith('www.')) {
$labels = $labels->merge($redirect_to_www);
$middlewares->push($to_www_name);
}
if ($middlewares->isNotEmpty()) {
$middlewares = $middlewares->join(',');
$labels->push("traefik.http.routers.{$http_label}.middlewares={$middlewares}");
}
2024-01-15 12:12:34 +01:00
} else {
$middlewares = collect([]);
if ($is_gzip_enabled) {
$middlewares->push('gzip');
}
if ($basic_auth && $basic_auth_middleware) {
$middlewares->push($basic_auth_middleware);
}
if ($redirect && $redirect_middleware) {
$middlewares->push($redirect_middleware);
}
2024-05-22 21:10:37 +02:00
if (str($image)->contains('ghost')) {
$middlewares->push('redir-ghost');
}
if ($redirect_direction === 'non-www' && str($host)->startsWith('www.')) {
$labels = $labels->merge($redirect_to_non_www);
$middlewares->push($to_non_www_name);
}
2024-08-23 14:21:12 +02:00
if ($redirect_direction === 'www' && ! str($host)->startsWith('www.')) {
$labels = $labels->merge($redirect_to_www);
$middlewares->push($to_www_name);
}
if ($middlewares->isNotEmpty()) {
$middlewares = $middlewares->join(',');
$labels->push("traefik.http.routers.{$http_label}.middlewares={$middlewares}");
}
2023-12-28 17:53:47 +01:00
}
}
2024-01-09 12:29:45 +01:00
} catch (\Throwable $e) {
2023-12-28 17:53:47 +01:00
continue;
2023-09-20 15:42:41 +02:00
}
}
2024-06-10 22:43:34 +02:00
return $labels->sort();
2023-09-20 15:42:41 +02:00
}
function generateLabelsApplication(Application $application, ?ApplicationPreview $preview = null): array
2023-09-19 15:51:13 +02:00
{
$ports = $application->settings->is_static ? [80] : $application->ports_exposes_array;
$onlyPort = null;
2024-01-09 12:29:45 +01:00
if (count($ports) > 0) {
$onlyPort = $ports[0];
}
2023-09-19 15:51:13 +02:00
$pull_request_id = data_get($preview, 'pull_request_id', 0);
$appUuid = $application->uuid;
if ($pull_request_id !== 0) {
2024-08-23 14:21:12 +02:00
$appUuid = $appUuid.'-pr-'.$pull_request_id;
2023-09-19 15:51:13 +02:00
}
2023-09-20 15:42:41 +02:00
$labels = collect([]);
if ($pull_request_id === 0) {
if ($application->fqdn) {
$domains = str(data_get($application, 'fqdn'))->explode(',');
$shouldGenerateLabelsExactly = $application->destination->server->settings->generate_exact_labels;
if ($shouldGenerateLabelsExactly) {
switch ($application->destination->server->proxyType()) {
case ProxyTypes::TRAEFIK->value:
$labels = $labels->merge(fqdnLabelsForTraefik(
uuid: $appUuid,
domains: $domains,
onlyPort: $onlyPort,
is_force_https_enabled: $application->isForceHttpsEnabled(),
is_gzip_enabled: $application->isGzipEnabled(),
is_stripprefix_enabled: $application->isStripprefixEnabled(),
redirect_direction: $application->redirect
));
break;
case ProxyTypes::CADDY->value:
$labels = $labels->merge(fqdnLabelsForCaddy(
network: $application->destination->network,
uuid: $appUuid,
domains: $domains,
onlyPort: $onlyPort,
is_force_https_enabled: $application->isForceHttpsEnabled(),
is_gzip_enabled: $application->isGzipEnabled(),
is_stripprefix_enabled: $application->isStripprefixEnabled(),
redirect_direction: $application->redirect
));
break;
}
} else {
$labels = $labels->merge(fqdnLabelsForTraefik(
uuid: $appUuid,
domains: $domains,
onlyPort: $onlyPort,
is_force_https_enabled: $application->isForceHttpsEnabled(),
is_gzip_enabled: $application->isGzipEnabled(),
is_stripprefix_enabled: $application->isStripprefixEnabled(),
redirect_direction: $application->redirect
));
$labels = $labels->merge(fqdnLabelsForCaddy(
network: $application->destination->network,
uuid: $appUuid,
domains: $domains,
onlyPort: $onlyPort,
is_force_https_enabled: $application->isForceHttpsEnabled(),
is_gzip_enabled: $application->isGzipEnabled(),
is_stripprefix_enabled: $application->isStripprefixEnabled(),
redirect_direction: $application->redirect
));
}
}
} else {
if (data_get($preview, 'fqdn')) {
$domains = str(data_get($preview, 'fqdn'))->explode(',');
} else {
$domains = collect([]);
}
$shouldGenerateLabelsExactly = $application->destination->server->settings->generate_exact_labels;
if ($shouldGenerateLabelsExactly) {
2024-07-18 11:42:41 +02:00
switch ($application->destination->server->proxyType()) {
case ProxyTypes::TRAEFIK->value:
2024-07-17 21:06:56 +02:00
$labels = $labels->merge(fqdnLabelsForTraefik(
uuid: $appUuid,
domains: $domains,
onlyPort: $onlyPort,
is_force_https_enabled: $application->isForceHttpsEnabled(),
is_gzip_enabled: $application->isGzipEnabled(),
is_stripprefix_enabled: $application->isStripprefixEnabled()
2024-07-17 21:06:56 +02:00
));
2024-07-18 11:42:41 +02:00
break;
2024-07-17 21:06:56 +02:00
case ProxyTypes::CADDY->value:
$labels = $labels->merge(fqdnLabelsForCaddy(
network: $application->destination->network,
uuid: $appUuid,
domains: $domains,
onlyPort: $onlyPort,
is_force_https_enabled: $application->isForceHttpsEnabled(),
is_gzip_enabled: $application->isGzipEnabled(),
is_stripprefix_enabled: $application->isStripprefixEnabled()
2024-07-17 21:06:56 +02:00
));
2024-07-18 11:42:41 +02:00
break;
2024-07-17 21:06:56 +02:00
}
} else {
$labels = $labels->merge(fqdnLabelsForTraefik(
uuid: $appUuid,
domains: $domains,
onlyPort: $onlyPort,
is_force_https_enabled: $application->isForceHttpsEnabled(),
is_gzip_enabled: $application->isGzipEnabled(),
is_stripprefix_enabled: $application->isStripprefixEnabled()
));
$labels = $labels->merge(fqdnLabelsForCaddy(
network: $application->destination->network,
uuid: $appUuid,
domains: $domains,
onlyPort: $onlyPort,
is_force_https_enabled: $application->isForceHttpsEnabled(),
is_gzip_enabled: $application->isGzipEnabled(),
is_stripprefix_enabled: $application->isStripprefixEnabled()
));
2024-07-17 21:06:56 +02:00
}
2023-09-19 15:51:13 +02:00
}
2024-06-10 22:43:34 +02:00
2023-09-20 15:42:41 +02:00
return $labels->all();
2023-09-19 15:51:13 +02:00
}
2023-11-28 14:27:38 +01:00
function isDatabaseImage(?string $image = null)
{
2023-11-28 14:27:38 +01:00
if (is_null($image)) {
return false;
}
$image = str($image);
if ($image->contains(':')) {
$image = str($image);
} else {
$image = str($image)->append(':latest');
}
$imageName = $image->before(':');
if (collect(DATABASE_DOCKER_IMAGES)->contains($imageName)) {
return true;
}
2024-06-10 22:43:34 +02:00
return false;
}
function convert_docker_run_to_compose(?string $custom_docker_run_options = null)
{
$options = [];
$compose_options = collect([]);
preg_match_all('/(--\w+(?:-\w+)*)(?:\s|=)?([^\s-]+)?/', $custom_docker_run_options, $matches, PREG_SET_ORDER);
$list_options = collect([
'--cap-add',
'--cap-drop',
'--security-opt',
'--sysctl',
'--ulimit',
2024-02-25 23:13:27 +01:00
'--device',
'--shm-size',
]);
$mapping = collect([
'--cap-add' => 'cap_add',
'--cap-drop' => 'cap_drop',
'--security-opt' => 'security_opt',
'--sysctl' => 'sysctls',
2024-02-14 08:42:47 +01:00
'--device' => 'devices',
'--init' => 'init',
'--ulimit' => 'ulimits',
'--privileged' => 'privileged',
2024-02-25 23:13:27 +01:00
'--ip' => 'ip',
'--shm-size' => 'shm_size',
]);
foreach ($matches as $match) {
$option = $match[1];
2024-02-14 08:42:47 +01:00
if (isset($match[2]) && $match[2] !== '') {
$value = $match[2];
$options[$option][] = $value;
$options[$option] = array_unique($options[$option]);
} else {
$value = true;
$options[$option] = $value;
}
}
$options = collect($options);
// Easily get mappings from https://github.com/composerize/composerize/blob/master/packages/composerize/src/mappings.js
foreach ($options as $option => $value) {
// ray($option,$value);
2024-08-23 14:21:12 +02:00
if (! data_get($mapping, $option)) {
continue;
}
if ($option === '--ulimit') {
$ulimits = collect([]);
2024-02-09 13:38:17 +01:00
collect($value)->map(function ($ulimit) use ($ulimits) {
$ulimit = explode('=', $ulimit);
$type = $ulimit[0];
$limits = explode(':', $ulimit[1]);
if (count($limits) == 2) {
$soft_limit = $limits[0];
$hard_limit = $limits[1];
$ulimits->put($type, [
'soft' => $soft_limit,
2024-06-10 22:43:34 +02:00
'hard' => $hard_limit,
]);
} else {
$soft_limit = $ulimit[1];
$ulimits->put($type, [
'soft' => $soft_limit,
]);
}
});
$compose_options->put($mapping[$option], $ulimits);
} elseif ($option === '--shm-size') {
2024-08-23 14:21:12 +02:00
if (! is_null($value) && is_array($value) && count($value) > 0) {
$compose_options->put($mapping[$option], $value[0]);
}
} else {
if ($list_options->contains($option)) {
if ($compose_options->has($mapping[$option])) {
2024-08-23 14:21:12 +02:00
$compose_options->put($mapping[$option], $options->get($mapping[$option]).','.$value);
} else {
$compose_options->put($mapping[$option], $value);
}
2024-06-10 22:43:34 +02:00
continue;
} else {
$compose_options->put($mapping[$option], $value);
2024-06-10 22:43:34 +02:00
continue;
}
$compose_options->forget($option);
}
}
2024-06-10 22:43:34 +02:00
return $compose_options->toArray();
}
2024-03-14 09:21:48 +01:00
function generate_custom_docker_run_options_for_databases($docker_run_options, $docker_compose, $container_name, $network)
{
$ipv4 = data_get($docker_run_options, 'ip.0');
$ipv6 = data_get($docker_run_options, 'ip6.0');
data_forget($docker_run_options, 'ip');
data_forget($docker_run_options, 'ip6');
if ($ipv4 || $ipv6) {
data_forget($docker_compose['services'][$container_name], 'networks');
}
if ($ipv4) {
$docker_compose['services'][$container_name]['networks'][$network]['ipv4_address'] = $ipv4;
}
if ($ipv6) {
$docker_compose['services'][$container_name]['networks'][$network]['ipv6_address'] = $ipv6;
}
$docker_compose['services'][$container_name] = array_merge_recursive($docker_compose['services'][$container_name], $docker_run_options);
2024-08-23 14:21:12 +02:00
return $docker_compose;
}
2024-08-23 14:21:12 +02:00
function validateComposeFile(string $compose, int $server_id): string|Throwable
{
return 'OK';
2024-03-14 09:21:48 +01:00
try {
$uuid = Str::random(10);
$server = Server::findOrFail($server_id);
$base64_compose = base64_encode($compose);
$output = instant_remote_process([
"echo {$base64_compose} | base64 -d | tee /tmp/{$uuid}.yml > /dev/null",
2024-03-14 09:21:48 +01:00
"docker compose -f /tmp/{$uuid}.yml config",
], $server);
ray($output);
2024-06-10 22:43:34 +02:00
2024-03-14 09:21:48 +01:00
return 'OK';
} catch (\Throwable $e) {
ray($e);
2024-06-10 22:43:34 +02:00
2024-03-14 09:21:48 +01:00
return $e->getMessage();
} finally {
instant_remote_process([
"rm /tmp/{$uuid}.yml",
], $server);
}
}
function escapeEnvVariables($value)
{
2024-06-10 22:43:34 +02:00
$search = ['\\', "\r", "\t", "\x0", '"', "'"];
$replace = ['\\\\', '\\r', '\\t', '\\0', '\"', "\'"];
return str_replace($search, $replace, $value);
}
2024-04-15 12:46:22 +02:00
function escapeDollarSign($value)
{
2024-06-10 22:43:34 +02:00
$search = ['$'];
$replace = ['$$'];
2024-04-15 12:46:22 +02:00
return str_replace($search, $replace, $value);
}