From 9c2fea4b2e8a775f05dcfe5a8a50f4cd5eb43607 Mon Sep 17 00:00:00 2001 From: Andras Bacsai Date: Fri, 22 Sep 2023 11:34:27 +0200 Subject: [PATCH] fix: delete persistent storages on resource deletion --- app/Http/Livewire/Project/Shared/Danger.php | 4 +++- app/Models/Application.php | 7 ++++--- app/Models/Service.php | 14 ++++++++++++++ app/Models/StandalonePostgresql.php | 2 +- 4 files changed, 22 insertions(+), 5 deletions(-) diff --git a/app/Http/Livewire/Project/Shared/Danger.php b/app/Http/Livewire/Project/Shared/Danger.php index c88ade583..5fc363359 100644 --- a/app/Http/Livewire/Project/Shared/Danger.php +++ b/app/Http/Livewire/Project/Shared/Danger.php @@ -2,6 +2,7 @@ namespace App\Http\Livewire\Project\Shared; +use App\Actions\Service\StopService; use Livewire\Component; use Visus\Cuid2\Cuid2; @@ -22,14 +23,15 @@ public function delete() try { if ($this->resource->type() === 'service') { $server = $this->resource->server; + StopService::run($this->resource); } else { $destination = data_get($this->resource, 'destination'); if ($destination) { $destination = $this->resource->destination->getMorphClass()::where('id', $this->resource->destination->id)->first(); $server = $destination->server; } + instant_remote_process(["docker rm -f {$this->resource->uuid}"], $server); } - instant_remote_process(["docker rm -f {$this->resource->uuid}"], $server); $this->resource->delete(); return redirect()->route('project.resources', [ 'project_uuid' => $this->parameters['project_uuid'], diff --git a/app/Models/Application.php b/app/Models/Application.php index 8f518efc9..1cdfdea29 100644 --- a/app/Models/Application.php +++ b/app/Models/Application.php @@ -4,10 +4,7 @@ use Illuminate\Database\Eloquent\Casts\Attribute; use Illuminate\Database\Eloquent\Relations\HasMany; -use Illuminate\Support\Collection; use Spatie\Activitylog\Models\Activity; -use Symfony\Component\Yaml\Yaml; -use Illuminate\Support\Str; class Application extends BaseModel { @@ -22,6 +19,10 @@ protected static function booted() }); static::deleting(function ($application) { $application->settings()->delete(); + $storages = $application->persistentStorages()->get(); + foreach ($storages as $storage) { + instant_remote_process(["docker volume rm -f $storage->name"], $application->destination->server); + } $application->persistentStorages()->delete(); $application->environment_variables()->delete(); $application->environment_variables_preview()->delete(); diff --git a/app/Models/Service.php b/app/Models/Service.php index fb04608b8..4e585aca3 100644 --- a/app/Models/Service.php +++ b/app/Models/Service.php @@ -17,15 +17,29 @@ class Service extends BaseModel protected static function booted() { static::deleted(function ($service) { + $storagesToDelete = collect([]); foreach($service->applications()->get() as $application) { + $storages = $application->persistentStorages()->get(); + foreach ($storages as $storage) { + $storagesToDelete->push($storage); + } $application->persistentStorages()->delete(); } foreach($service->databases()->get() as $database) { + $storages = $database->persistentStorages()->get(); + foreach ($storages as $storage) { + $storagesToDelete->push($storage); + } $database->persistentStorages()->delete(); } $service->environment_variables()->delete(); $service->applications()->delete(); $service->databases()->delete(); + if ($storagesToDelete->count() > 0) { + $storagesToDelete->each(function ($storage) use ($service) { + instant_remote_process(["docker volume rm -f $storage->name"], $service->server, false); + }); + } }); } public function type() diff --git a/app/Models/StandalonePostgresql.php b/app/Models/StandalonePostgresql.php index afc5cdc91..21ec17e40 100644 --- a/app/Models/StandalonePostgresql.php +++ b/app/Models/StandalonePostgresql.php @@ -31,8 +31,8 @@ protected static function booted() static::deleted(function ($database) { $database->scheduledBackups()->delete(); $database->persistentStorages()->delete(); - instant_remote_process(['docker volume rm postgres-data-' . $database->uuid], $database->destination->server, false); $database->environment_variables()->delete(); + instant_remote_process(['docker volume rm postgres-data-' . $database->uuid], $database->destination->server, false); }); }