refactor: Update environment variable handling in StartClickhouse.php and ApplicationDeploymentJob.php

This commit is contained in:
Andras Bacsai 2024-08-29 15:11:54 +02:00
parent 0df1163718
commit 301a3596e8
3 changed files with 52 additions and 29 deletions

View File

@ -21,7 +21,7 @@ public function handle(StandaloneClickhouse $database)
$this->database = $database;
$container_name = $this->database->uuid;
$this->configuration_dir = database_configuration_dir() . '/' . $container_name;
$this->configuration_dir = database_configuration_dir().'/'.$container_name;
$this->commands = [
"echo 'Starting {$database->name}.'",
@ -75,7 +75,7 @@ public function handle(StandaloneClickhouse $database)
],
],
];
if (!is_null($this->database->limits_cpuset)) {
if (! is_null($this->database->limits_cpuset)) {
data_set($docker_compose, "services.{$container_name}.cpuset", $this->database->limits_cpuset);
}
if ($this->database->destination->server->isLogDrainEnabled() && $this->database->isLogDrainEnabled()) {
@ -118,10 +118,10 @@ private function generate_local_persistent_volumes()
$local_persistent_volumes = [];
foreach ($this->database->persistentStorages as $persistentStorage) {
if ($persistentStorage->host_path !== '' && $persistentStorage->host_path !== null) {
$local_persistent_volumes[] = $persistentStorage->host_path . ':' . $persistentStorage->mount_path;
$local_persistent_volumes[] = $persistentStorage->host_path.':'.$persistentStorage->mount_path;
} else {
$volume_name = $persistentStorage->name;
$local_persistent_volumes[] = $volume_name . ':' . $persistentStorage->mount_path;
$local_persistent_volumes[] = $volume_name.':'.$persistentStorage->mount_path;
}
}
@ -152,11 +152,11 @@ private function generate_environment_variables()
$environment_variables->push("$env->key=$env->real_value");
}
if ($environment_variables->filter(fn($env) => str($env)->contains('CLICKHOUSE_ADMIN_USER'))->isEmpty()) {
if ($environment_variables->filter(fn ($env) => str($env)->contains('CLICKHOUSE_ADMIN_USER'))->isEmpty()) {
$environment_variables->push("CLICKHOUSE_ADMIN_USER={$this->database->clickhouse_admin_user}");
}
if ($environment_variables->filter(fn($env) => str($env)->contains('CLICKHOUSE_ADMIN_PASSWORD'))->isEmpty()) {
if ($environment_variables->filter(fn ($env) => str($env)->contains('CLICKHOUSE_ADMIN_PASSWORD'))->isEmpty()) {
$environment_variables->push("CLICKHOUSE_ADMIN_PASSWORD={$this->database->clickhouse_admin_password}");
}

View File

@ -922,7 +922,7 @@ private function save_environment_variables()
}
}
add_coolify_default_environment_variables($this->application, $environment, $this->application->environment_variables_preview);
add_coolify_default_environment_variables($this->application, $envs, $this->application->environment_variables_preview);
foreach ($sorted_environment_variables_preview as $env) {
$real_value = $env->real_value;

View File

@ -2103,10 +2103,10 @@ function parseDockerComposeFile(Service|Application $resource, bool $isNew = fal
$parsedServiceVariables->put('COOLIFY_APP_NAME', $resource->name);
}
if (! $parsedServiceVariables->has('COOLIFY_SERVER_IP')) {
$parsedServiceVariables->put("COOLIFY_SERVER_IP", $resource->destination->server->ip);
$parsedServiceVariables->put('COOLIFY_SERVER_IP', $resource->destination->server->ip);
}
if (! $parsedServiceVariables->has('COOLIFY_ENVIRONMENT_NAME')) {
$parsedServiceVariables->put("COOLIFY_ENVIRONMENT_NAME", $resource->environment->name);
$parsedServiceVariables->put('COOLIFY_ENVIRONMENT_NAME', $resource->environment->name);
}
if (! $parsedServiceVariables->has('COOLIFY_PROJECT_NAME')) {
$parsedServiceVariables->put('COOLIFY_PROJECT_NAME', $resource->project()->name);
@ -3429,6 +3429,7 @@ function newParser(Application|Service $resource, int $pull_request_id = 0, ?int
});
$environment->put('COOLIFY_FQDN', $urls->implode(','));
}
add_coolify_default_environment_variables($resource, $environment, $resource->environment_variables);
$serviceLabels = $labels->merge($defaultLabels);
if (! $isDatabase && $fqdns?->count() > 0) {
@ -3555,7 +3556,8 @@ function newParser(Application|Service $resource, int $pull_request_id = 0, ?int
return $topLevel;
}
function generate_fluentd_configuration() : array {
function generate_fluentd_configuration(): array
{
return [
'driver' => 'fluentd',
'options' => [
@ -3564,37 +3566,58 @@ function generate_fluentd_configuration() : array {
'fluentd-sub-second-precision' => 'true',
// env vars are used in the LogDrain configurations
'env' => 'COOLIFY_APP_NAME,COOLIFY_PROJECT_NAME,COOLIFY_SERVER_IP,COOLIFY_ENVIRONMENT_NAME',
]
],
];
}
/**
* This method adds the default environment variables to the resource.
* - COOLIFY_APP_NAME
* - COOLIFY_PROJECT_NAME
* - COOLIFY_SERVER_IP
* - COOLIFY_ENVIRONMENT_NAME
*
* Theses variables are added in place to the $where_to_add array.
*
* @param StandaloneRedis|StandalonePostgresql|StandaloneMongodb|StandaloneMysql|StandaloneMariadb|StandaloneKeydb|StandaloneDragonfly|StandaloneClickhouse|Application $resource
* @param Collection $where_to_add
* @param Collection|null $where_to_check
*
*/
function add_coolify_default_environment_variables(StandaloneRedis|StandalonePostgresql|StandaloneMongodb|StandaloneMysql|StandaloneMariadb|StandaloneKeydb|StandaloneDragonfly|StandaloneClickhouse|Application $resource, Collection &$where_to_add, ?Collection $where_to_check = null) {
* This method adds the default environment variables to the resource.
* - COOLIFY_APP_NAME
* - COOLIFY_PROJECT_NAME
* - COOLIFY_SERVER_IP
* - COOLIFY_ENVIRONMENT_NAME
*
* Theses variables are added in place to the $where_to_add array.
*/
function add_coolify_default_environment_variables(StandaloneRedis|StandalonePostgresql|StandaloneMongodb|StandaloneMysql|StandaloneMariadb|StandaloneKeydb|StandaloneDragonfly|StandaloneClickhouse|Application|Service $resource, Collection &$where_to_add, ?Collection $where_to_check = null)
{
if ($where_to_check != null && $where_to_check->where('key', 'COOLIFY_APP_NAME')->isEmpty()) {
$where_to_add->push("COOLIFY_APP_NAME={$resource->name}");
if ($resource instanceof Application && $resource->build_pack === 'dockercompose') {
$where_to_add->put('COOLIFY_APP_NAME', $resource->name);
} elseif ($resource instanceof Service) {
$where_to_add->put('COOLIFY_APP_NAME', $resource->name);
} else {
$where_to_add->push("COOLIFY_APP_NAME={$resource->name}");
}
}
if ($where_to_check != null && $where_to_check->where('key', 'COOLIFY_SERVER_IP')->isEmpty()) {
$where_to_add->push("COOLIFY_SERVER_IP={$resource->destination->server->ip}");
if ($resource instanceof Application && $resource->build_pack === 'dockercompose') {
$where_to_add->put('COOLIFY_SERVER_IP', $resource->destination->server->ip);
} elseif ($resource instanceof Service) {
$where_to_add->put('COOLIFY_SERVER_IP', $resource->server->ip);
} else {
$where_to_add->push("COOLIFY_SERVER_IP={$resource->destination->server->ip}");
}
}
if ($where_to_check != null && $where_to_check->where('key', 'COOLIFY_ENVIRONMENT_NAME')->isEmpty()) {
$where_to_add->push("COOLIFY_ENVIRONMENT_NAME={$resource->environment->name}");
if ($resource instanceof Application && $resource->build_pack === 'dockercompose') {
$where_to_add->put('COOLIFY_ENVIRONMENT_NAME', $resource->environment->name);
} elseif ($resource instanceof Service) {
$where_to_add->put('COOLIFY_ENVIRONMENT_NAME', $resource->environment->name);
} else {
$where_to_add->push("COOLIFY_ENVIRONMENT_NAME={$resource->environment->name}");
}
}
if ($where_to_check != null && $where_to_check->where('key', 'COOLIFY_PROJECT_NAME')->isEmpty()) {
$where_to_add->push("COOLIFY_PROJECT_NAME={$resource->project()->name}");
if ($resource instanceof Application && $resource->build_pack === 'dockercompose') {
$where_to_add->put('COOLIFY_PROJECT_NAME', $resource->project()->name);
} elseif ($resource instanceof Service) {
$where_to_add->put('COOLIFY_PROJECT_NAME', $resource->project()->name);
} else {
$where_to_add->push("COOLIFY_PROJECT_NAME={$resource->project()->name}");
}
}
}
function convertComposeEnvironmentToArray($environment)
{