diff --git a/doc/architecture.md b/doc/architecture.md index 6a5c3819..fa3d123e 100644 --- a/doc/architecture.md +++ b/doc/architecture.md @@ -55,6 +55,7 @@ panel ── gstfsd on each compute, at the compute's hostname, port 16510 (HMAC | `datasource` | cloud-init metadata (OpenStack format), a VM found by its client address | | `logs` | the audit log (`Logs`) and its page | | `operations` | jobs: `Operation`, `Claim`, `WorkerLease`, the worker and the Jobs pages | +| `vmbackups` | VM backups: targets (a storage pool per compute), backups and their files, their jobs and pages | ## vrtManager diff --git a/operations/claims.py b/operations/claims.py index 2cf302ba..ebedd279 100644 --- a/operations/claims.py +++ b/operations/claims.py @@ -79,6 +79,15 @@ def quota_key(user_id): return f"quota:user:{user_id}" +def backup_target_key(target_id): + """A backup target: one copy into or out of it at a time.""" + return f"backup_target:{target_id}" + + +def backup_key(uuid): + return f"backup:{uuid}" + + @cache def _node(): """The installation's id (worker.host_identity): the same after a container diff --git a/vmbackups/__init__.py b/vmbackups/__init__.py new file mode 100644 index 00000000..e69de29b diff --git a/vmbackups/apps.py b/vmbackups/apps.py new file mode 100644 index 00000000..61732f7e --- /dev/null +++ b/vmbackups/apps.py @@ -0,0 +1,6 @@ +from django.apps import AppConfig + + +class VmbackupsConfig(AppConfig): + name = "vmbackups" + verbose_name = "Backups" diff --git a/vmbackups/migrations/0001_initial.py b/vmbackups/migrations/0001_initial.py new file mode 100644 index 00000000..4494d9a8 --- /dev/null +++ b/vmbackups/migrations/0001_initial.py @@ -0,0 +1,76 @@ +# Generated by Django 5.2.18 on 2026-11-17 04:32 + +import django.db.models.deletion +import uuid +from django.db import migrations, models + + +class Migration(migrations.Migration): + + initial = True + + dependencies = [ + ('computes', '0011_maintenance_since'), + ('instances', '0016_vm_name_length'), + ('operations', '0004_parent'), + ] + + operations = [ + migrations.CreateModel( + name='BackupTarget', + fields=[ + ('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')), + ('name', models.CharField(max_length=64, unique=True)), + ('created_at', models.DateTimeField(auto_now_add=True)), + ], + ), + migrations.CreateModel( + name='Backup', + fields=[ + ('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')), + ('uuid', models.UUIDField(default=uuid.uuid4, editable=False, unique=True)), + ('vm_uuid', models.CharField(max_length=36)), + ('vm_name', models.CharField(max_length=255)), + ('compute_name', models.CharField(max_length=255)), + ('created_at', models.DateTimeField(auto_now_add=True)), + ('finished_at', models.DateTimeField(null=True)), + ('state', models.CharField(choices=[('creating', 'creating'), ('ready', 'ready'), ('failed', 'failed'), ('deleting', 'deleting')], default='creating', max_length=16)), + ('consistency', models.CharField(blank=True, choices=[('offline', 'offline'), ('frozen', 'frozen'), ('crash', 'crash-consistent')], max_length=16)), + ('size', models.BigIntegerField(default=0)), + ('manifest', models.JSONField(default=dict)), + ('instance', models.ForeignKey(null=True, on_delete=django.db.models.deletion.SET_NULL, related_name='backups', to='instances.instance')), + ('operation', models.ForeignKey(null=True, on_delete=django.db.models.deletion.SET_NULL, to='operations.operation')), + ('target', models.ForeignKey(on_delete=django.db.models.deletion.PROTECT, related_name='backups', to='vmbackups.backuptarget')), + ], + options={ + 'ordering': ['-created_at'], + }, + ), + migrations.CreateModel( + name='BackupFile', + fields=[ + ('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')), + ('role', models.CharField(choices=[('disk', 'disk'), ('nvram', 'nvram'), ('domain', 'domain'), ('manifest', 'manifest')], max_length=16)), + ('dev', models.CharField(blank=True, max_length=32)), + ('volume', models.CharField(max_length=255)), + ('format', models.CharField(blank=True, max_length=16)), + ('capacity', models.BigIntegerField(default=0)), + ('size', models.BigIntegerField(default=0)), + ('sha256', models.CharField(blank=True, max_length=64)), + ('backup', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, related_name='files', to='vmbackups.backup')), + ], + ), + migrations.CreateModel( + name='BackupTargetPool', + fields=[ + ('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')), + ('pool', models.CharField(max_length=128)), + ('pool_uuid', models.CharField(max_length=36)), + ('compute', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to='computes.compute')), + ('target', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, related_name='pools', to='vmbackups.backuptarget')), + ], + options={ + 'constraints': [models.UniqueConstraint(fields=('target', 'compute'), name='one_pool_per_target_and_compute')], + }, + ), + ] diff --git a/vmbackups/migrations/__init__.py b/vmbackups/migrations/__init__.py new file mode 100644 index 00000000..e69de29b diff --git a/vmbackups/models.py b/vmbackups/models.py new file mode 100644 index 00000000..db156d5a --- /dev/null +++ b/vmbackups/models.py @@ -0,0 +1,78 @@ +import uuid + +from django.db import models + +from computes.models import Compute +from instances.models import Instance +from operations.models import Operation + + +class BackupTarget(models.Model): + """Storage the backups go to: a pool on each compute that sees it.""" + + name = models.CharField(max_length=64, unique=True) + created_at = models.DateTimeField(auto_now_add=True) + + def __str__(self): + return self.name + + def pool_on(self, compute): + return self.pools.filter(compute=compute).first() + + +class BackupTargetPool(models.Model): + """The target's pool on one compute; its UUID is checked before each use, + so a pool replaced by another of the same name is refused. A removed + compute takes its pool row with it: the backups stay in the target.""" + + target = models.ForeignKey(BackupTarget, on_delete=models.CASCADE, related_name="pools") + compute = models.ForeignKey(Compute, on_delete=models.CASCADE) + pool = models.CharField(max_length=128) + pool_uuid = models.CharField(max_length=36) + + class Meta: + constraints = [models.UniqueConstraint(fields=["target", "compute"], name="one_pool_per_target_and_compute")] + + +class Backup(models.Model): + CREATING, READY, FAILED, DELETING = "creating", "ready", "failed", "deleting" + STATES = [(CREATING, "creating"), (READY, "ready"), (FAILED, "failed"), (DELETING, "deleting")] + OFFLINE, FROZEN, CRASH = "offline", "frozen", "crash" + CONSISTENCIES = [(OFFLINE, "offline"), (FROZEN, "frozen"), (CRASH, "crash-consistent")] + + uuid = models.UUIDField(unique=True, default=uuid.uuid4, editable=False) + target = models.ForeignKey(BackupTarget, on_delete=models.PROTECT, related_name="backups") + instance = models.ForeignKey(Instance, on_delete=models.SET_NULL, null=True, related_name="backups") + # kept when the VM row is gone + vm_uuid = models.CharField(max_length=36) + vm_name = models.CharField(max_length=255) + compute_name = models.CharField(max_length=255) + created_at = models.DateTimeField(auto_now_add=True) + finished_at = models.DateTimeField(null=True) + state = models.CharField(max_length=16, choices=STATES, default=CREATING) + consistency = models.CharField(max_length=16, choices=CONSISTENCIES, blank=True) + size = models.BigIntegerField(default=0) # bytes in the target + manifest = models.JSONField(default=dict) + operation = models.ForeignKey(Operation, on_delete=models.SET_NULL, null=True) + + class Meta: + ordering = ["-created_at"] + + @property + def prefix(self): + """Every file of the backup in the target is named from its UUID only.""" + return f"wvc-{self.uuid}" + + +class BackupFile(models.Model): + DISK, NVRAM, DOMAIN, MANIFEST = "disk", "nvram", "domain", "manifest" + ROLES = [(DISK, "disk"), (NVRAM, "nvram"), (DOMAIN, "domain"), (MANIFEST, "manifest")] + + backup = models.ForeignKey(Backup, on_delete=models.CASCADE, related_name="files") + role = models.CharField(max_length=16, choices=ROLES) + dev = models.CharField(max_length=32, blank=True) + volume = models.CharField(max_length=255) + format = models.CharField(max_length=16, blank=True) + capacity = models.BigIntegerField(default=0) + size = models.BigIntegerField(default=0) + sha256 = models.CharField(max_length=64, blank=True) diff --git a/vmbackups/tests/__init__.py b/vmbackups/tests/__init__.py new file mode 100644 index 00000000..e69de29b diff --git a/vmbackups/tests/test_models.py b/vmbackups/tests/test_models.py new file mode 100644 index 00000000..9ddabd8b --- /dev/null +++ b/vmbackups/tests/test_models.py @@ -0,0 +1,58 @@ +from uuid import UUID + +from django.db import IntegrityError +from django.db.models import ProtectedError +from django.test import TestCase + +from computes.models import Compute +from instances.models import Instance +from operations.claims import backup_key, backup_target_key +from vmbackups.models import Backup, BackupTarget, BackupTargetPool + + +class BackupModelTests(TestCase): + def setUp(self): + self.compute = Compute.objects.create(name="h1", hostname="192.0.2.10", login="u", password="p", type=1) + self.target = BackupTarget.objects.create(name="nfs1") + + def backup(self, instance=None): + return Backup.objects.create( + target=self.target, instance=instance, vm_uuid="11111111-1111-1111-1111-111111111111", vm_name="vm1", compute_name="h1" + ) + + def test_a_backup_keeps_its_vm_when_the_vm_row_goes(self): + vm = Instance.objects.create(compute=self.compute, name="vm1", uuid="11111111-1111-1111-1111-111111111111") + backup = self.backup(vm) + vm.delete() + backup.refresh_from_db() + self.assertIsNone(backup.instance) + self.assertEqual((backup.vm_name, backup.vm_uuid), ("vm1", "11111111-1111-1111-1111-111111111111")) + + def test_a_target_with_backups_cannot_be_deleted(self): + self.backup() + with self.assertRaises(ProtectedError): + self.target.delete() + + def test_one_pool_per_target_and_compute(self): + BackupTargetPool.objects.create(target=self.target, compute=self.compute, pool="b1", pool_uuid="u1") + self.assertEqual(self.target.pool_on(self.compute).pool, "b1") + with self.assertRaises(IntegrityError): + BackupTargetPool.objects.create(target=self.target, compute=self.compute, pool="b2", pool_uuid="u2") + + def test_a_removed_compute_takes_its_pool_row(self): + BackupTargetPool.objects.create(target=self.target, compute=self.compute, pool="b1", pool_uuid="u1") + self.backup() + self.compute.delete() + self.assertFalse(self.target.pools.exists()) + self.assertEqual(self.target.backups.count(), 1) + + def test_a_target_without_a_pool_on_a_compute(self): + self.assertIsNone(self.target.pool_on(self.compute)) + + def test_prefix(self): + backup = Backup(uuid=UUID("00000000-0000-4000-8000-000000000001")) + self.assertEqual(backup.prefix, "wvc-00000000-0000-4000-8000-000000000001") + + def test_claim_keys(self): + self.assertEqual(backup_target_key(3), "backup_target:3") + self.assertEqual(backup_key("00000000-0000-4000-8000-000000000001"), "backup:00000000-0000-4000-8000-000000000001") diff --git a/webvirtcloud/settings.py.template b/webvirtcloud/settings.py.template index 03ab1aee..d0ae3431 100644 --- a/webvirtcloud/settings.py.template +++ b/webvirtcloud/settings.py.template @@ -97,6 +97,7 @@ INSTALLED_APPS = [ "virtsecrets", "logs", "operations", + "vmbackups", "rest_framework", "knox", "axes",