mirror of
https://github.com/retspen/webvirtcloud
synced 2026-12-02 08:56:07 +00:00
fix(vmbackups): seventh audit: files of a VM named after a backup, restore-over page
- A VM may be named "<another backup's prefix>-x": its backups' files then begin with that prefix too, and deleting the other backup removed them. Backup.owns() takes a file only when no other backup UUID follows the prefix; deletion and the storage pages' refusal use it. - The restore-over page failed with a server error when the VM's pool could not be read while naming the copies; it now says why.
This commit is contained in:
parent
5b05e76ebc
commit
348628f13d
6 changed files with 36 additions and 3 deletions
|
|
@ -678,7 +678,7 @@ class DeleteBackup(Kind):
|
|||
pool.refresh(0)
|
||||
manifest = f"{backup.prefix}-manifest.json"
|
||||
remove_files(pool, [manifest])
|
||||
remove_files(pool, [n for n in pool.listVolumes() if n.startswith(backup.prefix + "-") and n != manifest])
|
||||
remove_files(pool, [n for n in pool.listVolumes() if backup.owns(n) and n != manifest])
|
||||
backup.delete()
|
||||
return {}
|
||||
|
||||
|
|
|
|||
|
|
@ -1,3 +1,4 @@
|
|||
import re
|
||||
import uuid
|
||||
|
||||
from django.conf import settings
|
||||
|
|
@ -86,6 +87,9 @@ class BackupRun(models.Model):
|
|||
constraints = [models.UniqueConstraint(fields=["policy", "instance", "day"], name="one_run_per_day")]
|
||||
|
||||
|
||||
UUID_IN_NAME = re.compile(r"[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}")
|
||||
|
||||
|
||||
class Backup(models.Model):
|
||||
CREATING, READY, FAILED, DELETING, MISSING = "creating", "ready", "failed", "deleting", "missing"
|
||||
STATES = [(CREATING, "creating"), (READY, "ready"), (FAILED, "failed"), (DELETING, "deleting"), (MISSING, "missing")]
|
||||
|
|
@ -128,6 +132,11 @@ class Backup(models.Model):
|
|||
"""Whether a backup builds on this one: a failed one holds nothing."""
|
||||
return self.children.filter(state__in=self.LIVING).exists()
|
||||
|
||||
def owns(self, name):
|
||||
"""Whether the target's file name is one of this backup's: its prefix, then no other backup's UUID (a
|
||||
VM may be named "<this prefix>-x", and its backups' files begin with this prefix too)."""
|
||||
return name.startswith(self.prefix + "-") and not UUID_IN_NAME.search(name, len(self.prefix))
|
||||
|
||||
def save(self, *args, **kwargs):
|
||||
if not self.prefix:
|
||||
self.prefix = backup_prefix(self.vm_name, self.uuid)
|
||||
|
|
|
|||
|
|
@ -63,6 +63,21 @@ class DeleteBackupTests(TransactionTestCase):
|
|||
self.assertEqual(self.removed, [[f"{prefix}-manifest.json"], [f"{prefix}-vda.qcow2", f"{prefix}-domain.xml"]])
|
||||
self.assertFalse(Backup.objects.exists())
|
||||
|
||||
def test_a_vm_named_after_this_backup_keeps_its_backups_files(self):
|
||||
# a VM may be named "<this backup's prefix>-x": its backups' files begin with this prefix too
|
||||
other = f"{self.backup.prefix}-x-00000000-0000-4000-8000-00000000000a"
|
||||
self.pool.listVolumes.return_value = [*self.pool.listVolumes.return_value, f"{other}-vda.qcow2",
|
||||
f"{other}-manifest.json"]
|
||||
self.run_job(self.submit())
|
||||
self.assertNotIn(f"{other}-vda.qcow2", sum(self.removed, []))
|
||||
|
||||
def test_its_volumes_are_refused_but_not_those_of_a_vm_named_after_it(self):
|
||||
from vmbackups.utils import refuse_backup_volume
|
||||
|
||||
with self.assertRaises(OperationError):
|
||||
refuse_backup_volume(self.compute, "b1", f"{self.backup.prefix}-vda.qcow2")
|
||||
refuse_backup_volume(self.compute, "b1", f"{self.backup.prefix}-x-00000000-0000-4000-8000-00000000000a-vda.qcow2")
|
||||
|
||||
def test_the_pool_is_refreshed_before_the_manifest_is_looked_up(self):
|
||||
# another compute made the backup: this one's pool lists its files only after a refresh
|
||||
self.pool.refresh.side_effect = lambda flags: self.removed.append("refresh")
|
||||
|
|
|
|||
|
|
@ -350,6 +350,15 @@ class RestoreOverTests(TransactionTestCase):
|
|||
self.assertContains(response, "Shut the VM down")
|
||||
self.assertNotContains(response, "key-") # no form
|
||||
|
||||
def test_the_page_says_why_when_the_pool_cannot_be_read(self):
|
||||
from django.urls import reverse
|
||||
|
||||
self.dest.refresh.side_effect = error(libvirt.VIR_ERR_INTERNAL_ERROR)
|
||||
self.client.force_login(self.admin)
|
||||
response = self.client.get(reverse("vmbackups:restore_over", args=[self.backup.uuid]))
|
||||
self.assertEqual(response.status_code, 200)
|
||||
self.assertNotContains(response, "key-") # no form
|
||||
|
||||
def test_only_superusers_and_a_backup_with_its_vm(self):
|
||||
from django.urls import reverse
|
||||
|
||||
|
|
|
|||
|
|
@ -23,7 +23,7 @@ def inside_vm_pool(conn, pool):
|
|||
def refuse_backup_volume(compute, pool, volume):
|
||||
"""A volume of a backup is deleted with its backup, on the Backups pages."""
|
||||
for backup in Backup.objects.filter(target__pools__compute=compute, target__pools__pool=pool):
|
||||
if volume.startswith(backup.prefix + "-"):
|
||||
if backup.owns(volume):
|
||||
raise InUse(
|
||||
gettext_noop("Volume %(volume)s belongs to a backup of %(vm)s; delete the backup on the Backups page"),
|
||||
volume=volume,
|
||||
|
|
|
|||
|
|
@ -287,7 +287,7 @@ def restore_over(request, uuid):
|
|||
over = _over_plan(backup, proxy, owner=not request.user.is_superuser)
|
||||
names = _over_names(backup, proxy, over)
|
||||
except (OperationError, libvirtError) as err:
|
||||
problem = error_text(request, err)
|
||||
over, problem = None, error_text(request, err)
|
||||
disks = [{"dev": dev, "old": over["old"][dev], "pool": pool, "name": names[dev]}
|
||||
for dev, pool in sorted(over["disks"].items())] if over else []
|
||||
return render(request, "vmbackups/restore_over.html", {"backup": backup, "disks": disks, "over": over,
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue