From 301a3596e8ffd6f3098ec7de7a23949bf93d00d7 Mon Sep 17 00:00:00 2001 From: Andras Bacsai Date: Thu, 29 Aug 2024 15:11:54 +0200 Subject: [PATCH] refactor: Update environment variable handling in StartClickhouse.php and ApplicationDeploymentJob.php --- app/Actions/Database/StartClickhouse.php | 12 ++--- app/Jobs/ApplicationDeploymentJob.php | 2 +- bootstrap/helpers/shared.php | 67 ++++++++++++++++-------- 3 files changed, 52 insertions(+), 29 deletions(-) diff --git a/app/Actions/Database/StartClickhouse.php b/app/Actions/Database/StartClickhouse.php index c8d787cb2..6d0063749 100644 --- a/app/Actions/Database/StartClickhouse.php +++ b/app/Actions/Database/StartClickhouse.php @@ -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}"); } diff --git a/app/Jobs/ApplicationDeploymentJob.php b/app/Jobs/ApplicationDeploymentJob.php index ac3fcdc25..7819613fb 100644 --- a/app/Jobs/ApplicationDeploymentJob.php +++ b/app/Jobs/ApplicationDeploymentJob.php @@ -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; diff --git a/bootstrap/helpers/shared.php b/bootstrap/helpers/shared.php index 52b5c8d9d..ee1ceb049 100644 --- a/bootstrap/helpers/shared.php +++ b/bootstrap/helpers/shared.php @@ -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) {