1
0
Fork 0
mirror of https://github.com/retspen/webvirtcloud synced 2026-12-01 16:36:06 +00:00

fix(instances): a failed power cycle says the VM stays off; templates have no autostart

Power Cycle forced the VM off and, when the start failed, showed only
libvirt's error: the page now also says the VM was forced off and stays
off, and the audit row says so. A template kept the autostart it had as a
VM, so the host started it although the panel refuses to: marking a
template turns autostart off, and autostart is refused on a template. The
autostart switch is disabled, with the reason, on a template and on a VM
that exists only while it runs (libvirt refuses autostart there).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01Kk4h2TcUiaWmU5MUszYSea
This commit is contained in:
catborise 2026-11-11 14:28:35 +03:00
parent 0ce1139c9d
commit 7dd68aaca4
10 changed files with 259 additions and 196 deletions

View file

@ -86,6 +86,10 @@ class Instance(models.Model):
def autostart(self) -> int:
return self.proxy.get_autostart()
@cached_property
def persistent(self) -> bool:
return self.proxy.instance.isPersistent()
@cached_property
def bootmenu(self) -> bool:
return self.proxy.get_bootmenu()

View file

@ -117,10 +117,10 @@
<div class="row g-3">
<form class="col-12 col-md-6" action="{% if instance.autostart %}{% url 'instances:unset_autostart' instance.id %}{% else %}{% url 'instances:set_autostart' instance.id %}{% endif %}" method="post">{% csrf_token %}
<div class="form-check form-switch">
<input class="form-check-input" type="checkbox" role="switch" id="autostart" aria-describedby="autostart-help"{% if instance.autostart %} checked{% endif %} data-submit-on-change>
<input class="form-check-input" type="checkbox" role="switch" id="autostart" aria-describedby="autostart-help"{% if instance.autostart %} checked{% elif instance.is_template or not instance.persistent %} disabled{% endif %} data-submit-on-change>
<label class="form-check-label" for="autostart">{% trans "Autostart" %}</label>
</div>
<p class="small text-body-secondary mb-0" id="autostart-help">{% trans "Start this VM when the host starts." %}</p>
<p class="small text-body-secondary mb-0" id="autostart-help">{% if instance.autostart %}{% trans "Start this VM when the host starts." %}{% elif instance.is_template %}{% trans "A template is not started: clone it to make a VM." %}{% elif not instance.persistent %}{% trans "A VM that exists only while it runs does not start with the host." %}{% else %}{% trans "Start this VM when the host starts." %}{% endif %}</p>
</form>
<form class="col-12 col-md-6" action="{% if instance.config_bootmenu %}{% url 'instances:unset_bootmenu' instance.id %}{% else %}{% url 'instances:set_bootmenu' instance.id %}{% endif %}" method="post">{% csrf_token %}
<div class="form-check form-switch">

View file

@ -51,7 +51,7 @@ class PageControls:
UserInstance.objects.create(instance=self.instance, user=user, **owner)
return user
def controls(self, user, status=1, saved=False):
def controls(self, user, status=1, saved=False, persistent=True):
self.client.force_login(user)
with patch("instances.models.wvmInstance") as mock_wvm, patch.object(
Compute, "status", new_callable=PropertyMock, return_value=True
@ -76,6 +76,8 @@ class PageControls:
proxy.get_console_type.return_value = "vnc"
proxy.get_status.return_value = status
proxy.get_managed_save_image.return_value = saved
proxy.get_autostart.return_value = 0
proxy.instance.isPersistent.return_value = persistent
response = self.client.get(reverse("instances:instance", args=[self.instance.id]))
self.assertEqual(response.status_code, 200)
self.last_page = response.content.decode()
@ -236,6 +238,15 @@ class PageControlsTestCase(PageControls, TestCase):
self.assertTrue(fresh.get("data-confirm"))
self.assertEqual(fresh.getparent().get("action"), reverse("instances:poweron", args=[self.instance.id]))
def test_autostart_is_off_for_a_template_and_a_vm_defined_only_while_it_runs(self):
admin = get_user_model().objects.create_superuser("autostart_admin", "as@example.com", "x")
for template, persistent, disabled in ((False, True, False), (True, True, True), (False, False, True)):
with self.subTest(template=template, persistent=persistent):
Instance.objects.filter(pk=self.instance.pk).update(is_template=template)
self.controls(admin, status=5, persistent=persistent)
(switch,) = lxml_html.fromstring(self.last_page).xpath("//input[@id='autostart']")
self.assertEqual(switch.get("disabled") is not None, disabled)
def test_the_power_tab_marks_the_current_state_or_names_another(self):
admin = get_user_model().objects.create_superuser("power_strip_admin", "ps@example.com", "x")
for status, now in ((1, "Active"), (3, "Suspended"), (5, "Off"), (6, None)):

View file

@ -87,7 +87,7 @@ class TemplateFlagTestCase(TestCase):
for user in (self.owner, self.staff_owner):
UserInstance.objects.create(instance=self.instance, user=user, is_change=True)
def _post(self, user, action, data, status=302):
def _post(self, user, action, data, status=302, autostart=0):
self.client.force_login(user)
with patch("instances.models.wvmInstance") as wvm:
wvm.return_value.get_status.return_value = 5
@ -95,9 +95,11 @@ class TemplateFlagTestCase(TestCase):
wvm.return_value.get_vcpu.return_value = 1
wvm.return_value.get_memory.return_value = 1024
wvm.return_value.get_disk_devices.return_value = []
wvm.return_value.get_autostart.return_value = autostart
res = self.client.post(reverse(f"instances:{action}", args=[self.instance.id]), data)
self.assertEqual(res.status_code, status)
self.instance.refresh_from_db()
return wvm.return_value
def _make_template(self):
self.instance.is_template = True
@ -122,6 +124,13 @@ class TemplateFlagTestCase(TestCase):
self.assertEqual(list(Logs.objects.filter(message__startswith="VM").order_by("id").values_list("message", "success")),
[("VM tpl-vm: mark as template", True), ("VM tpl-vm: unmark as template", True)])
def test_a_template_does_not_start_with_the_host(self):
proxy = self._post(self.superuser, "set_template", {"is_template": "True"}, autostart=1)
self.assertTrue(self.instance.is_template)
proxy.set_autostart.assert_called_once_with(0)
proxy = self._post(self.superuser, "set_autostart", {})
proxy.set_autostart.assert_not_called()
def test_superuser_can_unmark_template(self):
self._make_template()
self._post(self.superuser, "set_template", {})

View file

@ -48,6 +48,15 @@ class VMRowTests(TestCase):
self.proxy.start.assert_called_once_with(libvirt.VIR_DOMAIN_START_FORCE_BOOT)
self.assertEqual(self.rows(), [("VM vm: power on without the saved state", True)])
def test_a_power_cycle_that_does_not_start_again_says_the_vm_stays_off(self):
self.proxy.start.side_effect = libvirtError("Cannot access storage file")
response = self.post("powercycle")
self.proxy.force_shutdown.assert_called_once_with()
self.assertEqual(self.rows(), [("VM vm: power cycle: forced off, the start failed: Cannot access storage file",
False)])
self.assertIn("The VM was forced off but did not start again: it stays off.",
[m.message for m in get_messages(response.wsgi_request)])
def test_a_raised_failure_writes_a_failed_row(self):
self.proxy.start.side_effect = libvirtError("Requested operation is not valid")
self.post("poweron")

View file

@ -825,9 +825,14 @@ def powercycle(request, pk):
if instance.is_template:
messages.warning(request, _("Templates cannot be started."))
else:
with audit_vm(request, instance, "power cycle"):
with audit_vm(request, instance, "power cycle") as entry:
instance.proxy.force_shutdown()
instance.proxy.start()
try:
instance.proxy.start()
except Exception:
entry.action = "power cycle: forced off, the start failed"
messages.warning(request, _("The VM was forced off but did not start again: it stays off."))
raise
return get_safe_redirect(
request, default=reverse("instances:instance", args=[instance.id])
)
@ -1786,6 +1791,9 @@ def set_vcpu_hotplug(request, pk):
@serialize_instance_mutation
def set_autostart(request, pk):
instance = get_instance(request.user, pk)
if instance.is_template:
messages.error(request, _("Templates cannot be started."))
return _back(request, pk, "boot_opt")
with audit_vm(request, instance, "enable autostart"):
instance.proxy.set_autostart(1)
return _back(request, pk, "boot_opt")
@ -2406,6 +2414,8 @@ def set_template(request, pk):
instance_name=instance.name))
return _back(request, pk, "options")
with audit_vm(request, instance, "mark as template" if is_template else "unmark as template"):
if is_template and instance.autostart: # the host would start it
instance.proxy.set_autostart(0)
instance.is_template = is_template
instance.save(update_fields=["is_template"])
return _back(request, pk, "options")