mirror of
https://github.com/retspen/webvirtcloud
synced 2026-12-03 01:16:10 +00:00
feat(vmbackups): VM owners back up and restore their VMs within a quota
A permission 'Can back up instances' lets a user back up the VMs they may change into targets open to VM owners, restore those backups over the VM or as a new VM, and delete the backups they took by hand. Each job checks the rights at admission and again when it runs. - A backup quota (GiB, in the user's quota) counts the owner's own backups: a backup being made holds its estimate, what its disks and the files below them hold. An owner's backups run one at a time, so each run's check sees the others. - An owner restores as a new VM on the VM's compute, into its first disk's pool, within the VM quota; the job counts in it until its VM is recorded, as a clone does. - An owner's restore over the VM removes the disks it ran from once the switch is done, unless another VM uses them: the owner cannot remove them. - Recovery never begins a deletion the job had not begun. Superusers keep everything they had.
This commit is contained in:
parent
1b0a54e3d1
commit
db4cbf806a
22 changed files with 1155 additions and 520 deletions
|
|
@ -19,6 +19,7 @@ from .models import Permission
|
|||
|
||||
# each permission's name in the user's language: the database keeps the English one
|
||||
PERMISSION_NAMES = {
|
||||
"backup_instances": _("Can back up instances"),
|
||||
"change_password": _("Can change password"),
|
||||
"clone_instances": _("Can clone instances"),
|
||||
"passwordless_console": _("Can access console without password"),
|
||||
|
|
@ -28,6 +29,8 @@ PERMISSION_NAMES = {
|
|||
|
||||
# what each permission lets a user do, shown beside its switch
|
||||
PERMISSION_HELP = {
|
||||
"backup_instances": _("Back up the VMs they may change into targets open to VM owners, restore those backups over "
|
||||
"the VM or as a new VM, and delete the backups they took, within their quotas"),
|
||||
"change_password": _("Change their own password on their profile page"),
|
||||
"clone_instances": _("Clone the VMs they may change and the templates they may view, within their quota"),
|
||||
"passwordless_console": _("Open a console without the VM's console password"),
|
||||
|
|
@ -197,7 +200,7 @@ class UserCreateForm(UserForm):
|
|||
|
||||
|
||||
# the quota's limits, with the unit each is entered in (memory in GiB, kept in MiB)
|
||||
QUOTA_UNITS = {"max_instances": "", "max_cpus": "", "max_memory": "GiB", "max_disk_size": "GiB"}
|
||||
QUOTA_UNITS = {"max_instances": "", "max_cpus": "", "max_memory": "GiB", "max_disk_size": "GiB", "max_backup_size": "GiB"}
|
||||
|
||||
|
||||
class UserAttributesForm(forms.ModelForm):
|
||||
|
|
@ -208,6 +211,7 @@ class UserAttributesForm(forms.ModelForm):
|
|||
max_cpus = forms.IntegerField(label=_("vCPUs"), min_value=0, required=False)
|
||||
max_memory = forms.DecimalField(label=_("Memory"), min_value=0, required=False)
|
||||
max_disk_size = forms.IntegerField(label=_("Disk"), min_value=0, required=False)
|
||||
max_backup_size = forms.IntegerField(label=_("Backups"), min_value=0, required=False)
|
||||
|
||||
class Meta:
|
||||
model = UserAttributes
|
||||
|
|
|
|||
|
|
@ -7,7 +7,7 @@ from django.urls import reverse
|
|||
from logs.models import Logs
|
||||
from admin.tests.edits import group_seen, user_seen
|
||||
|
||||
ATTRIBUTES = {"role": "user", "max_instances": 2, "max_cpus": 2, "max_memory": 2, "max_disk_size": 20}
|
||||
ATTRIBUTES = {"role": "user", "max_instances": 2, "max_cpus": 2, "max_memory": 2, "max_disk_size": 20, "max_backup_size": 0}
|
||||
|
||||
|
||||
def rows():
|
||||
|
|
|
|||
|
|
@ -13,7 +13,7 @@ from computes.models import Compute
|
|||
from instances.models import Instance
|
||||
|
||||
RAW = "Raw-Passw0rd-in-the-form"
|
||||
LIMITS = {"role": "user", "max_instances": 1, "max_cpus": 1, "max_memory": 1, "max_disk_size": 4}
|
||||
LIMITS = {"role": "user", "max_instances": 1, "max_cpus": 1, "max_memory": 1, "max_disk_size": 4, "max_backup_size": 0}
|
||||
|
||||
|
||||
class UserCreateTests(TestCase):
|
||||
|
|
|
|||
|
|
@ -28,7 +28,7 @@ class UserPagesTests(TestCase):
|
|||
|
||||
def form_data(self, user, **changes):
|
||||
data = {"username": user.username, "is_active": "on", "role": "superuser" if user.is_superuser else "user",
|
||||
"max_instances": "2", "max_cpus": "2", "max_memory": "2", "max_disk_size": "20", "seen": user_seen(user),
|
||||
"max_instances": "2", "max_cpus": "2", "max_memory": "2", "max_disk_size": "20", "max_backup_size": 0, "seen": user_seen(user),
|
||||
**changes}
|
||||
return {key: value for key, value in data.items() if value != ""}
|
||||
|
||||
|
|
@ -77,7 +77,7 @@ class UserPagesTests(TestCase):
|
|||
def test_a_refused_choice_is_said_beside_its_field(self):
|
||||
url = reverse("admin:user_update", args=[self.alice.pk])
|
||||
response = self.client.post(url, {"username": "up-alice", "is_active": "on", "groups": "99999", "user_permissions": "99999",
|
||||
"role": "user", "max_instances": 1, "max_cpus": 1, "max_memory": 1, "max_disk_size": 1,
|
||||
"role": "user", "max_instances": 1, "max_cpus": 1, "max_memory": 1, "max_disk_size": 1, "max_backup_size": 0,
|
||||
"seen": user_seen(self.alice)})
|
||||
page = html.fromstring(response.content)
|
||||
self.assertEqual(len(page.xpath("//fieldset//div[contains(@class, 'invalid-feedback')]")), 2)
|
||||
|
|
@ -99,13 +99,13 @@ class UserPagesTests(TestCase):
|
|||
page = html.fromstring(self.client.get(reverse("admin:user_update", args=[self.alice.pk])).content)
|
||||
shown = {box.get("name"): (box.get("value"), box.get("disabled") is not None) for box in page.xpath("//input[@type='number']")}
|
||||
self.assertEqual(shown, {"max_instances": ("", True), "max_cpus": ("1", False), "max_memory": ("1.5", False),
|
||||
"max_disk_size": ("", True)})
|
||||
"max_disk_size": ("", True), "max_backup_size": ("0", False)})
|
||||
self.assertEqual(page.xpath("//input[@checked][@data-no-limit]/@name"), ["max_instances_unlimited", "max_disk_size_unlimited"])
|
||||
|
||||
def test_a_new_user_starts_with_the_default_quota(self):
|
||||
page = html.fromstring(self.client.get(reverse("admin:user_create")).content)
|
||||
shown = {box.get("name"): box.get("value") for box in page.xpath("//input[@type='number']")}
|
||||
self.assertEqual(shown, {"max_instances": "2", "max_cpus": "2", "max_memory": "2", "max_disk_size": "20"})
|
||||
self.assertEqual(shown, {"max_instances": "2", "max_cpus": "2", "max_memory": "2", "max_disk_size": "20", "max_backup_size": "0"})
|
||||
self.assertEqual(page.xpath("//input[@name='role'][@checked]/@value"), ["user"])
|
||||
|
||||
def test_no_limit_is_kept_as_minus_one_and_gib_as_mib(self):
|
||||
|
|
@ -222,7 +222,7 @@ class SavedAsCheckedTests(TestCase):
|
|||
with self.meanwhile(take):
|
||||
response = self.client.post(reverse("admin:user_create"), {
|
||||
"username": "sc-bob", "email": "bob@example.com", "password": "Wvc-bob-pass-9", "role": "user",
|
||||
"max_instances": 1, "max_cpus": 1, "max_memory": 1, "max_disk_size": 1})
|
||||
"max_instances": 1, "max_cpus": 1, "max_memory": 1, "max_disk_size": 1, "max_backup_size": 0})
|
||||
self.assertEqual(response.status_code, 200)
|
||||
self.assertFalse(get_user_model().objects.filter(username="sc-bob").exists())
|
||||
|
||||
|
|
@ -231,7 +231,7 @@ class SavedAsCheckedTests(TestCase):
|
|||
alice = get_user_model().objects.create_user("sc-alice")
|
||||
url = reverse("admin:user_update", args=[alice.pk])
|
||||
data = {"username": "sc-alice", "is_active": "on", "role": "user", "max_instances": 1, "max_cpus": 1,
|
||||
"max_memory": 1, "max_disk_size": 1, "seen": user_seen(alice)}
|
||||
"max_memory": 1, "max_disk_size": 1, "max_backup_size": 0, "seen": user_seen(alice)}
|
||||
with self.meanwhile(lambda: get_user_model().objects.filter(pk=alice.pk).update(is_active=False)):
|
||||
response = self.client.post(url, data)
|
||||
self.assertRedirects(response, url, fetch_redirect_response=False)
|
||||
|
|
@ -244,7 +244,7 @@ class SavedAsCheckedTests(TestCase):
|
|||
with patch("admin.views.users_lock", side_effect=claims.Busy("users")):
|
||||
response = self.client.post(reverse("admin:user_create"), {
|
||||
"username": "sc-carl", "password": "Wvc-carl-pass-9", "role": "user",
|
||||
"max_instances": 1, "max_cpus": 1, "max_memory": 1, "max_disk_size": 1})
|
||||
"max_instances": 1, "max_cpus": 1, "max_memory": 1, "max_disk_size": 1, "max_backup_size": 0})
|
||||
self.assertContains(response, "short operation")
|
||||
self.assertFalse(get_user_model().objects.filter(username="sc-carl").exists())
|
||||
|
||||
|
|
|
|||
|
|
@ -5,7 +5,7 @@ from django.test import TestCase
|
|||
from django.urls import reverse
|
||||
from admin.tests.edits import user_seen
|
||||
|
||||
LIMITS = {"role": "user", "max_instances": 1, "max_cpus": 1, "max_memory": 1, "max_disk_size": 4}
|
||||
LIMITS = {"role": "user", "max_instances": 1, "max_cpus": 1, "max_memory": 1, "max_disk_size": 4, "max_backup_size": 0}
|
||||
|
||||
|
||||
class UserUpdateNextTests(TestCase):
|
||||
|
|
|
|||
|
|
@ -106,7 +106,7 @@ class AdminTestCase(TestCase):
|
|||
for block in (lambda: self.client.post(reverse("admin:user_block", args=[user.id])),
|
||||
lambda: self.client.post(reverse("admin:user_update", args=[user.id]),
|
||||
{"username": "taken", "role": "user", "max_instances": 1, "max_cpus": 1,
|
||||
"max_memory": 1, "max_disk_size": 1, "seen": user_seen(user)})):
|
||||
"max_memory": 1, "max_disk_size": 1, "max_backup_size": 0, "seen": user_seen(user)})):
|
||||
_, token = ApiToken.objects.create(user)
|
||||
self.assertEqual(block().status_code, 302)
|
||||
self.client.post(reverse("admin:user_unblock", args=[user.id]))
|
||||
|
|
@ -126,7 +126,7 @@ class AdminTestCase(TestCase):
|
|||
"max_instances": 1,
|
||||
"max_cpus": 1,
|
||||
"max_memory": 1, # GiB
|
||||
"max_disk_size": 4,
|
||||
"max_disk_size": 4, "max_backup_size": 0,
|
||||
},
|
||||
)
|
||||
self.assertRedirects(response, reverse("admin:user_list"))
|
||||
|
|
@ -153,7 +153,7 @@ class AdminTestCase(TestCase):
|
|||
"max_instances": 2,
|
||||
"max_cpus": 2,
|
||||
"max_memory": 2, # GiB
|
||||
"max_disk_size": 8,
|
||||
"max_disk_size": 8, "max_backup_size": 0,
|
||||
},
|
||||
)
|
||||
self.assertRedirects(response, reverse("admin:user_list"))
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue