Files
napalm-proxmox/napalm_proxmox/running_backups.py
T
Christian Manivong 3877a5425a
driver / lint (pull_request) Successful in 13s
driver / standard (pull_request) Successful in 14s
driver / types (pull_request) Successful in 54s
driver / test (3.13) (pull_request) Successful in 1m2s
driver / test (3.12) (pull_request) Successful in 1m5s
driver / lowest (pull_request) Successful in 31s
driver / build (pull_request) Successful in 26s
driver / security (pull_request) Successful in 54s
CI / driver (pull_request) Successful in 2m10s
driver / publish (pull_request) Skipped
feat(running-backups): what a restart of a PVE node, a guest or a PBS would abort
netOrk holds a host's restart after patching while a backup runs on it, and
a guest's restart while its hypervisor is backing it up (NetOrk/netork#2318).
It asks right before the restart, so both drivers answer
get_running_backups() from napalm-device-types 4.34.0:

- PVE: the node's active vzdump tasks, each with the guests it is taking now
  (its own guest, or for a job the guests locked for their backup), and a
  guest locked without a task on its own, since the task list can lag and a
  lock can outlive its task.
- PBS: the running backup, sync, verify, gc, prune and tape tasks; a restore
  or housekeeping is no backup.

A failed read raises, so netOrk reads it as "not checked", never as "none".
2026-11-10 23:45:48 +01:00

127 lines
4.5 KiB
Python

"""get_running_backups() for Proxmox VE and Proxmox Backup Server.
What a restart would abort (napalm-device-types' ``RunningBackupsMixin``):
- **PVE:** the node's active vzdump tasks (``GET /nodes/<node>/tasks?source=active&
typefilter=vzdump``) and the guests on the node locked for their backup
(``GET /cluster/resources?type=vm``, ``lock: backup``). A task started for one
guest names it in its id; a job's task does not, and the guests it is taking now
are the locked ones. A guest locked without a task -- the list can lag behind,
a lock can outlive its task -- is listed on its own: its restart waits too.
- **PBS:** the running tasks that are a backup or one of the server's jobs
(``GET /nodes/localhost/tasks?running=1``). A restore or housekeeping is not.
PVE does not tell which scheduled job started a vzdump task, so ``job`` is None
there. A failed read raises: an empty list would say nothing runs.
"""
from __future__ import annotations
from typing import TYPE_CHECKING, Any
from napalm_device_types import RunningBackupDict, RunningBackupKind, RunningBackupsMixin
_JsonDict = dict[str, Any]
#: PBS worker types that are a backup or one of a backup server's jobs.
_PBS_KINDS: dict[str, RunningBackupKind] = {
"backup": "backup",
"sync": "sync",
"syncjob": "sync",
"verify": "verify",
"verificationjob": "verify",
"verify_group": "verify",
"verify_snapshot": "verify",
"garbage_collection": "gc",
"prune": "prune",
"prunejob": "prune",
"tape-backup": "tape",
"tape-backup-job": "tape",
}
def _since(task: _JsonDict) -> int | None:
value = task.get("starttime")
return int(value) if isinstance(value, int | float) and value else None
def _text(value: Any) -> str | None:
return str(value) if value not in (None, "") else None
class ProxmoxRunningBackupsMixin(RunningBackupsMixin):
"""PVE: the node's vzdump tasks and the guests locked for their backup."""
_api: Any
_node_name: str
if TYPE_CHECKING: # pragma: no cover - the base's, declared for type checkers
def _node_api(self) -> Any: ...
def _locked_guests(self) -> list[str]:
"""The guests on this node locked for their backup, in the API's order."""
return [
str(guest["vmid"])
for guest in self._api.cluster.resources.get(type="vm") or []
if guest.get("node") == self._node_name and guest.get("lock") == "backup"
]
def _vzdump_tasks(self) -> list[_JsonDict]:
tasks = self._node_api().tasks.get(source="active", typefilter="vzdump") or []
return [t for t in tasks if t.get("type") == "vzdump"]
def get_running_backups(self) -> list[RunningBackupDict]:
tasks = self._vzdump_tasks()
named = {str(t["id"]) for t in tasks if t.get("id")}
unnamed = [g for g in self._locked_guests() if g not in named]
found = [_vzdump(task, unnamed) for task in tasks]
if all(t.get("id") for t in tasks):
found += [_locked(guest) for guest in unnamed]
return found
def _vzdump(task: _JsonDict, unnamed: list[str]) -> RunningBackupDict:
"""A vzdump task: its own guest, or -- a job's task -- the guests locked now."""
guest = _text(task.get("id"))
return {
"kind": "backup",
"job": None,
"task": _text(task.get("upid")),
"guests": [guest] if guest else list(unnamed),
"since": _since(task),
}
def _locked(guest: str) -> RunningBackupDict:
"""A guest locked for its backup without a task that takes it."""
return {"kind": "backup", "job": None, "task": None, "guests": [guest], "since": None}
class PbsRunningBackupsMixin(RunningBackupsMixin):
"""PBS: the running backup, sync, verify, gc, prune and tape tasks."""
_node_name: str
if TYPE_CHECKING: # pragma: no cover - the backup-server mixin's, for type checkers
def _pbs(self, method: str, path: str, **params: Any) -> Any: ...
def get_running_backups(self) -> list[RunningBackupDict]:
tasks = self._pbs("get", f"nodes/{self._node_name}/tasks", running=1) or []
found: list[RunningBackupDict] = []
for task in tasks:
kind = _PBS_KINDS.get(str(task.get("worker_type") or ""))
if kind is None:
continue
found.append(
{
"kind": kind,
"job": _text(task.get("worker_id")),
"task": _text(task.get("upid")),
"guests": [],
"since": _since(task),
}
)
return found