Files
napalm-proxmox/napalm_proxmox/update_history.py
T
Christian Manivong 707b24748b feat(updates): the update history before PVE 9.3 from apt's log over SSH (NetOrk/netork#2728)
Below 9.3 get_update_history reads apt's history log over the node's SSH session
with napalm-device-types' AptHistoryMixin, which names a transaction as
GET /nodes/{node}/apt/history does, so the ids stay when the node moves to 9.3.
A node without an SSH login still raises NotImplementedError. Requires
napalm-device-types 4.47.0.
2026-11-26 23:07:22 +01:00

95 lines
3.7 KiB
Python

"""The node's past APT transactions (``UpdateMixin.get_update_history``, NetOrk/netork#2728).
PVE 9.3 reads apt's history log for ``GET /nodes/{node}/apt/history``, newest
first, also the transactions netOrk did not start (an operator's ``apt``,
unattended-upgrades). A transaction is named by its ``start-time`` and its
``start-index`` among those that started in the same second of the same log
file. An older node has no such endpoint: the driver reads the same log over
its SSH session with napalm-device-types' ``AptHistoryMixin``, which names a
transaction the same way, so the ids stay when the node moves to 9.3. A node
without an SSH login raises ``NotImplementedError`` there.
"""
from __future__ import annotations
from typing import TYPE_CHECKING, Any, cast
from napalm.base.exceptions import ConnectionException
from napalm_device_types import (
AptHistoryMixin,
UpdateHistoryAction,
UpdateHistoryDict,
UpdateHistoryPackageDict,
)
from napalm_proxmox.pve_values import PVE_9_3
_JsonDict = dict[str, Any]
def _text(value: Any) -> str | None:
return str(value) if value else None
def _package(row: _JsonDict) -> UpdateHistoryPackageDict:
return {
"name": str(row.get("package") or ""),
"action": cast(UpdateHistoryAction, row.get("action")),
"old_version": _text(row.get("old-version")),
"new_version": _text(row.get("new-version")),
}
def _transaction(row: _JsonDict) -> UpdateHistoryDict:
start = int(row["start-time"])
end = row.get("end-time")
return {
"id": f"{start}-{int(row.get('start-index') or 0)}",
"start": start,
"end": None if end is None else int(end),
"packages": [_package(p) for p in row.get("packages") or []],
"requested_by": _text(row.get("requested-by")),
"command": _text(row.get("command-line")),
"error": _text(row.get("error")),
}
class ProxmoxUpdateHistoryMixin(AptHistoryMixin):
"""Relies on the driver's ``_node_api``, ``_version_at_least``, ``_has_ssh_login``
and ``run_command``."""
_node_name: str
if TYPE_CHECKING: # pragma: no cover - the driver's (napalm_proxmox._base)
def _node_api(self) -> Any: ...
def _version_at_least(self, major: int, minor: int) -> bool: ...
def _has_ssh_login(self) -> bool: ...
def get_update_history(self, since: int | None = None) -> list[UpdateHistoryDict]:
"""The node's APT transactions, newest first, from ``since`` (unix seconds) on.
:raises NotImplementedError: before PVE 9.3 on a node without an SSH login.
:raises RuntimeError: the history could not be read; never an empty list.
"""
if not self._version_at_least(*PVE_9_3):
return self._history_over_ssh(since)
params = {} if since is None else {"since": since}
try:
rows = self._node_api().apt.history.get(**params) or []
except Exception as exc:
raise RuntimeError(f"Cannot read the APT history of {self._node_name}: {exc}") from exc
return [_transaction(row) for row in rows]
def _history_over_ssh(self, since: int | None) -> list[UpdateHistoryDict]:
"""Before 9.3: apt's log over the SSH session, root's login."""
if not self._has_ssh_login():
raise NotImplementedError(
f"Proxmox VE on {self._node_name} reports its APT history from 9.3 on; "
"before that it is read over SSH, and the node has no SSH login"
)
try:
return super().get_update_history(since)
except (ConnectionException, ValueError) as exc:
raise RuntimeError(f"Cannot read the APT history of {self._node_name}: {exc}") from exc