mirror of
https://github.com/retspen/webvirtcloud
synced 2026-12-02 17:06:09 +00:00
A VM that vanished from its compute, or whose compute was deleted and added again, came back with its owners and tags only: it was no longer protected, its schedules no longer backed it up, and its backups stayed unlinked, so its Backups tab no longer listed them and retention left them for good. The Removed VMs record now keeps its protection, its schedules and its own backups (not those of a copy under the same UUID), and gives them back. Turning protection on or off waits for the ownership step, so a VM dropped from its host's list meanwhile is not told protected.
276 lines
12 KiB
Python
276 lines
12 KiB
Python
from contextlib import contextmanager
|
|
import logging
|
|
from datetime import timedelta
|
|
|
|
from accounts.models import UserInstance
|
|
from django.conf import settings
|
|
from django.contrib.auth import get_user_model
|
|
from django.db import connection, transaction
|
|
from django.utils import timezone
|
|
from computes.models import ENDPOINT, Compute, Tag
|
|
from instances.models import Instance, InstanceTombstone
|
|
from logs.audit import write
|
|
from operations import claims
|
|
from operations.models import Claim
|
|
from vmbackups.models import Backup, BackupPolicy, BackupRun
|
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
|
|
@contextmanager
|
|
def libvirt_compute_lock(compute, *, shared=False):
|
|
"""The compute for the block: exclusive keeps out every VM operation and
|
|
reconcile on it; shared lets sibling VMs and reconciles go on. Busy if it is taken."""
|
|
compute_pk = getattr(compute, "pk", getattr(compute, "id", None))
|
|
if not compute_pk:
|
|
yield
|
|
return
|
|
with claims.hold({claims.compute_key(compute_pk): Claim.SHARED if shared else Claim.EXCLUSIVE}):
|
|
yield
|
|
|
|
|
|
@contextmanager
|
|
def hosts_lock(compute=None):
|
|
"""A compute added or its connection changed: checked (Compute.clean)
|
|
and saved one at a time, so two cannot be one host; a compute changed
|
|
is held exclusive, so its VMs are not listed from the old host meanwhile."""
|
|
wanted = {claims.hosts_key(): Claim.EXCLUSIVE}
|
|
if compute is not None:
|
|
wanted[claims.compute_key(compute.pk)] = Claim.EXCLUSIVE
|
|
with claims.hold(wanted):
|
|
yield
|
|
|
|
|
|
@contextmanager
|
|
def users_lock():
|
|
"""The panel's users for the block, in one transaction, while no other
|
|
change of them runs: what a form checked before (one user per e-mail
|
|
address, an active superuser left) is checked again as it is saved."""
|
|
with claims.hold({claims.users_key(): Claim.EXCLUSIVE}), transaction.atomic():
|
|
yield
|
|
|
|
|
|
@contextmanager
|
|
def user_quota_lock(user):
|
|
"""One user's quota check and the change it allows run one at a time."""
|
|
with claims.hold({claims.quota_key(user.pk): Claim.EXCLUSIVE}):
|
|
yield
|
|
|
|
|
|
@contextmanager
|
|
def libvirt_instance_lock(instance):
|
|
"""One VM for the block, sharing its compute with sibling VMs."""
|
|
compute = instance.compute
|
|
with claims.hold({
|
|
claims.compute_key(getattr(compute, "pk", getattr(compute, "id", None))): Claim.SHARED,
|
|
claims.instance_key(instance.uuid): Claim.EXCLUSIVE,
|
|
}):
|
|
yield
|
|
|
|
|
|
# pg_advisory_xact_lock key of the ownership step ("wvc_own")
|
|
OWNERSHIP_LOCK = 0x7776635F6F776E
|
|
|
|
|
|
def lock_ownership():
|
|
"""Hold the ownership step of every compute until this transaction ends:
|
|
a bury and a restore of the same UUID on two computes must not see each
|
|
other's work half done, nor two owners added at once each other's check.
|
|
SQLite's IMMEDIATE transactions already do this."""
|
|
if connection.vendor == "postgresql":
|
|
with connection.cursor() as cursor:
|
|
cursor.execute("SELECT pg_advisory_xact_lock(%s)", [OWNERSHIP_LOCK])
|
|
|
|
|
|
def _log(inst, what):
|
|
write("-", inst.compute.name, inst.name, f"VM {inst.name} ({inst.uuid}): {what}", True, uuid=inst.uuid)
|
|
|
|
|
|
def move_ownership(source, target):
|
|
"""Give target the owners, tags, template and protected flags, backups and schedules of source; a user's rights
|
|
are merged."""
|
|
target.tags.add(*source.tags.all())
|
|
Backup.objects.filter(instance=source).update(instance=target)
|
|
for policy in source.backup_policies.all():
|
|
policy.instances.add(target)
|
|
taken = set(BackupRun.objects.filter(instance=target).values_list("policy_id", "day"))
|
|
for run in BackupRun.objects.filter(instance=source): # a schedule's run of the day: not taken twice
|
|
if (run.policy_id, run.day) not in taken:
|
|
BackupRun.objects.filter(pk=run.pk).update(instance=target)
|
|
for ui in UserInstance.objects.filter(instance=source):
|
|
existing = UserInstance.objects.filter(instance=target, user=ui.user).first()
|
|
if existing is None:
|
|
ui.instance = target
|
|
ui.save(update_fields=["instance"])
|
|
continue
|
|
for flag in ("is_change", "is_delete", "is_vnc"):
|
|
if getattr(ui, flag):
|
|
setattr(existing, flag, True)
|
|
existing.save()
|
|
flags = [flag for flag in ("is_template", "protected") if getattr(source, flag) and not getattr(target, flag)]
|
|
for flag in flags:
|
|
setattr(target, flag, True)
|
|
if flags:
|
|
target.save(update_fields=flags)
|
|
|
|
|
|
def forget(inst):
|
|
"""Remove an instance whose VM its compute no longer lists, or whose
|
|
compute is deleted. Its ownership goes to the same UUID on another
|
|
compute (an out-of-band move) or is kept in a tombstone in case the VM
|
|
comes back. Two other computes with the UUID: either may be a copy, so
|
|
the Removed VMs page shows the owners for an administrator to add."""
|
|
elsewhere = list(Instance.objects.filter(uuid=inst.uuid).exclude(compute_id=inst.compute_id)[:2])
|
|
if len(elsewhere) == 1:
|
|
move_ownership(inst, elsewhere[0])
|
|
_log(inst, f"owners moved to {elsewhere[0].compute.name}")
|
|
else:
|
|
_bury(inst)
|
|
inst.delete()
|
|
|
|
|
|
def _bury(inst):
|
|
owners = [
|
|
{"user": ui.user_id, "is_change": ui.is_change, "is_delete": ui.is_delete, "is_vnc": ui.is_vnc}
|
|
for ui in UserInstance.objects.filter(instance=inst)
|
|
]
|
|
tombstone = InstanceTombstone(uuid=inst.uuid, name=inst.name, host=inst.compute.name, is_template=inst.is_template,
|
|
protected=inst.protected, owners=owners,
|
|
tags=list(inst.tags.values_list("name", flat=True)),
|
|
policies=list(inst.backup_policies.values_list("pk", flat=True)),
|
|
backups=list(inst.backups.values_list("pk", flat=True)))
|
|
if tombstone.keeps_anything():
|
|
tombstone.save()
|
|
_log(inst, "kept on the Removed VMs page")
|
|
else:
|
|
_log(inst, "left the panel (nothing to keep)")
|
|
|
|
|
|
def _restore(inst):
|
|
tombstone = InstanceTombstone.objects.filter(uuid=inst.uuid).order_by("-removed").first()
|
|
if tombstone is None:
|
|
return
|
|
users = set(get_user_model().objects.filter(id__in=[o["user"] for o in tombstone.owners]).values_list("id", flat=True))
|
|
for owner in tombstone.owners:
|
|
if owner["user"] in users:
|
|
UserInstance.objects.create(
|
|
instance=inst,
|
|
user_id=owner["user"],
|
|
is_change=owner["is_change"],
|
|
is_delete=owner["is_delete"],
|
|
is_vnc=owner["is_vnc"],
|
|
)
|
|
inst.tags.set(Tag.objects.filter(name__in=tombstone.tags))
|
|
# not a schedule that chooses its VMs by compute or tag since: it chooses this one, or not, by itself
|
|
inst.backup_policies.set(BackupPolicy.objects.filter(pk__in=tombstone.policies, compute=None, tag=None))
|
|
Backup.objects.filter(pk__in=tombstone.backups, instance__isnull=True).update(instance=inst)
|
|
inst.is_template, inst.protected = tombstone.is_template, tombstone.protected
|
|
inst.save(update_fields=["is_template", "protected"])
|
|
InstanceTombstone.objects.filter(uuid=inst.uuid).delete()
|
|
_log(inst, "restored from the Removed VMs page")
|
|
|
|
|
|
def _host_domains(compute):
|
|
"""{uuid: name} of the compute's VMs, or None if libvirt cannot list them."""
|
|
try:
|
|
return {d.UUIDString(): d.name() for d in compute.proxy.wvm.listAllDomains()}
|
|
except Exception as e:
|
|
logger.warning("Failed to list the domains of compute %s (%s): %s",
|
|
getattr(compute, "name", compute), getattr(compute, "hostname", ""), e)
|
|
return None
|
|
|
|
|
|
def refresh_instance_database(compute):
|
|
"""
|
|
Synchronizes the WebVirtCloud database with libvirt domain state.
|
|
Libvirt is the single source of truth:
|
|
- VMs removed in virt-manager/virsh are deleted from the database; their
|
|
ownership is moved to the same UUID on another compute or kept in a
|
|
tombstone for INSTANCE_OWNERSHIP_RETENTION_DAYS.
|
|
- VMs created in virt-manager/virsh are added to the database.
|
|
- VMs renamed in virt-manager/virsh have their name updated without losing
|
|
UserInstance permission relationships (matched by UUID).
|
|
- If libvirt is unreachable (connection/network error), the database is left
|
|
untouched to avoid false deletions.
|
|
True when the database matches the host's VMs afterwards; None when it was
|
|
not synchronized (host not answering, busy or changed meanwhile).
|
|
"""
|
|
compute_pk = getattr(compute, "pk", getattr(compute, "id", None))
|
|
if not compute_pk:
|
|
return
|
|
# libvirt is asked before any transaction: a silent host can keep a call
|
|
# waiting for 30 s, and on SQLite an open transaction makes every other
|
|
# write of the app fail meanwhile.
|
|
if hasattr(compute, "status") and compute.status is False:
|
|
return
|
|
host_domains = _host_domains(compute)
|
|
if host_domains is None:
|
|
return
|
|
# What nearly every VM list load finds: the database matches libvirt and no
|
|
# removed record has expired. Nothing to write: no claim, no transaction.
|
|
retention = getattr(settings, "INSTANCE_OWNERSHIP_RETENTION_DAYS", 30)
|
|
cutoff = timezone.now() - timedelta(days=retention)
|
|
if (dict(Instance.objects.filter(compute_id=compute_pk).values_list("uuid", "name")) == host_domains
|
|
and not InstanceTombstone.objects.filter(removed__lt=cutoff).exists()):
|
|
return True
|
|
|
|
try:
|
|
# The compute shared: VM operations and jobs on it go on; changes of
|
|
# its VM set (create, clone, delete, migration, define) hold it
|
|
# exclusive and stay out until the end. One reconcile of a compute at
|
|
# a time, and libvirt read again inside it: two would read libvirt at
|
|
# different times and could commit in reverse order, an older listing
|
|
# undoing a newer one.
|
|
with claims.hold({claims.compute_key(compute_pk): Claim.SHARED,
|
|
claims.reconcile_key(compute_pk): Claim.EXCLUSIVE}):
|
|
# a compute whose connection changed since it was read: the listing would be the old host's
|
|
if Compute.objects.filter(pk=compute_pk).values_list(*ENDPOINT).first() != \
|
|
tuple(getattr(compute, field, None) for field in ENDPOINT):
|
|
return
|
|
host_domains = _host_domains(compute)
|
|
if host_domains is None:
|
|
return
|
|
|
|
with transaction.atomic():
|
|
lock_ownership()
|
|
# the compute may have been deleted while libvirt was asked
|
|
comp_locked = Compute.objects.select_for_update().filter(pk=compute_pk).first()
|
|
if not comp_locked:
|
|
return
|
|
|
|
db_instances = list(Instance.objects.filter(compute=compute).select_for_update())
|
|
db_uuids = set(inst.uuid for inst in db_instances)
|
|
|
|
# 1. Update name for any VM whose name changed out-of-band in virsh/virt-manager
|
|
for inst in db_instances:
|
|
if inst.uuid in host_domains:
|
|
new_name = host_domains[inst.uuid]
|
|
if inst.name != new_name:
|
|
inst.name = new_name
|
|
inst.save(update_fields=["name"])
|
|
|
|
# 2. Instances gone from this compute
|
|
for inst in db_instances:
|
|
if inst.uuid not in host_domains:
|
|
forget(inst)
|
|
|
|
# 3. Instances created out-of-band. A UUID that also exists on another
|
|
# compute is a copy and gets no owners; otherwise a tombstone's
|
|
# ownership is restored.
|
|
expired = InstanceTombstone.objects.filter(removed__lt=cutoff)
|
|
for t in expired:
|
|
write("-", t.host or "-", t.name, f"VM {t.name} ({t.uuid}): removed record expired", True, uuid=t.uuid)
|
|
expired.delete()
|
|
for uuid, name in host_domains.items():
|
|
if uuid not in db_uuids:
|
|
inst, created = Instance.objects.get_or_create(
|
|
compute=compute,
|
|
uuid=uuid,
|
|
defaults={"name": name},
|
|
)
|
|
if created and not Instance.objects.filter(uuid=uuid).exclude(compute=compute).exists():
|
|
_restore(inst)
|
|
return True
|
|
except claims.Busy:
|
|
logger.debug("Compute %s is busy; not synchronized now", compute_pk)
|
|
return
|