1
0
Fork 0
mirror of https://github.com/retspen/webvirtcloud synced 2026-11-11 12:46:49 +00:00

fix(instances): a running VM's options and CPU edits keep the pending changes

On an active VM the options and CPU forms write the persistent definition
but showed the running VM's title, description and vCPUs, so saving one
posted the live values back over a pending change. They now read the
persistent definition (get_vcpu, get_cur_vcpu, get_title and
get_description take config=).

Buttons in the settings and resize tabs that were disabled only by class
get the disabled attribute: Enter in a field no longer submits a form
the page shows as unavailable while the VM runs.
This commit is contained in:
catborise 2026-10-11 11:00:49 +03:00
parent ea9424ee1f
commit 042530fbfd
8 changed files with 106 additions and 37 deletions

View file

@ -166,6 +166,25 @@ class Instance(models.Model):
"""QoS of the persistent definition, which the QoS form changes."""
return self.proxy.get_all_qos(config=True)
@cached_property
def config_vcpu(self) -> int:
"""vCPUs of the persistent definition, which the CPU form changes."""
return self.proxy.get_vcpu(config=True)
@cached_property
def config_cur_vcpu(self) -> int:
return self.proxy.get_cur_vcpu(config=True)
@cached_property
def config_title(self) -> str:
"""Title and description of the persistent definition, which the
options form changes."""
return self.proxy.get_title(config=True)
@cached_property
def config_description(self) -> str:
return self.proxy.get_description(config=True)
@cached_property
def telnet_port(self):
return self.proxy.get_telnet_port()

View file

@ -30,11 +30,7 @@
<div class="col-sm-4">
<select name="cur_vcpu" class="form-select">
{% for cpu in instance.vcpu_range %}
{% if instance.cur_vcpu %}
<option value="{{ cpu }}" {% if cpu == instance.cur_vcpu %}selected{% endif %}>{{ cpu }}</option>
{% else %}
<option value="{{ cpu }}" {% if cpu == instance.vcpu %}selected{% endif %}>{{ cpu }}</option>
{% endif %}
<option value="{{ cpu }}" {% if cpu == instance.config_cur_vcpu %}selected{% endif %}>{{ cpu }}</option>
{% endfor %}
</select>
</div>
@ -44,7 +40,7 @@
<div class="col-sm-4">
<select name="vcpu" class="form-select">
{% for cpu in instance.vcpu_range %}
<option value="{{ cpu }}" {% if cpu == instance.vcpu %}selected{% endif %}>{{ cpu }}</option>
<option value="{{ cpu }}" {% if cpu == instance.config_vcpu %}selected{% endif %}>{{ cpu }}</option>
{% endfor %}
</select>
</div>
@ -53,7 +49,7 @@
{% if instance.status == 5 %}
<button type="submit" class="btn btn-lg btn-success float-end" name="resizevm_cpu">{% trans "Resize" %}</button>
{% else %}
<button class="btn btn-lg btn-success float-end disabled">{% trans "Resize" %}</button>
<button class="btn btn-lg btn-success float-end disabled" disabled>{% trans "Resize" %}</button>
{% endif %}
</form>
<div class="clearfix"></div>
@ -82,7 +78,7 @@
{% endif %}
{% else %}
{% trans "You don't have permission for resizing instance" %}
<button class="btn btn-lg btn-success float-end disabled">{% trans "Resize" %}</button>
<button class="btn btn-lg btn-success float-end disabled" disabled>{% trans "Resize" %}</button>
{% endif %}
<div class="clearfix"></div>
</div>
@ -123,7 +119,7 @@
</form>
{% else %}
{% trans "You don't have permission for resizing instance" %}
<button class="btn btn-lg btn-success float-end disabled">{% trans "Resize" %}</button>
<button class="btn btn-lg btn-success float-end disabled" disabled>{% trans "Resize" %}</button>
{% endif %}
<div class="clearfix"></div>
</div>
@ -151,12 +147,12 @@
{% if instance.status == 1 or instance.status == 3 or instance.status == 5 %}
<button type="submit" class="btn btn-lg btn-success float-end" name="resizevm_disk">{% trans "Resize" %}</button>
{% else %}
<button class="btn btn-lg btn-success float-end disabled">{% trans "Resize" %}</button>
<button class="btn btn-lg btn-success float-end disabled" disabled>{% trans "Resize" %}</button>
{% endif %}
</form>
{% else %}
{% trans "You don't have permission for resizing instance" %}
<button class="btn btn-lg btn-success float-end disabled">{% trans "Resize" %}</button>
<button class="btn btn-lg btn-success float-end disabled" disabled>{% trans "Resize" %}</button>
{% endif %}
<div class="clearfix"></div>
</div>

View file

@ -201,7 +201,7 @@
{% if instance.media_iso %}
<button type="submit" class="btn btn-sm btn-success" name="mount_iso" value="{{ cd.dev }}">{% trans "Mount" %}</button>
{% else %}
<button class="btn btn-sm btn-success disabled">{% trans "Mount" %}</button>
<button class="btn btn-sm btn-success disabled" disabled>{% trans "Mount" %}</button>
{% endif %}
{% if instance.status == 5 %}
<button type="submit" class="btn btn-sm btn-danger float-end" formaction="{% url 'instances:detach_cdrom' instance.id cd.dev %}" formmethod="post" title="{% trans "Detach CD-ROM (remove device)" %}">
@ -312,7 +312,7 @@
{% bs_icon 'eject' %}
</button>
{% else %}
<button class="btn btn-sm btn-secondary disabled" value="{{ disk.dev }}" title="{% trans "Detach" %}" onclick="return confirm('{% trans "Are you sure? This may lead data corruption!" %}')">
<button class="btn btn-sm btn-secondary disabled" disabled value="{{ disk.dev }}" title="{% trans "Detach" %}" onclick="return confirm('{% trans "Are you sure? This may lead data corruption!" %}')">
{% bs_icon 'eject' %}
</button>
{% endif %}
@ -328,7 +328,7 @@
{% bs_icon 'trash' %}
</button>
{% else %}
<button class="btn btn-sm btn-secondary disabled" title="{% trans "Delete" %}" onclick="return confirm('{% trans "Are you sure? This may lead data corruption!" %}')">
<button class="btn btn-sm btn-secondary disabled" disabled title="{% trans "Delete" %}" onclick="return confirm('{% trans "Are you sure? This may lead data corruption!" %}')">
{% bs_icon 'trash' %}
</button>
{% endif %}
@ -640,7 +640,7 @@
{% if computes_count != 1 %}
<button type="submit" class="btn btn-lg btn-success float-end" onclick="showPleaseWaitDialog()" name="migrate" >{% trans "Migrate" %}</button>
{% else %}
<button class="btn btn-lg btn-success float-end disabled">{% trans "Migrate" %}</button>
<button class="btn btn-lg btn-success float-end disabled" disabled>{% trans "Migrate" %}</button>
{% endif %}
</form>
<div class="clearfix"></div>
@ -660,7 +660,7 @@
{% trans "Change" %}
</button>
{% else %}
<button class="btn btn-lg btn-success float-end disabled">
<button class="btn btn-lg btn-success float-end disabled" disabled>
{% trans "Change" %}
</button>
{% endif %}
@ -804,7 +804,7 @@
{% if instance.status == 5 %}
<button type="submit" class="btn btn-lg btn-success float-end" name="clone" onclick="showPleaseWaitDialog();">{% trans "Clone" %}</button>
{% else %}
<button class="btn btn-lg btn-success float-end disabled" name="clone">{% trans "Clone" %}</button>
<button class="btn btn-lg btn-success float-end disabled" disabled name="clone">{% trans "Clone" %}</button>
{% endif %}
</form>
<div class="clearfix"></div>
@ -817,13 +817,13 @@
<div class="row">
<label class="col-sm-3 col-form-label">{% trans "Title" %}</label>
<div class="col-sm-6">
<input type="text" name="title" class="form-control" value="{{ instance.title }}">
<input type="text" name="title" class="form-control" value="{{ instance.config_title }}">
</div>
</div>
<div class="row">
<label class="col-sm-3 col-form-label">{% trans "Description" %}</label>
<div class="col-sm-6">
<textarea name="description" class="form-control">{{ instance.description }}</textarea>
<textarea name="description" class="form-control">{{ instance.config_description }}</textarea>
</div>
</div>
<div class="row">
@ -842,7 +842,7 @@
{% if instance.status == 5 %}
<button type="submit" class="btn btn-success" name="change_options">{% trans "Change" %}</button>
{% else %}
<button class="btn btn-success disabled" name="change_options">{% trans "Change" %}</button>
<button class="btn btn-success disabled" disabled name="change_options">{% trans "Change" %}</button>
{% endif %}
</div>
</div>
@ -863,7 +863,7 @@
{% if instance.status == 5 %}
<button type="submit" class="btn btn-success" name="set_video_model">{% trans "Set" %}</button>
{% else %}
<button class="btn btn-success disabled" name="set_video_model">{% trans "Set" %}</button>
<button class="btn btn-success disabled" disabled name="set_video_model">{% trans "Set" %}</button>
{% endif %}
</div>
</div>

View file

@ -376,6 +376,31 @@ class LiveDataLossTestCase(TestCase):
self.assertEqual((disk.find("driver").get("cache"), disk.findtext("serial")), ("none", "wvc-serial"))
self.assertEqual(dom.XMLDesc(0), live)
def test_pending_options_and_cpu_changes_are_shown_and_kept_by_the_next_edit(self):
from lxml import etree, html
dom, inst = self.vm("options-pending", [])
dom.create()
live = dom.XMLDesc(0)
def form(view):
page = self.client.get(reverse("instances:instance", args=[inst.id])).content
(form,) = html.fromstring(page).xpath("//form[@action=$a]", a=reverse(f"instances:{view}", args=[inst.id]))
return dict(form.form_values()) # what the form would post
# the buttons are disabled while it runs; a direct post is pending
self.post("change_options", inst, {**form("change_options"), "title": "wvc-title"})
self.post("resizevm_cpu", inst, {**form("resizevm_cpu"), "vcpu": "2"})
options, cpu = form("change_options"), form("resizevm_cpu")
self.assertEqual((options["title"], cpu["vcpu"]), ("wvc-title", "2"))
self.post("change_options", inst, {**options, "description": "wvc-description"})
self.post("resizevm_cpu", inst, {**cpu, "cur_vcpu": "1"})
tree = etree.fromstring(dom.XMLDesc(libvirt.VIR_DOMAIN_XML_INACTIVE))
self.assertEqual((tree.findtext("title"), tree.findtext("description"), tree.findtext("vcpu")),
("wvc-title", "wvc-description", "2"))
self.assertEqual(dom.XMLDesc(0), live)
def test_pending_qos_change_is_shown_and_kept_by_the_next_edit(self):
from lxml import etree, html

View file

@ -26,7 +26,9 @@ class PendingEditsTestCase(TestCase):
compute = Compute.objects.create(name="pe", hostname="127.0.0.1:1", login="root", password="", type=1)
self.instance = Instance.objects.create(compute=compute, name="pe-vm", uuid="aaaaaaaa-1111-2222-3333-444444444444")
def page(self, live=(), config=(), qos=({}, {})): # disks and QoS of the live and the persistent definition
def page(self, live=(), config=(), qos=({}, {}), **getters):
"""live and config: disks; qos and getters (get_vcpu=(2, 4)): values of
the live and the persistent definition."""
with patch("instances.models.wvmInstance") as wvm, patch.object(
Compute, "status", new_callable=PropertyMock, return_value=True
):
@ -34,8 +36,11 @@ class PendingEditsTestCase(TestCase):
proxy.get_status.return_value = 1
proxy.get_memory.return_value = proxy.get_cur_memory.return_value = 1024
proxy.get_max_memory.return_value = 4096
proxy.get_vcpu.return_value = proxy.get_cur_vcpu.return_value = 1
proxy.get_max_cpus.return_value = [1, 2]
getters = {"get_vcpu": (1, 1), "get_cur_vcpu": (1, 1), "get_title": ("", ""),
"get_description": ("", ""), **getters}
for getter, values in getters.items():
getattr(proxy, getter).side_effect = lambda config=False, values=values: values[config]
proxy.get_max_cpus.return_value = [1, 2, 3, 4]
proxy.get_vcpus.return_value = {}
for getter in ("get_networks", "get_ifaces", "get_storages", "get_media_devices", "get_net_devices",
"get_snapshot", "get_external_snapshots"):
@ -78,3 +83,22 @@ class PendingEditsTestCase(TestCase):
self.client.post(reverse("instances:delete_vol", args=[self.instance.id]), {"dev": "vda"})
proxy.detach_disk.assert_not_called()
storage.return_value.del_volume.assert_not_called()
def test_the_options_and_cpu_forms_show_the_pending_definition(self):
doc = self.page(get_vcpu=(2, 4), get_cur_vcpu=(1, 3), get_title=("old", "new"),
get_description=("live text", "pending text"))
self.assertEqual(doc.xpath("//input[@name='title']/@value"), ["new"])
self.assertEqual(doc.xpath("//textarea[@name='description']/text()"), ["pending text"])
self.assertEqual(doc.xpath("//select[@name='vcpu']/option[@selected]/@value"), ["4"])
self.assertEqual(doc.xpath("//select[@name='cur_vcpu']/option[@selected]/@value"), ["3"])
def test_buttons_shown_as_disabled_cannot_submit(self):
# running: the CPU, options, video and XML forms wait for a shutdown
doc = self.page(live=[disk("vda", "default", "")], config=[disk("vda", "default", "")])
shown = doc.xpath("//button[contains(concat(' ', @class, ' '), ' disabled ')]")
self.assertGreater(len(shown), 5)
self.assertEqual([b for b in shown if b.get("disabled") is None], [])
for action in ("resizevm_cpu", "change_options", "set_video_model", "change_xml", "detach_vol", "delete_vol"):
with self.subTest(action):
(form,) = doc.xpath("//form[@action=$a]", a=reverse(f"instances:{action}", args=[self.instance.id]))
self.assertTrue(all(b.get("disabled") is not None for b in form.xpath(".//button")))