wip: services

This commit is contained in:
Andras Bacsai
2023-09-19 15:51:13 +02:00
parent 145af41c82
commit a86e971020
24 changed files with 652 additions and 87 deletions

View File

@@ -1,11 +1,15 @@
<?php
use App\Enums\ProxyTypes;
use App\Models\Application;
use App\Models\ApplicationPreview;
use App\Models\Server;
use Illuminate\Support\Collection;
use Illuminate\Support\Str;
use Spatie\Url\Url;
function getCurrentApplicationContainerStatus(Server $server, int $id): Collection {
function getCurrentApplicationContainerStatus(Server $server, int $id): Collection
{
$containers = instant_remote_process(["docker ps -a --filter='label=coolify.applicationId={$id}' --format '{{json .}}' "], $server);
if (!$containers) {
return collect([]);
@@ -26,7 +30,7 @@ function format_docker_command_output_to_json($rawOutput): Collection
->map(fn ($outputLine) => json_decode($outputLine, true, flags: JSON_THROW_ON_ERROR));
}
function format_docker_labels_to_json(string|Array $rawOutput): Collection
function format_docker_labels_to_json(string|array $rawOutput): Collection
{
if (is_array($rawOutput)) {
return collect($rawOutput);
@@ -59,7 +63,8 @@ function format_docker_envs_to_json($rawOutput)
return collect([]);
}
}
function checkMinimumDockerEngineVersion($dockerVersion) {
function checkMinimumDockerEngineVersion($dockerVersion)
{
$majorDockerVersion = Str::of($dockerVersion)->before('.')->value();
if ($majorDockerVersion <= 22) {
$dockerVersion = null;
@@ -72,8 +77,9 @@ function executeInDocker(string $containerId, string $command)
// return "docker exec {$this->deployment_uuid} bash -c '{$command} |& tee -a /proc/1/fd/1; [ \$PIPESTATUS -eq 0 ] || exit \$PIPESTATUS'";
}
function getApplicationContainerStatus(Application $application) {
$server = data_get($application,'destination.server');
function getApplicationContainerStatus(Application $application)
{
$server = data_get($application, 'destination.server');
$id = $application->id;
if (!$server) {
return 'exited';
@@ -98,13 +104,13 @@ function getContainerStatus(Server $server, string $container_id, bool $all_data
return data_get($container[0], 'State.Status', 'exited');
}
function generateApplicationContainerName(string $uuid, int $pull_request_id = 0)
function generateApplicationContainerName(Application $application)
{
$now = now()->format('Hisu');
if ($pull_request_id !== 0 && $pull_request_id !== null) {
return $uuid . '-pr-' . $pull_request_id;
if ($application->pull_request_id !== 0 && $application->pull_request_id !== null) {
return $application->uuid . '-pr-' . $application->pull_request_id;
} else {
return $uuid . '-' . $now;
return $application->uuid . '-' . $now;
}
}
function get_port_from_dockerfile($dockerfile): int
@@ -123,3 +129,74 @@ function get_port_from_dockerfile($dockerfile): int
}
return 80;
}
function generateLabelsApplication(Application $application, ?ApplicationPreview $preview = null)
{
$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;
}
$labels = [];
$labels[] = 'coolify.managed=true';
$labels[] = 'coolify.version=' . config('version');
$labels[] = 'coolify.applicationId=' . $appId;
$labels[] = 'coolify.type=application';
$labels[] = 'coolify.name=' . $application->name;
if ($pull_request_id !== 0) {
$labels[] = 'coolify.pullRequestId=' . $pull_request_id;
}
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) {
$labels[] = 'traefik.enable=true';
foreach ($domains as $domain) {
$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}";
}
}
}
}
}
return $labels;
}

View File

