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

fix(computes): a libvirt host is one compute, and a compute's VMs stay on their host

One host could be added twice, under another name, as TCP and SSH, or by
another address: its VMs were listed once per record, each with its own
owners, and counted twice in the capacity totals. A compute with VMs could
also be pointed at another host: the owners of a VM then reached the
VM with the same UUID there, a restored or copied one.

A compute now keeps its libvirt host UUID. Adding a compute or changing
its connection, on the web or the API, connects and reads it: a host that
is already a compute is refused, and a compute with VMs moves only to an
address of the same host, which must answer. Computes added before are
read when another one is checked.
This commit is contained in:
catborise 2026-11-12 08:42:04 +03:00
parent 2db25314ea
commit 29ec20682b
17 changed files with 567 additions and 259 deletions

View file

@ -1,7 +1,8 @@
from ipaddress import ip_address
from django.db import transaction
from django.db.models import CharField, IntegerField, Model
from django.core.exceptions import ValidationError
from django.db import IntegrityError, transaction
from django.db.models import CharField, IntegerField, Model, Q, UniqueConstraint
from django.utils.functional import cached_property
from django.utils.translation import gettext_lazy as _
from libvirt import libvirtError, virConnect
@ -11,6 +12,10 @@ from vrtManager.connection import connection_manager
from vrtManager.hostdetails import wvmHostDetails
# what the panel connects to a compute with
ENDPOINT = ("hostname", "login", "password", "type")
class Compute(Model):
name = CharField(_("name"), max_length=64, unique=True)
hostname = CharField(_("hostname"), max_length=64, validators=[validate_hostname])
@ -41,6 +46,13 @@ class Compute(Model):
),
)
# the libvirt host's UUID (its capabilities), read when the panel first
# reaches it: one host is one compute, and a compute's VMs stay on it
host_uuid = CharField(max_length=36, blank=True, default="", editable=False)
class Meta:
constraints = [UniqueConstraint(fields=["host_uuid"], condition=~Q(host_uuid=""), name="one_compute_per_host")]
@property
def migration_uri(self):
"""The native migration URI for VMs migrated to this host: its
@ -129,6 +141,55 @@ class Compute(Model):
def __str__(self):
return self.name
def record_host(self):
"""Reads and keeps the UUID of the host the compute reaches (host_uuid)
while its connection is still the one read with; its UUID, or ""
when it cannot be read. IntegrityError when another compute is that host."""
try:
uuid = self.proxy.host_uuid()
except libvirtError:
return ""
with transaction.atomic():
same = {field: getattr(self, field) for field in ENDPOINT}
if not Compute.objects.filter(pk=self.pk, host_uuid="", **same).update(host_uuid=uuid):
return ""
self.host_uuid = uuid
return uuid
# why a new or changed connection could not be tried (clean), for the form to tell
unreachable = None
def clean(self):
"""A host is one compute, and a compute's VMs stay on their host: a
new or changed connection is tried and its host UUID read
(host_uuid). Refused when another compute is that host, or when the
compute has VMs and the host is not theirs, cannot be reached, or
theirs is not known. Callers hold hosts_lock (computes.utils)."""
before = Compute.objects.filter(pk=self.pk).values(*ENDPOINT, "host_uuid").first() if self.pk else None
if before and all(before[field] == getattr(self, field) for field in ENDPOINT):
return
try:
uuid = wvmHostDetails(self.hostname, self.login, self.password, self.type).host_uuid()
except libvirtError as error:
uuid, self.unreachable = "", str(error)
if uuid and (other := Compute.objects.exclude(pk=self.pk).filter(host_uuid=uuid).first()):
raise ValidationError(_("This host is already added as %(name)s"), params={"name": other.name})
if before and self.instance_set.exists():
if not uuid:
raise ValidationError(_("The panel cannot reach the host to tell that its VMs are there: %(error)s"),
params={"error": self.unreachable})
try:
recorded = before["host_uuid"] or Compute.objects.get(pk=self.pk).record_host()
except IntegrityError:
recorded = ""
if not recorded:
raise ValidationError(_("The host its VMs are on is not known yet: change the connection when "
"the panel has reached the host at its current one"))
if uuid != recorded:
raise ValidationError(_("This is another host than the one its VMs are on: "
"add it as a new host"))
self.host_uuid = uuid
def delete(self, *args, **kwargs):
"""Its VMs keep running on the host: their owners wait in tombstones,
or follow a VM already listed on another compute. The compute is held