wip: services

This commit is contained in:
Andras Bacsai
2023-09-20 15:42:41 +02:00
parent a86e971020
commit b4d69a22df
32 changed files with 964 additions and 222 deletions

View File

@@ -10,3 +10,16 @@ const VALID_CRON_STRINGS = [
'yearly' => '0 0 1 1 *',
];
const RESTART_MODE = 'unless-stopped';
const DATABASE_DOCKER_IMAGES = [
'mysql',
'mariadb',
'postgres',
'mongo',
'redis',
'memcached',
'couchdb',
'neo4j',
'influxdb',
'clickhouse'
];

View File

@@ -130,7 +130,64 @@ function get_port_from_dockerfile($dockerfile): int
return 80;
}
function generateLabelsApplication(Application $application, ?ApplicationPreview $preview = null)
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
{
$pull_request_id = data_get($preview, 'pull_request_id', 0);
@@ -139,15 +196,8 @@ function generateLabelsApplication(Application $application, ?ApplicationPreview
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;
}
$labels = collect([]);
$labels = $labels->merge(defaultLabels($appId, $container_name, $pull_request_id));
if ($application->fqdn) {
if ($pull_request_id !== 0) {
$domains = Str::of(data_get($preview, 'fqdn'))->explode(',');
@@ -155,48 +205,48 @@ function generateLabelsApplication(Application $application, ?ApplicationPreview
$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);
$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";
// $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}";
}
// 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";
// $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}";
}
}
// // 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;
return $labels->all();
}

View File

@@ -1,6 +1,8 @@
<?php
use App\Enums\ProxyTypes;
use App\Models\Application;
use App\Models\Service;
use Symfony\Component\Yaml\Yaml;
use Illuminate\Support\Str;
@@ -10,24 +12,31 @@ use Illuminate\Support\Str;
# SERVICE_USER_*: Generated by your application, username (not encrypted)
# SERVICE_PASSWORD_*: Generated by your application, password (encrypted)
function generateServiceFromTemplate(string $template, Application $application)
function generateServiceFromTemplate(Service $service)
{
// ray()->clearAll();
$template = Str::of($template);
$network = data_get($application, 'destination.network');
$template = data_get($service, 'docker_compose_raw');
$network = data_get($service, 'destination.network');
$yaml = Yaml::parse($template);
$services = data_get($yaml, 'services');
$services = $service->parse();
$volumes = collect(data_get($yaml, 'volumes', []));
$composeVolumes = collect([]);
$env = collect([]);
$ports = collect([]);
foreach ($services as $serviceName => $service) {
$container_name = generateApplicationContainerName($application);
$domain = data_get($application, "service_configurations.{$serviceName}.fqdn", null);
if ($domain === '') {
$domain = null;
}
data_forget($service, 'documentation');
// Some default things
data_set($service, 'restart', RESTART_MODE);
data_set($service, 'container_name', generateApplicationContainerName($application));
$healthcheck = data_get($service, 'healthcheck', []);
data_set($service, 'container_name', $container_name);
$healthcheck = data_get($service, 'healthcheck');
if (is_null($healthcheck)) {
$healthcheck = [
'test' => [
@@ -41,6 +50,22 @@ function generateServiceFromTemplate(string $template, Application $application)
];
data_set($service, 'healthcheck', $healthcheck);
}
// Labels
$server = data_get($application, 'destination.server');
if ($server->proxyType() === ProxyTypes::TRAEFIK_V2->value) {
$labels = collect(data_get($service, 'labels', []));
$labels = collect([]);
$labels = $labels->merge(defaultLabels($application->id, $container_name));
if (!data_get($service, 'is_database')) {
if ($domain) {
$labels = $labels->merge(fqdnLabelsForTraefik($domain, $container_name, $application->settings->is_force_https_enabled));
}
}
data_set($service, 'labels', $labels->toArray());
}
data_forget($service, 'is_database');
// Add volumes to the volumes collection if they don't already exist
$serviceVolumes = collect(data_get($service, 'volumes', []));
@@ -76,7 +101,7 @@ function generateServiceFromTemplate(string $template, Application $application)
// 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, '=');
// $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(':')) {
@@ -111,7 +136,7 @@ function generateServiceFromTemplate(string $template, Application $application)
}
data_set($yaml, 'networks', [
$network => [
'name'=> $network
'name' => $network
],
]);
data_set($yaml, 'volumes', $composeVolumes->toArray());