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

@@ -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());