@@ -0,0 +1,185 @@
<?php
use App\Models\Application;
use Symfony\Component\Yaml\Yaml;
use Illuminate\Support\Str;
# Application generated variables
# SERVICE_FQDN_*: FQDN coming from your application (https://coolify.io)
# SERVICE_URL_*: URL coming from your application (coolify.io)
# SERVICE_USER_*: Generated by your application, username (not encrypted)
# SERVICE_PASSWORD_*: Generated by your application, password (encrypted)
function generateServiceFromTemplate(string $template, Application $application)
{
// ray()->clearAll();
$template = Str::of($template);
$network = data_get($application, 'destination.network');
$yaml = Yaml::parse($template);
$services = data_get($yaml, 'services');
$volumes = collect(data_get($yaml, 'volumes', []));
$composeVolumes = collect([]);
$env = collect([]);
$ports = collect([]);
foreach ($services as $serviceName => $service) {
// Some default things
data_set($service, 'restart', RESTART_MODE);
data_set($service, 'container_name', generateApplicationContainerName($application));
$healthcheck = data_get($service, 'healthcheck', []);
if (is_null($healthcheck)) {
$healthcheck = [
'test' => [
'CMD-SHELL',
'exit 0'
],
'interval' => $application->health_check_interval . 's',
'timeout' => $application->health_check_timeout . 's',
'retries' => $application->health_check_retries,
'start_period' => $application->health_check_start_period . 's'
];
data_set($service, 'healthcheck', $healthcheck);
}
// Add volumes to the volumes collection if they don't already exist
$serviceVolumes = collect(data_get($service, 'volumes', []));
if ($serviceVolumes->count() > 0) {
foreach ($serviceVolumes as $volume) {
$volumeName = Str::before($volume, ':');
$volumePath = Str::after($volume, ':');
if (Str::startsWith($volumeName, '/')) {
continue;
}
$volumeExists = $volumes->contains(function ($_, $key) use ($volumeName) {
return $key == $volumeName;
});
if ($volumeExists) {
ray('Volume already exists');
} else {
$composeVolumes->put($volumeName, null);
$volumes->put($volumeName, $volumePath);
}
}
}
// Add networks to the networks collection if they don't already exist
$serviceNetworks = collect(data_get($service, 'networks', []));
$networkExists = $serviceNetworks->contains(function ($_, $key) use ($network) {
return $key == $network;
});
if (is_null($networkExists) || !$networkExists) {
$serviceNetworks->push($network);
}
data_set($service, 'networks', $serviceNetworks->toArray());
data_set($yaml, "services.{$serviceName}", $service);
// Get variables from the service that does not start with SERVICE_*
$serviceVariables = collect(data_get($service, 'environment', []));
foreach ($serviceVariables as $variable) {
$key = Str::before($variable, '=');
$value = Str::after($variable, '=');
if (!Str::startsWith($value, '$SERVICE_') && !Str::startsWith($value, '${SERVICE_') && Str::startsWith($value, '$')) {
if (Str::of($value)->contains(':')) {
$nakedName = replaceVariables(Str::of($value)->before(':'));
$nakedValue = replaceVariables(Str::of($value)->after(':'));
}
if (Str::of($value)->contains('-')) {
$nakedName = replaceVariables(Str::of($value)->before('-'));
$nakedValue = replaceVariables(Str::of($value)->after('-'));
}
if (Str::of($value)->contains('+')) {
$nakedName = replaceVariables(Str::of($value)->before('+'));
$nakedValue = replaceVariables(Str::of($value)->after('+'));
}
if ($nakedValue->startsWith('-')) {
$nakedValue = Str::of($nakedValue)->after('-');
}
if ($nakedValue->startsWith('+')) {
$nakedValue = Str::of($nakedValue)->after('+');
}
if (!$env->contains("{$nakedName->value()}={$nakedValue->value()}")) {
$env->push("$nakedName=$nakedValue");
}
}
}
// Get ports from the service
$servicePorts = collect(data_get($service, 'ports', []));
foreach ($servicePorts as $port) {
$port = Str::of($port)->before(':');
$ports->push($port);
}
}
data_set($yaml, 'networks', [
$network => [
'name'=> $network
],
]);
data_set($yaml, 'volumes', $composeVolumes->toArray());
$compose = Str::of(Yaml::dump($yaml, 10, 2));
// Replace SERVICE_FQDN_* with the actual FQDN
preg_match_all(collectRegex('SERVICE_FQDN_'), $compose, $fqdns);
$fqdns = collect($fqdns)->flatten()->unique()->values();
$generatedFqdns = collect([]);
foreach ($fqdns as $fqdn) {
$generatedFqdns->put("$fqdn", data_get($application, 'fqdn'));
}
// Replace SERVICE_URL_*
preg_match_all(collectRegex('SERVICE_URL_'), $compose, $urls);
$urls = collect($urls)->flatten()->unique()->values();
$generatedUrls = collect([]);
foreach ($urls as $url) {
$generatedUrls->put("$url", data_get($application, 'url'));
}
// Generate SERVICE_USER_*
preg_match_all(collectRegex('SERVICE_USER_'), $compose, $users);
$users = collect($users)->flatten()->unique()->values();
$generatedUsers = collect([]);
foreach ($users as $user) {
$generatedUsers->put("$user", Str::random(10));
}
// Generate SERVICE_PASSWORD_*
preg_match_all(collectRegex('SERVICE_PASSWORD_'), $compose, $passwords);
$passwords = collect($passwords)->flatten()->unique()->values();
$generatedPasswords = collect([]);
foreach ($passwords as $password) {
$generatedPasswords->put("$password", Str::password(symbols: false));
}
// Save .env file
foreach ($generatedFqdns as $key => $value) {
$env->push("$key=$value");
}
foreach ($generatedUrls as $key => $value) {
$env->push("$key=$value");
}
foreach ($generatedUsers as $key => $value) {
$env->push("$key=$value");
}
foreach ($generatedPasswords as $key => $value) {
$env->push("$key=$value");
}
return [
'dockercompose' => $compose,
'yaml' => Yaml::parse($compose),
'envs' => $env,
'volumes' => $volumes,
'ports' => $ports->values(),
];
}
function replaceRegex(?string $name = null)
{
return "/\\\${?{$name}[^}]*}?|\\\${$name}\w+/";
}
function collectRegex(string $name)
{
return "/{$name}\w+/";
}
function replaceVariables($variable)
{
return $variable->replaceFirst('$', '')->replaceFirst('{', '')->replaceLast('}', '');
}

View File

@@ -151,10 +151,12 @@ function get_latest_version_of_coolify(): string
}
}
function generate_random_name(): string
function generate_random_name(?string $cuid = null): string
{
$generator = All::create();
$cuid = new Cuid2(7);
if (is_null($cuid)) {
$cuid = new Cuid2(7);
}
return Str::kebab("{$generator->getName()}-$cuid");
}
function generateSSHKey()
@@ -173,9 +175,11 @@ function formatPrivateKey(string $privateKey)
}
return $privateKey;
}
function generate_application_name(string $git_repository, string $git_branch): string
function generate_application_name(string $git_repository, string $git_branch, ?string $cuid = null): string
{
$cuid = new Cuid2(7);
if (is_null($cuid)) {
$cuid = new Cuid2(7);
}
return Str::kebab("$git_repository:$git_branch-$cuid");
}