add env variables + volumes for postgresql
This commit is contained in:
parent
971d7f703d
commit
a8ee779b31
@ -7,25 +7,30 @@ use App\Models\StandaloneDocker;
|
|||||||
use App\Models\Team;
|
use App\Models\Team;
|
||||||
use App\Models\StandalonePostgresql;
|
use App\Models\StandalonePostgresql;
|
||||||
use Symfony\Component\Yaml\Yaml;
|
use Symfony\Component\Yaml\Yaml;
|
||||||
|
use Illuminate\Support\Str;
|
||||||
|
|
||||||
class StartPostgresql
|
class StartPostgresql
|
||||||
{
|
{
|
||||||
|
public $database;
|
||||||
public function __invoke(Server $server, StandalonePostgresql $database)
|
public function __invoke(Server $server, StandalonePostgresql $database)
|
||||||
{
|
{
|
||||||
$container_name = generate_container_name($database->uuid);
|
$this->database = $database;
|
||||||
$destination = $database->destination;
|
|
||||||
$image = $database->image;
|
$container_name = generate_container_name($this->database->uuid);
|
||||||
|
$destination = $this->database->destination;
|
||||||
|
$image = $this->database->image;
|
||||||
|
|
||||||
|
$persistent_storages = $this->generate_local_persistent_volumes();
|
||||||
|
$volume_names = $this->generate_local_persistent_volumes_only_volume_names();
|
||||||
|
$environment_variables = $this->generate_environment_variables();
|
||||||
|
|
||||||
$docker_compose = [
|
$docker_compose = [
|
||||||
'version' => '3.8',
|
'version' => '3.8',
|
||||||
'services' => [
|
'services' => [
|
||||||
$container_name => [
|
$container_name => [
|
||||||
'image' => $image,
|
'image' => $image,
|
||||||
'container_name' => $container_name,
|
'container_name' => $container_name,
|
||||||
'environment'=> [
|
'environment' => $environment_variables,
|
||||||
'POSTGRES_USER' => $database->postgres_user,
|
|
||||||
'POSTGRES_PASSWORD' => $database->postgres_password,
|
|
||||||
'POSTGRES_DB' => $database->postgres_db,
|
|
||||||
],
|
|
||||||
'restart' => 'always',
|
'restart' => 'always',
|
||||||
'networks' => [
|
'networks' => [
|
||||||
$destination->network,
|
$destination->network,
|
||||||
@ -35,20 +40,22 @@ class StartPostgresql
|
|||||||
'CMD-SHELL',
|
'CMD-SHELL',
|
||||||
'pg_isready',
|
'pg_isready',
|
||||||
'-d',
|
'-d',
|
||||||
$database->postgres_db,
|
$this->database->postgres_db,
|
||||||
|
'-U',
|
||||||
|
$this->database->postgres_user,
|
||||||
],
|
],
|
||||||
'interval' => '5s',
|
'interval' => '5s',
|
||||||
'timeout' => '5s',
|
'timeout' => '5s',
|
||||||
'retries' => 10,
|
'retries' => 10,
|
||||||
'start_period' => '5s'
|
'start_period' => '5s'
|
||||||
],
|
],
|
||||||
'mem_limit' => $database->limits_memory,
|
'mem_limit' => $this->database->limits_memory,
|
||||||
'memswap_limit' => $database->limits_memory_swap,
|
'memswap_limit' => $this->database->limits_memory_swap,
|
||||||
'mem_swappiness' => $database->limits_memory_swappiness,
|
'mem_swappiness' => $this->database->limits_memory_swappiness,
|
||||||
'mem_reservation' => $database->limits_memory_reservation,
|
'mem_reservation' => $this->database->limits_memory_reservation,
|
||||||
'cpus' => $database->limits_cpus,
|
'cpus' => $this->database->limits_cpus,
|
||||||
'cpuset' => $database->limits_cpuset,
|
'cpuset' => $this->database->limits_cpuset,
|
||||||
'cpu_shares' => $database->limits_cpu_shares,
|
'cpu_shares' => $this->database->limits_cpu_shares,
|
||||||
]
|
]
|
||||||
],
|
],
|
||||||
'networks' => [
|
'networks' => [
|
||||||
@ -59,6 +66,13 @@ class StartPostgresql
|
|||||||
]
|
]
|
||||||
]
|
]
|
||||||
];
|
];
|
||||||
|
if (count($persistent_storages) > 0) {
|
||||||
|
$docker_compose['services'][$container_name]['volumes'] = $persistent_storages;
|
||||||
|
}
|
||||||
|
if (count($volume_names) > 0) {
|
||||||
|
$docker_compose['volumes'] = $volume_names;
|
||||||
|
}
|
||||||
|
|
||||||
$docker_compose = Yaml::dump($docker_compose, 10);
|
$docker_compose = Yaml::dump($docker_compose, 10);
|
||||||
$docker_compose_base64 = base64_encode($docker_compose);
|
$docker_compose_base64 = base64_encode($docker_compose);
|
||||||
$activity = remote_process([
|
$activity = remote_process([
|
||||||
@ -69,4 +83,50 @@ class StartPostgresql
|
|||||||
], $server);
|
], $server);
|
||||||
return $activity;
|
return $activity;
|
||||||
}
|
}
|
||||||
|
private function generate_environment_variables()
|
||||||
|
{
|
||||||
|
$environment_variables = collect();
|
||||||
|
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");
|
||||||
|
}
|
||||||
|
|
||||||
|
if ($environment_variables->filter(fn ($env) => Str::of($env)->contains('POSTGRES_USER'))->isEmpty()) {
|
||||||
|
$environment_variables->push("POSTGRES_USER={$this->database->postgres_user}");
|
||||||
|
}
|
||||||
|
|
||||||
|
if ($environment_variables->filter(fn ($env) => Str::of($env)->contains('POSTGRES_PASSWORD'))->isEmpty()) {
|
||||||
|
$environment_variables->push("POSTGRES_PASSWORD={$this->database->postgres_password}");
|
||||||
|
}
|
||||||
|
|
||||||
|
if ($environment_variables->filter(fn ($env) => Str::of($env)->contains('POSTGRES_DB'))->isEmpty()) {
|
||||||
|
$environment_variables->push("POSTGRES_DB={$this->database->postgres_db}");
|
||||||
|
}
|
||||||
|
return $environment_variables->all();
|
||||||
|
}
|
||||||
|
private function generate_local_persistent_volumes()
|
||||||
|
{
|
||||||
|
$local_persistent_volumes = [];
|
||||||
|
foreach ($this->database->persistentStorages as $persistentStorage) {
|
||||||
|
$volume_name = $persistentStorage->host_path ?? $persistentStorage->name;
|
||||||
|
$local_persistent_volumes[] = $volume_name . ':' . $persistentStorage->mount_path;
|
||||||
|
}
|
||||||
|
return $local_persistent_volumes;
|
||||||
|
}
|
||||||
|
private function generate_local_persistent_volumes_only_volume_names()
|
||||||
|
{
|
||||||
|
$local_persistent_volumes_names = [];
|
||||||
|
foreach ($this->database->persistentStorages as $persistentStorage) {
|
||||||
|
if ($persistentStorage->host_path) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
$name = $persistentStorage->name;
|
||||||
|
$local_persistent_volumes_names[$name] = [
|
||||||
|
'name' => $name,
|
||||||
|
'external' => false,
|
||||||
|
];
|
||||||
|
}
|
||||||
|
return $local_persistent_volumes_names;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -42,7 +42,10 @@ class StandalonePostgresql extends BaseModel
|
|||||||
{
|
{
|
||||||
return $this->hasMany(EnvironmentVariable::class);
|
return $this->hasMany(EnvironmentVariable::class);
|
||||||
}
|
}
|
||||||
|
public function runtime_environment_variables(): HasMany
|
||||||
|
{
|
||||||
|
return $this->hasMany(EnvironmentVariable::class);
|
||||||
|
}
|
||||||
public function persistentStorages()
|
public function persistentStorages()
|
||||||
{
|
{
|
||||||
return $this->morphMany(LocalPersistentVolume::class, 'resource');
|
return $this->morphMany(LocalPersistentVolume::class, 'resource');
|
||||||
|
Loading…
x
Reference in New Issue
Block a user