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.
This commit is contained in:
1 parent
dc0d6345a8
commit
707b24748b
5 files changed
+122
-16
No files matched your search
@@ -7,6 +7,17 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
|
||||
|
||||
## [Unreleased]
|
||||
|
||||
### Added
|
||||
- `get_update_history(since)` before PVE 9.3: apt's history log over the node's SSH
|
||||
session, through napalm-device-types' `AptHistoryMixin` (NetOrk/netork#2728). A
|
||||
transaction gets the id 9.3's `GET /nodes/{node}/apt/history` gives it (start time and
|
||||
the index among those that started in the same second of the same log file), so a node
|
||||
keeps its ids when it moves to 9.3; 9.3 and later still read the API. A node without an
|
||||
SSH login raises `NotImplementedError`, a failed read `RuntimeError`.
|
||||
|
||||
### Changed
|
||||
- Needs napalm-device-types 4.47.0 or later (`AptHistoryMixin`).
|
||||
|
||||
## [0.16.0] - 2026-11-26
|
||||
|
||||
### Added
|
||||
|
||||
@@ -251,6 +251,10 @@ class ProxmoxBase:
|
||||
f"Cannot run a command on {self.hostname} over SSH: {exc}"
|
||||
) from exc
|
||||
|
||||
def _has_ssh_login(self) -> bool:
|
||||
"""Whether the driver has something to log in to the node's SSH with."""
|
||||
return bool(self._ssh_password or self.password or self._ssh_key)
|
||||
|
||||
def _node_ssh(self) -> Any: # paramiko.SSHClient; paramiko is imported lazily
|
||||
"""The SSH session to the node on port 22, opened once.
|
||||
|
||||
|
||||
@@ -4,15 +4,23 @@ 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 raises
|
||||
``NotImplementedError`` and parses no log over SSH.
|
||||
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_device_types import UpdateHistoryAction, UpdateHistoryDict, UpdateHistoryPackageDict
|
||||
from napalm.base.exceptions import ConnectionException
|
||||
from napalm_device_types import (
|
||||
AptHistoryMixin,
|
||||
UpdateHistoryAction,
|
||||
UpdateHistoryDict,
|
||||
UpdateHistoryPackageDict,
|
||||
)
|
||||
|
||||
from napalm_proxmox.pve_values import PVE_9_3
|
||||
|
||||
@@ -46,8 +54,9 @@ def _transaction(row: _JsonDict) -> UpdateHistoryDict:
|
||||
}
|
||||
|
||||
|
||||
class ProxmoxUpdateHistoryMixin:
|
||||
"""Relies on the driver's ``_node_api`` and ``_version_at_least``."""
|
||||
class ProxmoxUpdateHistoryMixin(AptHistoryMixin):
|
||||
"""Relies on the driver's ``_node_api``, ``_version_at_least``, ``_has_ssh_login``
|
||||
and ``run_command``."""
|
||||
|
||||
_node_name: str
|
||||
|
||||
@@ -55,21 +64,31 @@ class ProxmoxUpdateHistoryMixin:
|
||||
|
||||
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, which has no history endpoint.
|
||||
: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):
|
||||
raise NotImplementedError(
|
||||
f"Proxmox VE on {self._node_name} keeps no APT history its API reports "
|
||||
"(from 9.3 on)"
|
||||
)
|
||||
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
|
||||
+1
-1
@@ -21,7 +21,7 @@ classifiers = [
|
||||
requires-python = ">=3.12"
|
||||
dependencies = [
|
||||
"napalm>=5.0.0",
|
||||
"napalm_device_types>=4.46.0", # ServicesRestartMixin with LXC left out; CredentialExpiryMixin
|
||||
"napalm_device_types>=4.47.0", # AptHistoryMixin; ServicesRestartMixin with LXC left out
|
||||
"paramiko>=5.0.0", # CVE-2026-44405; imported directly for the node's SSH session (_base.py)
|
||||
"proxmoxer>=2.0.0",
|
||||
"netaddr>=0.9.0",
|
||||
|
||||
@@ -4,14 +4,21 @@ PVE 9.3 reads apt's history log for ``GET /nodes/{node}/apt/history``, newest
|
||||
first. The payload below follows its schema: a transaction is identified by
|
||||
``start-time`` with ``start-index`` (default 0) among those that started in the
|
||||
same second; ``end-time`` is missing while it runs or after it was interrupted.
|
||||
An older node has no such endpoint, and the driver parses no log over SSH.
|
||||
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, and a node without an SSH login has no history.
|
||||
"""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
from unittest.mock import MagicMock
|
||||
from unittest.mock import MagicMock, patch
|
||||
|
||||
import pytest
|
||||
from napalm.base.exceptions import ConnectionException
|
||||
from napalm_device_types import APT_HISTORY_COMMAND, CommandResult
|
||||
|
||||
from napalm_proxmox.driver import ProxmoxDriver
|
||||
from tests.conftest import _build_mock_api
|
||||
|
||||
HISTORY = [
|
||||
{
|
||||
@@ -117,8 +124,73 @@ class TestUpdateHistory:
|
||||
with pytest.raises(RuntimeError, match="timed out"):
|
||||
driver.get_update_history()
|
||||
|
||||
def test_an_older_node_has_no_history(self, driver, history):
|
||||
|
||||
#: The node's log as AptHistoryMixin's command prints it: the transaction the API
|
||||
#: names 1764158400-1 is the second to start in that second (12:00:00 UTC) of the file.
|
||||
LOG = """\
|
||||
### apt-history zone /usr/share/zoneinfo/Etc/UTC
|
||||
### apt-history offset +0000
|
||||
### apt-history file /var/log/apt/history.log
|
||||
|
||||
Start-Date: 2025-11-26 12:00:00
|
||||
Commandline: apt-get update-kernel
|
||||
Install: proxmox-kernel-6.17.2-1-pve-signed:amd64 (6.17.2-1, automatic)
|
||||
End-Date: 2025-11-26 12:00:00
|
||||
|
||||
Start-Date: 2025-11-26 12:00:00
|
||||
Commandline: apt-get -y dist-upgrade
|
||||
Requested-By: admin (1000)
|
||||
Upgrade: openssh-server:amd64 (1:10.0p1-7, 1:10.0p1-7+deb13u1)
|
||||
Purge: ifupdown:amd64 (0.8.44)
|
||||
End-Date: 2025-11-26 12:01:00
|
||||
|
||||
### apt-history status 0
|
||||
### apt-history end
|
||||
"""
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def older(driver, history) -> MagicMock:
|
||||
"""A 9.2 node whose SSH session prints :data:`LOG`."""
|
||||
driver._api.version.get.return_value = {"version": "9.2.4", "release": "9.2"}
|
||||
with pytest.raises(NotImplementedError):
|
||||
driver.get_update_history()
|
||||
run = MagicMock(return_value=CommandResult(LOG, "", 0))
|
||||
driver.run_command = run
|
||||
return run
|
||||
|
||||
|
||||
class TestBeforeNineThree:
|
||||
def test_the_log_is_read_over_ssh(self, driver, history, older):
|
||||
newest, first = driver.get_update_history()
|
||||
assert newest["id"] == "1764158400-1" # the API's id for the same transaction
|
||||
assert newest["end"] == 1764158460
|
||||
assert newest["requested_by"] == "admin"
|
||||
assert [p["action"] for p in newest["packages"]] == ["upgrade", "purge"]
|
||||
assert first["id"] == "1764158400-0"
|
||||
older.assert_called_once()
|
||||
assert older.call_args.args == (APT_HISTORY_COMMAND,)
|
||||
history.assert_not_called()
|
||||
|
||||
def test_since_filters_the_log(self, driver, history, older):
|
||||
assert driver.get_update_history(since=1764158401) == []
|
||||
|
||||
def test_a_failed_ssh_read_raises(self, driver, history, older):
|
||||
older.side_effect = ConnectionException("Cannot run a command on pve1 over SSH: reset")
|
||||
with pytest.raises(RuntimeError, match="reset"):
|
||||
driver.get_update_history()
|
||||
|
||||
def test_a_log_cut_short_raises(self, driver, history, older):
|
||||
older.return_value = CommandResult(LOG.replace("### apt-history end\n", ""), "", 0)
|
||||
with pytest.raises(RuntimeError, match="cut short"):
|
||||
driver.get_update_history()
|
||||
|
||||
def test_a_node_without_ssh_login_has_no_history(self):
|
||||
drv = ProxmoxDriver(
|
||||
"pve1", "netork", "", optional_args={"token_name": "poll", "token_value": "s3cret"}
|
||||
)
|
||||
api = _build_mock_api(version={"version": "9.2.4", "release": "9.2"})
|
||||
with patch("napalm_proxmox.driver.ProxmoxAPI", return_value=api):
|
||||
drv.open()
|
||||
drv.run_command = MagicMock()
|
||||
with pytest.raises(NotImplementedError, match="SSH"):
|
||||
drv.get_update_history()
|
||||
drv.run_command.assert_not_called()
|
||||
Reference in new issue
Block a user