feat: shared environments

This commit is contained in:
Andras Bacsai
2024-01-23 17:13:23 +01:00
parent abcc004953
commit fb478c79b3
42 changed files with 495 additions and 78 deletions

View File

@@ -263,6 +263,10 @@ class Application extends BaseModel
: 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) {
@@ -431,7 +435,7 @@ class Application extends BaseModel
{
$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());
}

View File

@@ -17,6 +17,9 @@ class Environment extends Model
$this->services()->count() == 0;
}
public function environment_variables() {
return $this->hasMany(SharedEnvironmentVariable::class);
}
public function applications()
{
return $this->hasMany(Application::class);

View File

@@ -15,6 +15,7 @@ class EnvironmentVariable extends Model
'value' => 'encrypted',
'is_build_time' => 'boolean',
];
protected $appends = ['real_value', 'is_shared'];
protected static function booted()
{
@@ -48,24 +49,94 @@ class EnvironmentVariable extends Model
set: fn (?string $value = null) => $this->set_environment_variables($value),
);
}
private function get_environment_variables(?string $environment_variable = null): string|null
protected function realValue(): Attribute
{
return Attribute::make(
get: fn () => $this->get_real_environment_variables($this->value),
);
}
protected function isShared(): Attribute
{
return Attribute::make(
get: function () {
$type = str($this->value)->after("{{")->before(".")->value;
if (str($this->value)->startsWith('{{' . $type) && str($this->value)->endsWith('}}')) {
return true;
}
return false;
}
);
}
private function team()
{
if ($this->application_id) {
$application = Application::find($this->application_id);
if ($application) {
return $application->team();
}
}
if ($this->service_id) {
$service = Service::find($this->service_id);
if ($service) {
return $service->team();
}
}
if ($this->standalone_postgresql_id) {
$standalone_postgresql = StandalonePostgresql::find($this->standalone_postgresql_id);
if ($standalone_postgresql) {
return $standalone_postgresql->team();
}
}
if ($this->standalone_mysql_id) {
$standalone_mysql = StandaloneMysql::find($this->standalone_mysql_id);
if ($standalone_mysql) {
return $standalone_mysql->team();
}
}
if ($this->standalone_redis_id) {
$standalone_redis = StandaloneRedis::find($this->standalone_redis_id);
if ($standalone_redis) {
return $standalone_redis->team();
}
}
if ($this->standalone_mongodb_id) {
$standalone_mongodb = StandaloneMongodb::find($this->standalone_mongodb_id);
if ($standalone_mongodb) {
return $standalone_mongodb->team();
}
}
if ($this->standalone_mariadb_id) {
$standalone_mariadb = StandaloneMariadb::find($this->standalone_mariadb_id);
if ($standalone_mariadb) {
return $standalone_mariadb->team();
}
}
}
private function get_real_environment_variables(?string $environment_variable = null): string|null
{
// $team_id = currentTeam()->id;
if (!$environment_variable) {
return null;
}
$environment_variable = trim(decrypt($environment_variable));
if (Str::startsWith($environment_variable, '{{') && Str::endsWith($environment_variable, '}}') && Str::contains($environment_variable, 'global.')) {
$variable = Str::after($environment_variable, 'global.');
$environment_variable = trim($environment_variable);
$type = str($environment_variable)->after("{{")->before(".")->value;
if (str($environment_variable)->startsWith("{{" . $type) && str($environment_variable)->endsWith('}}')) {
$variable = Str::after($environment_variable, "{$type}.");
$variable = Str::before($variable, '}}');
$variable = Str::of($variable)->trim()->value;
// $environment_variable = GlobalEnvironmentVariable::where('name', $environment_variable)->where('team_id', $team_id)->first()?->value;
ray('global env variable');
return $environment_variable;
$environment_variable_found = SharedEnvironmentVariable::where("type", $type)->where('key', $variable)->where('team_id', $this->team()->id)->first();
if ($environment_variable_found) {
return $environment_variable_found->value;
}
}
return $environment_variable;
}
private function get_environment_variables(?string $environment_variable = null): string|null
{
if (!$environment_variable) {
return null;
}
return trim(decrypt($environment_variable));
}
private function set_environment_variables(?string $environment_variable = null): string|null
{
@@ -73,6 +144,10 @@ class EnvironmentVariable extends Model
return null;
}
$environment_variable = trim($environment_variable);
$type = str($environment_variable)->after("{{")->before(".")->value;
if (str($environment_variable)->startsWith("{{" . $type) && str($environment_variable)->endsWith('}}')) {
return encrypt((string) str($environment_variable)->replace(' ', ''));
}
return encrypt($environment_variable);
}

