Files
napalm-proxmox/napalm_proxmox/cluster_ha.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

106 lines
3.7 KiB
Python

"""get_clusters() for Proxmox VE: quorum votes and the guests the HA manager keeps.
Read from:
- ``GET /cluster/config/nodes``: corosync's nodelist, ``quorum_votes`` per node;
- ``GET /cluster/ha/resources``: each HA resource's ``sid`` (``vm:100``,
``ct:101``), requested ``state`` and, on PVE 8, ``group``;
- ``GET /cluster/ha/status/current``: per ``service`` entry the ``node`` and the
CRM's ``crm_state`` (only the verbose ``state`` while the CRM has not taken it
up yet);
- ``GET /cluster/ha/rules`` (PVE 9): each rule's ``rule`` id and its
``resources``, a comma-separated list of sids. PVE 8 has no such endpoint.
Each read is best-effort: what cannot be read is None, never a guess.
"""
from __future__ import annotations
import logging
from typing import TYPE_CHECKING, Any
from napalm_proxmox.pve_values import split_list
if TYPE_CHECKING:
# Type-only: newer than the napalm_device_types floor in pyproject.toml.
from napalm_device_types import HAResourceDict
logger = logging.getLogger(__name__)
_JsonDict = dict[str, Any]
#: HA resource type prefix -> guest kind.
_KINDS = {"vm": "qemu", "ct": "lxc"}
#: corosync counts a node without ``quorum_votes`` as one vote.
_DEFAULT_VOTES = 1
#: What HA keeps a resource at when its config names no state.
_DEFAULT_REQUESTED = "started"
def read_votes(api: Any) -> dict[str, int] | None:
"""Quorum votes per node name; None when the nodelist cannot be read."""
try:
rows = api.cluster.config.nodes.get() or []
except Exception as exc:
logger.debug("Cannot read the corosync nodelist: %s", exc)
return None
return {
str(row.get("node") or row.get("name")): int(row.get("quorum_votes", _DEFAULT_VOTES))
for row in rows
if row.get("node") or row.get("name")
}
def _services(api: Any) -> dict[str, _JsonDict] | None:
try:
rows = api.cluster.ha.status.current.get() or []
except Exception as exc:
logger.debug("Cannot read the HA status: %s", exc)
return None
return {str(r["sid"]): r for r in rows if r.get("type") == "service" and r.get("sid")}
def _rules(api: Any) -> dict[str, list[str]]:
"""Rule ids per sid. PVE 8 answers with an error: no rules there."""
try:
rows = api.cluster.ha.rules.get() or []
except Exception as exc:
logger.debug("No HA rules (PVE 8 has none): %s", exc)
return {}
by_sid: dict[str, list[str]] = {}
for rule in rows:
for sid in split_list(rule.get("resources")):
by_sid.setdefault(sid, []).append(str(rule.get("rule")))
return by_sid
def _resource(
row: _JsonDict, services: dict[str, _JsonDict] | None, rules: dict[str, list[str]]
) -> HAResourceDict | None:
sid = str(row.get("sid") or "")
prefix, _, vmid = sid.partition(":")
if prefix not in _KINDS or not vmid.isdigit():
return None
service = (services or {}).get(sid, {})
return {
"vmid": int(vmid),
"kind": _KINDS[prefix],
"requested": str(row.get("state") or _DEFAULT_REQUESTED),
"status": service.get("crm_state") or service.get("state") or None,
"node": service.get("node") or None,
"group": row.get("group") or None,
"rules": rules.get(sid, []),
}
def read_ha_resources(api: Any) -> list[HAResourceDict] | None:
"""Every guest HA manages, by vmid; None when the HA resources cannot be read."""
try:
rows = api.cluster.ha.resources.get() or []
except Exception as exc:
logger.debug("Cannot read the HA resources: %s", exc)
return None
services, rules = _services(api), _rules(api)
resources = [_resource(row, services, rules) for row in rows]
return sorted((r for r in resources if r is not None), key=lambda r: r["vmid"])