lasthourcloud/app/Actions/Service/DeleteService.php

56 lines
1.8 KiB
PHP
Raw Normal View History

2023-12-08 17:32:08 +00:00
<?php
namespace App\Actions\Service;
use App\Models\Service;
2024-06-10 20:43:34 +00:00
use Lorisleiva\Actions\Concerns\AsAction;
2023-12-08 17:32:08 +00:00
class DeleteService
{
use AsAction;
2024-06-10 20:43:34 +00:00
2023-12-08 17:32:08 +00:00
public function handle(Service $service)
{
try {
$server = data_get($service, 'server');
if ($server->isFunctional()) {
$storagesToDelete = collect([]);
$service->environment_variables()->delete();
$commands = [];
foreach ($service->applications()->get() as $application) {
$storages = $application->persistentStorages()->get();
foreach ($storages as $storage) {
$storagesToDelete->push($storage);
}
}
foreach ($service->databases()->get() as $database) {
$storages = $database->persistentStorages()->get();
foreach ($storages as $storage) {
$storagesToDelete->push($storage);
}
}
foreach ($storagesToDelete as $storage) {
$commands[] = "docker volume rm -f $storage->name";
}
$commands[] = "docker rm -f $service->uuid";
2023-12-08 17:32:08 +00:00
instant_remote_process($commands, $server, false);
2023-12-08 17:32:08 +00:00
}
} catch (\Exception $e) {
throw new \Exception($e->getMessage());
} finally {
foreach ($service->applications()->get() as $application) {
$application->forceDelete();
2023-12-08 17:32:08 +00:00
}
foreach ($service->databases()->get() as $database) {
$database->forceDelete();
2024-01-29 15:03:45 +00:00
}
2024-02-14 14:14:06 +00:00
foreach ($service->scheduled_tasks as $task) {
$task->delete();
}
2024-02-02 10:50:28 +00:00
$service->tags()->detach();
2024-01-29 15:03:45 +00:00
}
2023-12-08 17:32:08 +00:00
}
}