mirror of
https://github.com/coollabsio/coolify.git
synced 2026-11-06 13:10:37 -04:00
feat(env): support variable flag directives in developer view (#12349)
This commit is contained in:
@@ -987,27 +987,12 @@ class All extends Component
|
||||
|
||||
private function formatEnvironmentVariables($variables)
|
||||
{
|
||||
$hideValues = ! $this->canViewEnvironmentValues();
|
||||
$canViewValues = $this->canViewEnvironmentValues();
|
||||
|
||||
return $variables
|
||||
->reject(fn ($item): bool => $this->isProtectedEnvironmentVariable($item->key))
|
||||
->map(function ($item) use ($hideValues) {
|
||||
if ($hideValues) {
|
||||
return "$item->key=(Hidden, only admins can view)";
|
||||
}
|
||||
if ($item->is_shown_once) {
|
||||
return "$item->key=(Locked Secret, delete and add again to change)";
|
||||
}
|
||||
if ($item->is_multiline) {
|
||||
return "$item->key=(Multiline environment variable, edit in normal view)";
|
||||
}
|
||||
$value = formatEnvValueForDevView($item->value);
|
||||
if ($value === null) {
|
||||
return "$item->key=(Value cannot be shown in this view, edit in normal view)";
|
||||
}
|
||||
|
||||
return "$item->key=$value";
|
||||
})->join("\n");
|
||||
->map(fn ($item) => formatEnvVariableForDevView($item, $canViewValues))
|
||||
->join("\n");
|
||||
}
|
||||
|
||||
public function switch()
|
||||
@@ -1022,11 +1007,28 @@ class All extends Component
|
||||
|
||||
public function submit($data = null)
|
||||
{
|
||||
// Parse before the save, so that an invalid "# coolify:" line changes nothing and keeps the edited text.
|
||||
try {
|
||||
$this->authorize('manageEnvironment', $this->resource);
|
||||
$this->ensureEnvironmentVariablesLoaded();
|
||||
if ($data === null) {
|
||||
$this->handleBulkSubmit();
|
||||
$variables = $this->normalizeEnvironmentVariables(parseEnvDevView($this->variables, EnvironmentVariable::DEV_VIEW_FLAG_DEFAULTS));
|
||||
$previewVariables = [];
|
||||
if ($this->showPreview) {
|
||||
try {
|
||||
$previewVariables = $this->normalizeEnvironmentVariables(parseEnvDevView($this->variablesPreview, EnvironmentVariable::DEV_VIEW_FLAG_DEFAULTS));
|
||||
} catch (\InvalidArgumentException $e) {
|
||||
throw new \InvalidArgumentException('Preview deployments: '.$e->getMessage(), previous: $e);
|
||||
}
|
||||
}
|
||||
}
|
||||
} catch (\Throwable $e) {
|
||||
return handleError($e, $this);
|
||||
}
|
||||
|
||||
try {
|
||||
if ($data === null) {
|
||||
$this->handleBulkSubmit($variables, $previewVariables);
|
||||
} else {
|
||||
$this->handleSingleSubmit($data);
|
||||
}
|
||||
@@ -1076,9 +1078,8 @@ class All extends Component
|
||||
}
|
||||
}
|
||||
|
||||
private function handleBulkSubmit()
|
||||
private function handleBulkSubmit(array $variables, array $previewVariables)
|
||||
{
|
||||
$variables = $this->normalizeEnvironmentVariables(parseEnvFormatToArray($this->variables));
|
||||
$changesMade = false;
|
||||
$errorOccurred = false;
|
||||
|
||||
@@ -1097,8 +1098,6 @@ class All extends Component
|
||||
}
|
||||
|
||||
if ($this->showPreview) {
|
||||
$previewVariables = $this->normalizeEnvironmentVariables(parseEnvFormatToArray($this->variablesPreview));
|
||||
|
||||
// Try to delete removed preview variables
|
||||
$deletedPreviewCount = $this->deleteRemovedVariables(true, $previewVariables);
|
||||
if ($deletedPreviewCount > 0) {
|
||||
@@ -1244,7 +1243,10 @@ class All extends Component
|
||||
$changed = true;
|
||||
}
|
||||
|
||||
if ($changed) {
|
||||
// Flags change only when a "# coolify:" line sets them
|
||||
$found->fill($data['settings']);
|
||||
|
||||
if ($changed || $found->isDirty()) {
|
||||
$found->save();
|
||||
$count++;
|
||||
}
|
||||
@@ -1255,6 +1257,7 @@ class All extends Component
|
||||
$environment->value = $value;
|
||||
$environment->comment = $comment; // Set comment from inline comment
|
||||
$environment->is_multiline = false;
|
||||
$environment->fill($data['settings']);
|
||||
$environment->is_preview = $isPreview;
|
||||
$environment->resourceable_id = $this->resource->id;
|
||||
$environment->resourceable_type = $this->resource->getMorphClass();
|
||||
|
||||
@@ -5,6 +5,7 @@ namespace App\Livewire\SharedVariables\Environment;
|
||||
use App\Livewire\Concerns\RemembersCanvasView;
|
||||
use App\Models\Application;
|
||||
use App\Models\Project;
|
||||
use App\Models\SharedEnvironmentVariable;
|
||||
use Illuminate\Foundation\Auth\Access\AuthorizesRequests;
|
||||
use Illuminate\Support\Facades\DB;
|
||||
use Livewire\Component;
|
||||
@@ -83,30 +84,21 @@ class Show extends Component
|
||||
{
|
||||
$canViewValues = auth()->user()?->isAdminOfTeam($this->project->team_id) ?? false;
|
||||
|
||||
return $variables->map(function ($item) use ($canViewValues) {
|
||||
if (! $canViewValues) {
|
||||
return "$item->key=(Hidden, only admins can view)";
|
||||
}
|
||||
if ($item->is_shown_once) {
|
||||
return "$item->key=(Locked Secret, delete and add again to change)";
|
||||
}
|
||||
if ($item->is_multiline) {
|
||||
return "$item->key=(Multiline environment variable, edit in normal view)";
|
||||
}
|
||||
$value = formatEnvValueForDevView($item->value);
|
||||
if ($value === null) {
|
||||
return "$item->key=(Value cannot be shown in this view, edit in normal view)";
|
||||
}
|
||||
|
||||
return "$item->key=$value";
|
||||
})->join("\n");
|
||||
return $variables->map(fn ($item) => formatEnvVariableForDevView($item, $canViewValues))->join("\n");
|
||||
}
|
||||
|
||||
public function submit()
|
||||
{
|
||||
// Parse before the save, so that an invalid "# coolify:" line changes nothing and keeps the edited text.
|
||||
try {
|
||||
$this->authorize('update', $this->environment);
|
||||
$this->handleBulkSubmit();
|
||||
$variables = parseEnvDevView($this->variables, SharedEnvironmentVariable::DEV_VIEW_FLAG_DEFAULTS);
|
||||
} catch (\Throwable $e) {
|
||||
return handleError($e, $this);
|
||||
}
|
||||
|
||||
try {
|
||||
$this->handleBulkSubmit($variables);
|
||||
$this->getDevView();
|
||||
} catch (\Throwable $e) {
|
||||
return handleError($e, $this);
|
||||
@@ -115,9 +107,8 @@ class Show extends Component
|
||||
}
|
||||
}
|
||||
|
||||
private function handleBulkSubmit()
|
||||
private function handleBulkSubmit(array $variables)
|
||||
{
|
||||
$variables = parseEnvFormatToArray($this->variables);
|
||||
$changesMade = false;
|
||||
|
||||
DB::transaction(function () use ($variables, &$changesMade) {
|
||||
@@ -165,6 +156,9 @@ class Show extends Component
|
||||
if (! $found->is_shown_once && ! $found->is_multiline && formatEnvValueForDevView($found->value) !== null) {
|
||||
if ($found->value !== $value) {
|
||||
$found->value = $value;
|
||||
}
|
||||
$found->fill($data['settings']);
|
||||
if ($found->isDirty()) {
|
||||
$found->save();
|
||||
$count++;
|
||||
}
|
||||
@@ -175,6 +169,7 @@ class Show extends Component
|
||||
'value' => $value,
|
||||
'is_multiline' => false,
|
||||
'is_literal' => false,
|
||||
...$data['settings'],
|
||||
'type' => 'environment',
|
||||
'team_id' => $this->project->team_id,
|
||||
]);
|
||||
|
||||
@@ -4,6 +4,7 @@ namespace App\Livewire\SharedVariables\Project;
|
||||
|
||||
use App\Livewire\Concerns\RemembersCanvasView;
|
||||
use App\Models\Project;
|
||||
use App\Models\SharedEnvironmentVariable;
|
||||
use Illuminate\Foundation\Auth\Access\AuthorizesRequests;
|
||||
use Illuminate\Support\Facades\DB;
|
||||
use Livewire\Component;
|
||||
@@ -81,30 +82,21 @@ class Show extends Component
|
||||
{
|
||||
$canViewValues = auth()->user()?->isAdminOfTeam($this->project->team_id) ?? false;
|
||||
|
||||
return $variables->map(function ($item) use ($canViewValues) {
|
||||
if (! $canViewValues) {
|
||||
return "$item->key=(Hidden, only admins can view)";
|
||||
}
|
||||
if ($item->is_shown_once) {
|
||||
return "$item->key=(Locked Secret, delete and add again to change)";
|
||||
}
|
||||
if ($item->is_multiline) {
|
||||
return "$item->key=(Multiline environment variable, edit in normal view)";
|
||||
}
|
||||
$value = formatEnvValueForDevView($item->value);
|
||||
if ($value === null) {
|
||||
return "$item->key=(Value cannot be shown in this view, edit in normal view)";
|
||||
}
|
||||
|
||||
return "$item->key=$value";
|
||||
})->join("\n");
|
||||
return $variables->map(fn ($item) => formatEnvVariableForDevView($item, $canViewValues))->join("\n");
|
||||
}
|
||||
|
||||
public function submit()
|
||||
{
|
||||
// Parse before the save, so that an invalid "# coolify:" line changes nothing and keeps the edited text.
|
||||
try {
|
||||
$this->authorize('update', $this->project);
|
||||
$this->handleBulkSubmit();
|
||||
$variables = parseEnvDevView($this->variables, SharedEnvironmentVariable::DEV_VIEW_FLAG_DEFAULTS);
|
||||
} catch (\Throwable $e) {
|
||||
return handleError($e, $this);
|
||||
}
|
||||
|
||||
try {
|
||||
$this->handleBulkSubmit($variables);
|
||||
$this->getDevView();
|
||||
} catch (\Throwable $e) {
|
||||
return handleError($e, $this);
|
||||
@@ -113,10 +105,8 @@ class Show extends Component
|
||||
}
|
||||
}
|
||||
|
||||
private function handleBulkSubmit()
|
||||
private function handleBulkSubmit(array $variables)
|
||||
{
|
||||
$variables = parseEnvFormatToArray($this->variables);
|
||||
|
||||
$changesMade = DB::transaction(function () use ($variables) {
|
||||
// Delete removed variables
|
||||
$deletedCount = $this->deleteRemovedVariables($variables);
|
||||
@@ -157,6 +147,9 @@ class Show extends Component
|
||||
if (! $found->is_shown_once && ! $found->is_multiline && formatEnvValueForDevView($found->value) !== null) {
|
||||
if ($found->value !== $value) {
|
||||
$found->value = $value;
|
||||
}
|
||||
$found->fill($data['settings']);
|
||||
if ($found->isDirty()) {
|
||||
$found->save();
|
||||
$count++;
|
||||
}
|
||||
@@ -167,6 +160,7 @@ class Show extends Component
|
||||
'value' => $value,
|
||||
'is_multiline' => false,
|
||||
'is_literal' => false,
|
||||
...$data['settings'],
|
||||
'type' => 'project',
|
||||
'team_id' => $this->project->team_id,
|
||||
]);
|
||||
|
||||
@@ -4,6 +4,7 @@ namespace App\Livewire\SharedVariables\Server;
|
||||
|
||||
use App\Livewire\Concerns\RemembersCanvasView;
|
||||
use App\Models\Server;
|
||||
use App\Models\SharedEnvironmentVariable;
|
||||
use Illuminate\Foundation\Auth\Access\AuthorizesRequests;
|
||||
use Illuminate\Support\Facades\DB;
|
||||
use Livewire\Component;
|
||||
@@ -82,30 +83,21 @@ class Show extends Component
|
||||
{
|
||||
$canViewValues = auth()->user()?->isAdminOfTeam($this->server->team_id) ?? false;
|
||||
|
||||
return $variables->map(function ($item) use ($canViewValues) {
|
||||
if (! $canViewValues) {
|
||||
return "$item->key=(Hidden, only admins can view)";
|
||||
}
|
||||
if ($item->is_shown_once) {
|
||||
return "$item->key=(Locked Secret, delete and add again to change)";
|
||||
}
|
||||
if ($item->is_multiline) {
|
||||
return "$item->key=(Multiline environment variable, edit in normal view)";
|
||||
}
|
||||
$value = formatEnvValueForDevView($item->value);
|
||||
if ($value === null) {
|
||||
return "$item->key=(Value cannot be shown in this view, edit in normal view)";
|
||||
}
|
||||
|
||||
return "$item->key=$value";
|
||||
})->join("\n");
|
||||
return $variables->map(fn ($item) => formatEnvVariableForDevView($item, $canViewValues))->join("\n");
|
||||
}
|
||||
|
||||
public function submit()
|
||||
{
|
||||
// Parse before the save, so that an invalid "# coolify:" line changes nothing and keeps the edited text.
|
||||
try {
|
||||
$this->authorize('update', $this->server);
|
||||
$this->handleBulkSubmit();
|
||||
$variables = parseEnvDevView($this->variables, SharedEnvironmentVariable::DEV_VIEW_FLAG_DEFAULTS);
|
||||
} catch (\Throwable $e) {
|
||||
return handleError($e, $this);
|
||||
}
|
||||
|
||||
try {
|
||||
$this->handleBulkSubmit($variables);
|
||||
$this->getDevView();
|
||||
} catch (\Throwable $e) {
|
||||
return handleError($e, $this);
|
||||
@@ -114,10 +106,8 @@ class Show extends Component
|
||||
}
|
||||
}
|
||||
|
||||
private function handleBulkSubmit()
|
||||
private function handleBulkSubmit(array $variables)
|
||||
{
|
||||
$variables = parseEnvFormatToArray($this->variables);
|
||||
|
||||
$changesMade = DB::transaction(function () use ($variables) {
|
||||
// Delete removed variables
|
||||
$deletedCount = $this->deleteRemovedVariables($variables);
|
||||
@@ -167,9 +157,12 @@ class Show extends Component
|
||||
|
||||
if ($found) {
|
||||
if (! $found->is_shown_once && ! $found->is_multiline && formatEnvValueForDevView($found->value) !== null) {
|
||||
if ($found->value !== $value || $found->comment !== $comment) {
|
||||
if ($found->value !== $value) {
|
||||
$found->value = $value;
|
||||
$found->comment = $comment;
|
||||
}
|
||||
$found->comment = $comment;
|
||||
$found->fill($data['settings']);
|
||||
if ($found->isDirty()) {
|
||||
$found->save();
|
||||
$count++;
|
||||
}
|
||||
@@ -181,6 +174,7 @@ class Show extends Component
|
||||
'comment' => $comment,
|
||||
'is_multiline' => false,
|
||||
'is_literal' => false,
|
||||
...$data['settings'],
|
||||
'type' => 'server',
|
||||
'team_id' => $this->server->team_id,
|
||||
]);
|
||||
|
||||
@@ -3,6 +3,7 @@
|
||||
namespace App\Livewire\SharedVariables\Team;
|
||||
|
||||
use App\Livewire\Concerns\RemembersCanvasView;
|
||||
use App\Models\SharedEnvironmentVariable;
|
||||
use App\Models\Team;
|
||||
use Illuminate\Foundation\Auth\Access\AuthorizesRequests;
|
||||
use Illuminate\Support\Facades\DB;
|
||||
@@ -75,30 +76,21 @@ class Index extends Component
|
||||
{
|
||||
$canViewValues = auth()->user()?->isAdminOfTeam($this->team->id) ?? false;
|
||||
|
||||
return $variables->map(function ($item) use ($canViewValues) {
|
||||
if (! $canViewValues) {
|
||||
return "$item->key=(Hidden, only admins can view)";
|
||||
}
|
||||
if ($item->is_shown_once) {
|
||||
return "$item->key=(Locked Secret, delete and add again to change)";
|
||||
}
|
||||
if ($item->is_multiline) {
|
||||
return "$item->key=(Multiline environment variable, edit in normal view)";
|
||||
}
|
||||
$value = formatEnvValueForDevView($item->value);
|
||||
if ($value === null) {
|
||||
return "$item->key=(Value cannot be shown in this view, edit in normal view)";
|
||||
}
|
||||
|
||||
return "$item->key=$value";
|
||||
})->join("\n");
|
||||
return $variables->map(fn ($item) => formatEnvVariableForDevView($item, $canViewValues))->join("\n");
|
||||
}
|
||||
|
||||
public function submit()
|
||||
{
|
||||
// Parse before the save, so that an invalid "# coolify:" line changes nothing and keeps the edited text.
|
||||
try {
|
||||
$this->authorize('update', $this->team);
|
||||
$this->handleBulkSubmit();
|
||||
$variables = parseEnvDevView($this->variables, SharedEnvironmentVariable::DEV_VIEW_FLAG_DEFAULTS);
|
||||
} catch (\Throwable $e) {
|
||||
return handleError($e, $this);
|
||||
}
|
||||
|
||||
try {
|
||||
$this->handleBulkSubmit($variables);
|
||||
$this->getDevView();
|
||||
} catch (\Throwable $e) {
|
||||
return handleError($e, $this);
|
||||
@@ -107,9 +99,8 @@ class Index extends Component
|
||||
}
|
||||
}
|
||||
|
||||
private function handleBulkSubmit()
|
||||
private function handleBulkSubmit(array $variables)
|
||||
{
|
||||
$variables = parseEnvFormatToArray($this->variables);
|
||||
$changesMade = false;
|
||||
|
||||
DB::transaction(function () use ($variables, &$changesMade) {
|
||||
@@ -156,6 +147,9 @@ class Index extends Component
|
||||
if (! $found->is_shown_once && ! $found->is_multiline && formatEnvValueForDevView($found->value) !== null) {
|
||||
if ($found->value !== $value) {
|
||||
$found->value = $value;
|
||||
}
|
||||
$found->fill($data['settings']);
|
||||
if ($found->isDirty()) {
|
||||
$found->save();
|
||||
$count++;
|
||||
}
|
||||
@@ -166,6 +160,7 @@ class Index extends Component
|
||||
'value' => $value,
|
||||
'is_multiline' => false,
|
||||
'is_literal' => false,
|
||||
...$data['settings'],
|
||||
'type' => 'team',
|
||||
'team_id' => $this->team->id,
|
||||
]);
|
||||
|
||||
@@ -42,6 +42,13 @@ class EnvironmentVariable extends BaseModel
|
||||
{
|
||||
use Auditable;
|
||||
|
||||
/**
|
||||
* Flags that a "# coolify:" line of the developer view can set, with their defaults.
|
||||
*
|
||||
* @var array<string, bool>
|
||||
*/
|
||||
public const DEV_VIEW_FLAG_DEFAULTS = ['literal' => false, 'buildtime' => true, 'runtime' => true];
|
||||
|
||||
public const BUILDPACK_CONTROL_VARIABLE_PREFIXES = ['NIXPACKS_', 'RAILPACK_'];
|
||||
|
||||
/**
|
||||
@@ -101,6 +108,7 @@ class EnvironmentVariable extends BaseModel
|
||||
'value' => 'encrypted',
|
||||
'preserve_compose_null' => 'boolean',
|
||||
'is_multiline' => 'boolean',
|
||||
'is_literal' => 'boolean',
|
||||
'is_preview' => 'boolean',
|
||||
'is_runtime' => 'boolean',
|
||||
'is_buildtime' => 'boolean',
|
||||
|
||||
@@ -11,6 +11,14 @@ class SharedEnvironmentVariable extends Model
|
||||
{
|
||||
use Auditable;
|
||||
|
||||
/**
|
||||
* Flags that a "# coolify:" line of the developer view can set, with their defaults.
|
||||
* Shared variables have no buildtime or runtime flags.
|
||||
*
|
||||
* @var array<string, bool>
|
||||
*/
|
||||
public const DEV_VIEW_FLAG_DEFAULTS = ['literal' => false];
|
||||
|
||||
protected $fillable = [
|
||||
// Core identification
|
||||
'key',
|
||||
@@ -40,6 +48,7 @@ class SharedEnvironmentVariable extends Model
|
||||
protected $casts = [
|
||||
'key' => 'string',
|
||||
'value' => 'encrypted',
|
||||
'is_literal' => 'boolean',
|
||||
];
|
||||
|
||||
/**
|
||||
|
||||
Reference in New Issue
Block a user