mirror of
https://github.com/retspen/webvirtcloud
synced 2026-11-11 12:46:49 +00:00
fix(instances): a create whose definition answer was lost keeps the VM's disk
A definition can go through on the host while its answer is lost, as when the connection drops right then. The create job took every failed definition for none and deleted the new disk and seed, so the VM could be left defined without them, and recovery then reported it created. The job now asks the host for the VM's UUID first. Only when the host says there is no such VM do the new volumes go; otherwise they stay and recovery decides, recording the VM it finds. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01Kk4h2TcUiaWmU5MUszYSea
This commit is contained in:
parent
b7c5f98ba5
commit
590b8d956d
4 changed files with 34 additions and 1 deletions
|
|
@ -987,6 +987,15 @@ class CreateInstance(Kind):
|
|||
tpm_model=conn.get_tpm_model(data["arch"], data["machine"]) if data["tpm"] else None,
|
||||
)
|
||||
except Exception as err:
|
||||
# a definition whose answer was lost may have gone through: then, or
|
||||
# when the host cannot tell, the volumes stay and recovery decides
|
||||
try:
|
||||
conn.wvm.lookupByUUIDString(op.intent["uuid"])
|
||||
defined = True
|
||||
except libvirtError as lookup:
|
||||
defined = lookup.get_error_code() != VIR_ERR_NO_DOMAIN
|
||||
if defined:
|
||||
raise
|
||||
# the VM is not defined: its new volumes go
|
||||
leftovers = []
|
||||
for path in created:
|
||||
|
|
|
|||
|
|
@ -31,6 +31,9 @@ class CloudInitCreateTestCase(TransactionTestCase):
|
|||
patcher = patch("instances.jobs.wvmCreate")
|
||||
self.conn = patcher.start().return_value
|
||||
self.addCleanup(patcher.stop)
|
||||
missing = libvirt.libvirtError("no domain")
|
||||
missing.get_error_code = lambda: libvirt.VIR_ERR_NO_DOMAIN
|
||||
self.conn.wvm.lookupByUUIDString.side_effect = missing # until the job defines the VM
|
||||
self.conn.get_instances.return_value = []
|
||||
self.conn.get_cache_modes.return_value = {"none": "Disable cache"}
|
||||
self.conn.get_firmwares.return_value = ["BIOS", "UEFI", "UEFI Secure Boot"]
|
||||
|
|
|
|||
|
|
@ -36,6 +36,9 @@ class CreateJobTestCase(TransactionTestCase):
|
|||
self.conn.get_dom_capabilities.return_value = {"cpu_modes": ["host-model", "custom"],
|
||||
"cpu_custom_models": ["Skylake-Server-noTSX-IBRS"]}
|
||||
self.conn.create_volume.return_value = "/srv/default/api-vm.qcow2"
|
||||
missing = libvirt.libvirtError("no domain")
|
||||
missing.get_error_code = lambda: libvirt.VIR_ERR_NO_DOMAIN
|
||||
self.conn.wvm.lookupByUUIDString.side_effect = missing # until the job defines the VM
|
||||
|
||||
def test_the_api_queues_the_create_and_the_worker_defines_and_records_it(self):
|
||||
url = f"/api/v1/computes/{self.compute.pk}/instances/create/x86_64/q35/"
|
||||
|
|
@ -118,6 +121,21 @@ class CreateJobTestCase(TransactionTestCase):
|
|||
"Remove these volumes by hand: /srv/default/api-vm.qcow2")
|
||||
self.assertIn("RuntimeError: no such bus", traceback_of(raised.exception))
|
||||
|
||||
def test_a_definition_whose_answer_was_lost_keeps_the_disk(self):
|
||||
# it may have gone through: recovery records the VM, its disk with it
|
||||
self.client.post(f"/api/v1/computes/{self.compute.pk}/instances/create/x86_64/q35/", BODY,
|
||||
content_type="application/json")
|
||||
op = Operation.objects.get()
|
||||
acquire("wvc-test:1:a", "wvc-test")
|
||||
Operation.objects.filter(pk=op.pk).update(state=Operation.RUNNING, worker="wvc-test:1:a")
|
||||
op.refresh_from_db()
|
||||
self.conn.create_instance.side_effect = libvirt.libvirtError("End of file while reading data")
|
||||
for lookup in (None, libvirt.libvirtError("Cannot recv data")): # defined; the host cannot tell
|
||||
self.conn.wvm.lookupByUUIDString.side_effect = lookup
|
||||
with self.assertRaisesMessage(libvirt.libvirtError, "End of file while reading data"):
|
||||
CreateInstance().run(op, Context(op, SimpleNamespace(identity="wvc-test:1:a")))
|
||||
self.conn.delete_volume.assert_not_called()
|
||||
|
||||
@patch("instances.jobs.wvmConnect")
|
||||
def test_a_create_that_left_nothing_failed(self, conn):
|
||||
op = Operation(params={**BODY, "template": ""},
|
||||
|
|
|
|||
|
|
@ -5,7 +5,7 @@ from appsettings.settings import get_settings
|
|||
from computes.models import Compute
|
||||
from django.contrib.auth import get_user_model
|
||||
from django.test import TestCase
|
||||
from libvirt import libvirtError
|
||||
from libvirt import VIR_ERR_NO_DOMAIN, libvirtError
|
||||
|
||||
from instances.jobs import CreateInstance
|
||||
from operations import jobs
|
||||
|
|
@ -37,6 +37,9 @@ class CreateRollbackTestCase(TestCase):
|
|||
conn.get_dom_capabilities.return_value = {"cpu_modes": ["host-model"]}
|
||||
conn.get_instances.return_value = []
|
||||
conn.create_volume.return_value = "/pool/rb-vm.qcow2"
|
||||
missing = libvirtError("no domain")
|
||||
missing.get_error_code = lambda: VIR_ERR_NO_DOMAIN
|
||||
conn.wvm.lookupByUUIDString.side_effect = missing # until the job defines the VM
|
||||
op = jobs.submit("instance.create", self.admin, {**PARAMS, "compute": self.compute.pk}, "key-rollback1")
|
||||
acquire("wvc-test:1:a", "wvc-test")
|
||||
Operation.objects.filter(pk=op.pk).update(state=Operation.RUNNING, worker="wvc-test:1:a")
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue