Fix styling

This commit is contained in:
Thijmen
2024-06-10 20:43:34 +00:00
committed by github-actions[bot]
parent 41fb6a1fc9
commit d86274cc37
429 changed files with 5307 additions and 2831 deletions

View File

@@ -1,6 +1,5 @@
<?php
use App\Enums\ProxyTypes;
use App\Models\Application;
use App\Models\ApplicationPreview;
use App\Models\Server;
@@ -13,13 +12,14 @@ use Visus\Cuid2\Cuid2;
function getCurrentApplicationContainerStatus(Server $server, int $id, ?int $pullRequestId = null, ?bool $includePullrequests = false): Collection
{
$containers = collect([]);
if (!$server->isSwarm()) {
if (! $server->isSwarm()) {
$containers = instant_remote_process(["docker ps -a --filter='label=coolify.applicationId={$id}' --format '{{json .}}' "], $server);
$containers = format_docker_command_output_to_json($containers);
$containers = $containers->map(function ($container) use ($pullRequestId, $includePullrequests) {
$labels = data_get($container, 'Labels');
if (!str($labels)->contains("coolify.pullRequestId=")) {
data_set($container, 'Labels', $labels . ",coolify.pullRequestId={$pullRequestId}");
if (! str($labels)->contains('coolify.pullRequestId=')) {
data_set($container, 'Labels', $labels.",coolify.pullRequestId={$pullRequestId}");
return $container;
}
if ($includePullrequests) {
@@ -28,11 +28,14 @@ function getCurrentApplicationContainerStatus(Server $server, int $id, ?int $pul
if (str($labels)->contains("coolify.pullRequestId=$pullRequestId")) {
return $container;
}
return null;
});
$containers = $containers->filter();
return $containers;
}
return $containers;
}
@@ -44,6 +47,7 @@ function format_docker_command_output_to_json($rawOutput): Collection
} else {
$outputLines = collect($outputLines);
}
return $outputLines
->reject(fn ($line) => empty($line))
->map(fn ($outputLine) => json_decode($outputLine, true, flags: JSON_THROW_ON_ERROR));
@@ -60,6 +64,7 @@ function format_docker_labels_to_json(string|array $rawOutput): Collection
->reject(fn ($line) => empty($line))
->map(function ($outputLine) {
$outputArray = explode(',', $outputLine);
return collect($outputArray)
->map(function ($outputLine) {
return explode('=', $outputLine);
@@ -74,8 +79,10 @@ function format_docker_envs_to_json($rawOutput)
{
try {
$outputLines = json_decode($rawOutput, true, flags: JSON_THROW_ON_ERROR);
return collect(data_get($outputLines[0], 'Config.Env', []))->mapWithKeys(function ($env) {
$env = explode('=', $env);
return [$env[0] => $env[1]];
});
} catch (\Throwable $e) {
@@ -88,6 +95,7 @@ function checkMinimumDockerEngineVersion($dockerVersion)
if ($majorDockerVersion <= 22) {
$dockerVersion = null;
}
return $dockerVersion;
}
function executeInDocker(string $containerId, string $command)
@@ -103,7 +111,7 @@ function getContainerStatus(Server $server, string $container_id, bool $all_data
} else {
$container = instant_remote_process(["docker inspect --format '{{json .}}' {$container_id}"], $server, $throwError);
}
if (!$container) {
if (! $container) {
return 'exited';
}
$container = format_docker_command_output_to_json($container);
@@ -113,8 +121,8 @@ function getContainerStatus(Server $server, string $container_id, bool $all_data
if ($server->isSwarm()) {
$replicas = data_get($container[0], 'Replicas');
$replicas = explode('/', $replicas);
$active = (int)$replicas[0];
$total = (int)$replicas[1];
$active = (int) $replicas[0];
$total = (int) $replicas[1];
if ($active === $total) {
return 'running';
} else {
@@ -130,15 +138,16 @@ function generateApplicationContainerName(Application $application, $pull_reques
$consistent_container_name = $application->settings->is_consistent_container_name_enabled;
$now = now()->format('Hisu');
if ($pull_request_id !== 0 && $pull_request_id !== null) {
return $application->uuid . '-pr-' . $pull_request_id;
return $application->uuid.'-pr-'.$pull_request_id;
} else {
if ($consistent_container_name) {
return $application->uuid;
}
return $application->uuid . '-' . $now;
return $application->uuid.'-'.$now;
}
}
function get_port_from_dockerfile($dockerfile): int|null
function get_port_from_dockerfile($dockerfile): ?int
{
$dockerfile_array = explode("\n", $dockerfile);
$found_exposed_port = null;
@@ -150,8 +159,9 @@ function get_port_from_dockerfile($dockerfile): int|null
}
}
if ($found_exposed_port) {
return (int)$found_exposed_port->value();
return (int) $found_exposed_port->value();
}
return null;
}
@@ -159,15 +169,16 @@ function defaultLabels($id, $name, $pull_request_id = 0, string $type = 'applica
{
$labels = collect([]);
$labels->push('coolify.managed=true');
$labels->push('coolify.version=' . config('version'));
$labels->push("coolify." . $type . "Id=" . $id);
$labels->push('coolify.version='.config('version'));
$labels->push('coolify.'.$type.'Id='.$id);
$labels->push("coolify.type=$type");
$labels->push('coolify.name=' . $name);
$labels->push('coolify.pullRequestId=' . $pull_request_id);
$labels->push('coolify.name='.$name);
$labels->push('coolify.pullRequestId='.$pull_request_id);
if ($type === 'service') {
$subId && $labels->push('coolify.service.subId=' . $subId);
$subType && $labels->push('coolify.service.subType=' . $subType);
$subId && $labels->push('coolify.service.subId='.$subId);
$subType && $labels->push('coolify.service.subType='.$subType);
}
return $labels;
}
function generateServiceSpecificFqdns(ServiceApplication|Application $resource)
@@ -177,7 +188,7 @@ function generateServiceSpecificFqdns(ServiceApplication|Application $resource)
$server = data_get($resource, 'service.server');
$environment_variables = data_get($resource, 'service.environment_variables');
$type = $resource->serviceType();
} else if ($resource->getMorphClass() === 'App\Models\Application') {
} elseif ($resource->getMorphClass() === 'App\Models\Application') {
$uuid = data_get($resource, 'uuid');
$server = data_get($resource, 'destination.server');
$environment_variables = data_get($resource, 'environment_variables');
@@ -197,17 +208,17 @@ function generateServiceSpecificFqdns(ServiceApplication|Application $resource)
}
if (is_null($MINIO_BROWSER_REDIRECT_URL?->value)) {
$MINIO_BROWSER_REDIRECT_URL?->update([
"value" => generateFqdn($server, 'console-' . $uuid)
'value' => generateFqdn($server, 'console-'.$uuid),
]);
}
if (is_null($MINIO_SERVER_URL?->value)) {
$MINIO_SERVER_URL?->update([
"value" => generateFqdn($server, 'minio-' . $uuid)
'value' => generateFqdn($server, 'minio-'.$uuid),
]);
}
$payload = collect([
$MINIO_BROWSER_REDIRECT_URL->value . ':9001',
$MINIO_SERVER_URL->value . ':9000',
$MINIO_BROWSER_REDIRECT_URL->value.':9001',
$MINIO_SERVER_URL->value.':9000',
]);
break;
case $type?->contains('logto'):
@@ -218,20 +229,21 @@ function generateServiceSpecificFqdns(ServiceApplication|Application $resource)
}
if (is_null($LOGTO_ENDPOINT?->value)) {
$LOGTO_ENDPOINT?->update([
"value" => generateFqdn($server, 'logto-' . $uuid)
'value' => generateFqdn($server, 'logto-'.$uuid),
]);
}
if (is_null($LOGTO_ADMIN_ENDPOINT?->value)) {
$LOGTO_ADMIN_ENDPOINT?->update([
"value" => generateFqdn($server, 'logto-admin-' . $uuid)
'value' => generateFqdn($server, 'logto-admin-'.$uuid),
]);
}
$payload = collect([
$LOGTO_ENDPOINT->value . ':3001',
$LOGTO_ADMIN_ENDPOINT->value . ':3002',
$LOGTO_ENDPOINT->value.':3001',
$LOGTO_ADMIN_ENDPOINT->value.':3002',
]);
break;
}
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)
@@ -250,7 +262,7 @@ function fqdnLabelsForCaddy(string $network, string $uuid, Collection $domains,
$schema = $url->getScheme();
$port = $url->getPort();
if (is_null($port) && !is_null($onlyPort)) {
if (is_null($port) && ! is_null($onlyPort)) {
$port = $onlyPort;
}
$labels->push("caddy_{$loop}={$schema}://{$host}");
@@ -270,14 +282,15 @@ function fqdnLabelsForCaddy(string $network, string $uuid, Collection $domains,
// $labels->push("caddy_{$loop}.tls=internal");
}
}
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)
{
$labels = collect([]);
$labels->push('traefik.enable=true');
$labels->push("traefik.http.middlewares.gzip.compress=true");
$labels->push("traefik.http.middlewares.redirect-to-https.redirectscheme.scheme=https");
$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;
@@ -321,7 +334,7 @@ function fqdnLabelsForTraefik(string $uuid, Collection $domains, bool $is_force_
$path = $url->getPath();
$schema = $url->getScheme();
$port = $url->getPort();
if (is_null($port) && !is_null($onlyPort)) {
if (is_null($port) && ! is_null($onlyPort)) {
$port = $onlyPort;
}
$http_label = "http-{$loop}-{$uuid}";
@@ -332,7 +345,7 @@ function fqdnLabelsForTraefik(string $uuid, Collection $domains, bool $is_force_
}
if (str($image)->contains('ghost')) {
$labels->push("traefik.http.middlewares.redir-ghost.redirectregex.regex=^{$path}/(.*)");
$labels->push("traefik.http.middlewares.redir-ghost.redirectregex.replacement=/$1");
$labels->push('traefik.http.middlewares.redir-ghost.redirectregex.replacement=/$1');
}
if ($schema === 'https') {
// Set labels for https
@@ -344,7 +357,7 @@ function fqdnLabelsForTraefik(string $uuid, Collection $domains, bool $is_force_
}
if ($path !== '/') {
$middlewares = collect([]);
if ($is_stripprefix_enabled && !str($image)->contains('ghost')) {
if ($is_stripprefix_enabled && ! str($image)->contains('ghost')) {
$labels->push("traefik.http.middlewares.{$https_label}-stripprefix.stripprefix.prefixes={$path}");
$middlewares->push("{$https_label}-stripprefix");
}
@@ -406,7 +419,7 @@ function fqdnLabelsForTraefik(string $uuid, Collection $domains, bool $is_force_
}
if ($path !== '/') {
$middlewares = collect([]);
if ($is_stripprefix_enabled && !str($image)->contains('ghost')) {
if ($is_stripprefix_enabled && ! str($image)->contains('ghost')) {
$labels->push("traefik.http.middlewares.{$http_label}-stripprefix.stripprefix.prefixes={$path}");
$middlewares->push("{$https_label}-stripprefix");
}
@@ -450,6 +463,7 @@ function fqdnLabelsForTraefik(string $uuid, Collection $domains, bool $is_force_
continue;
}
}
return $labels->sort();
}
function generateLabelsApplication(Application $application, ?ApplicationPreview $preview = null): array
@@ -462,7 +476,7 @@ function generateLabelsApplication(Application $application, ?ApplicationPreview
$pull_request_id = data_get($preview, 'pull_request_id', 0);
$appUuid = $application->uuid;
if ($pull_request_id !== 0) {
$appUuid = $appUuid . '-pr-' . $pull_request_id;
$appUuid = $appUuid.'-pr-'.$pull_request_id;
}
$labels = collect([]);
if ($pull_request_id === 0) {
@@ -488,7 +502,7 @@ function generateLabelsApplication(Application $application, ?ApplicationPreview
));
}
} else {
if (data_get($preview,'fqdn')) {
if (data_get($preview, 'fqdn')) {
$domains = Str::of(data_get($preview, 'fqdn'))->explode(',');
} else {
$domains = collect([]);
@@ -513,6 +527,7 @@ function generateLabelsApplication(Application $application, ?ApplicationPreview
));
}
return $labels->all();
}
@@ -531,6 +546,7 @@ function isDatabaseImage(?string $image = null)
if (collect(DATABASE_DOCKER_IMAGES)->contains($imageName)) {
return true;
}
return false;
}
@@ -573,7 +589,7 @@ function convert_docker_run_to_compose(?string $custom_docker_run_options = null
$options = collect($options);
// Easily get mappings from https://github.com/composerize/composerize/blob/master/packages/composerize/src/mappings.js
foreach ($options as $option => $value) {
if (!data_get($mapping, $option)) {
if (! data_get($mapping, $option)) {
continue;
}
if ($option === '--ulimit') {
@@ -587,7 +603,7 @@ function convert_docker_run_to_compose(?string $custom_docker_run_options = null
$hard_limit = $limits[1];
$ulimits->put($type, [
'soft' => $soft_limit,
'hard' => $hard_limit
'hard' => $hard_limit,
]);
} else {
$soft_limit = $ulimit[1];
@@ -600,18 +616,21 @@ function convert_docker_run_to_compose(?string $custom_docker_run_options = null
} else {
if ($list_options->contains($option)) {
if ($compose_options->has($mapping[$option])) {
$compose_options->put($mapping[$option], $options->get($mapping[$option]) . ',' . $value);
$compose_options->put($mapping[$option], $options->get($mapping[$option]).','.$value);
} else {
$compose_options->put($mapping[$option], $value);
}
continue;
} else {
$compose_options->put($mapping[$option], $value);
continue;
}
$compose_options->forget($option);
}
}
return $compose_options->toArray();
}
@@ -627,9 +646,11 @@ function validateComposeFile(string $compose, int $server_id): string|Throwable
"docker compose -f /tmp/{$uuid}.yml config",
], $server);
ray($output);
return 'OK';
} catch (\Throwable $e) {
ray($e);
return $e->getMessage();
} finally {
instant_remote_process([
@@ -640,13 +661,15 @@ function validateComposeFile(string $compose, int $server_id): string|Throwable
function escapeEnvVariables($value)
{
$search = array("\\", "\r", "\t", "\x0", '"', "'");
$replace = array("\\\\", "\\r", "\\t", "\\0", '\"', "\'");
$search = ['\\', "\r", "\t", "\x0", '"', "'"];
$replace = ['\\\\', '\\r', '\\t', '\\0', '\"', "\'"];
return str_replace($search, $replace, $value);
}
function escapeDollarSign($value)
{
$search = array('$');
$replace = array('$$');
$search = ['$'];
$replace = ['$$'];
return str_replace($search, $replace, $value);
}