diff --git a/CHANGELOG.md b/CHANGELOG.md index 2872be4..c421297 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -8,6 +8,19 @@ recorded here; their history is the git log. ## [Unreleased] ### Added +- Time sync (napalm-device-types `TimeSyncMixin`, NetOrk/netork#2364): `get_time_sync()` + reads the router's NTP servers and its zone from one `uci -q show system`; + `configure_time_sync()` writes both. The servers replace `system.ntp.server`, enable + the client and turn off the servers of a DHCP lease (`use_dhcp=0`), so the router + takes time from exactly these; `sysntpd` restarts. The zone is written as its name + (`zonename`, in LuCI's spelling) and its POSIX rule (`timezone`, from + napalm-device-types' `posix_tz`), then `/etc/init.d/system reload` and a restart of + logd apply it -- the rule, not the name, is what libc reads (NetOrk/netork#1849). A + value the router already has is not written again and nothing restarts. The zone + reads by its name only where the rule in force is that zone's: a router whose rule + differs from its `zonename`, or that has a rule only, reads as the one zone with that + rule, or as no zone where several share it. `synchronized` is None: BusyBox ntpd keeps + no state to read. - `get_host_status()` (napalm-device-types `HostStatusMixin`), over an SSH exec channel: whether the router needs a reboot, how full its filesystems are, and -- with napalm-device-types 4.32 or newer -- whether apk (OpenWrt 25.12) or opkg (before) was @@ -32,8 +45,12 @@ recorded here; their history is the git log. to do on every poll (#8). Needs napalm-device-types with `LldpAgentMixin`. ### Changed -- The floor on napalm-device-types is 4.26.0, the first release with `PosixScheduledJobsMixin` - (it was 4.24.0, for the shared `lldpctl` parser). +- `get_ntp_servers()` and `get_ntp_peers()` read over an SSH exec channel, with the same + reading as `get_time_sync()`, and name no servers while the NTP client is disabled + (`system.ntp.enabled='0'`): sysntpd then starts no ntpd. +- The floor on napalm-device-types is 4.35.0, the first release with `TimeSyncMixin` + (it was 4.26.0 for `PosixScheduledJobsMixin`, and 4.24.0 for the shared `lldpctl` + parser before that). - Python 3.12 is the floor (`requires-python = ">=3.12"`): netOrk runs every driver on 3.12, so 3.10 and 3.11 are no longer tested (NAPALM/driver-standard#6). - The floor on napalm is 5.1: older releases import `pkg_resources`, which a Python @@ -45,6 +62,8 @@ recorded here; their history is the git log. `manage_service("lldpd", "enable")` and `manage_service("lldpd", "start")` (#8). ### Fixed +- `get_ntp_servers()` names every server of the `system.ntp.server` list; it kept only + the first, as uci prints a list as `'a' 'b' 'c'` on one line. - `get_lldp_neighbors()` and `get_lldp_neighbors_detail()` keep an interface name with a dot whole: the neighbours of a VLAN interface such as `eth0.1` used to come back under `eth0` with every field empty (#26). A neighbour that names its port by MAC diff --git a/README.md b/README.md index a91b3aa..b0f5e67 100644 --- a/README.md +++ b/README.md @@ -69,6 +69,8 @@ device.close() | `get_scheduled_jobs()` | ✅ | BusyBox crond's `/etc/crontabs/`, time zone from `system.@system[0].zonename`, owner from `opkg search` (napalm-device-types `PosixScheduledJobsMixin`) | | `manage_scheduled_job(key, action, digest=)` | ✅ | enable, disable, run now (bounded by `timeout` where the router has it; another user's job through `start-stop-daemon -c`) | | `put_scheduled_job(spec)` / `delete_scheduled_job(key, digest=)` | ✅ | an application's own job: two marked lines in the user's crontab; `@daily` and the other aliases as five fields, `@reboot` refused (BusyBox crond has no special times) | +| `get_ntp_servers` / `get_ntp_peers` | ✅ | `system.ntp.server` in UCI, every server of the list; none while `system.ntp.enabled` is off -- the same list as `get_time_sync()` | +| `get_time_sync()` | ✅ | napalm-device-types `TimeSyncMixin`: the servers as above, the zone from `zonename` where the rule in `timezone` is that zone's (else the one zone with that rule, or none); `synchronized` is not readable (None) | ## Configuration management @@ -114,6 +116,22 @@ to the interfaces given, enables it at boot and restarts it. A VLAN device such `br-ap.10` is written as the device under it (`br-ap`): lldpd sends on the L2 device. Which interfaces is the caller's choice; at least one is required. +## Time sync + +`configure_time_sync(ntp_servers=, timezone=)` (napalm-device-types `TimeSyncMixin`) +writes both values into UCI's `system` config and applies them; a value the router +already has is not written again, so nothing restarts. + +* The servers replace the `system.ntp.server` list; the client is enabled, the servers + of a DHCP lease are turned off (`use_dhcp=0`) so the router has exactly these, and + `sysntpd` restarts. +* The zone goes into `zonename` (as LuCI keeps it, with spaces for underscores) and its + POSIX rule into `timezone`, the value libc reads -- an IANA name there leaves the + router on UTC (NetOrk/netork#1849). `/etc/init.d/system reload` applies it, and logd + restarts, as it reads the zone when it starts. + +Writes run as root: OpenWrt has no sudo, so another login is refused before anything runs. + ## Development ```bash diff --git a/napalm_openwrt/openwrt.py b/napalm_openwrt/openwrt.py index bfa99fd..8b0ae30 100644 --- a/napalm_openwrt/openwrt.py +++ b/napalm_openwrt/openwrt.py @@ -50,6 +50,7 @@ from napalm_openwrt.packages_mixin import OpenWrtPackageMixin from napalm_openwrt.routing_mixin import OpenWrtRoutingMixin from napalm_openwrt.scheduled_jobs_mixin import OpenWrtScheduledJobsMixin from napalm_openwrt.system_mixin import _RC_MARKER, OpenWrtSystemMixin, _split_status +from napalm_openwrt.time_sync_mixin import OpenWrtTimeSyncMixin from napalm_openwrt.vlan_mixin import OpenWrtVLANMixin from napalm_openwrt.wireless_mixin import OpenWrtWirelessMixin @@ -76,6 +77,7 @@ class OpenWrtDriver( OpenWrtWirelessMixin, OpenWrtLLDPMixin, OpenWrtConfigMixin, + OpenWrtTimeSyncMixin, OpenWrtSystemMixin, OpenWrtPackageMixin, OpenWrtRoutingMixin, diff --git a/napalm_openwrt/system_mixin.py b/napalm_openwrt/system_mixin.py index 95f7179..447bde9 100644 --- a/napalm_openwrt/system_mixin.py +++ b/napalm_openwrt/system_mixin.py @@ -532,38 +532,9 @@ class OpenWrtSystemMixin: return {"success": False, "output": output, "error": error} # ------------------------------------------------------------------ - # NTP + # NTP statistics (the servers: time_sync_mixin) # ------------------------------------------------------------------ - def get_ntp_servers(self) -> dict[str, Any]: - """Return configured NTP servers from ``uci show system``. - - UCI example:: - - system.ntp.server='0.openwrt.pool.ntp.org 1.openwrt.pool.ntp.org' - """ - uci_out = self._send_command("uci show system") - servers: dict[str, Any] = {} - - for line in uci_out.splitlines(): - # Handles both list and single-value UCI representations - m = re.match(r"system\.ntp\.server(?:\[\d+\])?='([^']*)'", line.strip()) - if m: - for srv in m.group(1).split(): - srv = srv.strip() - if srv: - servers[srv] = {} - - return servers - - def get_ntp_peers(self) -> dict[str, Any]: - """Return NTP peers from ``uci show system``. - - OpenWrt's busybox ntpd does not differentiate peers from servers; - the same UCI ``ntp.server`` list is returned. - """ - return self.get_ntp_servers() - def get_ntp_stats(self) -> list[dict[str, Any]]: """Return NTP synchronisation statistics. diff --git a/napalm_openwrt/time_sync_mixin.py b/napalm_openwrt/time_sync_mixin.py new file mode 100644 index 0000000..2cd7099 --- /dev/null +++ b/napalm_openwrt/time_sync_mixin.py @@ -0,0 +1,302 @@ +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +"""Time sync on OpenWrt: UCI's ``system`` config (napalm-device-types ``TimeSyncMixin``). + +OpenWrt keeps both values in ``/etc/config/system`` (NetOrk/netork#2364): + +* **The servers** are the ``system.ntp.server`` list, which ``/etc/init.d/sysntpd`` + hands to BusyBox ntpd while ``system.ntp.enabled`` is on. A disabled client has no + servers: the list names nothing the clock takes time from. sysntpd also adds the + servers of a DHCP lease unless ``use_dhcp`` is ``0``; a write turns that off, so the + router ends up with exactly the servers given. +* **The zone** is kept twice: ``zonename`` (the IANA name, as LuCI writes it -- with + spaces where the IANA name has underscores) and ``timezone``, the POSIX rule + ``/etc/init.d/system`` writes to ``/tmp/TZ`` for libc. The rule is what the clock + runs on: an IANA name written there left an AP on UTC (NetOrk/netork#1849). So the + name is read as the zone only where the rule in force is that zone's; a rule + without a matching name is read as the one zone that has it, or as no zone where + several do (``CET-1CEST,...`` is Berlin, Paris, Madrid, ...). + +Reading is one ``uci -q show system`` as the login user. Every write compares +first and sends nothing when the router already has the values -- an application +aligns on every poll -- else sets the options, commits ``system`` and applies them: +``/etc/init.d/sysntpd restart`` for the servers, ``/etc/init.d/system reload`` and a +restart of logd, which reads the zone when it starts, for the zone. Writes run as +root (``run_command(privileged=True)``); OpenWrt has no sudo. + +``synchronized`` is None: BusyBox ntpd keeps no state another process can read, and +``ubus call system info`` says nothing about the clock's source. +""" + +from __future__ import annotations + +import re +import shlex +from collections import defaultdict +from dataclasses import dataclass, field +from functools import lru_cache +from importlib.resources import files +from typing import TYPE_CHECKING, Any, ClassVar + +from napalm_device_types import FixStepOutcomeDict +from napalm_device_types.posix_tz import is_iana_zone, parse_posix_tz, posix_tz +from napalm_device_types.time_sync import TIME_SYNC_VALUES, TimeSyncDict, TimeSyncMixin + +if TYPE_CHECKING: + from napalm_device_types import CommandResult + +READ_COMMAND = "uci -q show system" +#: The rule ``/etc/init.d/system`` uses where the section names none. +DEFAULT_RULE = "UTC" +#: A zone of the database that stands for "not known", not for a place. +_NO_PLACE = frozenset({"Factory"}) +#: ``system.
.