1
0
Fork 0
mirror of https://github.com/retspen/webvirtcloud synced 2026-12-02 17:06:09 +00:00
webvirtcloud/computes/reach.py
catborise ea20f12bc8 feat(computes): Bulk Migrate spreads a host's VMs over the others; host checks asked once
Bulk Migrate sent every chosen VM to one destination, without looking at
its memory. With several other hosts it now spreads them by default
(computes.balance.spread): the largest VMs first, each to the host that
has the least memory, then vCPUs, with it; a VM that holds its memory
(running or paused) only where it fits the memory the host's running and
paused VMs leave. The page shows where
each VM goes and makes the spread again when a VM is ticked or unticked;
a VM no host has room for says so. One destination stays a choice. The
host.bulk_migrate job takes a destination per VM ("moves").

The Balance and Bulk Migrate pages asked, for every VM and destination,
the host's identity and whether both hosts share each disk directory,
which writes a marker file on the shared storage: about 86 ms a pair,
sequential, so minutes with a few hundred VMs on ten hosts. A Reach
(computes/reach.py) asks each host pair and each directory once and
reads a VM's definitions once. vrtManager: wvmInstances.shares and
file_disks, also used by disk_plan.

read_fleet reads the hosts, their VMs and loads for both pages.
2026-11-16 10:29:07 +03:00

61 lines
3 KiB
Python

"""Where the VMs of a bulk migration or a balance can migrate without a disk
copy, each host and each directory a pair of hosts may share asked once:
the pages ask it for every VM and destination."""
import os
from django.utils.translation import gettext_lazy as _
from libvirt import VIR_DOMAIN_XML_INACTIVE, libvirtError
from vrtManager.instance import file_disks, wvmInstances
from webvirtcloud.middleware import error_text
class Reach:
def __init__(self, request):
self.request = request
self.conns = {} # compute pk: its connection
self.hosts = set() # (source pk, target pk) a VM of the source was found able to go to
self.shared = {} # (source pk, target pk, directory): whether both hosts see it as one, or why not known
self.directories = {} # VM pk: the directories of its file disks, or the error that read them
def _conn(self, compute):
if compute.pk not in self.conns:
self.conns[compute.pk] = wvmInstances(compute.hostname, compute.login, compute.password, compute.type)
return self.conns[compute.pk]
def refusal(self, instance, target):
"""Why the VM cannot migrate to target in a bulk migration, or None:
target is another host the panel reaches, and both hosts see every
disk file of the VM, now and at its next start (one they do not
takes a copy, chosen on the VM's Migrate tab)."""
from instances.jobs import migration_target
pair = (instance.compute_id, target.pk)
if pair not in self.hosts:
# kept once it passed: a refusal may be the VM's own (gone meanwhile), not the hosts'
try:
migration_target(instance, target.pk)
self.hosts.add(pair)
except libvirtError as err:
return error_text(self.request, err)
if instance.pk not in self.directories:
try:
domain = instance.proxy.instance
xmls = (domain.XMLDesc(0), domain.XMLDesc(VIR_DOMAIN_XML_INACTIVE))
self.directories[instance.pk] = {os.path.dirname(path) for xml in xmls for _disk, path in file_disks(xml)}
except libvirtError as err:
self.directories[instance.pk] = error_text(self.request, err)
if isinstance(self.directories[instance.pk], str):
return self.directories[instance.pk]
for directory in sorted(self.directories[instance.pk]):
key = (*pair, directory)
if key not in self.shared:
try:
self.shared[key] = self._conn(target).shares(self._conn(instance.compute), directory)
except libvirtError as err: # a host's pools could not be read: not that destination
self.shared[key] = error_text(self.request, err)
if isinstance(self.shared[key], str):
return self.shared[key]
if not self.shared[key]:
return _("A disk file is not on storage both hosts share: migrate it from its page")
return None