Files
coolify/app/Models/EnvironmentVariable.php
T

287 lines
9.5 KiB
PHP
Raw Normal View History

2023-05-04 22:29:14 +02:00
<?php
namespace App\Models;
2023-06-05 12:07:55 +02:00
use App\Models\EnvironmentVariable as ModelsEnvironmentVariable;
2023-05-04 22:29:14 +02:00
use Illuminate\Database\Eloquent\Casts\Attribute;
2024-07-09 13:19:21 +02:00
use OpenApi\Attributes as OA;
2023-05-04 22:29:14 +02:00
2024-07-09 13:19:21 +02:00
#[OA\Schema(
description: 'Environment Variable model',
type: 'object',
properties: [
'id' => ['type' => 'integer'],
'uuid' => ['type' => 'string'],
'resourceable_type' => ['type' => 'string'],
'resourceable_id' => ['type' => 'integer'],
2024-07-09 13:19:21 +02:00
'is_literal' => ['type' => 'boolean'],
'is_multiline' => ['type' => 'boolean'],
'is_preview' => ['type' => 'boolean'],
'is_runtime' => ['type' => 'boolean'],
'is_buildtime' => ['type' => 'boolean'],
2024-07-09 13:19:21 +02:00
'is_shared' => ['type' => 'boolean'],
'is_shown_once' => ['type' => 'boolean'],
'key' => ['type' => 'string'],
'value' => ['type' => 'string'],
'real_value' => ['type' => 'string'],
'comment' => ['type' => 'string', 'nullable' => true],
2024-07-09 13:19:21 +02:00
'version' => ['type' => 'string'],
'created_at' => ['type' => 'string'],
'updated_at' => ['type' => 'string'],
]
)]
class EnvironmentVariable extends BaseModel
2023-05-04 22:29:14 +02:00
{
protected $fillable = [
// Core identification
'key',
'value',
'comment',
// Polymorphic relationship
'resourceable_type',
'resourceable_id',
// Boolean flags
'is_preview',
'is_multiline',
'is_literal',
'is_runtime',
'is_buildtime',
'is_shown_once',
'is_shared',
'is_required',
// Metadata
'version',
'order',
];
2024-06-10 20:43:34 +00:00
2023-05-04 22:29:14 +02:00
protected $casts = [
2023-10-24 15:41:21 +02:00
'key' => 'string',
2023-05-04 22:29:14 +02:00
'value' => 'encrypted',
2024-03-18 11:36:36 +01:00
'is_multiline' => 'boolean',
'is_preview' => 'boolean',
'is_runtime' => 'boolean',
'is_buildtime' => 'boolean',
2024-06-10 20:43:34 +00:00
'version' => 'string',
'resourceable_type' => 'string',
'resourceable_id' => 'integer',
2023-05-04 22:29:14 +02:00
];
2024-06-10 20:43:34 +00:00
protected $appends = ['real_value', 'is_shared', 'is_really_required', 'is_nixpacks', 'is_coolify'];
2023-08-08 11:51:36 +02:00
2023-08-07 22:14:21 +02:00
protected static function booted()
{
2025-01-07 15:31:43 +01:00
static::created(function (EnvironmentVariable $environment_variable) {
if ($environment_variable->resourceable_type === Application::class && ! $environment_variable->is_preview) {
$found = ModelsEnvironmentVariable::where('key', $environment_variable->key)
->where('resourceable_type', Application::class)
2025-01-07 15:31:43 +01:00
->where('resourceable_id', $environment_variable->resourceable_id)
->where('is_preview', true)
->first();
2024-06-10 20:43:34 +00:00
if (! $found) {
2025-01-07 15:31:43 +01:00
$application = Application::find($environment_variable->resourceable_id);
if ($application) {
2024-04-02 15:40:19 +02:00
ModelsEnvironmentVariable::create([
2025-01-07 15:31:43 +01:00
'key' => $environment_variable->key,
'value' => $environment_variable->value,
'is_multiline' => $environment_variable->is_multiline ?? false,
'is_literal' => $environment_variable->is_literal ?? false,
'is_runtime' => $environment_variable->is_runtime ?? false,
'is_buildtime' => $environment_variable->is_buildtime ?? false,
'comment' => $environment_variable->comment,
'resourceable_type' => Application::class,
2025-01-07 15:31:43 +01:00
'resourceable_id' => $environment_variable->resourceable_id,
2024-06-10 20:43:34 +00:00
'is_preview' => true,
2024-04-02 15:40:19 +02:00
]);
}
2023-09-08 16:16:59 +02:00
}
2023-08-08 11:51:36 +02:00
}
2025-01-07 15:31:43 +01:00
$environment_variable->update([
'version' => config('constants.coolify.version'),
2024-03-18 11:36:36 +01:00
]);
2023-08-07 22:14:21 +02:00
});
2024-10-16 13:20:26 +02:00
static::saving(function (EnvironmentVariable $environmentVariable) {
$environmentVariable->updateIsShared();
});
2023-08-07 22:14:21 +02:00
}
2024-06-10 20:43:34 +00:00
2023-10-24 15:41:21 +02:00
public function service()
{
2023-09-20 15:42:41 +02:00
return $this->belongsTo(Service::class);
}
2024-06-10 20:43:34 +00:00
2023-08-08 11:51:36 +02:00
protected function value(): Attribute
{
return Attribute::make(
2023-09-20 15:42:41 +02:00
get: fn (?string $value = null) => $this->get_environment_variables($value),
set: fn (?string $value = null) => $this->set_environment_variables($value),
2023-08-08 11:51:36 +02:00
);
}
2024-06-10 20:43:34 +00:00
/**
* Get the parent resourceable model.
*/
public function resourceable()
2024-01-23 17:13:23 +01:00
{
return $this->morphTo();
}
2024-06-10 20:43:34 +00:00
public function resource()
{
return $this->resourceable;
2024-03-15 22:02:37 +01:00
}
2024-06-10 20:43:34 +00:00
2024-03-15 22:02:37 +01:00
public function realValue(): Attribute
{
2024-01-23 17:13:23 +01:00
return Attribute::make(
get: function () {
if (! $this->relationLoaded('resourceable')) {
$this->load('resourceable');
}
$resource = $this->resourceable;
if (! $resource) {
return null;
}
2024-06-10 20:43:34 +00:00
$real_value = $this->get_real_environment_variables($this->value, $resource);
// Skip escaping for valid JSON objects/arrays to prevent quote corruption (see #6160)
if (json_validate($real_value) && (str_starts_with($real_value, '{') || str_starts_with($real_value, '['))) {
return $real_value;
}
if ($this->is_literal || $this->is_multiline) {
$real_value = '\''.$real_value.'\'';
} else {
$real_value = escapeEnvVariables($real_value);
}
return $real_value;
}
2024-01-23 17:13:23 +01:00
);
}
2024-06-10 20:43:34 +00:00
2024-10-11 14:38:22 +02:00
protected function isReallyRequired(): Attribute
{
return Attribute::make(
get: fn () => $this->is_required && str($this->real_value)->isEmpty(),
);
}
protected function isNixpacks(): Attribute
{
return Attribute::make(
get: function () {
if (str($this->key)->startsWith('NIXPACKS_')) {
return true;
}
return false;
}
);
}
protected function isCoolify(): Attribute
{
return Attribute::make(
get: function () {
if (str($this->key)->startsWith('SERVICE_')) {
return true;
}
return false;
}
);
}
2024-01-23 17:13:23 +01:00
protected function isShared(): Attribute
{
return Attribute::make(
get: function () {
2024-06-10 20:43:34 +00:00
$type = str($this->value)->after('{{')->before('.')->value;
2025-01-07 15:31:43 +01:00
if (str($this->value)->startsWith('{{'.$type) && str($this->value)->endsWith('}}')) {
return true;
}
2024-06-10 20:43:34 +00:00
2025-01-07 15:31:43 +01:00
return false;
2024-01-23 17:13:23 +01:00
}
);
}
2024-06-10 20:43:34 +00:00
2024-03-15 22:02:37 +01:00
private function get_real_environment_variables(?string $environment_variable = null, $resource = null)
2023-05-04 22:29:14 +02:00
{
2025-01-07 15:31:43 +01:00
if ((is_null($environment_variable) && $environment_variable === '') || is_null($resource)) {
2023-09-21 21:30:13 +02:00
return null;
}
2024-01-23 17:13:23 +01:00
$environment_variable = trim($environment_variable);
$sharedEnvsFound = str($environment_variable)->matchAll('/{{(.*?)}}/');
if ($sharedEnvsFound->isEmpty()) {
return $environment_variable;
}
2025-01-07 15:31:43 +01:00
foreach ($sharedEnvsFound as $sharedEnv) {
$type = str($sharedEnv)->trim()->match('/(.*?)\./');
2024-06-10 20:43:34 +00:00
if (! collect(SHARED_VARIABLE_TYPES)->contains($type)) {
continue;
2024-01-31 13:40:15 +01:00
}
$variable = str($sharedEnv)->trim()->match('/\.(.*)/');
if ($type->value() === 'environment') {
$id = $resource->environment->id;
} elseif ($type->value() === 'project') {
$id = $resource->environment->project->id;
} elseif ($type->value() === 'team') {
$id = $resource->team()->id;
}
if (is_null($id)) {
continue;
}
2025-01-07 15:31:43 +01:00
$environment_variable_found = SharedEnvironmentVariable::where('type', $type)->where('key', $variable)->where('team_id', $resource->team()->id)->where("{$type}_id", $id)->first();
2024-01-23 17:13:23 +01:00
if ($environment_variable_found) {
2025-01-07 15:31:43 +01:00
$environment_variable = str($environment_variable)->replace("{{{$sharedEnv}}}", $environment_variable_found->value);
2024-01-23 17:13:23 +01:00
}
2023-05-04 22:29:14 +02:00
}
2024-06-10 20:43:34 +00:00
return str($environment_variable)->value();
2023-05-04 22:29:14 +02:00
}
2024-06-10 20:43:34 +00:00
private function get_environment_variables(?string $environment_variable = null): ?string
2024-01-23 17:13:23 +01:00
{
2024-06-10 20:43:34 +00:00
if (! $environment_variable) {
2024-01-23 17:13:23 +01:00
return null;
}
2024-06-10 20:43:34 +00:00
2024-01-23 17:13:23 +01:00
return trim(decrypt($environment_variable));
}
2023-08-08 11:51:36 +02:00
2024-06-10 20:43:34 +00:00
private function set_environment_variables(?string $environment_variable = null): ?string
2023-05-04 22:29:14 +02:00
{
2024-10-31 15:23:19 +01:00
if (is_null($environment_variable) && $environment_variable === '') {
2023-09-20 15:42:41 +02:00
return null;
}
2023-05-05 10:51:58 +02:00
$environment_variable = trim($environment_variable);
2024-06-10 20:43:34 +00:00
$type = str($environment_variable)->after('{{')->before('.')->value;
if (str($environment_variable)->startsWith('{{'.$type) && str($environment_variable)->endsWith('}}')) {
return encrypt($environment_variable);
2024-01-23 17:13:23 +01:00
}
2024-06-10 20:43:34 +00:00
2023-09-11 22:29:47 +02:00
return encrypt($environment_variable);
2023-05-04 22:29:14 +02:00
}
2023-08-08 11:51:36 +02:00
2023-05-05 10:51:58 +02:00
protected function key(): Attribute
{
return Attribute::make(
2024-06-26 13:00:36 +02:00
set: fn (string $value) => str($value)->trim()->replace(' ', '_')->value,
2023-05-05 10:51:58 +02:00
);
}
2024-10-16 13:20:26 +02:00
protected function updateIsShared(): void
{
$type = str($this->value)->after('{{')->before('.')->value;
$isShared = str($this->value)->startsWith('{{'.$type) && str($this->value)->endsWith('}}');
$this->is_shared = $isShared;
}
2023-05-04 22:29:14 +02:00
}