"""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})"}