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

142 lines
4.8 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\ServiceApplication;
use App\Models\ServiceDatabase;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Hash;
2023-05-18 11:26:35 +00:00
use Livewire\Component;
2023-07-13 11:16:24 +00:00
use Visus\Cuid2\Cuid2;
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;
2023-12-11 08:41:31 +00:00
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
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
if ($this->resource === null) {
$this->resourceName = 'Unknown Resource';
2024-08-29 16:53:49 +00:00
return;
}
2024-08-29 19:47:17 +00:00
if (! method_exists($this->resource, 'type')) {
2024-08-29 16:53:49 +00:00
$this->resourceName = 'Unknown Resource';
2024-08-29 16:53:49 +00:00
return;
}
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
}
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
{
if (isProduction()) {
if (! Hash::check($password, Auth::user()->password)) {
$this->addError('password', 'The provided password is incorrect.');
return;
}
2024-08-27 11:44:12 +00:00
}
if (! $this->resource) {
2024-08-29 16:01:16 +00:00
$this->addError('resource', 'Resource not found.');
2024-08-29 16:01:16 +00:00
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
}
public function render()
{
return view('livewire.project.shared.danger', [
'checkboxes' => [
['id' => 'delete_volumes', 'label' => __('resource.delete_volumes')],
['id' => 'delete_connected_networks', 'label' => __('resource.delete_connected_networks')],
['id' => 'delete_configurations', 'label' => __('resource.delete_configurations')],
['id' => 'docker_cleanup', 'label' => __('resource.docker_cleanup')],
2024-09-03 16:31:06 +00:00
// ['id' => 'delete_associated_backups_locally', 'label' => 'All backups associated with this Ressource will be permanently deleted from local storage.'],
// ['id' => 'delete_associated_backups_s3', 'label' => 'All backups associated with this Ressource will be permanently deleted from the selected S3 Storage.'],
// ['id' => 'delete_associated_backups_sftp', 'label' => 'All backups associated with this Ressource will be permanently deleted from the selected SFTP Storage.']
],
]);
}
2023-05-18 11:26:35 +00:00
}