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

fix(computes): a VM that returns gets its protection, schedules and backups back

A VM that vanished from its compute, or whose compute was deleted and added
again, came back with its owners and tags only: it was no longer protected,
its schedules no longer backed it up, and its backups stayed unlinked, so its
Backups tab no longer listed them and retention left them for good. The
Removed VMs record now keeps its protection, its schedules and its own
backups (not those of a copy under the same UUID), and gives them back.
Turning protection on or off waits for the ownership step, so a VM dropped
from its host's list meanwhile is not told protected.
This commit is contained in:
catborise 2026-11-20 07:55:26 +03:00
parent 5c7687c60d
commit dc5b1dbb2a
14 changed files with 251 additions and 138 deletions

View file

@ -0,0 +1,28 @@
# Generated by Django 5.2.18 on 2026-11-20 04:58
from django.db import migrations, models
class Migration(migrations.Migration):
dependencies = [
('instances', '0019_protected'),
]
operations = [
migrations.AddField(
model_name='instancetombstone',
name='backups',
field=models.JSONField(default=list),
),
migrations.AddField(
model_name='instancetombstone',
name='policies',
field=models.JSONField(default=list),
),
migrations.AddField(
model_name='instancetombstone',
name='protected',
field=models.BooleanField(default=False),
),
]

View file

@ -410,7 +410,7 @@ class Instance(models.Model):
class InstanceTombstone(models.Model):
"""Ownership and tags of a VM that disappeared from its compute.
"""Ownership, tags, protection, schedules and backups of a VM that disappeared from its compute.
Kept for INSTANCE_OWNERSHIP_RETENTION_DAYS so that they come back
when the same UUID shows up again, on this or another compute.
@ -420,13 +420,19 @@ class InstanceTombstone(models.Model):
name = models.CharField(max_length=255)
host = models.CharField(max_length=64, blank=True) # the compute's name: the compute may be deleted
is_template = models.BooleanField(default=False)
protected = models.BooleanField(default=False)
owners = models.JSONField(default=list) # [{"user": id, "is_change": .., "is_delete": .., "is_vnc": ..}]
tags = models.JSONField(default=list) # the names: a tag deleted meanwhile is not given back
policies = models.JSONField(default=list) # the ids of the schedules that chose it: one deleted is not
backups = models.JSONField(default=list) # the ids of its backups: not a copy's under the same UUID
removed = models.DateTimeField(auto_now_add=True)
def __str__(self):
return "%s (%s)" % (self.name, self.uuid)
def keeps_anything(self):
return bool(self.owners or self.is_template or self.protected or self.tags or self.policies or self.backups)
class PermissionSet(models.Model):
"""

View file

@ -1,5 +1,7 @@
"""A protected VM: only a superuser turns its protection on or off; while on, the VM is not deleted nor
restored over (refusals in test_destroy and vmbackups.tests.test_restore_over)."""
from unittest.mock import patch
from django.contrib.auth import get_user_model
from django.test import TestCase
from django.urls import reverse
@ -50,6 +52,18 @@ class ProtectedTestCase(TestCase):
self.client.post(reverse("instances:set_protected", args=[self.instance.id]), {"protected": "True"})
self.assertFalse(Instance.objects.get(pk=self.instance.pk).protected)
def test_not_changed_when_the_vm_left_its_host_meanwhile(self):
# its record may be kept, unprotected, for the Removed VMs page: no success is told
# the first check is the claim's (serialize_instance_mutation); the VM goes after it
with patch("instances.views._moved_meanwhile", side_effect=[False, True]), \
patch("instances.views.lock_ownership") as lock:
response = self.client.post(reverse("instances:set_protected", args=[self.instance.id]),
{"protected": "True"}, HTTP_X_REQUESTED_WITH="XMLHttpRequest")
lock.assert_called_once()
self.assertEqual(response.status_code, 409)
self.assertFalse(Instance.objects.get(pk=self.instance.pk).protected)
self.assertFalse(Logs.objects.filter(instance="pe-vm").exists())
def test_the_destroy_page_offers_no_form(self):
Instance.objects.filter(pk=self.instance.pk).update(protected=True)
page = self.client.get(reverse("instances:destroy", args=[self.instance.id]))

View file

@ -2550,9 +2550,12 @@ def set_protected(request, pk):
"""Turns a VM's protection against deletion and a restore over it on or off."""
instance = get_instance(request.user, pk)
protected = bool(request.POST.get("protected"))
with audit_vm(request, instance, "protect" if protected else "unprotect"):
Instance.objects.filter(pk=instance.pk).update(protected=protected)
return _back(request, pk, "options")
with audit_vm(request, instance, "protect" if protected else "unprotect") as entry, transaction.atomic():
lock_ownership() # not while the VM's protection is kept or given back (computes.utils.forget)
entry.skip = moved = _moved_meanwhile(pk, instance) # its host's list may have dropped it meanwhile
if not moved:
Instance.objects.filter(pk=instance.pk).update(protected=protected)
return _moved(request, pk) if moved else _back(request, pk, "options")
def keyfile_value(value):