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

fix(vmbackups): full audit: backup volumes attached, ended creates, refusals

- A backup's volume is no longer attached to a VM as an existing volume:
  the VM would write into the backup, and the backup's deletion (by hand
  or retention) would remove the VM's disk.
- A backup whose create job ended without it (uncertain, then resolved)
  stayed "creating" for good and could not be deleted. Only a backup whose
  job still runs is being made; the others show as failed and are deleted.
- A VM with an NVDIMM or virtio-pmem device is refused: the host file
  behind it is not copied, and its restore would use the original's file.
- A block device's or network NVRAM is refused by its path, instead of a
  ready backup without the firmware state.
- A VM owner's restore pages link back to the VM's Backups tab, not to the
  superusers' backup list.
This commit is contained in:
catborise 2026-11-18 14:30:51 +03:00
parent e1611efd53
commit 8e0a84a3bd
17 changed files with 375 additions and 305 deletions

View file

@ -647,7 +647,7 @@ class DeleteBackup(Kind):
def plan(self, user, params):
backup = _backup(params)
self._may_delete(user, backup)
if backup.state == Backup.CREATING:
if backup.being_made:
raise util.OperationError(_("This backup is being made; cancel its job to stop it"))
if backup.depended_on():
raise util.OperationError(_("Later backups depend on this one; delete them first"))

View file

@ -116,6 +116,12 @@ class Backup(models.Model):
class Meta:
ordering = ["-created_at"]
@property
def being_made(self):
"""Its job still runs. One that ended uncertain (its host unreachable) leaves the row creating: it is
deleted like a failed one."""
return self.state == self.CREATING and self.operation_id is not None and self.operation.state in Operation.ACTIVE
def depended_on(self):
"""Whether a backup builds on this one: a failed one holds nothing."""
return self.children.filter(state__in=self.LIVING).exists()

View file

@ -7,6 +7,6 @@
{% if backup.instance_id %}<li><a class="dropdown-item" href="{% url 'vmbackups:restore_over' backup.uuid %}">{% trans "Over the VM" %}</a></li>{% endif %}
{% if user.is_superuser %}<li><a class="dropdown-item" href="{% url 'vmbackups:restore_disk' backup.uuid %}">{% trans "One disk" %}</a></li>{% endif %}
</ul>
</div>{% endif %}{% if backup.state != "creating" %}{% if user.is_superuser or backup.taken_by_id == user.id and not backup.policy_id %}<form method="post" action="{% url 'vmbackups:delete' backup.uuid %}" class="d-inline">{% csrf_token %}{% submission_key %}
</div>{% endif %}{% if not backup.being_made %}{% if user.is_superuser or backup.taken_by_id == user.id and not backup.policy_id %}<form method="post" action="{% url 'vmbackups:delete' backup.uuid %}" class="d-inline">{% csrf_token %}{% submission_key %}
<button type="submit" class="btn btn-sm btn-outline-danger" aria-label="{% trans "Delete backup" %}" title="{% trans "Delete backup" %}"
data-confirm="{% blocktrans with time=backup.created_at|date:"Y-m-d H:i" vm=backup.vm_name target=backup.target.name %}Delete the backup of {{ vm }} taken {{ time }}? Its files are removed from {{ target }}; this cannot be undone.{% endblocktrans %}">{% bs_icon 'trash' %}</button></form>{% endif %}{% endif %}</div>{% endif %}

View file

@ -6,7 +6,7 @@
{% block title %}{% trans "Restore backup" %}{% endblock %}
{% block page_heading %}<a href="{% url 'vmbackups:index' %}">{% trans "Backups" %}</a> / {% trans "Restore" %}{% endblock page_heading %}
{% block page_heading %}<a href="{% if user.is_superuser %}{% url 'vmbackups:index' %}{% else %}{% url 'instances:instance' backup.instance_id %}#backups{% endif %}">{% trans "Backups" %}</a> / {% trans "Restore" %}{% endblock page_heading %}
{% block content %}
<p>{% blocktrans with vm=backup.vm_name time=backup.created_at|date:"Y-m-d H:i" target=backup.target.name %}The backup of {{ vm }} taken {{ time }}, in {{ target }}, is restored as a new VM with its own UUID and MAC addresses.{% endblocktrans %}</p>

View file

@ -5,7 +5,7 @@
{% block title %}{% trans "Restore over the VM" %}{% endblock %}
{% block page_heading %}<a href="{% url 'vmbackups:index' %}">{% trans "Backups" %}</a> / {% trans "Restore over the VM" %}{% endblock page_heading %}
{% block page_heading %}<a href="{% if user.is_superuser %}{% url 'vmbackups:index' %}{% else %}{% url 'instances:instance' backup.instance_id %}#backups{% endif %}">{% trans "Backups" %}</a> / {% trans "Restore over the VM" %}{% endblock page_heading %}
{% block content %}
<p>{% blocktrans with vm=backup.vm_name time=backup.created_at|date:"Y-m-d H:i" target=backup.target.name %}The backup of {{ vm }} taken {{ time }}, in {{ target }}, is restored over the VM: its disks are copied as new volumes and the VM is switched to them. Its settings, MAC addresses and UUID stay.{% endblocktrans %}</p>

View file

@ -1 +1 @@
{% load i18n %}{% if backup.uuid|stringformat:"s" in pending_deletes %}<span class="badge text-bg-warning" data-backup-pending>{% trans "Deleting" %}</span>{% elif backup.state == "deleting" %}<span class="badge text-bg-warning" title="{% trans "Its deletion did not end; delete it again" %}">{% trans "Deleting" %}</span>{% elif backup.state == "ready" %}<span class="badge text-bg-success">{% trans "Ready" %}</span>{% elif backup.state == "creating" %}<span class="badge text-bg-info" data-backup-pending>{% trans "Creating" %}</span>{% if backup.operation_id %}{% if user.is_superuser or backup.operation.user_id == user.id %} <a class="small" href="{% url 'operations:job' backup.operation_id %}">{% trans "Progress" %}</a>{% endif %}{% endif %}{% elif backup.state == "missing" %}<span class="badge text-bg-danger" title="{% trans "Files of the backup are missing from the target" %}">{% trans "Missing" %}</span>{% else %}<span class="badge text-bg-danger">{% trans "Failed" %}</span>{% if backup.operation_id %}{% if user.is_superuser or backup.operation.user_id == user.id %} <a class="small" href="{% url 'operations:job' backup.operation_id %}">{% trans "Why?" %}</a>{% endif %}{% endif %}{% endif %}
{% load i18n %}{% if backup.uuid|stringformat:"s" in pending_deletes %}<span class="badge text-bg-warning" data-backup-pending>{% trans "Deleting" %}</span>{% elif backup.state == "deleting" %}<span class="badge text-bg-warning" title="{% trans "Its deletion did not end; delete it again" %}">{% trans "Deleting" %}</span>{% elif backup.state == "ready" %}<span class="badge text-bg-success">{% trans "Ready" %}</span>{% elif backup.being_made %}<span class="badge text-bg-info" data-backup-pending>{% trans "Creating" %}</span>{% if backup.operation_id %}{% if user.is_superuser or backup.operation.user_id == user.id %} <a class="small" href="{% url 'operations:job' backup.operation_id %}">{% trans "Progress" %}</a>{% endif %}{% endif %}{% elif backup.state == "missing" %}<span class="badge text-bg-danger" title="{% trans "Files of the backup are missing from the target" %}">{% trans "Missing" %}</span>{% else %}<span class="badge text-bg-danger">{% trans "Failed" %}</span>{% if backup.operation_id %}{% if user.is_superuser or backup.operation.user_id == user.id %} <a class="small" href="{% url 'operations:job' backup.operation_id %}">{% trans "Why?" %}</a>{% endif %}{% endif %}{% endif %}

View file

@ -111,11 +111,22 @@ class DeleteBackupTests(TransactionTestCase):
self.run_job(op)
self.assertEqual(self.removed, [])
def creating(self, op_state):
maker = Operation.objects.create(kind="backup.create", username="del_admin", submission_key="k", resource="r",
resource_label="vm1", state=op_state)
Backup.objects.filter(pk=self.backup.pk).update(state=Backup.CREATING, operation=maker)
def test_a_creating_backup_is_not_deleted(self):
Backup.objects.filter(pk=self.backup.pk).update(state=Backup.CREATING)
self.creating(Operation.RUNNING)
with self.assertRaisesMessage(OperationError, "being made"):
self.submit()
def test_a_backup_whose_job_ended_without_it_is_deleted(self):
# its job ended uncertain (its host unreachable) and was resolved: the row stays creating
self.creating(Operation.FAILED)
self.run_job(self.submit())
self.assertFalse(Backup.objects.filter(pk=self.backup.pk).exists())
def test_a_backup_others_build_on_is_refused_until_they_go(self):
child = Backup.objects.create(target=self.target, instance=self.vm, vm_uuid=self.vm.uuid, vm_name="vm1",
compute_name="h1", state=Backup.READY, parent=self.backup)

View file

@ -315,6 +315,9 @@ class OwnerTests(TransactionTestCase):
page = lxml_html.fromstring(self.client.get(reverse("vmbackups:restore", args=[backup.uuid])).content)
self.assertFalse(page.xpath("//select[@name='compute']|//input[@name='pool']"))
self.assertIn("On h1, in storage pool default", page.text_content())
# the backups list is the superusers': the owner goes back to the VM's Backups tab
self.assertEqual(page.xpath("//a[normalize-space()='Backups']/@href"),
[reverse("instances:instance", args=[self.vm.pk]) + "#backups"])
with (patch("vmbackups.views._vm_pool", return_value="default"),
patch("vmbackups.jobs._vm_pool", return_value="default"),
patch("vmbackups.jobs.wvmConnect", return_value=MagicMock()),

View file

@ -196,7 +196,7 @@ def create(request, instance_id):
@superuser_only
def index(request):
"""Every backup, newest first; filtered by VM name, target and state."""
rows = Backup.objects.select_related("target", "instance", "policy")
rows = Backup.objects.select_related("target", "instance", "policy", "operation")
vm, target, state = request.GET.get("vm", "").strip(), request.GET.get("target", ""), request.GET.get("state", "")
if vm:
rows = rows.filter(vm_name__icontains=vm)