From 971d7f703d54995dbee8f7343fb2f0e33b2eb706 Mon Sep 17 00:00:00 2001 From: Andras Bacsai Date: Mon, 7 Aug 2023 22:14:21 +0200 Subject: [PATCH] lots of updates + refactoring --- app/Actions/Database/StartPostgresql.php | 65 ++++++++++++++++--- .../Controllers/ApplicationController.php | 3 +- app/Http/Controllers/DatabaseController.php | 3 +- app/Http/Livewire/ActivityMonitor.php | 1 + .../Application/EnvironmentVariable/All.php | 45 ------------- .../Livewire/Project/Application/Heading.php | 8 +-- .../Livewire/Project/Application/Previews.php | 6 +- .../Project/Application/ResourceLimits.php | 60 ----------------- .../Livewire/Project/Database/Heading.php | 31 ++++++++- .../Project/Database/Postgresql/General.php | 9 ++- .../{Application => Shared}/Danger.php | 11 ++-- .../{Application => Shared}/Destination.php | 2 +- .../EnvironmentVariable/Add.php | 2 +- .../Shared/EnvironmentVariable/All.php | 49 ++++++++++++++ .../EnvironmentVariable/Show.php | 2 +- .../Project/Shared/ResourceLimits.php | 59 +++++++++++++++++ .../{Application => Shared}/Storages/Add.php | 2 +- .../{Application => Shared}/Storages/All.php | 13 ++-- .../{Application => Shared}/Storages/Show.php | 2 +- ...erStatusJob.php => ContainerStatusJob.php} | 22 +++---- app/Jobs/InstanceApplicationsStatusJob.php | 4 +- app/Models/Application.php | 6 +- app/Models/Environment.php | 4 +- app/Models/EnvironmentVariable.php | 31 ++++----- app/Models/LocalPersistentVolume.php | 13 ++-- app/Models/Project.php | 4 +- app/Models/StandaloneDocker.php | 4 +- app/Models/StandalonePostgres.php | 25 ------- app/Models/StandalonePostgresql.php | 50 ++++++++++++++ .../Application/StatusChanged.php | 8 +-- ...0_create_standalone_postgresqls_table.php} | 18 ++++- ...remove_foreignId_environment_variables.php | 32 +++++++++ ...54_add_readonly_localpersistentvolumes.php | 28 ++++++++ database/seeders/DatabaseSeeder.php | 4 +- ...der.php => StandalonePostgresqlSeeder.php} | 8 +-- .../components/databases/navbar.blade.php | 11 ---- .../environment-variable/all.blade.php | 26 -------- .../application/storages/show.blade.php | 21 ------ .../project/database/heading.blade.php | 2 +- .../database/postgresql/general.blade.php | 30 +++++++-- .../{application => shared}/danger.blade.php | 6 +- .../project/shared/destination.blade.php | 8 +++ .../environment-variable/add.blade.php | 4 +- .../shared/environment-variable/all.blade.php | 26 ++++++++ .../environment-variable/show.blade.php | 4 +- .../resource-limits.blade.php | 14 ++-- .../storages/add.blade.php | 0 .../storages/all.blade.php | 8 +-- .../project/shared/storages/show.blade.php | 35 ++++++++++ .../application/configuration.blade.php | 10 +-- .../project/database/configuration.blade.php | 17 ++--- 51 files changed, 532 insertions(+), 324 deletions(-) delete mode 100644 app/Http/Livewire/Project/Application/EnvironmentVariable/All.php delete mode 100644 app/Http/Livewire/Project/Application/ResourceLimits.php rename app/Http/Livewire/Project/{Application => Shared}/Danger.php (58%) rename app/Http/Livewire/Project/{Application => Shared}/Destination.php (66%) rename app/Http/Livewire/Project/{Application => Shared}/EnvironmentVariable/Add.php (94%) create mode 100644 app/Http/Livewire/Project/Shared/EnvironmentVariable/All.php rename app/Http/Livewire/Project/{Application => Shared}/EnvironmentVariable/Show.php (93%) create mode 100644 app/Http/Livewire/Project/Shared/ResourceLimits.php rename app/Http/Livewire/Project/{Application => Shared}/Storages/Add.php (94%) rename app/Http/Livewire/Project/{Application => Shared}/Storages/All.php (68%) rename app/Http/Livewire/Project/{Application => Shared}/Storages/Show.php (93%) rename app/Jobs/{ApplicationContainerStatusJob.php => ContainerStatusJob.php} (59%) delete mode 100644 app/Models/StandalonePostgres.php create mode 100644 app/Models/StandalonePostgresql.php rename database/migrations/{2023_08_07_142950_create_standalone_postgres_table.php => 2023_08_07_142950_create_standalone_postgresqls_table.php} (62%) create mode 100644 database/migrations/2023_08_07_142952_remove_foreignId_environment_variables.php create mode 100644 database/migrations/2023_08_07_142954_add_readonly_localpersistentvolumes.php rename database/seeders/{StandalonePostgresSeeder.php => StandalonePostgresqlSeeder.php} (81%) delete mode 100644 resources/views/livewire/project/application/environment-variable/all.blade.php delete mode 100644 resources/views/livewire/project/application/storages/show.blade.php rename resources/views/livewire/project/{application => shared}/danger.blade.php (74%) create mode 100644 resources/views/livewire/project/shared/destination.blade.php rename resources/views/livewire/project/{application => shared}/environment-variable/add.blade.php (80%) create mode 100644 resources/views/livewire/project/shared/environment-variable/all.blade.php rename resources/views/livewire/project/{application => shared}/environment-variable/show.blade.php (83%) rename resources/views/livewire/project/{application => shared}/resource-limits.blade.php (75%) rename resources/views/livewire/project/{application => shared}/storages/add.blade.php (100%) rename resources/views/livewire/project/{application => shared}/storages/all.blade.php (71%) create mode 100644 resources/views/livewire/project/shared/storages/show.blade.php diff --git a/app/Actions/Database/StartPostgresql.php b/app/Actions/Database/StartPostgresql.php index 2ff91ce2f..2b7386904 100644 --- a/app/Actions/Database/StartPostgresql.php +++ b/app/Actions/Database/StartPostgresql.php @@ -5,21 +5,68 @@ namespace App\Actions\Database; use App\Models\Server; use App\Models\StandaloneDocker; use App\Models\Team; -use App\Models\StandalonePostgres; +use App\Models\StandalonePostgresql; +use Symfony\Component\Yaml\Yaml; class StartPostgresql { - public function __invoke(Server $server, StandalonePostgres $database) + public function __invoke(Server $server, StandalonePostgresql $database) { + $container_name = generate_container_name($database->uuid); + $destination = $database->destination; + $image = $database->image; + $docker_compose = [ + 'version' => '3.8', + 'services' => [ + $container_name => [ + 'image' => $image, + 'container_name' => $container_name, + 'environment'=> [ + 'POSTGRES_USER' => $database->postgres_user, + 'POSTGRES_PASSWORD' => $database->postgres_password, + 'POSTGRES_DB' => $database->postgres_db, + ], + 'restart' => 'always', + 'networks' => [ + $destination->network, + ], + 'healthcheck' => [ + 'test' => [ + 'CMD-SHELL', + 'pg_isready', + '-d', + $database->postgres_db, + ], + 'interval' => '5s', + 'timeout' => '5s', + 'retries' => 10, + 'start_period' => '5s' + ], + 'mem_limit' => $database->limits_memory, + 'memswap_limit' => $database->limits_memory_swap, + 'mem_swappiness' => $database->limits_memory_swappiness, + 'mem_reservation' => $database->limits_memory_reservation, + 'cpus' => $database->limits_cpus, + 'cpuset' => $database->limits_cpuset, + 'cpu_shares' => $database->limits_cpu_shares, + ] + ], + 'networks' => [ + $destination->network => [ + 'external' => false, + 'name' => $destination->network, + 'attachable' => true, + ] + ] + ]; + $docker_compose = Yaml::dump($docker_compose, 10); + $docker_compose_base64 = base64_encode($docker_compose); $activity = remote_process([ - "echo 'Creating required Docker networks...'", - "echo 'Creating required Docker networks...'", - "echo 'Creating required Docker networks...'", - "sleep 4", - "echo 'Creating required Docker networks...'", - "echo 'Creating required Docker networks...'", + "mkdir -p /tmp/{$container_name}", + "echo '{$docker_compose_base64}' | base64 -d > /tmp/{$container_name}/docker-compose.yml", + "docker compose -f /tmp/{$container_name}/docker-compose.yml up -d", ], $server); return $activity; } -} \ No newline at end of file +} diff --git a/app/Http/Controllers/ApplicationController.php b/app/Http/Controllers/ApplicationController.php index 84a6bc0ec..b6685276f 100644 --- a/app/Http/Controllers/ApplicationController.php +++ b/app/Http/Controllers/ApplicationController.php @@ -25,6 +25,7 @@ class ApplicationController extends Controller if (!$application) { return redirect()->route('dashboard'); } + ray($application->persistentStorages()->get()); return view('project.application.configuration', ['application' => $application]); } public function deployments() @@ -84,4 +85,4 @@ class ApplicationController extends Controller 'deployment_uuid' => $deploymentUuid, ]); } -} \ No newline at end of file +} diff --git a/app/Http/Controllers/DatabaseController.php b/app/Http/Controllers/DatabaseController.php index 7adb53f1f..6137c8a6b 100644 --- a/app/Http/Controllers/DatabaseController.php +++ b/app/Http/Controllers/DatabaseController.php @@ -25,6 +25,7 @@ class DatabaseController extends Controller if (!$database) { return redirect()->route('dashboard'); } + ray($database->persistentStorages()->get()); return view('project.database.configuration', ['database' => $database]); } -} \ No newline at end of file +} diff --git a/app/Http/Livewire/ActivityMonitor.php b/app/Http/Livewire/ActivityMonitor.php index b11bcdb38..1ce763a0f 100644 --- a/app/Http/Livewire/ActivityMonitor.php +++ b/app/Http/Livewire/ActivityMonitor.php @@ -42,6 +42,7 @@ class ActivityMonitor extends Component $this->setStatus(ProcessStatus::ERROR); } $this->isPollingActive = false; + $this->emit('activityFinished'); } } protected function setStatus($status) diff --git a/app/Http/Livewire/Project/Application/EnvironmentVariable/All.php b/app/Http/Livewire/Project/Application/EnvironmentVariable/All.php deleted file mode 100644 index f1ac75917..000000000 --- a/app/Http/Livewire/Project/Application/EnvironmentVariable/All.php +++ /dev/null @@ -1,45 +0,0 @@ -modalId = new Cuid2(7); - } - public function refreshEnvs() - { - $this->application->refresh(); - } - public function submit($data) - { - try { - $found = $this->application->environment_variables()->where('key', $data['key'])->first(); - if ($found) { - $this->emit('error', 'Environment variable already exists.'); - return; - } - EnvironmentVariable::create([ - 'key' => $data['key'], - 'value' => $data['value'], - 'is_build_time' => $data['is_build_time'], - 'is_preview' => $data['is_preview'], - 'application_id' => $this->application->id, - ]); - $this->application->refresh(); - - $this->emit('success', 'Environment variable added successfully.'); - } catch (\Exception $e) { - return general_error_handler(err: $e, that: $this); - } - } -} diff --git a/app/Http/Livewire/Project/Application/Heading.php b/app/Http/Livewire/Project/Application/Heading.php index 8a52061ef..b3c3cd876 100644 --- a/app/Http/Livewire/Project/Application/Heading.php +++ b/app/Http/Livewire/Project/Application/Heading.php @@ -2,7 +2,7 @@ namespace App\Http\Livewire\Project\Application; -use App\Jobs\ApplicationContainerStatusJob; +use App\Jobs\ContainerStatusJob; use App\Models\Application; use App\Notifications\Application\StatusChanged; use Livewire\Component; @@ -22,8 +22,8 @@ class Heading extends Component public function check_status() { - dispatch_sync(new ApplicationContainerStatusJob( - application: $this->application, + dispatch_sync(new ContainerStatusJob( + resource: $this->application, container_name: generate_container_name($this->application->uuid), )); $this->application->refresh(); @@ -62,4 +62,4 @@ class Heading extends Component $this->deploymentUuid = new Cuid2(7); $this->parameters['deployment_uuid'] = $this->deploymentUuid; } -} \ No newline at end of file +} diff --git a/app/Http/Livewire/Project/Application/Previews.php b/app/Http/Livewire/Project/Application/Previews.php index 8b26b7bc2..a708a7098 100644 --- a/app/Http/Livewire/Project/Application/Previews.php +++ b/app/Http/Livewire/Project/Application/Previews.php @@ -2,7 +2,7 @@ namespace App\Http\Livewire\Project\Application; -use App\Jobs\ApplicationContainerStatusJob; +use App\Jobs\ContainerStatusJob; use App\Models\Application; use App\Models\ApplicationPreview; use Illuminate\Support\Collection; @@ -24,8 +24,8 @@ class Previews extends Component } public function loadStatus($pull_request_id) { - dispatch(new ApplicationContainerStatusJob( - application: $this->application, + dispatch(new ContainerStatusJob( + resource: $this->application, container_name: generate_container_name($this->application->uuid, $pull_request_id), pull_request_id: $pull_request_id )); diff --git a/app/Http/Livewire/Project/Application/ResourceLimits.php b/app/Http/Livewire/Project/Application/ResourceLimits.php deleted file mode 100644 index 9db5626ac..000000000 --- a/app/Http/Livewire/Project/Application/ResourceLimits.php +++ /dev/null @@ -1,60 +0,0 @@ - 'required|string', - 'application.limits_memory_swap' => 'required|string', - 'application.limits_memory_swappiness' => 'required|integer|min:0|max:100', - 'application.limits_memory_reservation' => 'required|string', - 'application.limits_cpus' => 'nullable', - 'application.limits_cpuset' => 'nullable', - 'application.limits_cpu_shares' => 'nullable', - ]; - protected $validationAttributes = [ - 'application.limits_memory' => 'memory', - 'application.limits_memory_swap' => 'swap', - 'application.limits_memory_swappiness' => 'swappiness', - 'application.limits_memory_reservation' => 'reservation', - 'application.limits_cpus' => 'cpus', - 'application.limits_cpuset' => 'cpuset', - 'application.limits_cpu_shares' => 'cpu shares', - ]; - public function submit() - { - try { - if (!$this->application->limits_memory) { - $this->application->limits_memory = "0"; - } - if (!$this->application->limits_memory_swap) { - $this->application->limits_memory_swap = "0"; - } - if (!$this->application->limits_memory_swappiness) { - $this->application->limits_memory_swappiness = "60"; - } - if (!$this->application->limits_memory_reservation) { - $this->application->limits_memory_reservation = "0"; - } - if (!$this->application->limits_cpus) { - $this->application->limits_cpus = "0"; - } - if (!$this->application->limits_cpuset) { - $this->application->limits_cpuset = "0"; - } - if (!$this->application->limits_cpu_shares) { - $this->application->limits_cpu_shares = 1024; - } - $this->validate(); - $this->application->save(); - $this->emit('success', 'Resource limits updated successfully.'); - } catch (\Exception $e) { - return general_error_handler(err: $e, that: $this); - } - } -} diff --git a/app/Http/Livewire/Project/Database/Heading.php b/app/Http/Livewire/Project/Database/Heading.php index 8b653bc0a..f6dd7a12d 100644 --- a/app/Http/Livewire/Project/Database/Heading.php +++ b/app/Http/Livewire/Project/Database/Heading.php @@ -4,20 +4,47 @@ namespace App\Http\Livewire\Project\Database; use Livewire\Component; use App\Actions\Database\StartPostgresql; +use App\Jobs\ContainerStatusJob; +use App\Notifications\Application\StatusChanged; class Heading extends Component { public $database; public array $parameters; + protected $listeners = ['activityFinished']; + public function activityFinished() { + $this->database->update([ + 'started_at' => now(), + ]); + $this->emit('refresh'); + $this->check_status(); + } + public function check_status() + { + dispatch_sync(new ContainerStatusJob( + resource: $this->database, + container_name: generate_container_name($this->database->uuid), + )); + $this->database->refresh(); + } public function mount() { $this->parameters = getRouteParameters(); } + public function stop() { + remote_process( + ["docker rm -f {$this->database->uuid}"], + $this->database->destination->server + ); + $this->database->status = 'stopped'; + $this->database->save(); + $this->database->environment->project->team->notify(new StatusChanged($this->database)); + } public function start() { - if ($this->database->type() === 'postgresql') { + if ($this->database->type() === 'standalone-postgresql') { $activity = resolve(StartPostgresql::class)($this->database->destination->server, $this->database); $this->emit('newMonitorActivity', $activity->id); } } -} \ No newline at end of file +} diff --git a/app/Http/Livewire/Project/Database/Postgresql/General.php b/app/Http/Livewire/Project/Database/Postgresql/General.php index b3e2faa99..8b1d6c40a 100644 --- a/app/Http/Livewire/Project/Database/Postgresql/General.php +++ b/app/Http/Livewire/Project/Database/Postgresql/General.php @@ -7,6 +7,8 @@ use Livewire\Component; class General extends Component { public $database; + protected $listeners = ['refresh']; + protected $rules = [ 'database.name' => 'required', 'database.description' => 'nullable', @@ -16,6 +18,7 @@ class General extends Component 'database.postgres_initdb_args' => 'nullable', 'database.postgres_host_auth_method' => 'nullable', 'database.init_scripts' => 'nullable', + 'database.image' => 'required', ]; protected $validationAttributes = [ 'database.name' => 'Name', @@ -26,7 +29,11 @@ class General extends Component 'database.postgres_initdb_args' => 'Postgres Initdb Args', 'database.postgres_host_auth_method' => 'Postgres Host Auth Method', 'database.init_scripts' => 'Init Scripts', + 'database.image' => 'Image', ]; + public function refresh() { + $this->database->refresh(); + } public function submit() { try { $this->validate(); @@ -36,4 +43,4 @@ class General extends Component return general_error_handler(err: $e, that: $this); } } -} \ No newline at end of file +} diff --git a/app/Http/Livewire/Project/Application/Danger.php b/app/Http/Livewire/Project/Shared/Danger.php similarity index 58% rename from app/Http/Livewire/Project/Application/Danger.php rename to app/Http/Livewire/Project/Shared/Danger.php index 4af90927e..0bb5f8ef6 100644 --- a/app/Http/Livewire/Project/Application/Danger.php +++ b/app/Http/Livewire/Project/Shared/Danger.php @@ -1,14 +1,13 @@ application->destination->getMorphClass()::where('id', $this->application->destination->id)->first(); + $destination = $this->resource->destination->getMorphClass()::where('id', $this->resource->destination->id)->first(); - instant_remote_process(["docker rm -f {$this->application->uuid}"], $destination->server); - $this->application->delete(); + instant_remote_process(["docker rm -f {$this->resource->uuid}"], $destination->server); + $this->resource->delete(); return redirect()->route('project.resources', [ 'project_uuid' => $this->parameters['project_uuid'], 'environment_name' => $this->parameters['environment_name'] diff --git a/app/Http/Livewire/Project/Application/Destination.php b/app/Http/Livewire/Project/Shared/Destination.php similarity index 66% rename from app/Http/Livewire/Project/Application/Destination.php rename to app/Http/Livewire/Project/Shared/Destination.php index 571c81d9b..3bdb48af6 100644 --- a/app/Http/Livewire/Project/Application/Destination.php +++ b/app/Http/Livewire/Project/Shared/Destination.php @@ -1,6 +1,6 @@ modalId = new Cuid2(7); + } + public function refreshEnvs() + { + $this->resource->refresh(); + } + public function submit($data) + { + try { + $found = $this->resource->environment_variables()->where('key', $data['key'])->first(); + if ($found) { + $this->emit('error', 'Environment variable already exists.'); + return; + } + $environment = new EnvironmentVariable(); + $environment->key = $data['key']; + $environment->value = $data['value']; + $environment->is_build_time = $data['is_build_time']; + $environment->is_preview = $data['is_preview']; + + if($this->resource->type() === 'application') { + $environment->application_id = $this->resource->id; + } + if($this->resource->type() === 'standalone-postgresql') { + $environment->standalone_postgresql_id = $this->resource->id; + } + $environment->save(); + $this->resource->refresh(); + $this->emit('success', 'Environment variable added successfully.'); + } catch (\Exception $e) { + return general_error_handler(err: $e, that: $this); + } + } +} diff --git a/app/Http/Livewire/Project/Application/EnvironmentVariable/Show.php b/app/Http/Livewire/Project/Shared/EnvironmentVariable/Show.php similarity index 93% rename from app/Http/Livewire/Project/Application/EnvironmentVariable/Show.php rename to app/Http/Livewire/Project/Shared/EnvironmentVariable/Show.php index c5260e77c..1439e983d 100644 --- a/app/Http/Livewire/Project/Application/EnvironmentVariable/Show.php +++ b/app/Http/Livewire/Project/Shared/EnvironmentVariable/Show.php @@ -1,6 +1,6 @@ 'required|string', + 'resource.limits_memory_swap' => 'required|string', + 'resource.limits_memory_swappiness' => 'required|integer|min:0|max:100', + 'resource.limits_memory_reservation' => 'required|string', + 'resource.limits_cpus' => 'nullable', + 'resource.limits_cpuset' => 'nullable', + 'resource.limits_cpu_shares' => 'nullable', + ]; + protected $validationAttributes = [ + 'resource.limits_memory' => 'memory', + 'resource.limits_memory_swap' => 'swap', + 'resource.limits_memory_swappiness' => 'swappiness', + 'resource.limits_memory_reservation' => 'reservation', + 'resource.limits_cpus' => 'cpus', + 'resource.limits_cpuset' => 'cpuset', + 'resource.limits_cpu_shares' => 'cpu shares', + ]; + public function submit() + { + try { + if (!$this->resource->limits_memory) { + $this->resource->limits_memory = "0"; + } + if (!$this->resource->limits_memory_swap) { + $this->resource->limits_memory_swap = "0"; + } + if (!$this->resource->limits_memory_swappiness) { + $this->resource->limits_memory_swappiness = "60"; + } + if (!$this->resource->limits_memory_reservation) { + $this->resource->limits_memory_reservation = "0"; + } + if (!$this->resource->limits_cpus) { + $this->resource->limits_cpus = "0"; + } + if (!$this->resource->limits_cpuset) { + $this->resource->limits_cpuset = "0"; + } + if (!$this->resource->limits_cpu_shares) { + $this->resource->limits_cpu_shares = 1024; + } + $this->validate(); + $this->resource->save(); + $this->emit('success', 'Resource limits updated successfully.'); + } catch (\Exception $e) { + return general_error_handler(err: $e, that: $this); + } + } +} diff --git a/app/Http/Livewire/Project/Application/Storages/Add.php b/app/Http/Livewire/Project/Shared/Storages/Add.php similarity index 94% rename from app/Http/Livewire/Project/Application/Storages/Add.php rename to app/Http/Livewire/Project/Shared/Storages/Add.php index 818657a02..59a8213e1 100644 --- a/app/Http/Livewire/Project/Application/Storages/Add.php +++ b/app/Http/Livewire/Project/Shared/Storages/Add.php @@ -1,6 +1,6 @@ application->refresh(); + $this->resource->refresh(); } public function submit($data) { @@ -21,10 +20,10 @@ class All extends Component 'name' => $data['name'], 'mount_path' => $data['mount_path'], 'host_path' => $data['host_path'], - 'resource_id' => $this->application->id, - 'resource_type' => Application::class, + 'resource_id' => $this->resource->id, + 'resource_type' => $this->resource->getMorphClass(), ]); - $this->application->refresh(); + $this->resource->refresh(); $this->emit('success', 'Storage added successfully'); $this->emit('clearAddStorage'); } catch (\Exception $e) { diff --git a/app/Http/Livewire/Project/Application/Storages/Show.php b/app/Http/Livewire/Project/Shared/Storages/Show.php similarity index 93% rename from app/Http/Livewire/Project/Application/Storages/Show.php rename to app/Http/Livewire/Project/Shared/Storages/Show.php index ef6042baf..6244537f7 100644 --- a/app/Http/Livewire/Project/Application/Storages/Show.php +++ b/app/Http/Livewire/Project/Shared/Storages/Show.php @@ -1,6 +1,6 @@ application = $application; + $this->resource = $resource; $this->container_name = $container_name; $this->pull_request_id = $pull_request_id; } @@ -34,21 +34,21 @@ class ApplicationContainerStatusJob implements ShouldQueue, ShouldBeUnique public function handle(): void { try { - $status = get_container_status(server: $this->application->destination->server, container_id: $this->container_name, throwError: false); - if ($this->application->status === 'running' && $status === 'stopped') { - $this->application->environment->project->team->notify(new StatusChanged($this->application)); + $status = get_container_status(server: $this->resource->destination->server, container_id: $this->container_name, throwError: false); + if ($this->resource->status === 'running' && $status === 'stopped') { + $this->resource->environment->project->team->notify(new StatusChanged($this->resource)); } if ($this->pull_request_id) { - $preview = ApplicationPreview::findPreviewByApplicationAndPullId($this->application->id, $this->pull_request_id); + $preview = ApplicationPreview::findPreviewByApplicationAndPullId($this->resource->id, $this->pull_request_id); $preview->status = $status; $preview->save(); } else { - $this->application->status = $status; - $this->application->save(); + $this->resource->status = $status; + $this->resource->save(); } } catch (\Exception $e) { ray($e->getMessage()); } } -} \ No newline at end of file +} diff --git a/app/Jobs/InstanceApplicationsStatusJob.php b/app/Jobs/InstanceApplicationsStatusJob.php index a01883010..a7ab592b0 100644 --- a/app/Jobs/InstanceApplicationsStatusJob.php +++ b/app/Jobs/InstanceApplicationsStatusJob.php @@ -23,8 +23,8 @@ class InstanceApplicationsStatusJob implements ShouldQueue, ShouldBeUnique { try { foreach ($this->applications as $application) { - dispatch(new ApplicationContainerStatusJob( - application: $application, + dispatch(new ContainerStatusJob( + resource: $application, container_name: generate_container_name($application->uuid), )); } diff --git a/app/Models/Application.php b/app/Models/Application.php index f56a55f79..2c395511b 100644 --- a/app/Models/Application.php +++ b/app/Models/Application.php @@ -43,7 +43,9 @@ class Application extends BaseModel 'publish_directory', 'private_key_id' ]; - + public function type() { + return 'application'; + } public function publishDirectory(): Attribute { return Attribute::make( @@ -206,4 +208,4 @@ class Application extends BaseModel } throw new \Exception('No deployment type found'); } -} \ No newline at end of file +} diff --git a/app/Models/Environment.php b/app/Models/Environment.php index ca47fd292..3f8940cde 100644 --- a/app/Models/Environment.php +++ b/app/Models/Environment.php @@ -33,10 +33,10 @@ class Environment extends Model } public function postgresqls() { - return $this->hasMany(StandalonePostgres::class); + return $this->hasMany(StandalonePostgresql::class); } public function services() { return $this->hasMany(Service::class); } -} \ No newline at end of file +} diff --git a/app/Models/EnvironmentVariable.php b/app/Models/EnvironmentVariable.php index 3847e1700..f3be6716b 100644 --- a/app/Models/EnvironmentVariable.php +++ b/app/Models/EnvironmentVariable.php @@ -9,26 +9,27 @@ use Illuminate\Support\Str; class EnvironmentVariable extends Model { - protected static function booted() - { - static::created(function ($environment_variable) { - if (!$environment_variable->is_preview) { - ModelsEnvironmentVariable::create([ - 'key' => $environment_variable->key, - 'value' => $environment_variable->value, - 'is_build_time' => $environment_variable->is_build_time, - 'application_id' => $environment_variable->application_id, - 'is_preview' => true, - ]); - } - }); - } - protected $fillable = ['key', 'value', 'is_build_time', 'application_id', 'is_preview']; + protected $guarded = []; protected $casts = [ "key" => 'string', 'value' => 'encrypted', 'is_build_time' => 'boolean', ]; + protected static function booted() + { + static::created(function ($environment_variable) { + if ($environment_variable->application_id && !$environment_variable->is_preview) { + ModelsEnvironmentVariable::create([ + 'key' => $environment_variable->key, + 'value' => $environment_variable->value, + 'is_build_time' => $environment_variable->is_build_time, + 'application_id' => $environment_variable->application_id, + 'is_preview' => true, + ]); + } + }); + } + private function get_environment_variables(string $environment_variable): string|null { // $team_id = session('currentTeam')->id; diff --git a/app/Models/LocalPersistentVolume.php b/app/Models/LocalPersistentVolume.php index 96b550969..71546df4d 100644 --- a/app/Models/LocalPersistentVolume.php +++ b/app/Models/LocalPersistentVolume.php @@ -8,18 +8,15 @@ use Illuminate\Support\Str; class LocalPersistentVolume extends Model { - protected $fillable = [ - 'name', - 'mount_path', - 'host_path', - 'container_id', - 'resource_id', - 'resource_type', - ]; + protected $guarded = []; public function application() { return $this->morphTo(); } + public function standalone_postgresql() + { + return $this->morphTo(); + } protected function name(): Attribute { return Attribute::make( diff --git a/app/Models/Project.php b/app/Models/Project.php index 129df3072..dbba240af 100644 --- a/app/Models/Project.php +++ b/app/Models/Project.php @@ -48,6 +48,6 @@ class Project extends BaseModel } public function postgresqls() { - return $this->hasManyThrough(StandalonePostgres::class, Environment::class); + return $this->hasManyThrough(StandalonePostgresql::class, Environment::class); } -} \ No newline at end of file +} diff --git a/app/Models/StandaloneDocker.php b/app/Models/StandaloneDocker.php index c14bb855d..a21e22e45 100644 --- a/app/Models/StandaloneDocker.php +++ b/app/Models/StandaloneDocker.php @@ -15,7 +15,7 @@ class StandaloneDocker extends BaseModel } public function postgresqls() { - return $this->morphMany(StandalonePostgres::class, 'destination'); + return $this->morphMany(StandalonePostgresql::class, 'destination'); } public function server() { @@ -25,4 +25,4 @@ class StandaloneDocker extends BaseModel { return $this->applications->count() > 0 || $this->databases->count() > 0; } -} \ No newline at end of file +} diff --git a/app/Models/StandalonePostgres.php b/app/Models/StandalonePostgres.php deleted file mode 100644 index df4295512..000000000 --- a/app/Models/StandalonePostgres.php +++ /dev/null @@ -1,25 +0,0 @@ - 'encrypted', - ]; - public function type() { - return 'postgresql'; - } - public function environment() - { - return $this->belongsTo(Environment::class); - } - public function destination() - { - return $this->morphTo(); - } -} \ No newline at end of file diff --git a/app/Models/StandalonePostgresql.php b/app/Models/StandalonePostgresql.php new file mode 100644 index 000000000..c2b8f37aa --- /dev/null +++ b/app/Models/StandalonePostgresql.php @@ -0,0 +1,50 @@ + 'postgres-data-' . $database->uuid, + 'mount_path' => '/var/lib/postgresql/data', + 'host_path' => null, + 'resource_id' => $database->id, + 'resource_type' => $database->getMorphClass(), + 'is_readonly' => true + ]); + }); + } + protected $guarded = []; + protected $casts = [ + 'postgres_password' => 'encrypted', + ]; + public function type() { + return 'standalone-postgresql'; + } + public function environment() + { + return $this->belongsTo(Environment::class); + } + public function destination() + { + return $this->morphTo(); + } + public function environment_variables(): HasMany + { + return $this->hasMany(EnvironmentVariable::class); + } + + public function persistentStorages() + { + return $this->morphMany(LocalPersistentVolume::class, 'resource'); + } +} diff --git a/app/Notifications/Application/StatusChanged.php b/app/Notifications/Application/StatusChanged.php index afa8839a2..4fb279d44 100644 --- a/app/Notifications/Application/StatusChanged.php +++ b/app/Notifications/Application/StatusChanged.php @@ -15,21 +15,21 @@ use Illuminate\Support\Str; class StatusChanged extends Notification implements ShouldQueue { use Queueable; - public Application $application; + public $application; public string $application_name; public string|null $application_url = null; public string $project_uuid; public string $environment_name; - public string $fqdn; + public string|null $fqdn; - public function __construct(Application $application) + public function __construct($application) { $this->application = $application; $this->application_name = data_get($application, 'name'); $this->project_uuid = data_get($application, 'environment.project.uuid'); $this->environment_name = data_get($application, 'environment.name'); - $this->fqdn = data_get($application, 'fqdn'); + $this->fqdn = data_get($application, 'fqdn', null); if (Str::of($this->fqdn)->explode(',')->count() > 1) { $this->fqdn = Str::of($this->fqdn)->explode(',')->first(); } diff --git a/database/migrations/2023_08_07_142950_create_standalone_postgres_table.php b/database/migrations/2023_08_07_142950_create_standalone_postgresqls_table.php similarity index 62% rename from database/migrations/2023_08_07_142950_create_standalone_postgres_table.php rename to database/migrations/2023_08_07_142950_create_standalone_postgresqls_table.php index 3d2f1b7d3..c159434e6 100644 --- a/database/migrations/2023_08_07_142950_create_standalone_postgres_table.php +++ b/database/migrations/2023_08_07_142950_create_standalone_postgresqls_table.php @@ -11,7 +11,7 @@ return new class extends Migration */ public function up(): void { - Schema::create('standalone_postgres', function (Blueprint $table) { + Schema::create('standalone_postgresqls', function (Blueprint $table) { $table->id(); $table->string('uuid')->unique(); $table->string('name'); @@ -24,9 +24,21 @@ return new class extends Migration $table->string('postgres_host_auth_method')->nullable(); $table->json('init_scripts')->nullable(); + $table->string('status')->default('exited'); + + $table->string('image')->default('postgres:15-alpine'); $table->boolean('is_public')->default(false); $table->integer('public_port')->nullable(); + $table->string('limits_memory')->default("0"); + $table->string('limits_memory_swap')->default("0"); + $table->integer('limits_memory_swappiness')->default(60); + $table->string('limits_memory_reservation')->default("0"); + + $table->string('limits_cpus')->default("0"); + $table->string('limits_cpuset')->nullable()->default("0"); + $table->integer('limits_cpu_shares')->default(1024); + $table->timestamp('started_at')->nullable(); $table->morphs('destination'); @@ -40,6 +52,6 @@ return new class extends Migration */ public function down(): void { - Schema::dropIfExists('standalone_postgres'); + Schema::dropIfExists('standalone_postgresqls'); } -}; \ No newline at end of file +}; diff --git a/database/migrations/2023_08_07_142952_remove_foreignId_environment_variables.php b/database/migrations/2023_08_07_142952_remove_foreignId_environment_variables.php new file mode 100644 index 000000000..d24dbb446 --- /dev/null +++ b/database/migrations/2023_08_07_142952_remove_foreignId_environment_variables.php @@ -0,0 +1,32 @@ +dropColumn('service_id'); + $table->dropColumn('database_id'); + $table->foreignId('standalone_postgresql_id')->nullable(); + }); + } + + /** + * Reverse the migrations. + */ + public function down(): void + { + Schema::table('environment_variables', function (Blueprint $table) { + $table->foreignId('service_id')->nullable(); + $table->foreignId('database_id')->nullable(); + $table->dropColumn('standalone_postgresql_id'); + }); + } +}; diff --git a/database/migrations/2023_08_07_142954_add_readonly_localpersistentvolumes.php b/database/migrations/2023_08_07_142954_add_readonly_localpersistentvolumes.php new file mode 100644 index 000000000..f0bce760d --- /dev/null +++ b/database/migrations/2023_08_07_142954_add_readonly_localpersistentvolumes.php @@ -0,0 +1,28 @@ +boolean('is_readonly')->default(false); + }); + } + + /** + * Reverse the migrations. + */ + public function down(): void + { + Schema::table('local_persistent_volumes', function (Blueprint $table) { + $table->dropColumn('is_readonly'); + }); + } +}; diff --git a/database/seeders/DatabaseSeeder.php b/database/seeders/DatabaseSeeder.php index 2bff4f36f..96209ade7 100644 --- a/database/seeders/DatabaseSeeder.php +++ b/database/seeders/DatabaseSeeder.php @@ -32,7 +32,7 @@ class DatabaseSeeder extends Seeder EnvironmentVariableSeeder::class, LocalPersistentVolumeSeeder::class, S3StorageSeeder::class, - StandalonePostgresSeeder::class, + StandalonePostgresqlSeeder::class, ]); } -} \ No newline at end of file +} diff --git a/database/seeders/StandalonePostgresSeeder.php b/database/seeders/StandalonePostgresqlSeeder.php similarity index 81% rename from database/seeders/StandalonePostgresSeeder.php rename to database/seeders/StandalonePostgresqlSeeder.php index c045d7358..126f159e7 100644 --- a/database/seeders/StandalonePostgresSeeder.php +++ b/database/seeders/StandalonePostgresqlSeeder.php @@ -4,17 +4,17 @@ namespace Database\Seeders; use Illuminate\Database\Console\Seeds\WithoutModelEvents; use Illuminate\Database\Seeder; -use App\Models\StandalonePostgres; +use App\Models\StandalonePostgresql; use App\Models\StandaloneDocker; -class StandalonePostgresSeeder extends Seeder +class StandalonePostgresqlSeeder extends Seeder { /** * Run the database seeds. */ public function run(): void { - StandalonePostgres::create([ + StandalonePostgresql::create([ 'name' => 'Local PostgreSQL', 'description' => 'Local PostgreSQL for testing', 'postgres_password' => 'postgres', @@ -23,4 +23,4 @@ class StandalonePostgresSeeder extends Seeder 'destination_type' => StandaloneDocker::class, ]); } -} \ No newline at end of file +} diff --git a/resources/views/components/databases/navbar.blade.php b/resources/views/components/databases/navbar.blade.php index fc8e7b68a..d6c85ffaa 100644 --- a/resources/views/components/databases/navbar.blade.php +++ b/resources/views/components/databases/navbar.blade.php @@ -8,17 +8,6 @@ {{-- --}} @if ($database->status === 'running') -