From f6d649307c58c70288f320aadea9e77abc781d58 Mon Sep 17 00:00:00 2001 From: Vahor Date: Wed, 28 Aug 2024 13:30:59 +0200 Subject: [PATCH] test add_coolif_default_environment_variables --- app/Actions/Database/StartClickhouse.php | 15 +----------- app/Actions/Database/StartKeydb.php | 15 +----------- app/Actions/Database/StartMariadb.php | 15 +----------- app/Actions/Database/StartMongodb.php | 15 +----------- app/Actions/Database/StartMysql.php | 15 +----------- app/Actions/Database/StartPostgresql.php | 15 +----------- app/Actions/Database/StartRedis.php | 15 +----------- app/Jobs/ApplicationDeploymentJob.php | 28 ++--------------------- app/Models/Application.php | 14 +----------- bootstrap/helpers/shared.php | 29 ++++++++++++++++++++++++ 10 files changed, 39 insertions(+), 137 deletions(-) diff --git a/app/Actions/Database/StartClickhouse.php b/app/Actions/Database/StartClickhouse.php index 7316bcd32..c8d787cb2 100644 --- a/app/Actions/Database/StartClickhouse.php +++ b/app/Actions/Database/StartClickhouse.php @@ -160,20 +160,7 @@ private function generate_environment_variables() $environment_variables->push("CLICKHOUSE_ADMIN_PASSWORD={$this->database->clickhouse_admin_password}"); } - // TODO: move this in a shared function - if ($environment_variables->where('key', 'COOLIFY_APP_NAME')->isEmpty()) { - $environment_variables->push("COOLIFY_APP_NAME={$this->database->name}"); - } - if ($environment_variables->where('key', 'COOLIFY_SERVER_IP')->isEmpty()) { - $environment_variables->push("COOLIFY_SERVER_IP={$this->database->destination->server->ip}"); - } - if ($environment_variables->where('key', 'COOLIFY_ENVIRONMENT_NAME')->isEmpty()) { - $environment_variables->push("COOLIFY_ENVIRONMENT_NAME={$this->database->environment->name}"); - } - if ($environment_variables->where('key', 'COOLIFY_PROJECT_NAME')->isEmpty()) { - $environment_variables->push("COOLIFY_PROJECT_NAME={$this->database->project()->name}"); - } - + add_coolify_default_environment_variables($this->database, $environment_variables, $environment_variables); return $environment_variables->all(); } diff --git a/app/Actions/Database/StartKeydb.php b/app/Actions/Database/StartKeydb.php index 5b49b42e7..9290efc7c 100644 --- a/app/Actions/Database/StartKeydb.php +++ b/app/Actions/Database/StartKeydb.php @@ -163,20 +163,7 @@ private function generate_environment_variables() $environment_variables->push("REDIS_PASSWORD={$this->database->keydb_password}"); } - // TODO: move this in a shared function - if ($environment_variables->where('key', 'COOLIFY_APP_NAME')->isEmpty()) { - $environment_variables->push("COOLIFY_APP_NAME={$this->database->name}"); - } - if ($environment_variables->where('key', 'COOLIFY_SERVER_IP')->isEmpty()) { - $environment_variables->push("COOLIFY_SERVER_IP={$this->database->destination->server->ip}"); - } - if ($environment_variables->where('key', 'COOLIFY_ENVIRONMENT_NAME')->isEmpty()) { - $environment_variables->push("COOLIFY_ENVIRONMENT_NAME={$this->database->environment->name}"); - } - if ($environment_variables->where('key', 'COOLIFY_PROJECT_NAME')->isEmpty()) { - $environment_variables->push("COOLIFY_PROJECT_NAME={$this->database->project()->name}"); - } - + add_coolify_default_environment_variables($this->database, $environment_variables, $environment_variables); return $environment_variables->all(); } diff --git a/app/Actions/Database/StartMariadb.php b/app/Actions/Database/StartMariadb.php index 045ae4014..f37a5e361 100644 --- a/app/Actions/Database/StartMariadb.php +++ b/app/Actions/Database/StartMariadb.php @@ -169,20 +169,7 @@ private function generate_environment_variables() $environment_variables->push("MARIADB_PASSWORD={$this->database->mariadb_password}"); } - // TODO: move this in a shared function - if ($environment_variables->where('key', 'COOLIFY_APP_NAME')->isEmpty()) { - $environment_variables->push("COOLIFY_APP_NAME={$this->database->name}"); - } - if ($environment_variables->where('key', 'COOLIFY_SERVER_IP')->isEmpty()) { - $environment_variables->push("COOLIFY_SERVER_IP={$this->database->destination->server->ip}"); - } - if ($environment_variables->where('key', 'COOLIFY_ENVIRONMENT_NAME')->isEmpty()) { - $environment_variables->push("COOLIFY_ENVIRONMENT_NAME={$this->database->environment->name}"); - } - if ($environment_variables->where('key', 'COOLIFY_PROJECT_NAME')->isEmpty()) { - $environment_variables->push("COOLIFY_PROJECT_NAME={$this->database->project()->name}"); - } - + add_coolify_default_environment_variables($this->database, $environment_variables, $environment_variables); return $environment_variables->all(); } diff --git a/app/Actions/Database/StartMongodb.php b/app/Actions/Database/StartMongodb.php index 154510836..42fc8f348 100644 --- a/app/Actions/Database/StartMongodb.php +++ b/app/Actions/Database/StartMongodb.php @@ -182,20 +182,7 @@ private function generate_environment_variables() $environment_variables->push("MONGO_INITDB_DATABASE={$this->database->mongo_initdb_database}"); } - // TODO: move this in a shared function - if ($environment_variables->where('key', 'COOLIFY_APP_NAME')->isEmpty()) { - $environment_variables->push("COOLIFY_APP_NAME={$this->database->name}"); - } - if ($environment_variables->where('key', 'COOLIFY_SERVER_IP')->isEmpty()) { - $environment_variables->push("COOLIFY_SERVER_IP={$this->database->destination->server->ip}"); - } - if ($environment_variables->where('key', 'COOLIFY_ENVIRONMENT_NAME')->isEmpty()) { - $environment_variables->push("COOLIFY_ENVIRONMENT_NAME={$this->database->environment->name}"); - } - if ($environment_variables->where('key', 'COOLIFY_PROJECT_NAME')->isEmpty()) { - $environment_variables->push("COOLIFY_PROJECT_NAME={$this->database->project()->name}"); - } - + add_coolify_default_environment_variables($this->database, $environment_variables, $environment_variables); return $environment_variables->all(); } diff --git a/app/Actions/Database/StartMysql.php b/app/Actions/Database/StartMysql.php index 0ffc93578..2043342fe 100644 --- a/app/Actions/Database/StartMysql.php +++ b/app/Actions/Database/StartMysql.php @@ -169,20 +169,7 @@ private function generate_environment_variables() $environment_variables->push("MYSQL_PASSWORD={$this->database->mysql_password}"); } - // TODO: move this in a shared function - if ($environment_variables->where('key', 'COOLIFY_APP_NAME')->isEmpty()) { - $environment_variables->push("COOLIFY_APP_NAME={$this->database->name}"); - } - if ($environment_variables->where('key', 'COOLIFY_SERVER_IP')->isEmpty()) { - $environment_variables->push("COOLIFY_SERVER_IP={$this->database->destination->server->ip}"); - } - if ($environment_variables->where('key', 'COOLIFY_ENVIRONMENT_NAME')->isEmpty()) { - $environment_variables->push("COOLIFY_ENVIRONMENT_NAME={$this->database->environment->name}"); - } - if ($environment_variables->where('key', 'COOLIFY_PROJECT_NAME')->isEmpty()) { - $environment_variables->push("COOLIFY_PROJECT_NAME={$this->database->project()->name}"); - } - + add_coolify_default_environment_variables($this->database, $environment_variables, $environment_variables); return $environment_variables->all(); } diff --git a/app/Actions/Database/StartPostgresql.php b/app/Actions/Database/StartPostgresql.php index f719a0fed..bc37fd5cf 100644 --- a/app/Actions/Database/StartPostgresql.php +++ b/app/Actions/Database/StartPostgresql.php @@ -191,20 +191,7 @@ private function generate_environment_variables() $environment_variables->push("POSTGRES_DB={$this->database->postgres_db}"); } - // TODO: move this in a shared function - if ($environment_variables->where('key', 'COOLIFY_APP_NAME')->isEmpty()) { - $environment_variables->push("COOLIFY_APP_NAME={$this->database->name}"); - } - if ($environment_variables->where('key', 'COOLIFY_SERVER_IP')->isEmpty()) { - $environment_variables->push("COOLIFY_SERVER_IP={$this->database->destination->server->ip}"); - } - if ($environment_variables->where('key', 'COOLIFY_ENVIRONMENT_NAME')->isEmpty()) { - $environment_variables->push("COOLIFY_ENVIRONMENT_NAME={$this->database->environment->name}"); - } - if ($environment_variables->where('key', 'COOLIFY_PROJECT_NAME')->isEmpty()) { - $environment_variables->push("COOLIFY_PROJECT_NAME={$this->database->project()->name}"); - } - + add_coolify_default_environment_variables($this->database, $environment_variables, $environment_variables); return $environment_variables->all(); } diff --git a/app/Actions/Database/StartRedis.php b/app/Actions/Database/StartRedis.php index be33a4834..b837414d6 100644 --- a/app/Actions/Database/StartRedis.php +++ b/app/Actions/Database/StartRedis.php @@ -168,20 +168,7 @@ private function generate_environment_variables() $environment_variables->push("REDIS_PASSWORD={$this->database->redis_password}"); } - // TODO: move this in a shared function - if ($environment_variables->where('key', 'COOLIFY_APP_NAME')->isEmpty()) { - $environment_variables->push("COOLIFY_APP_NAME={$this->database->name}"); - } - if ($environment_variables->where('key', 'COOLIFY_SERVER_IP')->isEmpty()) { - $environment_variables->push("COOLIFY_SERVER_IP={$this->database->destination->server->ip}"); - } - if ($environment_variables->where('key', 'COOLIFY_ENVIRONMENT_NAME')->isEmpty()) { - $environment_variables->push("COOLIFY_ENVIRONMENT_NAME={$this->database->environment->name}"); - } - if ($environment_variables->where('key', 'COOLIFY_PROJECT_NAME')->isEmpty()) { - $environment_variables->push("COOLIFY_PROJECT_NAME={$this->database->project()->name}"); - } - + add_coolify_default_environment_variables($this->database, $environment_variables, $environment_variables); return $environment_variables->all(); } diff --git a/app/Jobs/ApplicationDeploymentJob.php b/app/Jobs/ApplicationDeploymentJob.php index 0cddf1d54..725b84c70 100644 --- a/app/Jobs/ApplicationDeploymentJob.php +++ b/app/Jobs/ApplicationDeploymentJob.php @@ -906,19 +906,7 @@ private function save_environment_variables() } } - // TODO: move this in a shared function - if ($this->application->environment_variables_preview->where('key', 'COOLIFY_APP_NAME')->isEmpty()) { - $envs->push("COOLIFY_APP_NAME={$this->application->name}"); - } - if ($this->application->environment_variables_preview->where('key', 'COOLIFY_SERVER_IP')->isEmpty()) { - $envs->push("COOLIFY_SERVER_IP={$this->application->destination->server->ip}"); - } - if ($this->application->environment_variables_preview->where('key', 'COOLIFY_ENVIRONMENT_NAME')->isEmpty()) { - $envs->push("COOLIFY_ENVIRONMENT_NAME={$this->application->environment->name}"); - } - if ($this->application->environment_variables_preview->where('key', 'COOLIFY_PROJECT_NAME')->isEmpty()) { - $envs->push("COOLIFY_PROJECT_NAME={$this->application->project()->name}"); - } + add_coolify_default_environment_variables($this->application, $environment, $this->application->environment_variables_preview); foreach ($sorted_environment_variables_preview as $env) { $real_value = $env->real_value; @@ -969,19 +957,7 @@ private function save_environment_variables() } } - // TODO: move this in a shared function - if ($this->application->environment_variables->where('key', 'COOLIFY_APP_NAME')->isEmpty()) { - $envs->push("COOLIFY_APP_NAME={$this->application->name}"); - } - if ($this->application->environment_variables->where('key', 'COOLIFY_SERVER_IP')->isEmpty()) { - $envs->push("COOLIFY_SERVER_IP={$this->application->destination->server->ip}"); - } - if ($this->application->environment_variables->where('key', 'COOLIFY_ENVIRONMENT_NAME')->isEmpty()) { - $envs->push("COOLIFY_ENVIRONMENT_NAME={$this->application->environment->name}"); - } - if ($this->application->environment_variables->where('key', 'COOLIFY_PROJECT_NAME')->isEmpty()) { - $envs->push("COOLIFY_PROJECT_NAME={$this->application->project()->name}"); - } + add_coolify_default_environment_variables($this->application, $envs, $this->application->environment_variables); foreach ($sorted_environment_variables as $env) { $real_value = $env->real_value; diff --git a/app/Models/Application.php b/app/Models/Application.php index 604568df4..a24301abc 100644 --- a/app/Models/Application.php +++ b/app/Models/Application.php @@ -1464,19 +1464,7 @@ public function newParser(int $pull_request_id = 0, ?int $preview_id = null) if ($this->environment_variables->where('key', 'COOLIFY_CONTAINER_NAME')->isEmpty()) { $environment->put('COOLIFY_CONTAINER_NAME', $containerName); } - // TODO: move this in a shared function - if ($this->environment_variables->where('key', 'COOLIFY_APP_NAME')->isEmpty()) { - $environment->push("COOLIFY_APP_NAME={$this->database->name}"); - } - if ($this->environment->where('key', 'COOLIFY_SERVER_IP')->isEmpty()) { - $environment->push("COOLIFY_SERVER_IP={$this->database->destination->server->ip}"); - } - if ($this->environment->where('key', 'COOLIFY_ENVIRONMENT_NAME')->isEmpty()) { - $environment->push("COOLIFY_ENVIRONMENT_NAME={$this->database->environment->name}"); - } - if ($this->environment->where('key', 'COOLIFY_PROJECT_NAME')->isEmpty()) { - $environment->push("COOLIFY_PROJECT_NAME={$this->database->project()->name}"); - } + add_coolify_default_environment_variables($this->database, $environment, $this->environment_variables); // Remove SERVICE_FQDN and SERVICE_URL from environment $environment = $environment->filter(function ($value, $key) { diff --git a/bootstrap/helpers/shared.php b/bootstrap/helpers/shared.php index a48c9754e..99f2b9d8d 100644 --- a/bootstrap/helpers/shared.php +++ b/bootstrap/helpers/shared.php @@ -3346,3 +3346,32 @@ function generate_fluentd_configuration() : array { ] ]; } + +/** +* 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) { + if ($where_to_check != null && $where_to_check->where('key', 'COOLIFY_APP_NAME')->isEmpty()) { + $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 ($where_to_check != null && $where_to_check->where('key', 'COOLIFY_ENVIRONMENT_NAME')->isEmpty()) { + $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}"); + } +}