add logic

This commit is contained in:
ayntk-ai
2024-08-09 00:30:11 +02:00
parent 2f95349888
commit 0135e2b5c0
7 changed files with 94 additions and 64 deletions

View File

@@ -43,8 +43,7 @@ class StopApplication
}
if ($application->build_pack === 'dockercompose') {
$uuid = $application->uuid;
instant_remote_process(["docker network disconnect {$uuid} coolify-proxy"], $server, false);
instant_remote_process(["docker network rm {$uuid}"], $server, false);
$application->delete_connected_networks($uuid);
CleanupDocker::run($server, true);
}

View File

@@ -10,14 +10,16 @@ class DeleteService
{
use AsAction;
public function handle(Service $service, bool $deleteConfigurations, bool $deleteVolumes, bool $deleteImages, bool $deleteNetworks)
public function handle(Service $service, bool $deleteConfigurations, bool $deleteVolumes, bool $deleteImages, bool $deleteConnectedNetworks)
{
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();
@@ -31,21 +33,34 @@ class DeleteService
$storagesToDelete->push($storage);
}
}
foreach ($storagesToDelete as $storage) {
$commands[] = "docker volume rm -f $storage->name";
// Delete volumes if the flag is set
if ($deleteVolumes) {
foreach ($service->applications()->get() as $application) {
$persistentStorages = $application->persistentStorages()->get();
$application->delete_volumes($persistentStorages);
}
}
// Delete networks if the flag is set
if ($deleteConnectedNetworks) {
$uuid = $service->uuid;
instant_remote_process(["docker network disconnect {$uuid} coolify-proxy"], $server, false);
instant_remote_process(["docker network rm {$uuid}"], $server, false);
$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 {
// Delete configurations if the flag is set
if ($deleteConfigurations) {
$service->delete_configurations();
}
foreach ($service->applications()->get() as $application) {
$application->forceDelete();
}
@@ -58,7 +73,10 @@ class DeleteService
$service->tags()->detach();
$service->forceDelete();
// Run cleanup if images need to be deleted
if ($deleteImages) {
CleanupDocker::run($server, true);
}
}
}
}

View File

@@ -31,10 +31,10 @@ class DeleteResourceJob implements ShouldBeEncrypted, ShouldQueue
public function __construct(
public Application|Service|StandalonePostgresql|StandaloneRedis|StandaloneMongodb|StandaloneMysql|StandaloneMariadb|StandaloneKeydb|StandaloneDragonfly|StandaloneClickhouse $resource,
public bool $deleteConfigurations = false,
public bool $deleteVolumes = false,
public bool $deleteImages = false,
public bool $deleteNetworks = false
public bool $deleteConfigurations,
public bool $deleteVolumes,
public bool $deleteImages,
public bool $deleteConnectedNetworks
) {
}
@@ -60,7 +60,7 @@ class DeleteResourceJob implements ShouldBeEncrypted, ShouldQueue
break;
case 'service':
StopService::run($this->resource);
DeleteService::run($this->resource);
DeleteService::run($this->resource, $this->deleteConfigurations, $this->deleteVolumes, $this->deleteImages, $this->deleteConnectedNetworks);
break;
}
@@ -72,20 +72,16 @@ class DeleteResourceJob implements ShouldBeEncrypted, ShouldQueue
$this->resource?->delete_volumes($persistentStorages);
}
if ($this->deleteImages) {
// Logic to delete images
}
if ($this->deleteNetworks) {
// Logic to delete networks
}
$server = data_get($this->resource, 'server');
if ($server) {
if ($this->deleteImages && $server) {
CleanupDocker::run($server, true);
}
if ($this->deleteConnectedNetworks) {
$uuid = $this->resource->uuid; // Get the UUID from the resource
$this->resource?->delete_connected_networks($uuid); // Pass the UUID to the method
}
} catch (\Throwable $e) {
ray($e->getMessage());
send_internal_notification('ContainerStoppingJob failed with: ' . $e->getMessage());
throw $e;
} finally {

View File

@@ -18,6 +18,10 @@ class Danger extends Component
public bool $delete_volumes = true;
public bool $delete_images = true;
public bool $delete_connected_networks = true;
public ?string $modalId = null;
public function mount()
@@ -33,7 +37,13 @@ class Danger extends Component
try {
// $this->authorize('delete', $this->resource);
$this->resource->delete();
DeleteResourceJob::dispatch($this->resource, $this->delete_configurations, $this->delete_volumes);
DeleteResourceJob::dispatch(
$this->resource,
$this->delete_configurations,
$this->delete_volumes,
$this->delete_images,
$this->delete_connected_networks
);
return redirect()->route('project.resource.index', [
'project_uuid' => $this->projectUuid,

View File

@@ -173,6 +173,15 @@ class Application extends BaseModel
}
}
public function delete_connected_networks($uuid)
{
$server = data_get($this, 'destination.server');
ray($uuid);
instant_remote_process(["docker network disconnect {$uuid} coolify-proxy"], $server, false);
instant_remote_process(["docker network rm {$uuid}"], $server, false);
}
public function additional_servers()
{
return $this->belongsToMany(Server::class, 'additional_destinations')
@@ -1178,7 +1187,6 @@ class Application extends BaseModel
} else {
throw new \RuntimeException("Docker Compose file not found at: $workdir$composeFile<br><br>Check if you used the right extension (.yaml or .yml) in the compose file name.");
}
}
public function parseContainerLabels(?ApplicationPreview $preview = null)

View File

@@ -9,10 +9,9 @@
<div class="px-2">This resource will be deleted. It is not reversible. <strong class="text-error">Please think
again.</strong><br><br></div>
<h4>Actions</h4>
<x-forms.checkbox id="delete_configurations"
label="Permanently delete configuration files from the server?"></x-forms.checkbox>
<x-forms.checkbox id="delete_volumes" label="Permanently delete associated volumes?"></x-forms.checkbox>
<x-forms.checkbox id="delete_images" label="Permanently delete associated unused images?"></x-forms.checkbox>
<x-forms.checkbox id="delete_connected_networks" label="Permanently delete all connected networks, this includes predfined ones?"></x-forms.checkbox>
<x-forms.checkbox id="delete_configurations" wire:model="delete_configurations" label="Permanently delete configuration files from the server?"></x-forms.checkbox>
<x-forms.checkbox id="delete_volumes" wire:model="delete_volumes" label="Permanently delete associated volumes?"></x-forms.checkbox>
<x-forms.checkbox id="delete_images" wire:model="delete_images" label="Permanently delete associated unused images?"></x-forms.checkbox>
<x-forms.checkbox id="delete_connected_networks" wire:model="delete_connected_networks" label="Permanently delete all connected networks, this includes predefined ones?"></x-forms.checkbox>
</x-modal-confirmation>
</div>