Files
napalm-proxmox/napalm_proxmox/time_sync.py
T
Christian Manivong 83d1ef780e test(time-sync): follow napalm-device-types 4.36.0, which keeps DHCP's servers
napalm-device-types 4.36.0 (released after main's last CI run) changed
PosixTimeSyncMixin: get_time_sync() reports DHCP-provided servers apart
(dhcp_ntp_servers), and configure_time_sync() no longer comments out the
DHCP client's chrony sourcedir; it gives back one that 4.35.0 took out
(NetOrk/netork#2380). CI installs the newest release, so five time-sync
tests failed on every branch.

The tests now expect that behaviour, read DHCP's servers apart, and check
that a node 4.35.0 aligned gets its DHCP sourcedir back. The floor moves
to 4.36.0, so the lowest-versions job tests the same behaviour; README,
CHANGELOG and the module docstring say what a write leaves alone.
2026-11-13 08:55:37 +01:00

67 lines
2.8 KiB
Python

"""Time sync on a Proxmox VE node and a Proxmox Backup Server (``TimeSyncMixin``).
Neither API keeps time servers: ``/nodes/{node}/time`` knows only the zone, and
both products run chrony on Debian. So the servers are
:class:`~napalm_device_types.PosixTimeSyncMixin`'s, over the driver's SSH session
(``run_command``, root): read from chrony's configuration, DHCP's apart, written to
``/etc/chrony/sources.d/netork.sources`` with every other configured source commented
out; the DHCP client's ``sourcedir`` stays.
The zone is read with them, from the host (``timedatectl``, the zone the node's
clock really runs in), and the API's ``GET /nodes/{node}/time`` answers only when
the host names none. It is written through ``PUT /nodes/{node}/time``, not
``timedatectl set-timezone``: Proxmox's own call validates the name and keeps
``/etc/timezone``, which its web UI and API show, in step with ``/etc/localtime``.
The token needs ``Sys.Modify`` for it (PVE on ``/nodes/{node}``, PBS on
``/system/time``).
One device is one node: in a PVE cluster every node has its own clock, and only
the node this connection resolved is read and written.
"""
from __future__ import annotations
from typing import TYPE_CHECKING, Any, ClassVar
from napalm_device_types import TIME_SYNC_VALUES, PosixTimeSyncMixin, TimeSyncDict
from napalm_device_types.models import FixStepOutcomeDict
_JsonDict = dict[str, Any]
class ProxmoxTimeSyncMixin(PosixTimeSyncMixin):
"""Servers through chrony over SSH, the zone through the node's API."""
time_sync_writes: ClassVar[frozenset[str]] = TIME_SYNC_VALUES
_node_name: str
if TYPE_CHECKING: # pragma: no cover - the driver's (napalm_proxmox._base)
def _node_api(self) -> Any: ...
def _get_node_time(self) -> _JsonDict: ...
def get_time_sync(self) -> TimeSyncDict:
"""The node's time sources, zone and client: one round trip over SSH, and the
API's zone only where the host names none.
:raises ValueError: when the reading came back cut short.
:raises ConnectionException: when the node's shell could not be reached.
"""
found = super().get_time_sync()
if found["timezone"] is None:
found["timezone"] = self._get_node_time().get("timezone") or None
return found
def get_ntp_servers(self) -> dict[str, _JsonDict]:
"""NAPALM's shape of the servers ``get_time_sync`` reads, in chrony's order."""
return {server: {} for server in super().get_time_sync()["ntp_servers"]}
def _write_time_zone(self, zone: str) -> FixStepOutcomeDict:
path = f"PUT /nodes/{self._node_name}/time"
try:
self._node_api().time.put(timezone=zone)
except Exception as exc:
return {"ok": False, "output": f"{path}: {exc}"}
return {"ok": True, "output": f"time zone set to {zone} ({path})"}