wip: services

This commit is contained in:
Andras Bacsai
2023-09-21 17:48:31 +02:00
parent 301469de6b
commit 6b75ff7de4
29 changed files with 632 additions and 427 deletions

View File

@@ -130,13 +130,14 @@ function get_port_from_dockerfile($dockerfile): int
return 80;
}
function defaultLabels($id, $name, $pull_request_id = 0)
function defaultLabels($id, $name, $pull_request_id = 0, string $type = 'application')
{
ray($type);
$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." . $type . "Id=" . $id);
$labels->push("coolify.type=$type");
$labels->push('coolify.name=' . $name);
if ($pull_request_id !== 0) {
$labels->push('coolify.pullRequestId=' . $pull_request_id);

View File

@@ -33,7 +33,6 @@ function remote_process(
throw new \Exception("User is not part of the team that owns this server");
}
}
return resolve(PrepareCoolifyTask::class, [
'remoteProcessArgs' => new CoolifyTaskArgs(
server_uuid: $server->uuid,

View File

@@ -13,188 +13,188 @@ use Illuminate\Support\Str;
# SERVICE_PASSWORD_*: Generated by your application, password (encrypted)
function generateServiceFromTemplate(Service $service)
{
// ray()->clearAll();
$template = data_get($service, 'docker_compose_raw');
$network = data_get($service, 'destination.network');
$yaml = Yaml::parse($template);
// function generateServiceFromTemplate(Service $service)
// {
// // ray()->clearAll();
// $template = data_get($service, 'docker_compose_raw');
// $network = data_get($service, 'destination.network');
// $yaml = Yaml::parse($template);
$services = $service->parse();
$volumes = collect(data_get($yaml, 'volumes', []));
$composeVolumes = collect([]);
$env = collect([]);
$ports = collect([]);
// $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', $container_name);
$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);
}
// 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));
}
// 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', $container_name);
// $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);
// }
// // 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_set($service, 'labels', $labels->toArray());
// }
data_forget($service, 'is_database');
// data_forget($service, 'is_database');
// 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);
// // 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));
// // 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_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'));
}
// // 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_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));
}
// // 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(),
];
}
// // 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)
{

View File

@@ -21,23 +21,29 @@ use Poliander\Cron\CronExpression;
use Visus\Cuid2\Cuid2;
use phpseclib3\Crypt\RSA;
function base_configuration_dir(): string
{
return '/data/coolify';
}
function application_configuration_dir(): string
{
return '/data/coolify/applications';
return base_configuration_dir() . "/applications";
}
function service_configuration_dir(): string
{
return base_configuration_dir() . "/services";
}
function database_configuration_dir(): string
{
return '/data/coolify/databases';
return base_configuration_dir() . "/databases";
}
function database_proxy_dir($uuid): string
{
return "/data/coolify/databases/$uuid/proxy";
return base_configuration_dir() . "/databases/$uuid/proxy";
}
function backup_dir(): string
{
return '/data/coolify/backups';
return base_configuration_dir() . "/backups";
}
function generate_readme_file(string $name, string $updated_at): string