feat(time-sync): servers and zone on Proxmox VE and Backup Server
driver / standard (pull_request) Successful in 14s
driver / lint (pull_request) Successful in 14s
driver / types (pull_request) Successful in 56s
driver / test (3.12) (pull_request) Successful in 1m2s
driver / test (3.13) (pull_request) Successful in 59s
driver / lowest (pull_request) Successful in 32s
driver / build (pull_request) Successful in 31s
driver / security (pull_request) Successful in 1m11s
CI / driver (pull_request) Successful in 2m41s
driver / publish (pull_request) Skipped

netOrk keeps one list of NTP servers and a time zone per site and aligns
a device that differs (NetOrk/netork#2360, #2365). Both drivers now
implement napalm-device-types' TimeSyncMixin (4.35.0) and write both
values.

Neither API keeps time servers: /nodes/{node}/time knows only the zone,
and both products run chrony on Debian. The servers are therefore
PosixTimeSyncMixin's, over the driver's existing SSH session
(run_command, root): read from chrony's configuration, written to
/etc/chrony/sources.d/netork.sources with every other source commented
out, then a reload or a restart of chrony.

The zone is read with them from the host (timedatectl: the zone the
clock really runs in), and from GET /nodes/{node}/time only where the
host names none. It is written through PUT /nodes/{node}/time rather
than 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.

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.

get_ntp_servers() read GET /nodes/{node}/ntp, a path Proxmox VE does not
have, so it always reported no servers. It now reports what
get_time_sync() reads, on the backup server too.
This commit is contained in:
Christian Manivong committed 2026-11-11 15:18:08 +01:00
1 parent aeb12eae00
commit 410d34bdee
11 files changed
+457 -46

No files matched your search

+2
View File
@@ -43,6 +43,7 @@ from napalm_proxmox.pbs_backups import ProxmoxBackupServerMixin
from napalm_proxmox.pbs_host import ProxmoxPbsHostMixin
from napalm_proxmox.running_backups import PbsRunningBackupsMixin
from napalm_proxmox.scheduled_jobs import PBS_PLATFORM_SOURCES, ProxmoxScheduledJobsMixin
from napalm_proxmox.time_sync import ProxmoxTimeSyncMixin
logger = logging.getLogger(__name__)
@@ -56,6 +57,7 @@ class ProxmoxBackupDriver(
PbsRunningBackupsMixin,
ProxmoxPbsHostMixin,
ProxmoxHostMixin,
ProxmoxTimeSyncMixin,
BackupServerDriver,
StorageDriver,
KernelFactsMixin,
+2 -12
View File
@@ -59,6 +59,7 @@ from napalm_proxmox.running_backups import ProxmoxRunningBackupsMixin
from napalm_proxmox.scheduled_jobs import PVE_PLATFORM_SOURCES, ProxmoxScheduledJobsMixin
from napalm_proxmox.sdn_mixin import ProxmoxSDNMixin
from napalm_proxmox.system_mixin import ProxmoxSystemMixin
from napalm_proxmox.time_sync import ProxmoxTimeSyncMixin
from napalm_proxmox.vm_contract_mixin import ProxmoxVMContractMixin
from napalm_proxmox.vm_migrate_mixin import ProxmoxVMMigrateMixin
from napalm_proxmox.vm_mixin import ProxmoxVMMixin
@@ -100,6 +101,7 @@ class ProxmoxDriver(
SystemdServicesMixin,
ProxmoxScheduledJobsMixin,
ProxmoxRunningBackupsMixin,
ProxmoxTimeSyncMixin,
HostStatusMixin,
LicenseMixin,
SnmpAgentMixin,
@@ -198,18 +200,6 @@ class ProxmoxDriver(
f"set the 'node' driver argument"
)
# ------------------------------------------------------------------ #
# Node info helpers (PVE only)
# ------------------------------------------------------------------ #
def _get_node_ntp(self) -> _JsonDict:
"""Return NTP configuration for this node."""
try:
return self._node_api().ntp.get() or {}
except Exception as exc:
logger.debug("Failed to fetch node NTP: %s", exc)
return {}
# ------------------------------------------------------------------ #
# NAPALM getters kept in driver
# ------------------------------------------------------------------ #
+1 -13
View File
@@ -176,21 +176,9 @@ class ProxmoxSystemMixin:
return env
# ------------------------------------------------------------------ #
# get_ntp_servers / get_ntp_stats
# get_ntp_stats (get_ntp_servers: time_sync.ProxmoxTimeSyncMixin)
# ------------------------------------------------------------------ #
def get_ntp_servers(self) -> dict[str, _JsonDict]:
"""Return configured NTP servers."""
ntp = self._get_node_ntp()
servers: dict[str, _JsonDict] = {}
# Proxmox reports a comma-separated or space-separated server list
raw = ntp.get("server", "") or ntp.get("servers", "")
for srv in re.split(r"[\s,]+", raw):
srv = srv.strip()
if srv:
servers[srv] = {}
return servers
def get_ntp_stats(self) -> list[_JsonDict]:
"""Return NTP synchronisation statistics from chrony, or ntpd where chrony is missing."""
return parse_ntp_stats(self._exec_ssh_command(NTP_STATS_COMMAND))
+65
View File
@@ -0,0 +1,65 @@
"""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, written to
``/etc/chrony/sources.d/netork.sources`` with every other source commented out.
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})"}