coolify/app/Models/EnvironmentVariable.php

254 lines
8.5 KiB
PHP
Raw Normal View History

2023-05-04 20:29:14 +00:00
<?php
namespace App\Models;
2023-06-05 10:07:55 +00:00
use App\Models\EnvironmentVariable as ModelsEnvironmentVariable;
2023-05-04 20:29:14 +00:00
use Illuminate\Database\Eloquent\Casts\Attribute;
2024-07-09 11:19:21 +00:00
use OpenApi\Attributes as OA;
2023-05-04 20:29:14 +00:00
2024-07-09 11:19:21 +00: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 11:19:21 +00:00
'is_literal' => ['type' => 'boolean'],
'is_multiline' => ['type' => 'boolean'],
'is_preview' => ['type' => 'boolean'],
'is_runtime' => ['type' => 'boolean'],
'is_buildtime' => ['type' => 'boolean'],
2024-07-09 11:19:21 +00:00
'is_shared' => ['type' => 'boolean'],
'is_shown_once' => ['type' => 'boolean'],
'key' => ['type' => 'string'],
'value' => ['type' => 'string'],
'real_value' => ['type' => 'string'],
'version' => ['type' => 'string'],
'created_at' => ['type' => 'string'],
'updated_at' => ['type' => 'string'],
]
)]
class EnvironmentVariable extends BaseModel
2023-05-04 20:29:14 +00:00
{
2023-08-07 20:14:21 +00:00
protected $guarded = [];
2024-06-10 20:43:34 +00:00
2023-05-04 20:29:14 +00:00
protected $casts = [
2023-10-24 13:41:21 +00:00
'key' => 'string',
2023-05-04 20:29:14 +00:00
'value' => 'encrypted',
2024-03-18 10:36:36 +00: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 20:29:14 +00:00
];
2024-06-10 20:43:34 +00:00
protected $appends = ['real_value', 'is_shared', 'is_really_required', 'is_nixpacks', 'is_coolify'];
2023-08-07 20:14:21 +00:00
protected static function booted()
{
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)
->where('resourceable_id', $environment_variable->resourceable_id)
->where('is_preview', true)
->first();
2024-06-10 20:43:34 +00:00
if (! $found) {
$application = Application::find($environment_variable->resourceable_id);
if ($application) {
ModelsEnvironmentVariable::create([
'key' => $environment_variable->key,
'value' => $environment_variable->value,
'is_multiline' => $environment_variable->is_multiline ?? false,
'is_literal' => $environment_variable->is_literal ?? false,
'resourceable_type' => Application::class,
'resourceable_id' => $environment_variable->resourceable_id,
2024-06-10 20:43:34 +00:00
'is_preview' => true,
]);
}
2023-09-08 14:16:59 +00:00
}
}
$environment_variable->update([
'version' => config('constants.coolify.version'),
2024-03-18 10:36:36 +00:00
]);
2023-08-07 20:14:21 +00:00
});
2024-10-16 11:20:26 +00:00
static::saving(function (EnvironmentVariable $environmentVariable) {
$environmentVariable->updateIsShared();
});
2023-08-07 20:14:21 +00:00
}
2024-06-10 20:43:34 +00:00
2023-10-24 13:41:21 +00:00
public function service()
{
2023-09-20 13:42:41 +00:00
return $this->belongsTo(Service::class);
}
2024-06-10 20:43:34 +00:00
protected function value(): Attribute
{
return Attribute::make(
2023-09-20 13:42:41 +00:00
get: fn (?string $value = null) => $this->get_environment_variables($value),
set: fn (?string $value = null) => $this->set_environment_variables($value),
);
}
2024-06-10 20:43:34 +00:00
/**
* Get the parent resourceable model.
*/
public function resourceable()
2024-01-23 16:13:23 +00:00
{
return $this->morphTo();
}
2024-06-10 20:43:34 +00:00
public function resource()
{
return $this->resourceable;
2024-03-15 21:02:37 +00:00
}
2024-06-10 20:43:34 +00:00
2024-03-15 21:02:37 +00:00
public function realValue(): Attribute
{
2024-01-23 16:13:23 +00: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);
if ($this->is_literal || $this->is_multiline) {
$real_value = '\''.$real_value.'\'';
} else {
$real_value = escapeEnvVariables($real_value);
}
return $real_value;
}
2024-01-23 16:13:23 +00:00
);
}
2024-06-10 20:43:34 +00:00
2024-10-11 12:38:22 +00: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 16:13:23 +00: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;
if (str($this->value)->startsWith('{{'.$type) && str($this->value)->endsWith('}}')) {
return true;
}
2024-06-10 20:43:34 +00:00
return false;
2024-01-23 16:13:23 +00:00
}
);
}
2024-06-10 20:43:34 +00:00
2024-03-15 21:02:37 +00:00
private function get_real_environment_variables(?string $environment_variable = null, $resource = null)
2023-05-04 20:29:14 +00:00
{
if ((is_null($environment_variable) && $environment_variable === '') || is_null($resource)) {
2023-09-21 19:30:13 +00:00
return null;
}
2024-01-23 16:13:23 +00:00
$environment_variable = trim($environment_variable);
$sharedEnvsFound = str($environment_variable)->matchAll('/{{(.*?)}}/');
if ($sharedEnvsFound->isEmpty()) {
return $environment_variable;
}
foreach ($sharedEnvsFound as $sharedEnv) {
$type = str($sharedEnv)->match('/(.*?)\./');
2024-06-10 20:43:34 +00:00
if (! collect(SHARED_VARIABLE_TYPES)->contains($type)) {
continue;
2024-01-31 12:40:15 +00:00
}
$variable = str($sharedEnv)->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;
}
$environment_variable_found = SharedEnvironmentVariable::where('type', $type)->where('key', $variable)->where('team_id', $resource->team()->id)->where("{$type}_id", $id)->first();
2024-01-23 16:13:23 +00:00
if ($environment_variable_found) {
$environment_variable = str($environment_variable)->replace("{{{$sharedEnv}}}", $environment_variable_found->value);
2024-01-23 16:13:23 +00:00
}
2023-05-04 20:29:14 +00:00
}
2024-06-10 20:43:34 +00:00
return str($environment_variable)->value();
2023-05-04 20:29:14 +00:00
}
2024-06-10 20:43:34 +00:00
private function get_environment_variables(?string $environment_variable = null): ?string
2024-01-23 16:13:23 +00:00
{
2024-06-10 20:43:34 +00:00
if (! $environment_variable) {
2024-01-23 16:13:23 +00:00
return null;
}
2024-06-10 20:43:34 +00:00
2024-01-23 16:13:23 +00:00
return trim(decrypt($environment_variable));
}
2024-06-10 20:43:34 +00:00
private function set_environment_variables(?string $environment_variable = null): ?string
2023-05-04 20:29:14 +00:00
{
if (is_null($environment_variable) && $environment_variable === '') {
2023-09-20 13:42:41 +00:00
return null;
}
$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('}}')) {
2024-01-23 16:13:23 +00:00
return encrypt((string) str($environment_variable)->replace(' ', ''));
}
2024-06-10 20:43:34 +00:00
2023-09-11 20:29:47 +00:00
return encrypt($environment_variable);
2023-05-04 20:29:14 +00:00
}
protected function key(): Attribute
{
return Attribute::make(
2024-06-26 11:00:36 +00:00
set: fn (string $value) => str($value)->trim()->replace(' ', '_')->value,
);
}
2024-10-16 11:20:26 +00: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 20:29:14 +00:00
}