2023-05-24 12:26:50 +00:00
|
|
|
<?php
|
|
|
|
|
2023-09-19 13:51:13 +00:00
|
|
|
use App\Enums\ProxyTypes;
|
2023-08-21 16:00:12 +00:00
|
|
|
use App\Models\Application;
|
2023-09-19 13:51:13 +00:00
|
|
|
use App\Models\ApplicationPreview;
|
2023-05-24 12:26:50 +00:00
|
|
|
use App\Models\Server;
|
|
|
|
use Illuminate\Support\Collection;
|
2023-08-17 11:14:46 +00:00
|
|
|
use Illuminate\Support\Str;
|
2023-09-19 13:51:13 +00:00
|
|
|
use Spatie\Url\Url;
|
2023-05-24 12:26:50 +00:00
|
|
|
|
2023-09-19 13:51:13 +00:00
|
|
|
function getCurrentApplicationContainerStatus(Server $server, int $id): Collection
|
|
|
|
{
|
2023-08-21 16:00:12 +00:00
|
|
|
$containers = instant_remote_process(["docker ps -a --filter='label=coolify.applicationId={$id}' --format '{{json .}}' "], $server);
|
|
|
|
if (!$containers) {
|
|
|
|
return collect([]);
|
|
|
|
}
|
|
|
|
return format_docker_command_output_to_json($containers);
|
|
|
|
}
|
|
|
|
|
2023-05-24 12:26:50 +00:00
|
|
|
function format_docker_command_output_to_json($rawOutput): Collection
|
|
|
|
{
|
|
|
|
$outputLines = explode(PHP_EOL, $rawOutput);
|
2023-08-21 16:00:12 +00:00
|
|
|
if (count($outputLines) === 1) {
|
|
|
|
$outputLines = collect($outputLines[0]);
|
|
|
|
} else {
|
|
|
|
$outputLines = collect($outputLines);
|
|
|
|
}
|
|
|
|
return $outputLines
|
2023-08-17 11:14:46 +00:00
|
|
|
->reject(fn ($line) => empty($line))
|
|
|
|
->map(fn ($outputLine) => json_decode($outputLine, true, flags: JSON_THROW_ON_ERROR));
|
2023-05-24 12:26:50 +00:00
|
|
|
}
|
2023-08-08 09:51:36 +00:00
|
|
|
|
2023-09-19 13:51:13 +00:00
|
|
|
function format_docker_labels_to_json(string|array $rawOutput): Collection
|
2023-05-24 12:26:50 +00:00
|
|
|
{
|
2023-09-12 13:47:30 +00:00
|
|
|
if (is_array($rawOutput)) {
|
|
|
|
return collect($rawOutput);
|
|
|
|
}
|
2023-05-24 12:26:50 +00:00
|
|
|
$outputLines = explode(PHP_EOL, $rawOutput);
|
|
|
|
|
|
|
|
return collect($outputLines)
|
2023-08-17 11:14:46 +00:00
|
|
|
->reject(fn ($line) => empty($line))
|
2023-05-24 12:26:50 +00:00
|
|
|
->map(function ($outputLine) {
|
|
|
|
$outputArray = explode(',', $outputLine);
|
|
|
|
return collect($outputArray)
|
|
|
|
->map(function ($outputLine) {
|
|
|
|
return explode('=', $outputLine);
|
|
|
|
})
|
|
|
|
->mapWithKeys(function ($outputLine) {
|
|
|
|
return [$outputLine[0] => $outputLine[1]];
|
|
|
|
});
|
|
|
|
})[0];
|
|
|
|
}
|
|
|
|
|
2023-08-11 14:13:53 +00:00
|
|
|
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]];
|
|
|
|
});
|
2023-09-11 15:36:30 +00:00
|
|
|
} catch (\Throwable $e) {
|
2023-08-11 14:13:53 +00:00
|
|
|
return collect([]);
|
|
|
|
}
|
|
|
|
}
|
2023-09-19 13:51:13 +00:00
|
|
|
function checkMinimumDockerEngineVersion($dockerVersion)
|
|
|
|
{
|
2023-09-15 13:34:25 +00:00
|
|
|
$majorDockerVersion = Str::of($dockerVersion)->before('.')->value();
|
|
|
|
if ($majorDockerVersion <= 22) {
|
|
|
|
$dockerVersion = null;
|
|
|
|
}
|
|
|
|
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 14:13:53 +00:00
|
|
|
|
2023-09-19 13:51:13 +00:00
|
|
|
function getApplicationContainerStatus(Application $application)
|
|
|
|
{
|
|
|
|
$server = data_get($application, 'destination.server');
|
2023-08-21 16:00:12 +00:00
|
|
|
$id = $application->id;
|
2023-08-28 08:44:11 +00:00
|
|
|
if (!$server) {
|
|
|
|
return 'exited';
|
|
|
|
}
|
2023-08-21 16:00:12 +00:00
|
|
|
$containers = getCurrentApplicationContainerStatus($server, $id);
|
|
|
|
if ($containers->count() > 0) {
|
|
|
|
$status = data_get($containers[0], 'State', 'exited');
|
|
|
|
return $status;
|
|
|
|
}
|
|
|
|
return 'exited';
|
|
|
|
}
|
|
|
|
function getContainerStatus(Server $server, string $container_id, bool $all_data = false, bool $throwError = false)
|
2023-05-24 12:26:50 +00:00
|
|
|
{
|
2023-06-02 13:15:12 +00:00
|
|
|
$container = instant_remote_process(["docker inspect --format '{{json .}}' {$container_id}"], $server, $throwError);
|
2023-05-24 12:26:50 +00:00
|
|
|
if (!$container) {
|
|
|
|
return 'exited';
|
|
|
|
}
|
|
|
|
$container = format_docker_command_output_to_json($container);
|
2023-06-02 13:15:12 +00:00
|
|
|
if ($all_data) {
|
2023-08-24 19:42:47 +00:00
|
|
|
return $container[0];
|
2023-06-02 13:15:12 +00:00
|
|
|
}
|
2023-08-17 11:14:46 +00:00
|
|
|
return data_get($container[0], 'State.Status', 'exited');
|
2023-05-24 12:26:50 +00:00
|
|
|
}
|
2023-05-30 13:52:17 +00:00
|
|
|
|
2023-09-19 13:51:13 +00:00
|
|
|
function generateApplicationContainerName(Application $application)
|
2023-05-30 13:52:17 +00:00
|
|
|
{
|
2023-09-01 14:07:46 +00:00
|
|
|
$now = now()->format('Hisu');
|
2023-09-19 13:51:13 +00:00
|
|
|
if ($application->pull_request_id !== 0 && $application->pull_request_id !== null) {
|
|
|
|
return $application->uuid . '-pr-' . $application->pull_request_id;
|
2023-05-30 13:52:17 +00:00
|
|
|
} else {
|
2023-09-19 13:51:13 +00:00
|
|
|
return $application->uuid . '-' . $now;
|
2023-05-30 13:52:17 +00:00
|
|
|
}
|
|
|
|
}
|
2023-08-11 20:41:47 +00:00
|
|
|
function get_port_from_dockerfile($dockerfile): int
|
|
|
|
{
|
2023-08-17 11:14:46 +00:00
|
|
|
$dockerfile_array = explode("\n", $dockerfile);
|
|
|
|
$found_exposed_port = null;
|
|
|
|
foreach ($dockerfile_array as $line) {
|
|
|
|
$line_str = Str::of($line)->trim();
|
|
|
|
if ($line_str->startsWith('EXPOSE')) {
|
|
|
|
$found_exposed_port = $line_str->replace('EXPOSE', '')->trim();
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if ($found_exposed_port) {
|
|
|
|
return (int)$found_exposed_port->value();
|
2023-08-11 20:41:47 +00:00
|
|
|
}
|
2023-08-17 11:14:46 +00:00
|
|
|
return 80;
|
2023-08-11 20:41:47 +00:00
|
|
|
}
|
2023-09-19 13:51:13 +00:00
|
|
|
|
2023-09-20 13:42:41 +00:00
|
|
|
function defaultLabels($id, $name, $pull_request_id = 0)
|
|
|
|
{
|
|
|
|
$labels = collect([]);
|
|
|
|
$labels->push('coolify.managed=true');
|
|
|
|
$labels->push('coolify.version=' . config('version'));
|
|
|
|
$labels->push('coolify.applicationId=' . $id);
|
|
|
|
$labels->push('coolify.type=application');
|
|
|
|
$labels->push('coolify.name=' . $name);
|
|
|
|
if ($pull_request_id !== 0) {
|
|
|
|
$labels->push('coolify.pullRequestId=' . $pull_request_id);
|
|
|
|
}
|
|
|
|
return $labels;
|
|
|
|
}
|
|
|
|
function fqdnLabelsForTraefik($domain, $container_name, $is_force_https_enabled)
|
|
|
|
{
|
|
|
|
$labels = collect([]);
|
|
|
|
$labels->push('traefik.enable=true');
|
|
|
|
$url = Url::fromString($domain);
|
|
|
|
$host = $url->getHost();
|
|
|
|
$path = $url->getPath();
|
|
|
|
$schema = $url->getScheme();
|
|
|
|
$slug = Str::slug($host . $path);
|
|
|
|
|
|
|
|
$http_label = "{$container_name}-{$slug}-http";
|
|
|
|
$https_label = "{$container_name}-{$slug}-https";
|
|
|
|
|
|
|
|
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");
|
|
|
|
$labels->push("traefik.http.routers.{$https_label}.middlewares=gzip");
|
|
|
|
if ($path !== '/') {
|
|
|
|
$labels->push("traefik.http.routers.{$https_label}.middlewares={$https_label}-stripprefix");
|
|
|
|
$labels->push("traefik.http.middlewares.{$https_label}-stripprefix.stripprefix.prefixes={$path}");
|
|
|
|
}
|
|
|
|
|
|
|
|
$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");
|
|
|
|
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");
|
|
|
|
$labels->push("traefik.http.routers.{$http_label}.middlewares=gzip");
|
|
|
|
if ($path !== '/') {
|
|
|
|
$labels->push("traefik.http.routers.{$http_label}.middlewares={$http_label}-stripprefix");
|
|
|
|
$labels->push("traefik.http.middlewares.{$http_label}-stripprefix.stripprefix.prefixes={$path}");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return $labels;
|
|
|
|
}
|
|
|
|
function generateLabelsApplication(Application $application, ?ApplicationPreview $preview = null): array
|
2023-09-19 13:51:13 +00:00
|
|
|
{
|
|
|
|
|
|
|
|
$pull_request_id = data_get($preview, 'pull_request_id', 0);
|
|
|
|
$container_name = generateApplicationContainerName($application);
|
|
|
|
$appId = $application->id;
|
|
|
|
if ($pull_request_id !== 0) {
|
|
|
|
$appId = $appId . '-pr-' . $application->pull_request_id;
|
|
|
|
}
|
2023-09-20 13:42:41 +00:00
|
|
|
$labels = collect([]);
|
|
|
|
$labels = $labels->merge(defaultLabels($appId, $container_name, $pull_request_id));
|
2023-09-19 13:51:13 +00:00
|
|
|
if ($application->fqdn) {
|
|
|
|
if ($pull_request_id !== 0) {
|
|
|
|
$domains = Str::of(data_get($preview, 'fqdn'))->explode(',');
|
|
|
|
} else {
|
|
|
|
$domains = Str::of(data_get($application, 'fqdn'))->explode(',');
|
|
|
|
}
|
|
|
|
if ($application->destination->server->proxy->type === ProxyTypes::TRAEFIK_V2->value) {
|
|
|
|
foreach ($domains as $domain) {
|
2023-09-20 13:42:41 +00:00
|
|
|
$labels = $labels->merge(fqdnLabelsForTraefik($domain, $container_name, $application->settings->is_force_https_enabled));
|
|
|
|
// $url = Url::fromString($domain);
|
|
|
|
// $host = $url->getHost();
|
|
|
|
// $path = $url->getPath();
|
|
|
|
// $schema = $url->getScheme();
|
|
|
|
// $slug = Str::slug($host . $path);
|
|
|
|
|
|
|
|
// $http_label = "{$container_name}-{$slug}-http";
|
|
|
|
// $https_label = "{$container_name}-{$slug}-https";
|
|
|
|
|
|
|
|
// if ($schema === 'https') {
|
|
|
|
// // Set labels for https
|
|
|
|
// $labels[] = "traefik.http.routers.{$https_label}.rule=Host(`{$host}`) && PathPrefix(`{$path}`)";
|
|
|
|
// $labels[] = "traefik.http.routers.{$https_label}.entryPoints=https";
|
|
|
|
// $labels[] = "traefik.http.routers.{$https_label}.middlewares=gzip";
|
|
|
|
// if ($path !== '/') {
|
|
|
|
// $labels[] = "traefik.http.routers.{$https_label}.middlewares={$https_label}-stripprefix";
|
|
|
|
// $labels[] = "traefik.http.middlewares.{$https_label}-stripprefix.stripprefix.prefixes={$path}";
|
|
|
|
// }
|
|
|
|
|
|
|
|
// $labels[] = "traefik.http.routers.{$https_label}.tls=true";
|
|
|
|
// $labels[] = "traefik.http.routers.{$https_label}.tls.certresolver=letsencrypt";
|
|
|
|
|
|
|
|
// // Set labels for http (redirect to https)
|
|
|
|
// $labels[] = "traefik.http.routers.{$http_label}.rule=Host(`{$host}`) && PathPrefix(`{$path}`)";
|
|
|
|
// $labels[] = "traefik.http.routers.{$http_label}.entryPoints=http";
|
|
|
|
// if ($application->settings->is_force_https_enabled) {
|
|
|
|
// $labels[] = "traefik.http.routers.{$http_label}.middlewares=redirect-to-https";
|
|
|
|
// }
|
|
|
|
// } else {
|
|
|
|
// // Set labels for http
|
|
|
|
// $labels[] = "traefik.http.routers.{$http_label}.rule=Host(`{$host}`) && PathPrefix(`{$path}`)";
|
|
|
|
// $labels[] = "traefik.http.routers.{$http_label}.entryPoints=http";
|
|
|
|
// $labels[] = "traefik.http.routers.{$http_label}.middlewares=gzip";
|
|
|
|
// if ($path !== '/') {
|
|
|
|
// $labels[] = "traefik.http.routers.{$http_label}.middlewares={$http_label}-stripprefix";
|
|
|
|
// $labels[] = "traefik.http.middlewares.{$http_label}-stripprefix.stripprefix.prefixes={$path}";
|
|
|
|
// }
|
|
|
|
// }
|
2023-09-19 13:51:13 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2023-09-20 13:42:41 +00:00
|
|
|
return $labels->all();
|
2023-09-19 13:51:13 +00:00
|
|
|
}
|