View File

@@ -27,7 +27,9 @@ class Project extends BaseModel
$project->settings()->delete();
});
}
public function environment_variables() {
return $this->hasMany(SharedEnvironmentVariable::class);
}
public function environments()
{
return $this->hasMany(Environment::class);

View File

@@ -8,6 +8,7 @@ use Illuminate\Database\Eloquent\Relations\HasMany;
use Illuminate\Database\Eloquent\SoftDeletes;
use Illuminate\Support\Collection;
use Illuminate\Support\Str;
use Symfony\Component\Yaml\Yaml;
class Service extends BaseModel
{
@@ -17,6 +18,10 @@ class Service extends BaseModel
{
return 'service';
}
public function team()
{
return data_get($this, 'environment.project.team');
}
public function extraFields()
{
$fields = collect([]);
@@ -423,7 +428,7 @@ class Service extends BaseModel
$envs = $this->environment_variables()->get();
$commands[] = "rm -f .env || true";
foreach ($envs as $env) {
$commands[] = "echo '{$env->key}={$env->value}' >> .env";
$commands[] = "echo '{$env->key}={$env->real_value}' >> .env";
}
if ($envs->count() === 0) {
$commands[] = "touch .env";

View File

@@ -0,0 +1,14 @@
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
class SharedEnvironmentVariable extends Model
{
protected $guarded = [];
protected $casts = [
'key' => 'string',
'value' => 'encrypted',
];
}

View File

@@ -42,6 +42,10 @@ class StandaloneMariadb extends BaseModel
$database->environment_variables()->delete();
});
}
public function team()
{
return data_get($this, 'environment.project.team');
}
public function link()
{
if (data_get($this, 'environment.project.uuid')) {

View File

@@ -45,6 +45,10 @@ class StandaloneMongodb extends BaseModel
$database->environment_variables()->delete();
});
}
public function team()
{
return data_get($this, 'environment.project.team');
}
public function isLogDrainEnabled()
{
return data_get($this, 'is_log_drain_enabled', false);

View File

@@ -42,6 +42,10 @@ class StandaloneMysql extends BaseModel
$database->environment_variables()->delete();
});
}
public function team()
{
return data_get($this, 'environment.project.team');
}
public function link()
{
if (data_get($this, 'environment.project.uuid')) {

View File

@@ -74,7 +74,10 @@ class StandalonePostgresql extends BaseModel
);
}
public function team()
{
return data_get($this, 'environment.project.team');
}
public function type(): string
{
return 'standalone-postgresql';

View File

@@ -37,6 +37,10 @@ class StandaloneRedis extends BaseModel
$database->environment_variables()->delete();
});
}
public function team()
{
return data_get($this, 'environment.project.team');
}
public function link()
{
if (data_get($this, 'environment.project.uuid')) {

View File

@@ -70,7 +70,9 @@ class Team extends Model implements SendsDiscord, SendsEmail
);
}
public function environment_variables() {
return $this->hasMany(SharedEnvironmentVariable::class)->whereNull('project_id')->whereNull('environment_id');
}
public function members()
{
return $this->belongsToMany(User::class, 'team_user', 'team_id', 'user_id')->withPivot('role');