coolify/app/Livewire/Project/Shared/Danger.php

122 lines
4 KiB
PHP
Raw Normal View History

2023-05-18 11:26:35 +00:00
<?php
2023-12-07 18:06:32 +00:00
namespace App\Livewire\Project\Shared;
2023-05-18 11:26:35 +00:00
use App\Jobs\DeleteResourceJob;
2024-08-29 16:01:16 +00:00
use App\Models\Service;
use App\Models\ServiceDatabase;
use App\Models\ServiceApplication;
2023-05-18 11:26:35 +00:00
use Livewire\Component;
2023-07-13 11:16:24 +00:00
use Visus\Cuid2\Cuid2;
2024-08-27 11:44:12 +00:00
use Illuminate\Support\Facades\Hash;
use Illuminate\Support\Facades\Auth;
2023-05-18 11:26:35 +00:00
class Danger extends Component
{
2023-08-07 20:14:21 +00:00
public $resource;
2024-08-29 16:01:16 +00:00
public $resourceName;
2023-12-11 08:41:31 +00:00
public $projectUuid;
public $environmentName;
public bool $delete_configurations = true;
2024-07-11 10:38:54 +00:00
public bool $delete_volumes = true;
2024-08-27 12:19:37 +00:00
public bool $docker_cleanup = true;
2024-08-08 22:30:11 +00:00
public bool $delete_connected_networks = true;
public ?string $modalId = null;
2024-08-29 16:01:16 +00:00
public string $resourceDomain = '';
2023-05-22 10:47:15 +00:00
public function mount()
{
2023-12-11 08:41:31 +00:00
$parameters = get_route_parameters();
2024-08-29 16:01:16 +00:00
$this->modalId = new Cuid2;
2024-01-31 13:22:48 +00:00
$this->projectUuid = data_get($parameters, 'project_uuid');
$this->environmentName = data_get($parameters, 'environment_name');
2024-08-29 19:47:17 +00:00
2024-08-29 16:53:49 +00:00
ray('Mount method called');
2024-08-29 19:47:17 +00:00
2024-08-29 16:53:49 +00:00
if ($this->resource === null) {
if (isset($parameters['service_uuid'])) {
$this->resource = Service::where('uuid', $parameters['service_uuid'])->first();
} elseif (isset($parameters['stack_service_uuid'])) {
$this->resource = ServiceApplication::where('uuid', $parameters['stack_service_uuid'])->first()
?? ServiceDatabase::where('uuid', $parameters['stack_service_uuid'])->first();
}
}
2024-08-29 19:47:17 +00:00
2024-08-29 16:53:49 +00:00
ray('Resource:', $this->resource);
2024-08-29 19:47:17 +00:00
2024-08-29 16:53:49 +00:00
if ($this->resource === null) {
ray('Resource is null');
$this->resourceName = 'Unknown Resource';
return;
}
2024-08-29 19:47:17 +00:00
2024-08-29 16:53:49 +00:00
if (!method_exists($this->resource, 'type')) {
ray('Resource does not have type() method');
$this->resourceName = 'Unknown Resource';
return;
}
2024-08-29 19:47:17 +00:00
2024-08-29 16:53:49 +00:00
ray('Resource type:', $this->resource->type());
2024-08-29 19:47:17 +00:00
2024-08-29 16:53:49 +00:00
switch ($this->resource->type()) {
case 'application':
$this->resourceName = $this->resource->name ?? 'Application';
break;
case 'standalone-postgresql':
case 'standalone-redis':
case 'standalone-mongodb':
case 'standalone-mysql':
case 'standalone-mariadb':
case 'standalone-keydb':
case 'standalone-dragonfly':
case 'standalone-clickhouse':
$this->resourceName = $this->resource->name ?? 'Database';
break;
case 'service':
$this->resourceName = $this->resource->name ?? 'Service';
break;
case 'service-application':
$this->resourceName = $this->resource->name ?? 'Service Application';
break;
case 'service-database':
$this->resourceName = $this->resource->name ?? 'Service Database';
break;
default:
$this->resourceName = 'Unknown Resource';
2024-08-29 16:01:16 +00:00
}
2024-08-29 16:53:49 +00:00
ray('Final resource name:', $this->resourceName);
2023-05-22 10:47:15 +00:00
}
2024-06-10 20:43:34 +00:00
2024-08-29 16:01:16 +00:00
public function delete($password)
2023-05-22 10:47:15 +00:00
{
2024-08-27 11:44:12 +00:00
if (!Hash::check($password, Auth::user()->password)) {
$this->addError('password', 'The provided password is incorrect.');
return;
}
2024-08-29 16:01:16 +00:00
if (!$this->resource) {
$this->addError('resource', 'Resource not found.');
return;
}
2023-09-22 09:23:49 +00:00
try {
$this->resource->delete();
2024-08-08 22:30:11 +00:00
DeleteResourceJob::dispatch(
$this->resource,
$this->delete_configurations,
$this->delete_volumes,
2024-08-27 12:19:37 +00:00
$this->docker_cleanup,
2024-08-08 22:30:11 +00:00
$this->delete_connected_networks
);
2024-06-10 20:43:34 +00:00
2024-01-07 15:23:41 +00:00
return redirect()->route('project.resource.index', [
2023-12-11 08:41:31 +00:00
'project_uuid' => $this->projectUuid,
2024-06-10 20:43:34 +00:00
'environment_name' => $this->environmentName,
2023-12-27 15:45:01 +00:00
]);
2023-09-22 09:23:49 +00:00
} catch (\Throwable $e) {
return handleError($e, $this);
2023-09-22 09:23:49 +00:00
}
2023-05-22 10:47:15 +00:00
}
2023-05-18 11:26:35 +00:00
}