1
0
Fork 0
mirror of https://github.com/retspen/webvirtcloud synced 2026-12-02 08:56:07 +00:00

feat(vmbackups): the backups app and its records

A new app holds VM backups: a backup target (a storage pool on each
compute that sees it, its UUID kept to refuse a replaced pool), the
backups (kept with the VM's name and UUID when the VM row goes) and their
files. Claim keys for a target and a backup. Nothing uses them yet.

Installs with their own settings.py add "vmbackups" to INSTALLED_APPS.
This commit is contained in:
catborise 2026-11-17 07:30:37 +03:00
parent a03f12aa5b
commit c7d6f90823
10 changed files with 229 additions and 0 deletions

View file

@ -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

View file

@ -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

0
vmbackups/__init__.py Normal file
View file

6
vmbackups/apps.py Normal file
View file

@ -0,0 +1,6 @@
from django.apps import AppConfig
class VmbackupsConfig(AppConfig):
name = "vmbackups"
verbose_name = "Backups"

View file

@ -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')],
},
),
]

View file

78
vmbackups/models.py Normal file
View file

@ -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)

View file

View file

@ -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")

View file

@ -97,6 +97,7 @@ INSTALLED_APPS = [
"virtsecrets",
"logs",
"operations",
"vmbackups",
"rest_framework",
"knox",
"axes",