coolify/app/Models/EnvironmentVariable.php

227 lines
7.9 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;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Str;
use Symfony\Component\Yaml\Yaml;
2023-05-04 22:29:14 +02:00
class EnvironmentVariable extends Model
{
2023-08-07 22:14:21 +02:00
protected $guarded = [];
2024-06-10 22:43:34 +02: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',
'is_build_time' => 'boolean',
2024-03-18 11:36:36 +01:00
'is_multiline' => 'boolean',
'is_preview' => 'boolean',
2024-06-10 22:43:34 +02:00
'version' => 'string',
2023-05-04 22:29:14 +02:00
];
2024-06-10 22:43:34 +02:00
2024-01-23 17:13:23 +01:00
protected $appends = ['real_value', 'is_shared'];
2023-08-07 22:14:21 +02:00
protected static function booted()
{
2024-03-18 11:36:36 +01:00
static::created(function (EnvironmentVariable $environment_variable) {
2024-06-10 22:43:34 +02:00
if ($environment_variable->application_id && ! $environment_variable->is_preview) {
2023-09-11 22:29:47 +02:00
$found = ModelsEnvironmentVariable::where('key', $environment_variable->key)->where('application_id', $environment_variable->application_id)->where('is_preview', true)->first();
2024-06-10 22:43:34 +02:00
if (! $found) {
$application = Application::find($environment_variable->application_id);
if ($application->build_pack !== 'dockerfile') {
ModelsEnvironmentVariable::create([
'key' => $environment_variable->key,
'value' => $environment_variable->value,
'is_build_time' => $environment_variable->is_build_time,
'is_multiline' => $environment_variable->is_multiline ?? false,
'application_id' => $environment_variable->application_id,
2024-06-10 22:43:34 +02:00
'is_preview' => true,
]);
}
2023-09-08 16:16:59 +02:00
}
}
2024-03-18 11:36:36 +01:00
$environment_variable->update([
2024-06-10 22:43:34 +02:00
'version' => config('version'),
2024-03-18 11:36:36 +01:00
]);
2023-08-07 22:14:21 +02:00
});
}
2024-06-10 22:43:34 +02: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 22:43:34 +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),
);
}
2024-06-10 22:43:34 +02:00
2024-03-15 22:02:37 +01:00
public function resource()
2024-01-23 17:13:23 +01:00
{
$resource = null;
if ($this->application_id) {
$resource = Application::find($this->application_id);
2024-06-10 22:43:34 +02:00
} elseif ($this->service_id) {
$resource = Service::find($this->service_id);
2024-06-10 22:43:34 +02:00
} elseif ($this->database_id) {
2024-04-10 15:00:46 +02:00
$resource = getResourceByUuid($this->parameters['database_uuid'], data_get(auth()->user()->currentTeam(), 'id'));
}
2024-06-10 22:43:34 +02:00
2024-03-15 22:02:37 +01:00
return $resource;
}
2024-06-10 22:43:34 +02:00
2024-03-15 22:02:37 +01:00
public function realValue(): Attribute
{
$resource = $this->resource();
2024-06-10 22:43:34 +02:00
2024-01-23 17:13:23 +01:00
return Attribute::make(
get: function () use ($resource) {
2024-03-15 22:02:37 +01:00
$env = $this->get_real_environment_variables($this->value, $resource);
2024-06-10 22:43:34 +02:00
2024-03-15 22:02:37 +01:00
return data_get($env, 'value', $env);
if (is_string($env)) {
return $env;
}
2024-06-10 22:43:34 +02:00
2024-03-15 22:02:37 +01:00
return $env->value;
}
2024-01-23 17:13:23 +01:00
);
}
2024-06-10 22:43:34 +02:00
protected function isFoundInCompose(): Attribute
{
return Attribute::make(
get: function () {
2024-06-10 22:43:34 +02:00
if (! $this->application_id) {
return true;
}
$found_in_compose = false;
$found_in_args = false;
$resource = $this->resource();
$compose = data_get($resource, 'docker_compose_raw');
2024-06-10 22:43:34 +02:00
if (! $compose) {
return true;
}
$yaml = Yaml::parse($compose);
$services = collect(data_get($yaml, 'services'));
if ($services->isEmpty()) {
return false;
}
foreach ($services as $service) {
$environments = collect(data_get($service, 'environment'));
$args = collect(data_get($service, 'build.args'));
if ($environments->isEmpty() && $args->isEmpty()) {
$found_in_compose = false;
break;
}
$found_in_compose = $environments->contains(function ($item) {
if (str($item)->contains('=')) {
$item = str($item)->before('=');
}
2024-06-10 22:43:34 +02:00
return strpos($item, $this->key) !== false;
});
if ($found_in_compose) {
break;
}
$found_in_args = $args->contains(function ($item) {
if (str($item)->contains('=')) {
$item = str($item)->before('=');
}
2024-06-10 22:43:34 +02:00
return strpos($item, $this->key) !== false;
});
if ($found_in_args) {
break;
}
}
2024-06-10 22:43:34 +02:00
return $found_in_compose || $found_in_args;
}
);
}
2024-06-10 22:43:34 +02:00
2024-01-23 17:13:23 +01:00
protected function isShared(): Attribute
{
return Attribute::make(
get: function () {
2024-06-10 22:43:34 +02:00
$type = str($this->value)->after('{{')->before('.')->value;
if (str($this->value)->startsWith('{{'.$type) && str($this->value)->endsWith('}}')) {
2024-01-23 17:13:23 +01:00
return true;
}
2024-06-10 22:43:34 +02:00
2024-01-23 17:13:23 +01:00
return false;
}
);
}
2024-06-10 22:43:34 +02: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
{
2024-03-18 11:49:26 +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);
2024-06-10 22:43:34 +02:00
$type = str($environment_variable)->after('{{')->before('.')->value;
if (str($environment_variable)->startsWith('{{'.$type) && str($environment_variable)->endsWith('}}')) {
2024-01-23 17:13:23 +01:00
$variable = Str::after($environment_variable, "{$type}.");
2023-09-11 22:29:47 +02:00
$variable = Str::before($variable, '}}');
$variable = Str::of($variable)->trim()->value;
2024-06-10 22:43:34 +02:00
if (! collect(SHARED_VARIABLE_TYPES)->contains($type)) {
2024-01-31 13:40:15 +01:00
return $variable;
}
if ($type === 'environment') {
$id = $resource->environment->id;
2024-06-10 22:43:34 +02:00
} elseif ($type === 'project') {
$id = $resource->environment->project->id;
} else {
$id = $resource->team()->id;
}
2024-06-10 22:43:34 +02: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) {
2024-03-15 22:02:37 +01:00
return $environment_variable_found;
2024-01-23 17:13:23 +01:00
}
2023-05-04 22:29:14 +02:00
}
2024-06-10 22:43:34 +02:00
2023-09-11 22:29:47 +02:00
return $environment_variable;
2023-05-04 22:29:14 +02:00
}
2024-06-10 22:43:34 +02:00
private function get_environment_variables(?string $environment_variable = null): ?string
2024-01-23 17:13:23 +01:00
{
2024-06-10 22:43:34 +02:00
if (! $environment_variable) {
2024-01-23 17:13:23 +01:00
return null;
}
2024-06-10 22:43:34 +02:00
2024-01-23 17:13:23 +01:00
return trim(decrypt($environment_variable));
}
2024-06-10 22:43:34 +02:00
private function set_environment_variables(?string $environment_variable = null): ?string
2023-05-04 22:29:14 +02:00
{
2023-09-20 15:42:41 +02:00
if (is_null($environment_variable) && $environment_variable == '') {
return null;
}
$environment_variable = trim($environment_variable);
2024-06-10 22:43:34 +02:00
$type = str($environment_variable)->after('{{')->before('.')->value;
if (str($environment_variable)->startsWith('{{'.$type) && str($environment_variable)->endsWith('}}')) {
2024-01-23 17:13:23 +01:00
return encrypt((string) str($environment_variable)->replace(' ', ''));
}
2024-06-10 22:43:34 +02:00
2023-09-11 22:29:47 +02:00
return encrypt($environment_variable);
2023-05-04 22:29:14 +02:00
}
protected function key(): Attribute
{
return Attribute::make(
2023-08-11 20:48:52 +02:00
set: fn (string $value) => Str::of($value)->trim(),
);
}
2023-05-04 22:29:14 +02:00
}