1
0
Fork 0
mirror of https://github.com/retspen/webvirtcloud synced 2026-12-03 17:36:05 +00:00

feat(computes): a host evacuates its chosen VMs to another host

The host's Instances page gets an Evacuate button. Its page lists the
host's VMs for a chosen destination: running ones ticked, shut-off ones
left to choose ("Also migrate the shut-off VMs"), and a VM with a disk
file the two hosts do not share refused with the reason, since it needs
a copy chosen on its own Migrate tab.

One "Evacuate host" job migrates them: a migrate job each (live with
auto converge; a shut-off VM offline), at most the chosen number at
once, so a VM that fails stays on the host and the rest go on. The
choice goes up to half the worker's slots, which a host's jobs may take,
less the evacuation's own. Cancelling it cancels the migrations that
run and starts no more. One evacuation runs at a time: it waits in a
worker slot, and several could leave none for their migrations. A VM
that left the host meanwhile is skipped (a migrate job takes the host it
is to leave from the evacuation and refuses another); a host whose record points
elsewhere since, or a worker lowered to one slot, stops it before the
next VM. A busy VM lets the others go first; one an uncertain job holds is
not waited for.

Migrations now hold the hosts' VM sets shared, so several migrate at
once; creates, clones, deletes and the VM list reconcile still wait for
them. A destination gone since the form was shown is refused, not
replaced by another. The uses of the NVRAM directory's pool take turns
(NVRAM_LOCK): two UEFI migrations at once would share a transient pool
that the first destroys under the second.
This commit is contained in:
catborise 2026-11-15 15:36:00 +03:00
parent c9fef349d5
commit d20254f757
18 changed files with 861 additions and 106 deletions

View file

@ -10,6 +10,7 @@ import subprocess
import unittest
import libvirt
from lxml import html
from django.contrib.auth import get_user_model
from django.core.files.uploadedfile import SimpleUploadedFile
from django.test import TransactionTestCase
@ -91,8 +92,8 @@ class LiveJobsTestCase(TransactionTestCase):
op.refresh_from_db()
self.assertEqual(DeleteExternalSnapshot().reconcile(op).state, Operation.SUCCEEDED)
def run_job(self, op):
worker = Worker(concurrency=1, tick=0.1, heartbeat_every=1, progress_every=1, exit_on_lost_lease=False)
def run_job(self, op, concurrency=1):
worker = Worker(concurrency=concurrency, tick=0.1, heartbeat_every=1, progress_every=1, exit_on_lost_lease=False)
thread = fakes.start_worker(worker)
try:
self.assertTrue(wait_for(lambda: Operation.objects.get(pk=op.pk).state in Operation.TERMINAL, 300))
@ -182,6 +183,26 @@ class LiveJobsTestCase(TransactionTestCase):
self.assertFalse(self.conn.lookupByUUIDString(uuid).isActive()) # defined on the source again
self.assertEqual(Instance.objects.get(uuid=uuid).compute, self.compute)
def test_an_evacuation_migrates_the_chosen_vms_two_at_once(self):
dest, dest_conn = self.second_host()
vms = []
for name, running in (("evac-run", True), ("evac-off", False)):
dom = livetest.define_vm(self.conn, P + name, [])
if running:
dom.create()
vms.append(Instance.objects.create(compute=self.compute, name=dom.name(), uuid=dom.UUIDString()))
local = self.vm("evac-disk")[0] # its disk is in a pool of this host only
local = Instance.objects.create(compute=self.compute, name=local.name(), uuid=local.UUIDString())
self.client.force_login(self.admin)
page = html.fromstring(self.client.get(reverse("evacuate", args=[self.compute.pk]) + f"?target={dest.pk}").content)
disabled = {int(box.get("value")): box.get("disabled") is not None for box in page.xpath("//input[@name='instance']")}
self.assertEqual([disabled[vm.pk] for vm in (*vms, local)], [False, False, True]) # the host's other VMs as they are
raw = {"compute": self.compute.pk, "target": dest.pk, "parallel": 2, "instances": [vm.pk for vm in vms]}
self.run_job(jobs.submit("host.evacuate", self.admin, raw, "key-evac0001"), concurrency=4)
self.assertEqual(Operation.objects.filter(kind="instance.migrate", state=Operation.SUCCEEDED).count(), 2)
self.assertEqual([dest_conn.lookupByUUIDString(vm.uuid).isActive() for vm in vms], [True, False])
self.assertEqual({vm.compute for vm in Instance.objects.filter(pk__in=[vm.pk for vm in vms])}, {dest})
def test_a_disk_neither_host_shares_is_copied_there_and_back(self):
"""The test pool is a directory of each host's own: the disk is
copied into it, running there with its source file deleted on

View file

@ -58,8 +58,8 @@ class MigrateJobTestCase(TransactionTestCase):
op = self.queue(live_migrate="true")
self.assertEqual(set(Claim.objects.values_list("resource", "mode")), {
(f"instance:{UUID}", "exclusive"), ("name:mig-vm", "exclusive"),
(f"compute:{self.source.pk}", "shared"), (f"reconcile:{self.source.pk}", "exclusive"),
(f"compute:{self.dest.pk}", "shared"), (f"reconcile:{self.dest.pk}", "exclusive")})
(f"compute:{self.source.pk}", "shared"), (f"reconcile:{self.source.pk}", "shared"),
(f"compute:{self.dest.pk}", "shared"), (f"reconcile:{self.dest.pk}", "shared")})
self.dest_conn.moveto.assert_not_called() # nothing ran in the request
ctx = self.start(op)
get_kind(op.kind).precheck(op, ctx)