mirror of
https://github.com/retspen/webvirtcloud
synced 2026-12-03 17:36:05 +00:00
fix(instances): a VM with a saved state says so and can start without it
A VM with a managed save image showed as plain Off; Power On restored the saved memory and the panel had no way to boot it fresh or drop the saved state. The power tab now says the VM has a saved state and offers Start Without Saved State (confirmed), which boots it with VIR_DOMAIN_START_FORCE_BOOT and so discards the image. Owners get it as they get Power On; the audit row names it.
This commit is contained in:
parent
1957a5a85a
commit
dba2c2b1c2
11 changed files with 308 additions and 225 deletions
|
|
@ -84,6 +84,13 @@ class DestroyOptionsSerializer(serializers.Serializer):
|
|||
delete_nvram = serializers.BooleanField(default=False)
|
||||
|
||||
|
||||
class PowerOnSerializer(serializers.Serializer):
|
||||
"""Power On's options, from the page's form or an API body: fresh boots
|
||||
a VM with a saved state anew and discards the saved state."""
|
||||
|
||||
fresh = serializers.BooleanField(default=False)
|
||||
|
||||
|
||||
class DestroySerializer(DestroyOptionsSerializer):
|
||||
"""The API's delete: a job, which a new submission key per request names."""
|
||||
|
||||
|
|
|
|||
|
|
@ -33,6 +33,7 @@ from .serializers import (
|
|||
InstanceDetailsSerializer,
|
||||
InstanceSerializer,
|
||||
MigrateSerializer,
|
||||
PowerOnSerializer,
|
||||
)
|
||||
|
||||
|
||||
|
|
@ -138,7 +139,7 @@ class InstanceViewSet(viewsets.ViewSet):
|
|||
return Response(OperationSerializer(op, context={"request": request}).data, status=status.HTTP_202_ACCEPTED,
|
||||
headers={"Location": reverse("operation-detail", args=[op.pk])})
|
||||
|
||||
@extend_schema(request=None, responses={200: ActionStatusSerializer, 409: ErrorSerializer})
|
||||
@extend_schema(request=PowerOnSerializer, responses={200: ActionStatusSerializer, 409: ErrorSerializer})
|
||||
@action(detail=True, methods=["post"])
|
||||
def poweron(self, request, pk=None, compute_pk=None):
|
||||
queryset = get_instance(request.user, pk, perm_type="power")
|
||||
|
|
|
|||
|
|
@ -61,6 +61,13 @@
|
|||
</div>
|
||||
</div>
|
||||
</form>
|
||||
{% if "poweron" in power_actions and instance.has_managed_save_image %}
|
||||
<form method="post" action="{% url 'instances:poweron' instance.id %}" class="d-flex flex-wrap align-items-center gap-2 border-top mt-4 pt-3" data-please-wait>{% csrf_token %}
|
||||
<span class="small me-auto">{% trans "The VM has a saved state: Power On resumes it as it was when saved." %}</span>
|
||||
<button type="submit" class="btn btn-sm btn-outline-danger" name="fresh" value="1"
|
||||
data-confirm="{% blocktrans with name=instance.name %}Start {{ name }} without its saved state? The saved state is discarded and the VM boots anew; what it had in memory is lost.{% endblocktrans %}">{% trans "Start Without Saved State" %}</button>
|
||||
</form>
|
||||
{% endif %}
|
||||
{% if "force_off" in power_actions or "powercycle" in power_actions %}
|
||||
<form method="post" class="d-flex flex-wrap align-items-center gap-2 border-top mt-4 pt-3">{% csrf_token %}
|
||||
<span class="small text-danger me-auto">{% trans "A power cut: what the guest has not written to disk is lost." %}</span>
|
||||
|
|
|
|||
|
|
@ -4,6 +4,8 @@ web forms take."""
|
|||
|
||||
from unittest.mock import patch
|
||||
|
||||
import libvirt
|
||||
|
||||
from accounts.models import UserInstance
|
||||
from computes.models import Compute
|
||||
from django.contrib.auth import get_user_model
|
||||
|
|
@ -22,6 +24,15 @@ class ApiParityTests(TestCase):
|
|||
UserInstance.objects.create(user=self.user, instance=self.vm)
|
||||
self.url = f"/api/v1/computes/{self.compute.id}/instances/{self.vm.id}/"
|
||||
|
||||
def test_a_json_body_starts_a_vm_without_its_saved_state(self):
|
||||
Instance.objects.filter(pk=self.vm.pk).update(is_template=False)
|
||||
self.client.force_login(self.user)
|
||||
with patch("instances.models.wvmInstance") as wvm:
|
||||
wvm.return_value.get_status.return_value = 5
|
||||
response = self.client.post(self.url + "poweron/", {"fresh": True}, content_type="application/json")
|
||||
self.assertEqual(response.status_code, 200)
|
||||
wvm.return_value.start.assert_called_once_with(libvirt.VIR_DOMAIN_START_FORCE_BOOT)
|
||||
|
||||
def test_a_template_is_not_started(self):
|
||||
self.client.force_login(self.user)
|
||||
with patch("instances.models.wvmInstance") as wvm:
|
||||
|
|
|
|||
|
|
@ -51,7 +51,7 @@ class PageControls:
|
|||
UserInstance.objects.create(instance=self.instance, user=user, **owner)
|
||||
return user
|
||||
|
||||
def controls(self, user, status=1):
|
||||
def controls(self, user, status=1, saved=False):
|
||||
self.client.force_login(user)
|
||||
with patch("instances.models.wvmInstance") as mock_wvm, patch.object(
|
||||
Compute, "status", new_callable=PropertyMock, return_value=True
|
||||
|
|
@ -75,6 +75,7 @@ class PageControls:
|
|||
proxy.get_hostdevs.return_value = ([], [], [])
|
||||
proxy.get_console_type.return_value = "vnc"
|
||||
proxy.get_status.return_value = status
|
||||
proxy.get_managed_save_image.return_value = saved
|
||||
response = self.client.get(reverse("instances:instance", args=[self.instance.id]))
|
||||
self.assertEqual(response.status_code, 200)
|
||||
self.last_page = response.content.decode()
|
||||
|
|
@ -225,6 +226,16 @@ class PageControlsTestCase(PageControls, TestCase):
|
|||
if status == 5:
|
||||
self.assertEqual(tab.xpath(".//form/@data-please-wait"), [""])
|
||||
|
||||
def test_a_vm_with_a_saved_state_says_so_and_can_start_without_it(self):
|
||||
owner = self.user("power_saved_owner", owner={})
|
||||
self.controls(owner, status=5, saved=True)
|
||||
(tab,) = lxml_html.fromstring(self.last_page).xpath("//div[@id='power']")
|
||||
self.assertEqual(tab.xpath(".//button/@name"), ["poweron", "fresh"])
|
||||
self.assertIn("Power On resumes it as it was when saved.", tab.text_content())
|
||||
(fresh,) = tab.xpath(".//button[@name='fresh']")
|
||||
self.assertTrue(fresh.get("data-confirm"))
|
||||
self.assertEqual(fresh.getparent().get("action"), reverse("instances:poweron", args=[self.instance.id]))
|
||||
|
||||
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)):
|
||||
|
|
|
|||
|
|
@ -43,6 +43,11 @@ class VMRowTests(TestCase):
|
|||
self.assertEqual((row.user, row.host, row.instance, row.instance_uuid, row.ip),
|
||||
("vm-admin", "c1", "vm", UUID, "192.0.2.50"))
|
||||
|
||||
def test_a_start_without_the_saved_state_boots_anew(self):
|
||||
self.post("poweron", {"fresh": "1"})
|
||||
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_raised_failure_writes_a_failed_row(self):
|
||||
self.proxy.start.side_effect = libvirtError("Requested operation is not valid")
|
||||
self.post("poweron")
|
||||
|
|
|
|||
|
|
@ -43,6 +43,7 @@ from passlib.hash import sha512_crypt
|
|||
from libvirt import (VIR_DOMAIN_PAUSED,
|
||||
VIR_DOMAIN_RUNNING,
|
||||
VIR_DOMAIN_SHUTOFF,
|
||||
VIR_DOMAIN_START_FORCE_BOOT,
|
||||
VIR_DOMAIN_XML_INACTIVE,
|
||||
VIR_ERR_NO_DOMAIN,
|
||||
VIR_ERR_NO_SUPPORT,
|
||||
|
|
@ -63,7 +64,7 @@ from vrtManager.util import randomPasswd
|
|||
from instances.models import Instance
|
||||
|
||||
from . import addresses, report, utils
|
||||
from .api.serializers import DestroyOptionsSerializer
|
||||
from .api.serializers import DestroyOptionsSerializer, PowerOnSerializer
|
||||
from .forms import ConsoleForm, FlavorForm, NewVMForm
|
||||
from .models import Flavor
|
||||
from .states import POWER_STATES, STATES, UNKNOWN, allowed_actions, state_label
|
||||
|
|
@ -800,8 +801,14 @@ def reject_disk_options(request, pk, invalid):
|
|||
@serialize_instance_mutation
|
||||
def poweron(request, pk):
|
||||
instance = _power_instance(request.user, pk)
|
||||
# the API passes its parsed body (data), the page its form
|
||||
options = PowerOnSerializer(data=getattr(request, "data", request.POST))
|
||||
options.is_valid(raise_exception=True)
|
||||
if instance.is_template:
|
||||
messages.warning(request, _("Templates cannot be started."))
|
||||
elif options.validated_data["fresh"]: # the saved state is discarded: the VM boots anew
|
||||
with audit_vm(request, instance, "power on without the saved state"):
|
||||
instance.proxy.start(VIR_DOMAIN_START_FORCE_BOOT)
|
||||
else:
|
||||
with audit_vm(request, instance, "power on"):
|
||||
instance.proxy.start()
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue