Skip to content

Commit

Permalink
feat: shared environments
Browse files Browse the repository at this point in the history
  • Loading branch information
andrasbacsai committed Jan 23, 2024
1 parent abcc004 commit fb478c7
Show file tree
Hide file tree
Showing 42 changed files with 495 additions and 78 deletions.
2 changes: 1 addition & 1 deletion app/Actions/Database/StartMariadb.php
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ private function generate_environment_variables()
{
$environment_variables = collect();
foreach ($this->database->runtime_environment_variables as $env) {
$environment_variables->push("$env->key=$env->value");
$environment_variables->push("$env->key=$env->real_value");
}

if ($environment_variables->filter(fn ($env) => Str::of($env)->contains('MARIADB_ROOT_PASSWORD'))->isEmpty()) {
Expand Down
2 changes: 1 addition & 1 deletion app/Actions/Database/StartMongodb.php
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,7 @@ private function generate_environment_variables()
{
$environment_variables = collect();
foreach ($this->database->runtime_environment_variables as $env) {
$environment_variables->push("$env->key=$env->value");
$environment_variables->push("$env->key=$env->real_value");
}

if ($environment_variables->filter(fn ($env) => Str::of($env)->contains('MONGO_INITDB_ROOT_USERNAME'))->isEmpty()) {
Expand Down
2 changes: 1 addition & 1 deletion app/Actions/Database/StartMysql.php
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ private function generate_environment_variables()
{
$environment_variables = collect();
foreach ($this->database->runtime_environment_variables as $env) {
$environment_variables->push("$env->key=$env->value");
$environment_variables->push("$env->key=$env->real_value");
}

if ($environment_variables->filter(fn ($env) => Str::of($env)->contains('MYSQL_ROOT_PASSWORD'))->isEmpty()) {
Expand Down
2 changes: 1 addition & 1 deletion app/Actions/Database/StartPostgresql.php
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,7 @@ private function generate_environment_variables()
ray('Generate Environment Variables')->green();
ray($this->database->runtime_environment_variables)->green();
foreach ($this->database->runtime_environment_variables as $env) {
$environment_variables->push("$env->key=$env->value");
$environment_variables->push("$env->key=$env->real_value");
}

if ($environment_variables->filter(fn ($env) => Str::of($env)->contains('POSTGRES_USER'))->isEmpty()) {
Expand Down
2 changes: 1 addition & 1 deletion app/Actions/Database/StartRedis.php
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,7 @@ private function generate_environment_variables()
{
$environment_variables = collect();
foreach ($this->database->runtime_environment_variables as $env) {
$environment_variables->push("$env->key=$env->value");
$environment_variables->push("$env->key=$env->real_value");
}

if ($environment_variables->filter(fn ($env) => Str::of($env)->contains('REDIS_PASSWORD'))->isEmpty()) {
Expand Down
31 changes: 14 additions & 17 deletions app/Jobs/ApplicationDeploymentJob.php
Original file line number Diff line number Diff line change
Expand Up @@ -417,11 +417,11 @@ private function save_environment_variables()
$envs = collect([]);
if ($this->pull_request_id !== 0) {
foreach ($this->application->environment_variables_preview as $env) {
$envs->push($env->key . '=' . $env->value);
$envs->push($env->key . '=' . $env->real_value);
}
} else {
foreach ($this->application->environment_variables as $env) {
$envs->push($env->key . '=' . $env->value);
$envs->push($env->key . '=' . $env->real_value);
}
}
$envs_base64 = base64_encode($envs->implode("\n"));
Expand Down Expand Up @@ -929,11 +929,11 @@ private function generate_nixpacks_env_variables()
$this->env_nixpacks_args = collect([]);
if ($this->pull_request_id === 0) {
foreach ($this->application->nixpacks_environment_variables as $env) {
$this->env_nixpacks_args->push("--env {$env->key}={$env->value}");
$this->env_nixpacks_args->push("--env {$env->key}={$env->real_value}");
}
} else {
foreach ($this->application->nixpacks_environment_variables_preview as $env) {
$this->env_nixpacks_args->push("--env {$env->key}={$env->value}");
$this->env_nixpacks_args->push("--env {$env->key}={$env->real_value}");
}
}

Expand All @@ -944,11 +944,11 @@ private function generate_env_variables()
$this->env_args = collect([]);
if ($this->pull_request_id === 0) {
foreach ($this->application->build_environment_variables as $env) {
$this->env_args->put($env->key, $env->value);
$this->env_args->put($env->key, $env->real_value);
}
} else {
foreach ($this->application->build_environment_variables_preview as $env) {
$this->env_args->put($env->key, $env->value);
$this->env_args->put($env->key, $env->real_value);
}
}
$this->env_args->put('SOURCE_COMMIT', $this->commit);
Expand Down Expand Up @@ -1159,22 +1159,19 @@ private function generate_local_persistent_volumes_only_volume_names()
private function generate_environment_variables($ports)
{
$environment_variables = collect();
// ray('Generate Environment Variables')->green();
if ($this->pull_request_id === 0) {
// ray($this->application->runtime_environment_variables)->green();
foreach ($this->application->runtime_environment_variables as $env) {
$environment_variables->push("$env->key=$env->value");
$environment_variables->push("$env->key=$env->real_value");
}
foreach ($this->application->nixpacks_environment_variables as $env) {
$environment_variables->push("$env->key=$env->value");
$environment_variables->push("$env->key=$env->real_value");
}
} else {
// ray($this->application->runtime_environment_variables_preview)->green();
foreach ($this->application->runtime_environment_variables_preview as $env) {
$environment_variables->push("$env->key=$env->value");
$environment_variables->push("$env->key=$env->real_value");
}
foreach ($this->application->nixpacks_environment_variables_preview as $env) {
$environment_variables->push("$env->key=$env->value");
$environment_variables->push("$env->key=$env->real_value");
}
}
// Add PORT if not exists, use the first port as default
Expand Down Expand Up @@ -1457,12 +1454,12 @@ private function generate_build_env_variables()
$this->build_args = collect(["--build-arg SOURCE_COMMIT=\"{$this->commit}\""]);
if ($this->pull_request_id === 0) {
foreach ($this->application->build_environment_variables as $env) {
$value = escapeshellarg($env->value);
$value = escapeshellarg($env->real_value);
$this->build_args->push("--build-arg {$env->key}={$value}");
}
} else {
foreach ($this->application->build_environment_variables_preview as $env) {
$value = escapeshellarg($env->value);
$value = escapeshellarg($env->real_value);
$this->build_args->push("--build-arg {$env->key}={$value}");
}
}
Expand All @@ -1478,11 +1475,11 @@ private function add_build_env_variables_to_dockerfile()
$dockerfile = collect(Str::of($this->saved_outputs->get('dockerfile'))->trim()->explode("\n"));
if ($this->pull_request_id === 0) {
foreach ($this->application->build_environment_variables as $env) {
$dockerfile->splice(1, 0, "ARG {$env->key}={$env->value}");
$dockerfile->splice(1, 0, "ARG {$env->key}={$env->real_value}");
}
} else {
foreach ($this->application->build_environment_variables_preview as $env) {
$dockerfile->splice(1, 0, "ARG {$env->key}={$env->value}");
$dockerfile->splice(1, 0, "ARG {$env->key}={$env->real_value}");
}
}
$dockerfile_base64 = base64_encode($dockerfile->implode("\n"));
Expand Down
19 changes: 18 additions & 1 deletion app/Livewire/Project/Edit.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,24 @@ class Edit extends Component
'project.name' => 'required|min:3|max:255',
'project.description' => 'nullable|string|max:255',
];
public function mount() {
protected $listeners = ['refreshEnvs' => '$refresh', 'saveKey' => 'saveKey'];

public function saveKey($data)
{
try {
$this->project->environment_variables()->create([
'key' => $data['key'],
'value' => $data['value'],
'type' => 'project',
'team_id' => currentTeam()->id,
]);
$this->project->refresh();
} catch (\Throwable $e) {
return handleError($e, $this);
}
}
public function mount()
{
$projectUuid = request()->route('project_uuid');
$teamId = currentTeam()->id;
$project = Project::where('team_id', $teamId)->where('uuid', $projectUuid)->first();
Expand Down
22 changes: 19 additions & 3 deletions app/Livewire/Project/EnvironmentEdit.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,30 @@ class EnvironmentEdit extends Component
public Application $application;
public $environment;
public array $parameters;

protected $rules = [
'environment.name' => 'required|min:3|max:255',
'environment.description' => 'nullable|min:3|max:255',
];
public function mount() {
$this->parameters = get_route_parameters();
protected $listeners = ['refreshEnvs' => '$refresh', 'saveKey' => 'saveKey'];

public function saveKey($data)
{
try {
$this->environment->environment_variables()->create([
'key' => $data['key'],
'value' => $data['value'],
'type' => 'environment',
'team_id' => currentTeam()->id,
]);
$this->environment->refresh();
} catch (\Throwable $e) {
return handleError($e, $this);
}
}

public function mount()
{
$this->parameters = get_route_parameters();
$this->project = Project::ownedByCurrentTeam()->where('uuid', request()->route('project_uuid'))->first();
$this->environment = $this->project->environments()->where('name', request()->route('environment_name'))->first();
}
Expand Down
1 change: 0 additions & 1 deletion app/Livewire/Project/Shared/EnvironmentVariable/Add.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@ public function mount()
public function submit()
{
$this->validate();
ray($this->key, $this->value, $this->is_build_time);
$this->dispatch('saveKey', [
'key' => $this->key,
'value' => $this->value,
Expand Down
44 changes: 35 additions & 9 deletions app/Livewire/Project/Shared/EnvironmentVariable/Show.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,33 +3,39 @@
namespace App\Livewire\Project\Shared\EnvironmentVariable;

use App\Models\EnvironmentVariable as ModelsEnvironmentVariable;
use App\Models\SharedEnvironmentVariable;
use Livewire\Component;
use Visus\Cuid2\Cuid2;

class Show extends Component
{
public $parameters;
public ModelsEnvironmentVariable $env;
public ModelsEnvironmentVariable|SharedEnvironmentVariable $env;
public ?string $modalId = null;
public bool $isDisabled = false;
public bool $isLocked = false;
public bool $isSharedVariable = false;
public string $type;

protected $rules = [
'env.key' => 'required|string',
'env.value' => 'nullable',
'env.is_build_time' => 'required|boolean',
'env.is_shown_once' => 'required|boolean',
'env.real_value' => 'nullable',
];
protected $validationAttributes = [
'key' => 'Key',
'value' => 'Value',
'is_build_time' => 'Build Time',
'is_shown_once' => 'Shown Once',
'env.key' => 'Key',
'env.value' => 'Value',
'env.is_build_time' => 'Build Time',
'env.is_shown_once' => 'Shown Once',
];

public function mount()
{
if ($this->env->getMorphClass() === 'App\Models\SharedEnvironmentVariable') {
$this->isSharedVariable = true;
}
$this->modalId = new Cuid2(7);
$this->parameters = get_route_parameters();
$this->checkEnvs();
Expand All @@ -44,9 +50,16 @@ public function checkEnvs()
$this->isLocked = true;
}
}
public function serialize() {
data_forget($this->env, 'real_value');
if ($this->env->getMorphClass() === 'App\Models\SharedEnvironmentVariable') {
data_forget($this->env, 'is_build_time');
}
}
public function lock()
{
$this->env->is_shown_once = true;
$this->serialize();
$this->env->save();
$this->checkEnvs();
$this->dispatch('refreshEnvs');
Expand All @@ -57,10 +70,23 @@ public function instantSave()
}
public function submit()
{
$this->validate();
$this->env->save();
$this->dispatch('success', 'Environment variable updated successfully.');
$this->dispatch('refreshEnvs');
try {
if ($this->isSharedVariable) {
$this->validate([
'env.key' => 'required|string',
'env.value' => 'nullable',
'env.is_shown_once' => 'required|boolean',
]);
} else {
$this->validate();
}
$this->serialize();
$this->env->save();
$this->dispatch('success', 'Environment variable updated successfully.');
$this->dispatch('refreshEnvs');
} catch(\Exception $e) {
return handleError($e);
}
}

public function delete()
Expand Down
36 changes: 36 additions & 0 deletions app/Livewire/TeamSharedVariablesIndex.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<?php

namespace App\Livewire;

use App\Models\Team;
use Livewire\Component;

class TeamSharedVariablesIndex extends Component
{
public Team $team;
protected $listeners = ['refreshEnvs' => '$refresh', 'saveKey' => 'saveKey'];

public function saveKey($data)
{
try {
$this->team->environment_variables()->create([
'key' => $data['key'],
'value' => $data['value'],
'type' => 'team',
'team_id' => currentTeam()->id,
]);
$this->team->refresh();
} catch (\Throwable $e) {
return handleError($e, $this);
}
}

public function mount()
{
$this->team = currentTeam();
}
public function render()
{
return view('livewire.team-shared-variables-index');
}
}
6 changes: 5 additions & 1 deletion app/Models/Application.php
Original file line number Diff line number Diff line change
Expand Up @@ -263,6 +263,10 @@ public function portsExposesArray(): Attribute
: explode(',', $this->ports_exposes)
);
}
public function team()
{
return data_get($this, 'environment.project.team');
}
public function serviceType()
{
$found = str(collect(SPECIFIC_SERVICES)->filter(function ($service) {
Expand Down Expand Up @@ -431,7 +435,7 @@ public function isConfigurationChanged($save = false)
{
$newConfigHash = $this->fqdn . $this->git_repository . $this->git_branch . $this->git_commit_sha . $this->build_pack . $this->static_image . $this->install_command . $this->build_command . $this->start_command . $this->port_exposes . $this->port_mappings . $this->base_directory . $this->publish_directory . $this->dockerfile . $this->dockerfile_location . $this->custom_labels;
if ($this->pull_request_id === 0 || $this->pull_request_id === null) {
$newConfigHash .= json_encode($this->environment_variables->all());
$newConfigHash .= json_encode($this->environment_variables());
} else {
$newConfigHash .= json_encode($this->environment_variables_preview->all());
}
Expand Down
3 changes: 3 additions & 0 deletions app/Models/Environment.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@ public function isEmpty()
$this->services()->count() == 0;
}

public function environment_variables() {
return $this->hasMany(SharedEnvironmentVariable::class);
}
public function applications()
{
return $this->hasMany(Application::class);
Expand Down
Loading

0 comments on commit fb478c7

Please sign in to comment.