1
0
Fork 0
mirror of https://github.com/retspen/webvirtcloud synced 2026-12-03 17:36:05 +00:00

feat(jobs): deleting a VM is a job that recovery finishes

The delete ran in the request: undefine, then the VM's record, then its
disk files. A request that died in between (a gunicorn worker past its
timeout, OOM, a restart), or a database write that failed after the
undefine, left a record that later showed as removed outside the panel,
and the disk files the user asked to delete were never deleted.

Deleting a VM is now an instance.delete job. It holds the compute
exclusively, as the request did, so no other VM attaches a file it is
about to delete. The job chooses the files before it stops the VM (a host
that cannot be checked still stops nothing) and records them first. It
then stops and undefines the VM, removes its record, and deletes the
files, each with its own Logs row. Recovery decides from libvirt: a VM
still on its host means the delete failed; a VM gone means libvirt did
it, so recovery removes the record and deletes the recorded files that
are still there.

The page queues the job and returns to the VM page, where the job panel
follows it and opens the VM list when it ends; disks that were kept are
the result's warning. The API's delete answers 202 with the job and
needs a submission_key. backing_path moves to wvmConnect, as recovery
has no domain to ask.
This commit is contained in:
catborise 2026-11-06 09:49:31 +03:00
parent aa8ac00c40
commit 0582e4173a
19 changed files with 923 additions and 764 deletions

View file

@ -42,10 +42,6 @@ from django.views.decorators.debug import sensitive_post_parameters
from libvirt import (VIR_DOMAIN_PAUSED,
VIR_DOMAIN_RUNNING,
VIR_DOMAIN_SHUTOFF,
VIR_DOMAIN_UNDEFINE_KEEP_NVRAM,
VIR_DOMAIN_UNDEFINE_MANAGED_SAVE,
VIR_DOMAIN_UNDEFINE_SNAPSHOTS_METADATA,
VIR_DOMAIN_UNDEFINE_NVRAM,
VIR_DOMAIN_XML_INACTIVE,
VIR_ERR_NO_DOMAIN,
VIR_ERR_NO_SUPPORT,
@ -861,62 +857,21 @@ def force_off(request, pk):
)
@serialize_compute_mutation
def destroy(request, pk):
if request.method in ["POST", "DELETE"]:
instance = get_instance(request.user, pk, perm_type="delete")
# an API request is a DRF request whose body is in .data; refused
# options stop nothing
options = DestroyOptionsSerializer(data=getattr(request, "data", request.POST))
get_instance(request.user, pk, perm_type="delete")
options = DestroyOptionsSerializer(data=request.POST)
options.is_valid(raise_exception=True)
with libvirt_instance_lock(instance):
with audit_vm(request, instance, "delete"):
proxy = instance.proxy
# the files to delete are known before the VM is stopped: a
# host that cannot be checked refuses the destruction
to_delete, shared = [], []
if options.validated_data["delete_disk"]:
to_delete, shared = proxy.split_disk_paths_by_use(
lambda own: utils.paths_used_on_other_computes(instance.compute, own))
# Paused and other active states must stop too, or the VM keeps
# running as a transient domain after the undefine.
if proxy.instance.isActive():
proxy.force_shutdown()
# Undefine before deleting any disk: if it fails, nothing is lost.
# Snapshot metadata goes with the domain; internal snapshot data
# stays in the disk images that are kept.
flags = VIR_DOMAIN_UNDEFINE_MANAGED_SAVE | VIR_DOMAIN_UNDEFINE_SNAPSHOTS_METADATA
if options.validated_data["delete_nvram"]:
flags |= VIR_DOMAIN_UNDEFINE_NVRAM
else:
flags |= VIR_DOMAIN_UNDEFINE_KEEP_NVRAM
proxy.delete(flags)
instance.delete()
# each disk has its own row: the VM is gone even if one is kept; a
# file that stays keeps the files below it, which it needs
kept = set()
for path in to_delete:
if path in kept:
messages.warning(request, _("Disk %(path)s was kept: a disk file that stays needs it") % {"path": path})
continue
try:
with audit_vm(request, instance, f"delete volume {path}"):
proxy.get_volume_by_path(path).delete(0)
except libvirtError as err:
messages.error(request, _("The VM is removed, but disk %(path)s was not deleted: %(err)s") % {
"path": path, "err": error_text(request, err)})
try:
while (path := proxy.backing_path(path)) and path not in kept:
kept.add(path)
except libvirtError:
# the files it needs are unknown: none of the rest goes
kept.update(to_delete)
for path in shared:
messages.warning(request, _("Disk %(path)s is used by another VM and was kept") % {"path": path})
return redirect(reverse("instances:index"))
try:
op = jobs.submit("instance.delete", request.user, {"instance": pk, **options.validated_data},
request.POST.get("submission_key", ""))
except claims.Busy as busy:
return _busy(request, pk, busy)
except (jobs.SubmissionConflict, libvirtError) as err:
messages.error(request, error_text(request, err))
return redirect(reverse("instances:destroy", args=[pk]))
jobs.remember_job(request, op)
return redirect(reverse("instances:instance", args=[pk]))
instance = get_instance(request.user, pk, perm_type="delete")
try: