diff --git a/app/Actions/Database/StartClickhouse.php b/app/Actions/Database/StartClickhouse.php index 7cac97dd4..c8d787cb2 100644 --- a/app/Actions/Database/StartClickhouse.php +++ b/app/Actions/Database/StartClickhouse.php @@ -79,14 +79,7 @@ class StartClickhouse data_set($docker_compose, "services.{$container_name}.cpuset", $this->database->limits_cpuset); } if ($this->database->destination->server->isLogDrainEnabled() && $this->database->isLogDrainEnabled()) { - $docker_compose['services'][$container_name]['logging'] = [ - 'driver' => 'fluentd', - 'options' => [ - 'fluentd-address' => 'tcp://127.0.0.1:24224', - 'fluentd-async' => 'true', - 'fluentd-sub-second-precision' => 'true', - ], - ]; + $docker_compose['services'][$container_name]['logging'] = generate_fluentd_configuration(); } if (count($this->database->ports_mappings_array) > 0) { $docker_compose['services'][$container_name]['ports'] = $this->database->ports_mappings_array; @@ -167,6 +160,8 @@ class StartClickhouse $environment_variables->push("CLICKHOUSE_ADMIN_PASSWORD={$this->database->clickhouse_admin_password}"); } + add_coolify_default_environment_variables($this->database, $environment_variables, $environment_variables); + return $environment_variables->all(); } } diff --git a/app/Actions/Database/StartDragonfly.php b/app/Actions/Database/StartDragonfly.php index b71e12e8e..621834df0 100644 --- a/app/Actions/Database/StartDragonfly.php +++ b/app/Actions/Database/StartDragonfly.php @@ -79,14 +79,7 @@ class StartDragonfly data_set($docker_compose, "services.{$container_name}.cpuset", $this->database->limits_cpuset); } if ($this->database->destination->server->isLogDrainEnabled() && $this->database->isLogDrainEnabled()) { - $docker_compose['services'][$container_name]['logging'] = [ - 'driver' => 'fluentd', - 'options' => [ - 'fluentd-address' => 'tcp://127.0.0.1:24224', - 'fluentd-async' => 'true', - 'fluentd-sub-second-precision' => 'true', - ], - ]; + $docker_compose['services'][$container_name]['logging'] = generate_fluentd_configuration(); } if (count($this->database->ports_mappings_array) > 0) { $docker_compose['services'][$container_name]['ports'] = $this->database->ports_mappings_array; diff --git a/app/Actions/Database/StartKeydb.php b/app/Actions/Database/StartKeydb.php index 46a6a760e..9290efc7c 100644 --- a/app/Actions/Database/StartKeydb.php +++ b/app/Actions/Database/StartKeydb.php @@ -78,14 +78,7 @@ class StartKeydb data_set($docker_compose, "services.{$container_name}.cpuset", $this->database->limits_cpuset); } if ($this->database->destination->server->isLogDrainEnabled() && $this->database->isLogDrainEnabled()) { - $docker_compose['services'][$container_name]['logging'] = [ - 'driver' => 'fluentd', - 'options' => [ - 'fluentd-address' => 'tcp://127.0.0.1:24224', - 'fluentd-async' => 'true', - 'fluentd-sub-second-precision' => 'true', - ], - ]; + $docker_compose['services'][$container_name]['logging'] = generate_fluentd_configuration(); } if (count($this->database->ports_mappings_array) > 0) { $docker_compose['services'][$container_name]['ports'] = $this->database->ports_mappings_array; @@ -170,6 +163,8 @@ class StartKeydb $environment_variables->push("REDIS_PASSWORD={$this->database->keydb_password}"); } + 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 4868a5d33..f37a5e361 100644 --- a/app/Actions/Database/StartMariadb.php +++ b/app/Actions/Database/StartMariadb.php @@ -73,14 +73,7 @@ class StartMariadb data_set($docker_compose, "services.{$container_name}.cpuset", $this->database->limits_cpuset); } if ($this->database->destination->server->isLogDrainEnabled() && $this->database->isLogDrainEnabled()) { - $docker_compose['services'][$container_name]['logging'] = [ - 'driver' => 'fluentd', - 'options' => [ - 'fluentd-address' => 'tcp://127.0.0.1:24224', - 'fluentd-async' => 'true', - 'fluentd-sub-second-precision' => 'true', - ], - ]; + $docker_compose['services'][$container_name]['logging'] = generate_fluentd_configuration(); } if (count($this->database->ports_mappings_array) > 0) { $docker_compose['services'][$container_name]['ports'] = $this->database->ports_mappings_array; @@ -176,6 +169,8 @@ class StartMariadb $environment_variables->push("MARIADB_PASSWORD={$this->database->mariadb_password}"); } + 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 b606cfe9e..42fc8f348 100644 --- a/app/Actions/Database/StartMongodb.php +++ b/app/Actions/Database/StartMongodb.php @@ -81,14 +81,7 @@ class StartMongodb data_set($docker_compose, "services.{$container_name}.cpuset", $this->database->limits_cpuset); } if ($this->database->destination->server->isLogDrainEnabled() && $this->database->isLogDrainEnabled()) { - $docker_compose['services'][$container_name]['logging'] = [ - 'driver' => 'fluentd', - 'options' => [ - 'fluentd-address' => 'tcp://127.0.0.1:24224', - 'fluentd-async' => 'true', - 'fluentd-sub-second-precision' => 'true', - ], - ]; + $docker_compose['services'][$container_name]['logging'] = generate_fluentd_configuration(); } if (count($this->database->ports_mappings_array) > 0) { $docker_compose['services'][$container_name]['ports'] = $this->database->ports_mappings_array; @@ -189,6 +182,8 @@ class StartMongodb $environment_variables->push("MONGO_INITDB_DATABASE={$this->database->mongo_initdb_database}"); } + 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 003a853f8..2043342fe 100644 --- a/app/Actions/Database/StartMysql.php +++ b/app/Actions/Database/StartMysql.php @@ -73,14 +73,7 @@ class StartMysql data_set($docker_compose, "services.{$container_name}.cpuset", $this->database->limits_cpuset); } if ($this->database->destination->server->isLogDrainEnabled() && $this->database->isLogDrainEnabled()) { - $docker_compose['services'][$container_name]['logging'] = [ - 'driver' => 'fluentd', - 'options' => [ - 'fluentd-address' => 'tcp://127.0.0.1:24224', - 'fluentd-async' => 'true', - 'fluentd-sub-second-precision' => 'true', - ], - ]; + $docker_compose['services'][$container_name]['logging'] = generate_fluentd_configuration(); } if (count($this->database->ports_mappings_array) > 0) { $docker_compose['services'][$container_name]['ports'] = $this->database->ports_mappings_array; @@ -176,6 +169,8 @@ class StartMysql $environment_variables->push("MYSQL_PASSWORD={$this->database->mysql_password}"); } + 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 5f13471b6..bc37fd5cf 100644 --- a/app/Actions/Database/StartPostgresql.php +++ b/app/Actions/Database/StartPostgresql.php @@ -81,14 +81,7 @@ class StartPostgresql data_set($docker_compose, "services.{$container_name}.cpuset", $this->database->limits_cpuset); } if ($this->database->destination->server->isLogDrainEnabled() && $this->database->isLogDrainEnabled()) { - $docker_compose['services'][$container_name]['logging'] = [ - 'driver' => 'fluentd', - 'options' => [ - 'fluentd-address' => 'tcp://127.0.0.1:24224', - 'fluentd-async' => 'true', - 'fluentd-sub-second-precision' => 'true', - ], - ]; + $docker_compose['services'][$container_name]['logging'] = generate_fluentd_configuration(); } if (count($this->database->ports_mappings_array) > 0) { $docker_compose['services'][$container_name]['ports'] = $this->database->ports_mappings_array; @@ -198,6 +191,8 @@ class StartPostgresql $environment_variables->push("POSTGRES_DB={$this->database->postgres_db}"); } + 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 3e1181522..b837414d6 100644 --- a/app/Actions/Database/StartRedis.php +++ b/app/Actions/Database/StartRedis.php @@ -82,14 +82,7 @@ class StartRedis data_set($docker_compose, "services.{$container_name}.cpuset", $this->database->limits_cpuset); } if ($this->database->destination->server->isLogDrainEnabled() && $this->database->isLogDrainEnabled()) { - $docker_compose['services'][$container_name]['logging'] = [ - 'driver' => 'fluentd', - 'options' => [ - 'fluentd-address' => 'tcp://127.0.0.1:24224', - 'fluentd-async' => 'true', - 'fluentd-sub-second-precision' => 'true', - ], - ]; + $docker_compose['services'][$container_name]['logging'] = generate_fluentd_configuration(); } if (count($this->database->ports_mappings_array) > 0) { $docker_compose['services'][$container_name]['ports'] = $this->database->ports_mappings_array; @@ -175,6 +168,8 @@ class StartRedis $environment_variables->push("REDIS_PASSWORD={$this->database->redis_password}"); } + add_coolify_default_environment_variables($this->database, $environment_variables, $environment_variables); + return $environment_variables->all(); } diff --git a/app/Actions/Server/InstallLogDrain.php b/app/Actions/Server/InstallLogDrain.php index 034d89fe7..9b6741211 100644 --- a/app/Actions/Server/InstallLogDrain.php +++ b/app/Actions/Server/InstallLogDrain.php @@ -47,7 +47,11 @@ class InstallLogDrain [FILTER] Name modify Match * - Set server_name {$server->name} + Set coolify.server_name {$server->name} + Rename COOLIFY_APP_NAME coolify.app_name + Rename COOLIFY_PROJECT_NAME coolify.project_name + Rename COOLIFY_SERVER_IP coolify.server_ip + Rename COOLIFY_ENVIRONMENT_NAME coolify.environment_name [OUTPUT] Name nrlogs Match * @@ -98,7 +102,11 @@ class InstallLogDrain [FILTER] Name modify Match * - Set server_name {$server->name} + Set coolify.server_name {$server->name} + Rename COOLIFY_APP_NAME coolify.app_name + Rename COOLIFY_PROJECT_NAME coolify.project_name + Rename COOLIFY_SERVER_IP coolify.server_ip + Rename COOLIFY_ENVIRONMENT_NAME coolify.environment_name [OUTPUT] Name http Match * diff --git a/app/Jobs/ApplicationDeploymentJob.php b/app/Jobs/ApplicationDeploymentJob.php index 828c36447..ac3fcdc25 100644 --- a/app/Jobs/ApplicationDeploymentJob.php +++ b/app/Jobs/ApplicationDeploymentJob.php @@ -921,6 +921,9 @@ class ApplicationDeploymentJob implements ShouldBeEncrypted, ShouldQueue $envs->push("COOLIFY_CONTAINER_NAME={$this->container_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; if ($env->version === '4.0.0-beta.239') { @@ -977,6 +980,9 @@ class ApplicationDeploymentJob implements ShouldBeEncrypted, ShouldQueue $envs->push("COOLIFY_CONTAINER_NAME={$this->container_name}"); } } + + add_coolify_default_environment_variables($this->application, $envs, $this->application->environment_variables); + foreach ($sorted_environment_variables as $env) { $real_value = $env->real_value; if ($env->version === '4.0.0-beta.239') { @@ -1752,14 +1758,7 @@ class ApplicationDeploymentJob implements ShouldBeEncrypted, ShouldQueue $docker_compose['services'][$this->container_name]['labels'] = $labels; } if ($this->server->isLogDrainEnabled() && $this->application->isLogDrainEnabled()) { - $docker_compose['services'][$this->container_name]['logging'] = [ - 'driver' => 'fluentd', - 'options' => [ - 'fluentd-address' => 'tcp://127.0.0.1:24224', - 'fluentd-async' => 'true', - 'fluentd-sub-second-precision' => 'true', - ], - ]; + $docker_compose['services'][$this->container_name]['logging'] = generate_fluentd_configuration(); } if ($this->application->settings->is_gpu_enabled) { $docker_compose['services'][$this->container_name]['deploy']['resources']['reservations']['devices'] = [ diff --git a/bootstrap/helpers/shared.php b/bootstrap/helpers/shared.php index 0031ce365..52b5c8d9d 100644 --- a/bootstrap/helpers/shared.php +++ b/bootstrap/helpers/shared.php @@ -2049,14 +2049,7 @@ function parseDockerComposeFile(Service|Application $resource, bool $isNew = fal } } if ($resource->server->isLogDrainEnabled() && $savedService->isLogDrainEnabled()) { - data_set($service, 'logging', [ - 'driver' => 'fluentd', - 'options' => [ - 'fluentd-address' => 'tcp://127.0.0.1:24224', - 'fluentd-async' => 'true', - 'fluentd-sub-second-precision' => 'true', - ], - ]); + data_set($service, 'logging', generate_fluentd_configuration()); } if ($serviceLabels->count() > 0) { if ($resource->is_container_label_escape_enabled) { @@ -2104,6 +2097,21 @@ function parseDockerComposeFile(Service|Application $resource, bool $isNew = fal } } $parsedServiceVariables->put('COOLIFY_CONTAINER_NAME', "$serviceName-{$resource->uuid}"); + + // TODO: move this in a shared function + if (! $parsedServiceVariables->has('COOLIFY_APP_NAME')) { + $parsedServiceVariables->put('COOLIFY_APP_NAME', $resource->name); + } + if (! $parsedServiceVariables->has('COOLIFY_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); + } + if (! $parsedServiceVariables->has('COOLIFY_PROJECT_NAME')) { + $parsedServiceVariables->put('COOLIFY_PROJECT_NAME', $resource->project()->name); + } + $parsedServiceVariables = $parsedServiceVariables->map(function ($value, $key) use ($envs_from_coolify) { if (! str($value)->startsWith('$')) { $found_env = $envs_from_coolify->where('key', $key)->first(); @@ -2812,14 +2820,7 @@ function parseDockerComposeFile(Service|Application $resource, bool $isNew = fal $serviceLabels = $serviceLabels->merge($defaultLabels); if ($server->isLogDrainEnabled() && $resource->isLogDrainEnabled()) { - data_set($service, 'logging', [ - 'driver' => 'fluentd', - 'options' => [ - 'fluentd-address' => 'tcp://127.0.0.1:24224', - 'fluentd-async' => 'true', - 'fluentd-sub-second-precision' => 'true', - ], - ]); + data_set($service, 'logging', generate_fluentd_configuration()); } if ($serviceLabels->count() > 0) { if ($resource->settings->is_container_label_escape_enabled) { @@ -2923,14 +2924,7 @@ function newParser(Application|Service $resource, int $pull_request_id = 0, ?int $logging = data_get($service, 'logging'); if ($server->isLogDrainEnabled() && $resource->isLogDrainEnabled()) { - $logging = [ - 'driver' => 'fluentd', - 'options' => [ - 'fluentd-address' => 'tcp://127.0.0.1:24224', - 'fluentd-async' => 'true', - 'fluentd-sub-second-precision' => 'true', - ], - ]; + $logging = generate_fluentd_configuration(); } $volumes = collect(data_get($service, 'volumes', [])); $networks = collect(data_get($service, 'networks', [])); @@ -3561,6 +3555,47 @@ function newParser(Application|Service $resource, int $pull_request_id = 0, ?int return $topLevel; } +function generate_fluentd_configuration() : array { + return [ + 'driver' => 'fluentd', + 'options' => [ + 'fluentd-address' => 'tcp://127.0.0.1:24224', + 'fluentd-async' => 'true', + '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) { + 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}"); + } + function convertComposeEnvironmentToArray($environment) { $convertedServiceVariables = collect([]); @@ -3590,4 +3625,5 @@ function convertComposeEnvironmentToArray($environment) } return $convertedServiceVariables; + }