"""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//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