mirror of
https://github.com/retspen/webvirtcloud
synced 2026-12-03 01:16:10 +00:00
feat(instances): a protected VM is not deleted nor restored over
A superuser turns a VM's protection on or off on its Settings tab, not while a job holds the VM (a deletion or a restore over it that runs would not stop). While it is on, its deletion (page, API, a queued job at its start) and a restore of a backup over it are refused; the Destroy tab, the destroy page and the restore page say so. A VM moved outside the panel keeps it. The API shows the flag read-only.
This commit is contained in:
parent
57e90e0d27
commit
adb4231db7
21 changed files with 706 additions and 491 deletions
|
|
@ -24,7 +24,8 @@ from libvirt import (VIR_DOMAIN_PAUSED, VIR_DOMAIN_RUNNING, VIR_DOMAIN_SHUTOFF,
|
|||
from accounts.models import UserAttributes
|
||||
from appsettings.settings import app_settings
|
||||
from computes.models import Compute
|
||||
from instances.jobs import MOVED, PAUSED_FROZEN, THAW_FAILED, _instance, _log, _snapshot_free_space, record_vm, vm_claims
|
||||
from instances.jobs import (MOVED, PAUSED_FROZEN, THAW_FAILED, _instance, _log, _snapshot_free_space, record_vm, unprotected,
|
||||
vm_claims)
|
||||
from instances.utils import QUOTA_UNVERIFIED, paths_used_on_other_computes, QUOTA_UNVERIFIED_TEXT, check_user_quota, get_random_mac_address, quota_message
|
||||
from instances.models import Instance
|
||||
from operations import alerts, claims
|
||||
|
|
@ -1222,6 +1223,7 @@ class RestoreOver(Kind):
|
|||
_may_use(user)
|
||||
raise util.OperationError(_("The backup's VM is no longer in the panel; restore it as a new VM"))
|
||||
_may_use(user, instance, backup.target)
|
||||
unprotected(instance)
|
||||
_target_row(backup.target, instance.compute)
|
||||
proxy = _proxy(instance)
|
||||
over = _over_plan(backup, proxy, owner=not user.is_superuser)
|
||||
|
|
@ -1238,6 +1240,9 @@ class RestoreOver(Kind):
|
|||
"time": timezone.localtime(backup.created_at).strftime("%Y-%m-%d %H:%M")},
|
||||
)
|
||||
|
||||
def precheck(self, op, ctx):
|
||||
unprotected(_instance(_backup(op.params).instance_id)) # protected since it was queued
|
||||
|
||||
def run(self, op, ctx):
|
||||
backup = _backup(op.params)
|
||||
instance = _instance(backup.instance_id)
|
||||
|
|
|
|||
|
|
@ -17,6 +17,7 @@ from lxml import etree
|
|||
|
||||
from appsettings.settings import get_settings
|
||||
from computes.models import Compute
|
||||
from django.urls import reverse
|
||||
from instances.models import Instance
|
||||
from operations import jobs
|
||||
from operations.jobs import Context
|
||||
|
|
@ -193,6 +194,20 @@ class RestoreOverTests(TransactionTestCase):
|
|||
with self.assertRaisesMessage(OperationError, "Shut the VM down"):
|
||||
self.submit()
|
||||
|
||||
def test_a_protected_vm_is_not_restored_over(self):
|
||||
Instance.objects.filter(pk=self.backup.instance_id).update(protected=True)
|
||||
with self.assertRaisesMessage(OperationError, "vm1 is protected"):
|
||||
self.submit()
|
||||
self.client.force_login(self.admin)
|
||||
page = self.client.get(reverse("vmbackups:restore_over", args=[self.backup.uuid]))
|
||||
self.assertContains(page, "vm1 is protected")
|
||||
self.assertNotContains(page, 'name="submission_key"') # no form
|
||||
Instance.objects.filter(pk=self.backup.instance_id).update(protected=False)
|
||||
op = self.submit()
|
||||
Instance.objects.filter(pk=self.backup.instance_id).update(protected=True) # after it was queued
|
||||
with self.assertRaisesMessage(OperationError, "vm1 is protected"):
|
||||
RestoreOver().precheck(op, Context(op, SimpleNamespace(identity=WORKER)))
|
||||
|
||||
def test_a_vm_with_snapshots_is_refused(self):
|
||||
self.proxy.instance.snapshotNum.return_value = 2
|
||||
with self.assertRaisesMessage(OperationError, "snapshots"):
|
||||
|
|
|
|||
|
|
@ -16,6 +16,7 @@ from django.utils.translation import gettext_noop
|
|||
from libvirt import libvirtError
|
||||
|
||||
from admin.decorators import superuser_only
|
||||
from instances.jobs import unprotected
|
||||
from instances.models import Instance
|
||||
from instances.views import get_instance
|
||||
from logs.audit import audit
|
||||
|
|
@ -299,7 +300,7 @@ def restore_over(request, uuid):
|
|||
return redirect("operations:job", op.pk)
|
||||
over, problem = None, None
|
||||
try:
|
||||
proxy = _proxy(backup.instance)
|
||||
proxy = _proxy(unprotected(backup.instance))
|
||||
over = _over_plan(backup, proxy, owner=not request.user.is_superuser)
|
||||
names = _over_names(backup, proxy, over)
|
||||
except (OperationError, libvirtError) as err:
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue