mirror of
https://github.com/retspen/webvirtcloud
synced 2026-12-02 08:56:07 +00:00
A download into a pool listed its volume while it was written. The page let a superuser copy it (the copy got a half-written file, live), attach it to a VM, or stop the pool under it. Now: - the copy job claims its source volume shared, and the worker refuses a copy whose name the source's format changed before the claim; - attaching an existing volume holds it exclusive from its first read, so neither a download nor a copy is under way, and its format is read after the download ended; - stopping or deleting a pool (page and API) holds the pool exclusive, and its backup target's claim if it is one; restores claim the pools they write into shared. Each is refused with the job in the way named, as a volume delete already was. The download recorded its progress every 16 MiB, so a slow server's bar stayed empty for a long time; it records it each second, with the cancel check. The worker checks the name is still free before it starts. A snapshot name or tag typed in the VM list's bulk form keeps the list from reloading, as a chosen VM did.
96 lines
4.1 KiB
Python
96 lines
4.1 KiB
Python
from django.db.models import Q
|
|
from django.utils.translation import gettext, gettext_noop
|
|
from libvirt import libvirtError
|
|
from lxml import etree
|
|
|
|
from operations import claims
|
|
from operations.models import Claim
|
|
from storages.forms import inside
|
|
from vrtManager.backup import wvmBackup
|
|
from vrtManager.util import InUse
|
|
|
|
from .models import Backup, BackupPolicy, BackupTarget, BackupTargetPool
|
|
|
|
|
|
def inside_vm_pool(conn, pool):
|
|
"""The name of another pool of the host whose directory holds the target
|
|
pool's: its backups would share that storage's failures."""
|
|
path = etree.fromstring(pool.XMLDesc(0)).findtext("target/path").rstrip("/")
|
|
for name, (_pool, directory) in conn.file_pools().items():
|
|
if name != pool.name() and inside(path, directory):
|
|
return name
|
|
return None
|
|
|
|
|
|
def backups_of(compute, pool, volumes):
|
|
"""{volume name: its backup} for those of volumes that are files of a backup in the compute's pool."""
|
|
if not volumes:
|
|
return {}
|
|
backups = list(Backup.objects.filter(target__pools__compute=compute, target__pools__pool=pool)
|
|
.only("prefix", "vm_name"))
|
|
return {volume: backup for volume in volumes for backup in backups if backup.owns(volume)}
|
|
|
|
|
|
def refuse_backup_volume(compute, pool, volume):
|
|
"""A volume of a backup is deleted with its backup, on the Backups pages."""
|
|
if backup := backups_of(compute, pool, [volume]).get(volume):
|
|
raise InUse(
|
|
gettext_noop("Volume %(volume)s belongs to a backup of %(vm)s; delete the backup on the Backups page"),
|
|
volume=volume,
|
|
vm=backup.vm_name,
|
|
)
|
|
|
|
|
|
def refuse_target_pool(compute, pool):
|
|
row = BackupTargetPool.objects.filter(compute=compute, pool=pool).select_related("target").first()
|
|
if row:
|
|
raise InUse(
|
|
gettext_noop("Storage pool %(pool)s is backup target %(target)s; remove it from the target first"),
|
|
pool=pool,
|
|
target=row.target.name,
|
|
)
|
|
|
|
|
|
def pool_stop_claims(compute, pool):
|
|
"""The claims that keep out a pool's jobs while it is stopped or deleted: the pool's, and its
|
|
backup target's if it is one (backup jobs claim the target)."""
|
|
keys = {claims.pool_key(compute.pk, pool): Claim.EXCLUSIVE}
|
|
row = BackupTargetPool.objects.filter(compute=compute, pool=pool).first()
|
|
if row:
|
|
keys[claims.backup_target_key(row.target_id)] = Claim.EXCLUSIVE
|
|
return keys
|
|
|
|
|
|
def pending_deletes():
|
|
"""The UUIDs of the backups a deletion job was asked for and has not
|
|
finished: shown as being deleted before the job marks them."""
|
|
from operations.models import Operation
|
|
|
|
ops = Operation.objects.filter(kind="backup.delete", state__in=Operation.ACTIVE).values_list("params", flat=True)
|
|
return {params["backup"] for params in ops}
|
|
|
|
|
|
def vm_tab(instance, user):
|
|
"""The VM page's Backups tab: the VM's backups, the targets of its host,
|
|
and why it cannot be backed up, if it cannot. A VM owner sees only
|
|
targets open to VM owners, and no schedule."""
|
|
owner = not user.is_superuser
|
|
targets = BackupTarget.objects.filter(pools__compute=instance.compute)
|
|
rows = instance.backups.select_related("target", "operation")
|
|
if owner:
|
|
targets, rows = targets.filter(owners=True), rows.filter(target__owners=True)
|
|
targets = list(targets.order_by("name"))
|
|
problem = None
|
|
if not targets:
|
|
problem = (gettext("No backup target open to VM owners has a storage pool on this VM's host.") if owner
|
|
else gettext("No backup target has a storage pool on this VM's host."))
|
|
else:
|
|
c = instance.compute
|
|
try:
|
|
wvmBackup(c.hostname, c.login, c.password, c.type, instance.name, uuid=instance.uuid).backup_plan()
|
|
except libvirtError as err:
|
|
problem = str(err)
|
|
schedules = [] if owner else BackupPolicy.objects.filter(
|
|
Q(instances=instance) | Q(compute=instance.compute_id) | Q(tag__instances=instance)).distinct().order_by("name")
|
|
return {"rows": rows, "targets": targets, "problem": problem, "schedules": schedules,
|
|
"pending_deletes": pending_deletes()}
|