Files
napalm-proxmox/napalm_proxmox/cluster_backups.py
T
Christian Manivong 0704d9a102
driver / standard (pull_request) Successful in 14s
driver / lint (pull_request) Successful in 13s
driver / types (pull_request) Successful in 1m3s
driver / test (3.12) (pull_request) Successful in 1m10s
driver / test (3.13) (pull_request) Successful in 1m3s
driver / lowest (pull_request) Successful in 33s
driver / build (pull_request) Successful in 33s
driver / security (pull_request) Successful in 1m27s
CI / driver (pull_request) Successful in 3m4s
driver / publish (pull_request) Skipped
feat: HA state, backups and votes in get_clusters(); migrate_vm, preflight, maintenance
get_clusters() reads /cluster/ha/* (PVE 8 and 9), /cluster/backup with pool
members, the newest backup per guest on each backup storage (PBS included) and
/cluster/config/nodes votes; each read is best-effort. migrate_vm() moves a
guest of the connected node and follows the task (HA-managed guests through
the HA manager), raising with the task log's ERROR lines;
get_vm_migration_preflight() maps PVE's own pre-flight; set_node_maintenance()
runs ha-manager over SSH.

Part of NetOrk/netork#1820 (stage 7: #759, #760, #761).
2026-11-01 22:15:01 +01:00

152 lines
5.4 KiB
Python

"""get_clusters() for Proxmox VE: the backup jobs, and the backups on the storages.
Read from:
- ``GET /cluster/backup``: each vzdump job. ``enabled`` absent means enabled;
old jobs carry ``starttime``/``dow`` instead of ``schedule``, which PVE itself
turns into ``"<dow> <starttime>"`` (``PVE/API2/Backup.pm``). ``storage`` and
``mode`` are None where the job does not set them: a node's
``/etc/vzdump.conf`` then decides.
- a pool job's guests: ``GET /pools?poolid=<pool>`` (PVE 8.1 and later), else
``GET /pools/<pool>``, which older releases have;
- ``GET /nodes/<node>/storage/<storage>/content?content=backup`` for each storage
that holds backups (PBS answers the same): a shared one once, on the own node
if it sees it, else on the next node that does; a local one on every node
that has it.
Each list is best-effort: when part of it cannot be read it is None, because
half a list would say a guest has no backup, or a job no guest, when it may.
"""
from __future__ import annotations
import logging
from typing import TYPE_CHECKING, Any
from napalm_proxmox.pve_values import split_list, to_flag, to_int, vmid_list
if TYPE_CHECKING:
# Type-only: newer than the napalm_device_types floor in pyproject.toml.
from napalm_device_types import BackupDict, BackupJobDict, ClusterStorageDict
logger = logging.getLogger(__name__)
_JsonDict = dict[str, Any]
_ALL_DAYS = "mon,tue,wed,thu,fri,sat,sun"
_GUEST_TYPES = ("qemu", "lxc")
def _schedule(job: _JsonDict) -> str | None:
if job.get("schedule"):
return str(job["schedule"])
start = job.get("starttime")
if not start:
return None
dow = ",".join(split_list(job.get("dow")))
return str(start) if dow in ("", _ALL_DAYS) else f"{dow} {start}"
def _pool_info(api: Any, pool: str) -> _JsonDict:
try:
for entry in api.pools.get(poolid=pool) or []:
if entry.get("poolid") == pool:
return dict(entry)
except Exception as exc:
logger.debug("GET /pools?poolid=%s failed, trying /pools/%s: %s", pool, pool, exc)
return dict(api.pools(pool).get() or {})
def _pool_members(api: Any, pool: str) -> list[int]:
members = _pool_info(api, pool).get("members") or []
return [
int(m["vmid"])
for m in members
if m.get("type") in _GUEST_TYPES and m.get("vmid") is not None
]
def _job(api: Any, job: _JsonDict) -> BackupJobDict:
pool = job.get("pool") or None
return {
"id": str(job.get("id") or ""),
"enabled": to_flag(job.get("enabled", 1)),
"schedule": _schedule(job),
"storage": job.get("storage") or None,
"mode": job.get("mode") or None,
"all": to_flag(job.get("all")),
"vmids": _pool_members(api, str(pool)) if pool else vmid_list(job.get("vmid")),
"exclude": vmid_list(job.get("exclude")),
"pool": pool,
"node": job.get("node") or None,
}
def read_backup_jobs(api: Any) -> list[BackupJobDict] | None:
"""The cluster's backup jobs; None when they, or a pool's guests, cannot be read."""
try:
return [_job(api, job) for job in api.cluster.backup.get() or []]
except Exception as exc:
logger.debug("Cannot read the backup jobs: %s", exc)
return None
def _content(api: Any, node: str, storage: str) -> list[_JsonDict]:
return list(api.nodes(node).storage(storage).content.get(content="backup") or [])
def _first_answer(api: Any, storage: str, nodes: list[str]) -> list[_JsonDict]:
error: Exception | None = None
for node in nodes:
try:
return _content(api, node, storage)
except Exception as exc:
logger.debug("Cannot read backups on %s from %s: %s", storage, node, exc)
error = exc
if error is not None:
raise error
return []
def _storage_rows(api: Any, storage: ClusterStorageDict, local_node: str) -> list[_JsonDict]:
nodes = storage["nodes"]
if storage["shared"]:
return _first_answer(api, storage["name"], sorted(nodes, key=lambda n: n != local_node))
return [row for node in nodes for row in _content(api, node, storage["name"])]
def _merge(rows: list[_JsonDict], storage: str, merged: dict[tuple[int, str], BackupDict]) -> None:
for row in rows:
if row.get("vmid") is None or row.get("ctime") is None:
continue # not a vzdump/PBS backup of a guest
key, ctime = (int(row["vmid"]), storage), int(row["ctime"])
entry = merged.get(key)
if entry is None:
merged[key] = {
"vmid": key[0],
"storage": storage,
"time": ctime,
"size": to_int(row.get("size")),
"count": 1,
}
continue
entry["count"] += 1
if ctime > entry["time"]:
entry["time"], entry["size"] = ctime, to_int(row.get("size"))
def read_backups(
api: Any, storages: list[ClusterStorageDict], local_node: str
) -> list[BackupDict] | None:
"""One entry per guest and storage with its newest backup; None if a storage
that holds backups cannot be read."""
merged: dict[tuple[int, str], BackupDict] = {}
try:
for storage in storages:
if "backup" in storage["content"]:
_merge(_storage_rows(api, storage, local_node), storage["name"], merged)
except Exception as exc:
logger.debug("Cannot read the backups: %s", exc)
return None
return [merged[key] for key in sorted(merged, key=lambda k: (k[1], k[0]))]