From 86a087056e289873b474dacf0dd063b2b5e285cd Mon Sep 17 00:00:00 2001
From: ayntk-ai <122374094+ayntk-ai@users.noreply.github.com>
Date: Fri, 9 Aug 2024 02:11:42 +0200
Subject: [PATCH] fix volume deletion for services
---
app/Actions/Service/DeleteService.php | 46 ++++++++++---------
.../livewire/project/shared/danger.blade.php | 2 +-
2 files changed, 26 insertions(+), 22 deletions(-)
diff --git a/app/Actions/Service/DeleteService.php b/app/Actions/Service/DeleteService.php
index f32a44262..13b5cc50b 100644
--- a/app/Actions/Service/DeleteService.php
+++ b/app/Actions/Service/DeleteService.php
@@ -14,12 +14,10 @@ public function handle(Service $service, bool $deleteConfigurations, bool $delet
{
try {
$server = data_get($service, 'server');
-
- if ($server->isFunctional()) {
+ if ($deleteVolumes && $server->isFunctional()) {
$storagesToDelete = collect([]);
$service->environment_variables()->delete();
-
$commands = [];
foreach ($service->applications()->get() as $application) {
$storages = $application->persistentStorages()->get();
@@ -33,27 +31,33 @@ public function handle(Service $service, bool $deleteConfigurations, bool $delet
$storagesToDelete->push($storage);
}
}
-
- // Delete volumes if the flag is set
- if ($deleteVolumes) {
- foreach ($service->applications()->get() as $application) {
- $persistentStorages = $application->persistentStorages()->get();
- $application->delete_volumes($persistentStorages);
- }
+ foreach ($storagesToDelete as $storage) {
+ $commands[] = "docker volume rm -f $storage->name";
}
- // Delete networks if the flag is set
- if ($deleteConnectedNetworks) {
- $uuid = $service->uuid;
- $service->delete_connected_networks($uuid);
- }
-
- // Command to remove the service itself
- $commands[] = "docker rm -f $service->uuid";
-
// Execute all commands
- instant_remote_process($commands, $server, false);
+ if (!empty($commands)) {
+ foreach ($commands as $command) {
+ $result = instant_remote_process([$command], $server, false);
+ if ($result !== 0) {
+ ray("Failed to execute: $command");
+ }
+ }
+ }
}
+
+ // Delete networks if the flag is set
+ if ($deleteConnectedNetworks) {
+ $uuid = $service->uuid;
+ $service->delete_connected_networks($uuid);
+ }
+
+ // Command to remove the service itself
+ $commands[] = "docker rm -f $service->uuid";
+
+ // Execute all commands
+ instant_remote_process($commands, $server, false);
+
} catch (\Exception $e) {
throw new \Exception($e->getMessage());
} finally {
@@ -79,4 +83,4 @@ public function handle(Service $service, bool $deleteConfigurations, bool $delet
}
}
}
-}
+}
\ No newline at end of file
diff --git a/resources/views/livewire/project/shared/danger.blade.php b/resources/views/livewire/project/shared/danger.blade.php
index 6d00f357c..174731eea 100644
--- a/resources/views/livewire/project/shared/danger.blade.php
+++ b/resources/views/livewire/project/shared/danger.blade.php
@@ -10,7 +10,7 @@
again.