diff --git a/CHANGELOG.md b/CHANGELOG.md index 9aeb309..134962d 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -9,13 +9,14 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ### Added - Time sync on both drivers (NetOrk/netork#2365), through napalm-device-types' - `TimeSyncMixin` (4.35.0); both write the servers and the zone. Neither API keeps time + `TimeSyncMixin` (4.36.0); both write the servers and the zone. Neither API keeps time servers -- both products run chrony on Debian -- so `get_time_sync()` reads chrony's - sources, whether it is synchronised and the zone (`timedatectl`) in one round trip over - the driver's SSH session (`PosixTimeSyncMixin`), and the zone from `GET - /nodes/{node}/time` only where the host names none. `configure_time_sync()` writes the - servers to `/etc/chrony/sources.d/netork.sources` with every other source commented out - (Debian's vendor pool, the DHCP `sourcedir`) and reloads or restarts chrony; the zone + sources, the ones DHCP gave apart (`dhcp_ntp_servers`), whether it is synchronised and + the zone (`timedatectl`) in one round trip over the driver's SSH session + (`PosixTimeSyncMixin`), and the zone from `GET /nodes/{node}/time` only where the host + names none. `configure_time_sync()` writes the servers to + `/etc/chrony/sources.d/netork.sources` with every other configured source commented out + (Debian's vendor pool; the DHCP `sourcedir` stays) and reloads or restarts chrony; the zone goes through `PUT /nodes/{node}/time`, Proxmox's own call, so `/etc/timezone` and the web UI follow `/etc/localtime`. The token needs `Sys.Modify` for it (PVE on `/nodes/{node}`, PBS on `/system/time`). Only the node of the connection is read and @@ -45,7 +46,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 in its own daemons; `get_clusters()` and `get_backup_jobs()` keep them. ### Changed -- Requires napalm-device-types 4.35.0. +- Requires napalm-device-types 4.36.0. ### Fixed - `set_hostname()` writes the new `/etc/hosts` to a `mktemp` file beside it diff --git a/README.md b/README.md index 53d7826..6c6a1c3 100644 --- a/README.md +++ b/README.md @@ -129,8 +129,8 @@ device = driver( | `get_scheduled_jobs` | cron, `/etc/cron.d`, user crontabs, run-parts, anacron, systemd timers, as root over SSH (napalm-device-types' `PosixScheduledJobsMixin`); `pve-*` units, `pvesr.timer` and `/etc/cron.d/vzdump` are `platform` | | `manage_scheduled_job(key, action, digest=)` | enable, disable, run now; a `/etc/cron.d/vzdump` line only runs | | `put_scheduled_job(spec)` / `delete_scheduled_job(key, digest=)` | an application's own job, as a systemd timer | -| `get_time_sync` | chrony's sources and whether it is synchronised, the zone from `timedatectl`, in one round trip over SSH (napalm-device-types' `PosixTimeSyncMixin`); the zone from `GET /nodes/{node}/time` only where the host names none | -| `configure_time_sync(ntp_servers=, timezone=)` | servers to `/etc/chrony/sources.d/netork.sources`, every other source commented out, then a reload or a restart of chrony; the zone through `PUT /nodes/{node}/time` (the token needs `Sys.Modify` on `/nodes/{node}`), so `/etc/timezone` and the web UI follow. Only this node: in a cluster each node keeps its own clock | +| `get_time_sync` | chrony's sources, the ones DHCP gave apart (`dhcp_ntp_servers`), whether it is synchronised, the zone from `timedatectl`, in one round trip over SSH (napalm-device-types' `PosixTimeSyncMixin`); the zone from `GET /nodes/{node}/time` only where the host names none | +| `configure_time_sync(ntp_servers=, timezone=)` | servers to `/etc/chrony/sources.d/netork.sources`, every other configured source commented out (DHCP's `sourcedir` stays), then a reload or a restart of chrony; the zone through `PUT /nodes/{node}/time` (the token needs `Sys.Modify` on `/nodes/{node}`), so `/etc/timezone` and the web UI follow. Only this node: in a cluster each node keeps its own clock | **Not implemented** (raise `NotImplementedError`): `get_bgp_config`, `get_bgp_neighbors`, `get_bgp_neighbors_detail`, diff --git a/napalm_proxmox/time_sync.py b/napalm_proxmox/time_sync.py index efaab2c..35d3dac 100644 --- a/napalm_proxmox/time_sync.py +++ b/napalm_proxmox/time_sync.py @@ -3,8 +3,9 @@ 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. +(``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 diff --git a/pyproject.toml b/pyproject.toml index 572c7b9..2f763cb 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -21,7 +21,7 @@ classifiers = [ requires-python = ">=3.12" dependencies = [ "napalm>=5.0.0", - "napalm_device_types>=4.35.0", # TimeSyncMixin, PosixTimeSyncMixin + "napalm_device_types>=4.36.0", # TimeSyncMixin, PosixTimeSyncMixin (DHCP's servers) "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", diff --git a/tests/test_time_sync.py b/tests/test_time_sync.py index bfaebfc..2268d62 100644 --- a/tests/test_time_sync.py +++ b/tests/test_time_sync.py @@ -69,12 +69,19 @@ _MARK = "#napalm-time-sync: " def _reading( - zone: str = "Europe/Berlin", *, servers: tuple[str, ...] = (), timedatectl: bool = True + zone: str = "Europe/Berlin", + *, + servers: tuple[str, ...] = (), + dhcp: tuple[str, ...] = (), + deaf_to_dhcp: bool = False, + timedatectl: bool = True, ) -> str: """What the reading prints; *servers* in netOrk's sources file, the vendor's pool - commented out with it (as a write leaves the host).""" + commented out with it (as a write leaves the host), *dhcp* in the DHCP client's + sources file. *deaf_to_dhcp* is a host napalm-device-types 4.35.0 aligned: its write + commented out the DHCP ``sourcedir`` too.""" written = bool(servers) - conf = _CHRONY_CONF.format(pool=_MARK if written else "", dhcp=_MARK if written else "") + conf = _CHRONY_CONF.format(pool=_MARK if written else "", dhcp=_MARK if deaf_to_dhcp else "") parts = ["@@ts timedatectl"] if timedatectl: parts += [ @@ -89,6 +96,10 @@ def _reading( parts += ["@@ts zone", zone if timedatectl else ""] parts += ["@@ts active", _ACTIVE_CHRONY, "@@ts timesync", "@@ts chronyc", _CHRONYC_SYNCED] parts += ["@@ts chrony 0 main /etc/chrony/chrony.conf", conf, ""] + if dhcp: + parts += ["@@ts chrony 11 sourcedir /run/chrony-dhcp/vmbr0.sources"] + parts += [f"server {s} iburst" for s in dhcp] + parts += [""] if written: parts += ["@@ts chrony 14 sourcedir /etc/chrony/sources.d/netork.sources"] parts += [f"{'pool' if '.pool.' in s else 'server'} {s} iburst" for s in servers] @@ -169,6 +180,7 @@ def test_pve_reads_chrony_and_the_zone_in_one_round_trip() -> None: assert driver.get_time_sync() == { "ntp_servers": ["2.debian.pool.ntp.org"], + "dhcp_ntp_servers": [], "timezone": "Europe/Berlin", "synchronized": True, "daemon": "chrony", @@ -183,6 +195,7 @@ def test_pbs_reads_chrony_and_the_zone() -> None: assert driver.get_time_sync() == { "ntp_servers": ["10.0.0.1"], + "dhcp_ntp_servers": [], "timezone": "Europe/Vienna", "synchronized": True, "daemon": "chrony", @@ -191,6 +204,17 @@ def test_pbs_reads_chrony_and_the_zone() -> None: assert not [c for c in api.calls if c[0] != "GET"] +def test_servers_from_dhcp_are_read_apart_from_the_configured_ones() -> None: + """A node that gets its time giver by DHCP (option 42): chrony reads the DHCP + client's sources file, and those servers are not the node's own (NetOrk/netork#2380).""" + driver, _, _ = _pve(_reading(dhcp=("192.168.1.1",))) + + reading = driver.get_time_sync() + + assert reading["ntp_servers"] == ["2.debian.pool.ntp.org"] + assert reading["dhcp_ntp_servers"] == ["192.168.1.1"] + + def test_a_host_without_a_zone_of_its_own_reads_the_apis() -> None: """No timedatectl and no /etc/timezone or /etc/localtime link: the API's word.""" driver, api, _ = _pve(_reading(timedatectl=False)) @@ -315,10 +339,25 @@ def test_servers_go_to_chronys_sources_d_and_the_vendor_pool_stops_counting(make assert any( "/etc/chrony/sources.d/netork.sources" in c and c.startswith("install") for c in root ) - # line 8 is the vendor's pool, line 11 the DHCP sourcedir: both in the main file, - # which chronyd reads only when it starts. + # line 8 is the vendor's pool, in the main file, which chronyd reads only when it + # starts. Line 11, the DHCP client's sourcedir, stays: DHCP's servers count. sed = next(c for c in root if c.startswith("sed -i")) - assert "8{" in sed and "11{" in sed and sed.endswith("/etc/chrony/chrony.conf") + assert "8{" in sed and "11{" not in sed and sed.endswith("/etc/chrony/chrony.conf") + assert root[-1] == "systemctl restart chrony.service" + + +def test_a_write_gives_back_the_dhcp_sourcedir_an_earlier_write_took() -> None: + """A node napalm-device-types 4.35.0 aligned hears DHCP again (NetOrk/netork#2380).""" + servers = ("10.0.0.1",) + driver, _, shell = _pve(_reading(servers=servers, deaf_to_dhcp=True), _reading(servers=servers)) + + result = driver.configure_time_sync(ntp_servers=list(servers)) + + assert result["success"], result["output"] + root = shell.root() + sed = next(c for c in root if c.startswith("sed -i")) + assert "11{s/^#napalm-time-sync: (sourcedir" in sed + assert sed.endswith("/etc/chrony/chrony.conf") assert root[-1] == "systemctl restart chrony.service"