mirror of
https://github.com/retspen/webvirtcloud
synced 2026-12-03 17:36:05 +00:00
feat(vmbackups): a schedule of every VM with a tag
A backup schedule backs up a compute's VMs, chosen VMs, or now every VM with a tag: those that have it at each run, templates left out, so a VM given the tag is backed up from the next day on. The form checks the target's pools on the hosts of the tag's VMs now; a VM tagged later on a host without one fails its run with that reason, as a compute's new VM does. The schedules list says "VMs tagged prod". A tag a schedule uses is not deleted: the Tags page says which schedules back up its VMs.
This commit is contained in:
parent
a492031d12
commit
f8cb6f1e0f
15 changed files with 157 additions and 46 deletions
|
|
@ -72,14 +72,14 @@ class RestoreDiskForm(forms.Form):
|
|||
|
||||
|
||||
class PolicyForm(forms.ModelForm):
|
||||
"""A backup schedule: a compute's VMs or chosen VMs, all on hosts that
|
||||
have a pool of its target."""
|
||||
"""A backup schedule: a compute's VMs, chosen VMs or a tag's VMs, all on
|
||||
hosts that have a pool of its target (a tag's: those that have it now)."""
|
||||
|
||||
class Meta:
|
||||
model = BackupPolicy
|
||||
fields = ["name", "target", "compute", "instances", "time", "keep", "full_every", "frozen", "enabled"]
|
||||
fields = ["name", "target", "compute", "instances", "tag", "time", "keep", "full_every", "frozen", "enabled"]
|
||||
labels = {"name": _("Name"), "target": _("Backup target"), "compute": _("Every VM of a compute"),
|
||||
"instances": _("Or these VMs"), "time": _("Daily at"), "keep": _("Backups kept per VM"),
|
||||
"instances": _("Or these VMs"), "tag": _("Or every VM with a tag"), "time": _("Daily at"), "keep": _("Backups kept per VM"),
|
||||
"full_every": _("A full backup every N backups"),
|
||||
"frozen": _("Require frozen file systems"), "enabled": _("Enabled")}
|
||||
widgets = {"time": forms.TimeInput(attrs={"type": "time"}, format="%H:%M"),
|
||||
|
|
@ -98,11 +98,16 @@ class PolicyForm(forms.ModelForm):
|
|||
|
||||
def clean(self):
|
||||
data = super().clean()
|
||||
compute, vms, target = data.get("compute"), data.get("instances"), data.get("target")
|
||||
if bool(compute) == bool(vms):
|
||||
raise forms.ValidationError(_("Choose a compute or VMs, not both"))
|
||||
compute, vms, tag, target = data.get("compute"), data.get("instances"), data.get("tag"), data.get("target")
|
||||
if [bool(compute), bool(vms), bool(tag)].count(True) != 1:
|
||||
raise forms.ValidationError(_("Choose a compute, VMs or a tag: one of them"))
|
||||
if target is not None:
|
||||
hosts = {compute} if compute else {vm.compute for vm in vms}
|
||||
if compute:
|
||||
hosts = {compute}
|
||||
elif tag:
|
||||
hosts = {vm.compute for vm in tag.instances.filter(is_template=False).select_related("compute")}
|
||||
else:
|
||||
hosts = {vm.compute for vm in vms}
|
||||
for host in sorted(hosts, key=lambda c: c.name):
|
||||
if target.pool_on(host) is None:
|
||||
raise forms.ValidationError(_("Backup target %(target)s has no storage pool on %(compute)s"),
|
||||
|
|
|
|||
20
vmbackups/migrations/0008_policy_tag.py
Normal file
20
vmbackups/migrations/0008_policy_tag.py
Normal file
|
|
@ -0,0 +1,20 @@
|
|||
# Generated by Django 5.2.18 on 2026-11-18 18:22
|
||||
|
||||
import django.db.models.deletion
|
||||
from django.db import migrations, models
|
||||
|
||||
|
||||
class Migration(migrations.Migration):
|
||||
|
||||
dependencies = [
|
||||
('computes', '0012_tags'),
|
||||
('vmbackups', '0007_backup_prefix'),
|
||||
]
|
||||
|
||||
operations = [
|
||||
migrations.AddField(
|
||||
model_name='backuppolicy',
|
||||
name='tag',
|
||||
field=models.ForeignKey(blank=True, null=True, on_delete=django.db.models.deletion.PROTECT, related_name='backup_policies', to='computes.tag'),
|
||||
),
|
||||
]
|
||||
|
|
@ -4,7 +4,7 @@ import uuid
|
|||
from django.conf import settings
|
||||
from django.db import models
|
||||
|
||||
from computes.models import Compute
|
||||
from computes.models import Compute, Tag
|
||||
from instances.models import Instance
|
||||
from operations.models import Operation
|
||||
from vrtManager.backup import backup_prefix
|
||||
|
|
@ -40,7 +40,7 @@ class BackupTargetPool(models.Model):
|
|||
|
||||
|
||||
class BackupPolicy(models.Model):
|
||||
"""A schedule: its VMs (those chosen, or every VM of a compute) backed
|
||||
"""A schedule: its VMs (those chosen, every VM of a compute, or every VM with a tag) backed
|
||||
up into its target once a day at its time, the newest keep of each VM
|
||||
kept. Its jobs run as its owner, a superuser."""
|
||||
|
||||
|
|
@ -48,6 +48,8 @@ class BackupPolicy(models.Model):
|
|||
target = models.ForeignKey(BackupTarget, on_delete=models.PROTECT, related_name="policies")
|
||||
compute = models.ForeignKey(Compute, on_delete=models.CASCADE, null=True, blank=True)
|
||||
instances = models.ManyToManyField(Instance, blank=True, related_name="backup_policies")
|
||||
# a tag a schedule backs up is not deleted: the schedule would back up nothing
|
||||
tag = models.ForeignKey(Tag, on_delete=models.PROTECT, null=True, blank=True, related_name="backup_policies")
|
||||
time = models.TimeField() # in the panel's time zone
|
||||
keep = models.PositiveSmallIntegerField()
|
||||
full_every = models.PositiveSmallIntegerField(default=1) # a full backup every N; the others incremental
|
||||
|
|
@ -63,9 +65,11 @@ class BackupPolicy(models.Model):
|
|||
return self.name
|
||||
|
||||
def vms(self):
|
||||
"""Its VMs now: a compute's are those it has at this moment, templates left out."""
|
||||
"""Its VMs now: a compute's or a tag's are those it has at this moment, templates left out."""
|
||||
if self.compute_id:
|
||||
return Instance.objects.filter(compute_id=self.compute_id, is_template=False).order_by("name")
|
||||
if self.tag_id:
|
||||
return Instance.objects.filter(tags=self.tag_id, is_template=False).order_by("name")
|
||||
return self.instances.select_related("compute").order_by("name")
|
||||
|
||||
|
||||
|
|
|
|||
|
|
@ -21,7 +21,7 @@
|
|||
{% for policy in policies %}
|
||||
<tr>
|
||||
<td><a href="{% url 'vmbackups:policy' policy.id %}">{{ policy.name }}</a></td>
|
||||
<td>{% if policy.compute %}{% blocktrans with compute=policy.compute.name %}Every VM of {{ compute }}{% endblocktrans %}{% else %}{% blocktrans count counter=policy.vm_count %}{{ counter }} VM{% plural %}{{ counter }} VMs{% endblocktrans %}{% endif %}</td>
|
||||
<td>{% if policy.compute %}{% blocktrans with compute=policy.compute.name %}Every VM of {{ compute }}{% endblocktrans %}{% elif policy.tag %}{% blocktrans with tag=policy.tag.name %}VMs tagged {{ tag }}{% endblocktrans %}{% else %}{% blocktrans count counter=policy.vm_count %}{{ counter }} VM{% plural %}{{ counter }} VMs{% endblocktrans %}{% endif %}</td>
|
||||
<td>{{ policy.target.name }}</td>
|
||||
<td>{{ policy.time|time:"H:i" }}</td>
|
||||
<td class="text-end">{{ policy.keep }}</td>
|
||||
|
|
|
|||
|
|
@ -5,8 +5,9 @@
|
|||
{% bootstrap_field form.name %}
|
||||
{% bootstrap_field form.target %}
|
||||
<div class="row g-2">
|
||||
<div class="col-md-5">{% bootstrap_field form.compute %}</div>
|
||||
<div class="col-md-7">{% bootstrap_field form.instances %}</div>
|
||||
<div class="col-md-4">{% bootstrap_field form.compute %}</div>
|
||||
<div class="col-md-5">{% bootstrap_field form.instances %}</div>
|
||||
<div class="col-md-3">{% bootstrap_field form.tag %}</div>
|
||||
</div>
|
||||
<div class="row g-2">
|
||||
<div class="col-sm-6">{% blocktrans asvar zone_help %}Time zone: {{ time_zone }}{% endblocktrans %}{% bootstrap_field form.time help_text=zone_help %}</div>
|
||||
|
|
|
|||
|
|
@ -8,7 +8,7 @@ from django.test import TestCase
|
|||
from django.urls import reverse
|
||||
from lxml import html as lxml_html
|
||||
|
||||
from computes.models import Compute
|
||||
from computes.models import Compute, Tag
|
||||
from instances.models import Instance
|
||||
from instances.tests.test_page_controls import PageControls
|
||||
from logs.models import Logs
|
||||
|
|
@ -46,12 +46,21 @@ class PolicyPageTests(TestCase):
|
|||
self.post(compute=self.c1.pk)
|
||||
self.assertEqual(BackupPolicy.objects.get().compute, self.c1)
|
||||
|
||||
def test_compute_or_vms_not_both_nor_neither(self):
|
||||
for fields in ({}, {"compute": self.c1.pk, "instances": [self.vm1.pk]}):
|
||||
def test_a_schedule_of_a_tag(self):
|
||||
prod = Tag.objects.create(name="prod")
|
||||
self.vm1.tags.add(prod)
|
||||
self.post(tag=prod.pk)
|
||||
self.assertEqual(BackupPolicy.objects.get().tag, prod)
|
||||
self.assertContains(self.client.get(reverse("vmbackups:policies")), "VMs tagged prod")
|
||||
|
||||
def test_compute_vms_or_tag_one_only(self):
|
||||
prod = Tag.objects.create(name="prod")
|
||||
for fields in ({}, {"compute": self.c1.pk, "instances": [self.vm1.pk]}, {"compute": self.c1.pk, "tag": prod.pk},
|
||||
{"instances": [self.vm1.pk], "tag": prod.pk}):
|
||||
with self.subTest(fields):
|
||||
response = self.post(**fields)
|
||||
self.assertEqual(response.status_code, 200)
|
||||
self.assertContains(response, "Choose a compute or VMs")
|
||||
self.assertContains(response, "Choose a compute, VMs or a tag: one of them")
|
||||
self.assertFalse(BackupPolicy.objects.exists())
|
||||
|
||||
def test_vms_on_a_host_without_the_target_are_refused(self):
|
||||
|
|
@ -59,6 +68,9 @@ class PolicyPageTests(TestCase):
|
|||
self.assertContains(response, "has no storage pool on h2")
|
||||
response = self.post(compute=self.c2.pk)
|
||||
self.assertContains(response, "has no storage pool on h2")
|
||||
prod = Tag.objects.create(name="prod")
|
||||
self.vm3.tags.add(prod)
|
||||
self.assertContains(self.post(tag=prod.pk), "has no storage pool on h2") # the tag's VMs now
|
||||
self.assertFalse(BackupPolicy.objects.exists())
|
||||
|
||||
def test_the_list_leads_to_the_new_schedule_page(self):
|
||||
|
|
@ -151,8 +163,12 @@ class PolicyTabTests(PageControls, TestCase):
|
|||
chosen.instances.add(self.instance)
|
||||
BackupPolicy.objects.create(name="whole-host", target=target, compute=self.compute, time=datetime.time(3), keep=2,
|
||||
owner=admin)
|
||||
prod = Tag.objects.create(name="prod")
|
||||
self.instance.tags.add(prod)
|
||||
BackupPolicy.objects.create(name="tagged", target=target, tag=prod, time=datetime.time(4), keep=2, owner=admin)
|
||||
with patch("vmbackups.utils.wvmBackup"):
|
||||
self.controls(admin)
|
||||
tab = lxml_html.fromstring(self.last_page).xpath("string(//div[@id='backups'])")
|
||||
self.assertIn("chosen", tab)
|
||||
self.assertIn("whole-host", tab)
|
||||
self.assertIn("tagged", tab)
|
||||
|
|
|
|||
|
|
@ -40,5 +40,17 @@ class PolicyModelTests(TestCase):
|
|||
keep=3, owner=self.admin)
|
||||
self.assertEqual([vm.name for vm in whole.vms()], ["vm1", "vm2"]) # the compute's VMs now, no template
|
||||
|
||||
def test_the_vms_of_a_tag_s_policy(self):
|
||||
from computes.models import Tag
|
||||
prod = Tag.objects.create(name="prod")
|
||||
self.vm.tags.add(prod)
|
||||
Instance.objects.create(compute=self.compute, name="tpl", uuid="61616161-0000-4000-8000-000000000004",
|
||||
is_template=True).tags.add(prod)
|
||||
tagged = BackupPolicy.objects.create(name="prod", target=self.target, tag=prod, time=datetime.time(3), keep=3,
|
||||
owner=self.admin)
|
||||
self.assertEqual([vm.name for vm in tagged.vms()], ["vm1"])
|
||||
Instance.objects.create(compute=self.compute, name="vm0", uuid="61616161-0000-4000-8000-000000000005").tags.add(prod)
|
||||
self.assertEqual([vm.name for vm in tagged.vms()], ["vm0", "vm1"]) # the VMs with it now
|
||||
|
||||
def test_missing_state(self):
|
||||
self.assertIn(Backup.MISSING, dict(Backup.STATES))
|
||||
|
|
|
|||
|
|
@ -71,6 +71,6 @@ def vm_tab(instance, user):
|
|||
except libvirtError as err:
|
||||
problem = str(err)
|
||||
schedules = [] if owner else BackupPolicy.objects.filter(
|
||||
Q(instances=instance) | Q(compute=instance.compute_id)).distinct().order_by("name")
|
||||
Q(instances=instance) | Q(compute=instance.compute_id) | Q(tag__instances=instance)).distinct().order_by("name")
|
||||
return {"rows": rows, "targets": targets, "problem": problem, "schedules": schedules,
|
||||
"pending_deletes": pending_deletes()}
|
||||
|
|
|
|||
|
|
@ -311,7 +311,7 @@ def restore_disk(request, uuid):
|
|||
@superuser_only
|
||||
def policies(request):
|
||||
"""The backup schedules."""
|
||||
rows = BackupPolicy.objects.select_related("target", "compute", "owner").annotate(vm_count=Count("instances"))
|
||||
rows = BackupPolicy.objects.select_related("target", "compute", "tag", "owner").annotate(vm_count=Count("instances"))
|
||||
return render(request, "vmbackups/policies.html", {"policies": rows.order_by("name")})
|
||||
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue