mirror of
https://github.com/retspen/webvirtcloud
synced 2026-11-11 12:46:49 +00:00
fix(jobs): a recovered migration restores autostart and removes the source NVRAM file
migrate_instance sets autostart on the destination (it is per host and the migration does not carry it) and deletes the NVRAM file libvirt keeps on the source. A worker that stopped after libvirt moved the VM skipped both: recovery only recorded the move, so an autostart VM no longer started after the destination rebooted, and a later VM of the same name on the source started with these UEFI variables. The job now records both before the move, and recovery does them after a proven move; a step that fails is a warning of the job's result.
This commit is contained in:
parent
c3dfab287d
commit
65345bd980
6 changed files with 96 additions and 27 deletions
|
|
@ -930,11 +930,15 @@ def migration_target(instance, target_pk):
|
|||
return target
|
||||
|
||||
|
||||
def _connect(compute_pk):
|
||||
c = Compute.objects.get(pk=compute_pk)
|
||||
return wvmConnect(c.hostname, c.login, c.password, c.type)
|
||||
|
||||
|
||||
def _domain(compute_pk, uuid):
|
||||
"""The VM on the compute, or None when it has none there."""
|
||||
c = Compute.objects.get(pk=compute_pk)
|
||||
try:
|
||||
return wvmConnect(c.hostname, c.login, c.password, c.type).wvm.lookupByUUIDString(uuid)
|
||||
return _connect(compute_pk).wvm.lookupByUUIDString(uuid)
|
||||
except libvirtError as err:
|
||||
if err.get_error_code() != VIR_ERR_NO_DOMAIN:
|
||||
raise # unreachable: recovery waits and retries
|
||||
|
|
@ -947,6 +951,25 @@ def _shut_off_again(op, dom):
|
|||
dom.destroy()
|
||||
|
||||
|
||||
def _moved(op, dest):
|
||||
"""What migrate_instance does once libvirt moved the VM, for a job that
|
||||
stopped before it: autostart is per host, and the source keeps the
|
||||
NVRAM file. The job's result, with a warning for a step that failed."""
|
||||
notes = []
|
||||
if op.intent.get("autostart"):
|
||||
try:
|
||||
dest.setAutostart(1)
|
||||
except libvirtError as err:
|
||||
notes += say(_("The VM migrated, but autostart could not be set on the destination: %(error)s"),
|
||||
error=err)
|
||||
if nvram := op.intent.get("nvram"):
|
||||
try:
|
||||
_connect(op.intent["compute"]).remove_nvram(nvram, _connect(op.params["target"]))
|
||||
except libvirtError as err:
|
||||
notes += say(_("The VM migrated, but its NVRAM file stays on the source: %(error)s"), error=err)
|
||||
return {"warning": notes} if notes else {}
|
||||
|
||||
|
||||
@register
|
||||
class MigrateInstance(Kind):
|
||||
name = "instance.migrate"
|
||||
|
|
@ -993,8 +1016,10 @@ class MigrateInstance(Kind):
|
|||
def run(self, op, ctx):
|
||||
p = op.params
|
||||
instance = _instance(p["instance"])
|
||||
# a shut-off VM may migrate started paused (migrate_running): recovery shuts it off again
|
||||
ctx.record_intent(shut_off=instance.proxy.get_status() == 5)
|
||||
# a shut-off VM may migrate started paused (migrate_running): recovery shuts it off again;
|
||||
# it also does what migrate_instance does after the move (_moved)
|
||||
proxy = instance.proxy
|
||||
ctx.record_intent(shut_off=proxy.get_status() == 5, autostart=proxy.get_autostart(), nvram=proxy.get_nvram())
|
||||
ctx.set_phase(gettext_noop("Migrating the VM"))
|
||||
try:
|
||||
utils.migrate_instance(Compute.objects.get(pk=p["target"]), instance, op.user,
|
||||
|
|
@ -1048,7 +1073,7 @@ class MigrateInstance(Kind):
|
|||
if instance is not None: # the job stopped before it moved the record
|
||||
utils.record_migration(instance, Compute.objects.get(pk=op.params["target"]))
|
||||
_shut_off_again(op, dest)
|
||||
return Outcome(Operation.SUCCEEDED)
|
||||
return Outcome(Operation.SUCCEEDED, result=_moved(op, dest))
|
||||
if source is not None and dest is None:
|
||||
_shut_off_again(op, source)
|
||||
return Outcome(Operation.FAILED, error=say(_("The VM stayed on the source host")))
|
||||
|
|
|
|||
|
|
@ -100,6 +100,27 @@ class MigrateJobTestCase(TransactionTestCase):
|
|||
MigrateInstance().reconcile(op)
|
||||
dom.destroy.assert_not_called()
|
||||
|
||||
def test_recovery_of_a_moved_vm_sets_its_autostart_and_removes_the_source_nvram(self):
|
||||
self.vm.proxy.get_autostart.return_value = True
|
||||
self.vm.proxy.get_nvram.return_value = "/var/lib/libvirt/qemu/nvram/mig-vm_VARS.fd"
|
||||
op = self.queue()
|
||||
ctx = self.start(op)
|
||||
self.dest_conn.moveto.side_effect = OSError("the worker stops here")
|
||||
with self.assertRaises(OSError):
|
||||
get_kind(op.kind).run(op, ctx)
|
||||
dom = MagicMock()
|
||||
with patch("instances.jobs._domain", side_effect=[None, dom]), patch("instances.jobs._connect") as connect:
|
||||
outcome = MigrateInstance().reconcile(op)
|
||||
self.assertEqual((outcome.state, outcome.result), (Operation.SUCCEEDED, {}))
|
||||
dom.setAutostart.assert_called_once_with(1)
|
||||
connect.return_value.remove_nvram.assert_called_once_with(
|
||||
"/var/lib/libvirt/qemu/nvram/mig-vm_VARS.fd", connect.return_value)
|
||||
dom.setAutostart.side_effect = libvirtError("refused")
|
||||
with patch("instances.jobs._domain", side_effect=[None, dom]), patch("instances.jobs._connect"):
|
||||
outcome = MigrateInstance().reconcile(op)
|
||||
self.assertEqual(outcome.state, Operation.SUCCEEDED)
|
||||
self.assertIn("autostart could not be set", outcome.result["warning"][0]["text"])
|
||||
|
||||
def test_a_copy_claims_its_volume_on_both_computes(self):
|
||||
Instance.proxy.get_disk_devices.return_value = [
|
||||
{"dev": "vda", "storage": "default", "image": "mig-vm.qcow2", "path": "/i/mig-vm.qcow2"},
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue