Compare commits

...
8 Commits
Author SHA1 Message Date
christianmanivong 4e79cd1fc8 Merge pull request 'fix(host-status): an Oracle Linux host's UEK kernels are not an interrupted dnf (4.36.1)' (#108) from fix/dnf-installonly-defaults into main
driver / types (push) Successful in 1m0s
driver / standard (push) Successful in 16s
driver / lint (push) Successful in 13s
driver / test (3.12) (push) Successful in 1m17s
driver / test (3.13) (push) Successful in 1m12s
driver / lowest (push) Successful in 41s
driver / build (push) Successful in 30s
driver / security (push) Successful in 1m3s
driver / publish (push) Successful in 30s
CI / driver (push) Successful in 3m21s
2026-11-12 09:37:34 +00:00
christianmanivong 304f404567 fix(host-status): an Oracle Linux host's UEK kernels are not an interrupted dnf (4.36.1)
driver / lint (pull_request) Successful in 16s
driver / standard (pull_request) Successful in 18s
driver / types (pull_request) Successful in 59s
driver / test (3.13) (pull_request) Successful in 1m12s
driver / test (3.12) (pull_request) Successful in 1m17s
driver / lowest (pull_request) Successful in 41s
driver / build (pull_request) Successful in 30s
driver / security (pull_request) Successful in 56s
CI / driver (pull_request) Successful in 2m26s
driver / publish (pull_request) Skipped
The installonly packages were dnf's upstream defaults plus dnf.conf's.
Oracle builds kernel-uek and its parts into libdnf's defaults, and the
UEK packages provide nothing of upstream's list, so every host with more
than one UEK kernel was reported as left halfway. The defaults are now
also asked of the host's libdnf through its Python binding; upstream's
list stays the floor for dnf 5 and yum.

Closes #107
2026-11-12 10:30:48 +01:00
christianmanivong aaa9f0504a Merge pull request 'feat(time-sync): DHCP-provided NTP servers count and are read apart (4.36.0)' (#105) from feat/time-sync-dhcp-ntp-2380 into main
driver / types (push) Successful in 59s
driver / standard (push) Successful in 18s
driver / lint (push) Successful in 15s
driver / test (3.12) (push) Successful in 1m16s
driver / lowest (push) Successful in 42s
driver / test (3.13) (push) Successful in 1m11s
driver / build (push) Successful in 31s
driver / security (push) Successful in 1m2s
driver / publish (push) Successful in 31s
CI / driver (push) Successful in 3m17s
2026-11-11 21:25:27 +00:00
christianmanivong bc47220318 Merge remote-tracking branch 'origin/main' into feat/time-sync-dhcp-ntp-2380
driver / lint (pull_request) Successful in 15s
driver / standard (pull_request) Successful in 19s
driver / types (pull_request) Successful in 1m34s
driver / test (3.13) (pull_request) Successful in 1m32s
driver / test (3.12) (pull_request) Successful in 1m52s
driver / lowest (pull_request) Successful in 1m3s
driver / build (pull_request) Successful in 48s
driver / security (pull_request) Successful in 1m26s
CI / driver (pull_request) Successful in 3m17s
driver / publish (pull_request) Skipped
# Conflicts:
#	CHANGELOG.md
#	pyproject.toml
2026-11-11 22:21:34 +01:00
christianmanivong 08a62838f7 Merge pull request 'fix(dhcp): one refused reservation no longer stops the apply' (#104) from fix/dhcp-reservations-partial-apply into main
driver / standard (push) Successful in 18s
driver / lint (push) Successful in 14s
driver / types (push) Successful in 1m4s
driver / test (3.13) (push) Successful in 1m9s
driver / test (3.12) (push) Successful in 1m18s
driver / lowest (push) Successful in 41s
driver / build (push) Successful in 33s
driver / security (push) Successful in 1m7s
driver / publish (push) Successful in 32s
CI / driver (push) Successful in 3m29s
2026-11-11 21:12:31 +00:00
christianmanivong ac38c68feb feat(time-sync): DHCP-provided NTP servers count and are read apart
driver / standard (pull_request) Successful in 17s
driver / lint (pull_request) Successful in 31s
driver / types (pull_request) Successful in 1m16s
driver / test (3.13) (pull_request) Successful in 1m29s
driver / test (3.12) (pull_request) Successful in 1m33s
driver / lowest (pull_request) Successful in 41s
driver / build (pull_request) Successful in 29s
driver / security (pull_request) Successful in 1m5s
CI / driver (pull_request) Successful in 3m22s
driver / publish (pull_request) Skipped
A device that gets the site's time giver through DHCP option 42 is
aligned (NetOrk/netork#2380): most clients already learn it that way.
4.35.0 switched that off on every write -- chrony's DHCP sourcedir was
commented out, and systemd-timesyncd's link servers failed the verify
step -- and folded DHCP's servers into ntp_servers, so an application
could not tell configured from DHCP-provided servers.

TimeSyncDict gains dhcp_ntp_servers (not required): the servers the
device got by DHCP and uses, in its order; [] for none, absent or None
where the driver cannot tell. ntp_servers keeps only the configured ones.

PosixTimeSyncMixin reads chrony's sources from the DHCP client's
sourcedir (CHRONY_DHCP_DIRS, /run/chrony-dhcp by default) as chronyd
reads them, and systemd-timesyncd's LinkNTPServers; ntpd and OpenNTPD
keep DHCP's servers in a configuration of their own, so there it is
unknown. A chrony write now comments out only server, pool and peer
lines and leaves every sourcedir and the DHCP client's files alone. A
DHCP sourcedir that 4.35.0 commented out behind the mark is given back
on the next write, with a restart, so hosts it aligned hear DHCP again.

Version 4.36.0.
2026-11-11 22:08:52 +01:00
christianmanivong 8516f2a1ee refactor(dhcp): one reservation's write in a helper of its own
driver / standard (pull_request) Successful in 17s
driver / lint (pull_request) Successful in 15s
driver / types (pull_request) Successful in 1m6s
driver / test (3.12) (pull_request) Successful in 1m19s
driver / test (3.13) (pull_request) Successful in 1m10s
driver / lowest (pull_request) Successful in 42s
driver / build (pull_request) Successful in 37s
driver / security (pull_request) Successful in 1m9s
CI / driver (pull_request) Successful in 3m0s
driver / publish (pull_request) Skipped
apply_dhcp_reservationset grew to 59 lines with the per-write handling;
the quality ratchet allows no new function that long.

Refs NetOrk/netork#2381
2026-11-11 22:08:50 +01:00
christianmanivong c74173523a fix(dhcp): one refused reservation no longer stops the apply
driver / lint (pull_request) Successful in 18s
driver / standard (pull_request) Failing after 22s
driver / types (pull_request) Successful in 1m22s
driver / test (3.12) (pull_request) Successful in 1m28s
driver / test (3.13) (pull_request) Successful in 1m26s
driver / lowest (pull_request) Successful in 38s
driver / build (pull_request) Successful in 38s
driver / security (pull_request) Successful in 1m11s
CI / driver (pull_request) Failing after 2m57s
driver / publish (pull_request) Skipped
apply_dhcp_reservationset wrote the diff one reservation at a time and
committed only after the last. A write the device refused -- on
OPNsense, an address in a subnet Kea does not manage -- ended the run
before the commit: every later reservation went unwritten, and the
earlier ones stayed saved but inactive. The next diff read those back as
configured, reported no changes and never committed them.

A refused change now gets a "[failed] <mac> -> <ip>: <reason>" line and
the others are still written. Any non-empty diff commits, also one whose
every write was refused, so writes an interrupted earlier run saved take
effect. DhcpApplyError (a RuntimeError, exported) is raised after the
commit with how many of how many changes were refused.

Refs NetOrk/netork#2381
2026-11-11 22:03:52 +01:00
14 changed files with 606 additions and 115 deletions

No files matched your search

+43
View File
@@ -7,6 +7,49 @@ recorded here; their history is the git log.
## [Unreleased]
## [4.36.1] - 2026-11-12
### Fixed
- The host status no longer reports an Oracle Linux host's UEK kernels as an interrupted
dnf transaction. The installonly packages are now also the ones the host's libdnf was
built with, read from its Python binding: Oracle adds `kernel-uek` and its parts there,
and their packages provide nothing of upstream's defaults (#107).
## [4.36.0] - 2026-11-11
### Added
- `TimeSyncDict.dhcp_ntp_servers` (not required): the NTP servers a device got by DHCP
(option 42) and uses, in its order; `[]` for none, absent or None where the driver cannot
tell. `ntp_servers` stays the servers configured on the device and no longer holds
DHCP's. A device that gets the site's time giver by DHCP is aligned, so an application
has to tell the two apart (NetOrk/netork#2380).
- `PosixTimeSyncMixin` reads them: chrony's sources from the DHCP client's `sourcedir`
(`CHRONY_DHCP_DIRS`, `/run/chrony-dhcp` by default), systemd-timesyncd's
`LinkNTPServers`. Where ntpd or OpenNTPD runs they are unknown (None).
`timesyncd_link_servers` is public.
### Changed
- `configure_time_sync` leaves DHCP-provided NTP servers alone. chrony: only `server`,
`pool` and `peer` lines are commented out; no `sourcedir` line is any more, and the DHCP
client's files are left alone. systemd-timesyncd: a link's servers stay and no longer
fail the `verify` step.
- A write gives back a DHCP client's `sourcedir` that 4.35.0 commented out
(`#napalm-time-sync: sourcedir /run/chrony-dhcp`), so a host aligned by 4.35.0 hears DHCP
again; chronyd restarts for it. Another `sourcedir` 4.35.0 took out stays out.
- `timesyncd_servers` returns the configured servers (runtime, then system) without a
link's; the fallback only while there is no server at all.
## [4.35.1] - 2026-11-11
### Fixed
- `apply_dhcp_reservationset()` no longer stops at the first reservation the device refuses.
One refused write (on OPNsense, an address in a subnet Kea does not manage) ended the run
before the commit: every later reservation went unwritten, and the earlier ones stayed
saved but inactive, where the next diff found them configured and never committed them. A
refused change now gets a `[failed] <mac> -> <ip>: <reason>` line, the others are written,
any non-empty diff commits, and `DhcpApplyError` (a `RuntimeError`, exported) is raised
after the commit with how many of how many were refused (NetOrk/netork#2381).
## [4.35.0] - 2026-11-11
### Added
+15 -9
View File
@@ -348,10 +348,13 @@ cron expressions, gives their next runs and writes them as `OnCalendar` lines.
`TimeSyncMixin` (`get_time_sync`, `configure_time_sync`) is how a device keeps its clock:
the NTP servers it is configured with, in its order, its time zone as an IANA name, whether
it is synchronised now and which client does it (`TimeSyncDict`). `get_time_sync` is
declared only; `configure_time_sync(ntp_servers=None, timezone=None)` is a fix template --
`None` leaves a value alone, the device ends up with exactly the servers given, in that
order, and a `verify` step reads it back. A zone that is not in the IANA database, a
it is synchronised now and which client does it (`TimeSyncDict`). The servers it got by
DHCP (option 42) and uses are apart, in `dhcp_ntp_servers` (`[]` for none, left out where
the driver cannot tell): DHCP counts, a device that gets the site's time giver that way is
aligned. `get_time_sync` is declared only; `configure_time_sync(ntp_servers=None,
timezone=None)` is a fix template -- `None` leaves a value alone, the device ends up
configured with exactly the servers given, in that order, its DHCP-provided servers left
alone, and a `verify` step reads it back. A zone that is not in the IANA database, a
malformed server, an empty list or a value outside the driver's `time_sync_writes` is
refused before anything is written. A driver states in `time_sync_writes` which of
`ntp_servers` and `timezone` it can write (a BMC that runs on UTC only: `ntp_servers`).
@@ -362,11 +365,14 @@ login user (`timedatectl show`, the active client: chrony's configuration and `c
sources`, `timedatectl show-timesync`, `ntp.conf`, OpenNTPD's `ntpd.conf`); the zone by
`timedatectl set-timezone`; chrony's servers in a file of their own --
`/etc/chrony/sources.d/netork.sources` where chrony reads that directory, else a `confdir`
or an included `/etc/chrony.d/netork.conf` -- with every other `server`, `pool`, `peer` and
foreign `sourcedir` line commented out behind `#napalm-time-sync: `, then `chronyc reload
sources` or a restart; systemd-timesyncd's in `/etc/systemd/timesyncd.conf.d/netork.conf`
(`NTP=` reset, `FallbackNTP=` emptied), then a restart. ntpd and OpenNTPD are read, not
written.
or an included `/etc/chrony.d/netork.conf` -- with every other `server`, `pool` and `peer`
line commented out behind `#napalm-time-sync: `, then `chronyc reload sources` or a
restart; systemd-timesyncd's in `/etc/systemd/timesyncd.conf.d/netork.conf` (`NTP=` reset,
`FallbackNTP=` emptied), then a restart. What DHCP brings is read apart and kept: chrony's
DHCP `sourcedir` (`CHRONY_DHCP_DIRS`, `/run/chrony-dhcp`) is never commented out -- one
that 4.35.0 commented out comes back on the next write -- and systemd-timesyncd's
`LinkNTPServers` stay. ntpd and OpenNTPD are read, not written; what DHCP gives them is
unknown.
### Access channels: why there is no container model here
+2 -1
View File
@@ -127,7 +127,7 @@ from napalm_device_types.container_engine import (
ContainerEngineMixin,
ContainerEngineUnavailable,
)
from napalm_device_types.dhcp import DhcpServerMixin, normalize_cidr, normalize_mac
from napalm_device_types.dhcp import DhcpApplyError, DhcpServerMixin, normalize_cidr, normalize_mac
from napalm_device_types.dns_forward import DnsForwardMixin
from napalm_device_types.firewall import FirewallDriver
from napalm_device_types.firewall_rules import FirewallRuleMixin
@@ -404,6 +404,7 @@ __all__ = [
"DhcpHaDict",
"DhcpHaPeerDict",
"DeviceTypeDriver",
"DhcpApplyError",
"DhcpServerMixin",
"FingerprintRule",
"rules_max_weight",
+62 -30
View File
@@ -10,10 +10,17 @@ The sources go to one file of their own (:func:`plan_chrony` picks it):
4. else the include file, with an ``include`` line appended to the main configuration
(RHEL's ``/etc/chrony.conf``).
Every other source stops counting: a ``server``, ``pool`` or ``peer`` line in any
other file chrony reads is commented out behind :data:`COMMENT_MARK`, and so is a
``sourcedir`` line other than the one the sources file is in -- the DHCP client's
``/run/chrony-dhcp``, whose files are rewritten on every lease and so are left alone.
Every other configured source stops counting: a ``server``, ``pool`` or ``peer``
line in any other file chrony reads is commented out behind :data:`COMMENT_MARK`.
``sourcedir`` lines stay. The DHCP client's directory (``/run/chrony-dhcp``) and its
files are left alone: DHCP option 42 counts (NetOrk/netork#2380), and the client
rewrites those files on every lease anyway.
4.35.0 commented out every ``sourcedir`` other than the sources file's, the DHCP
client's among them, which left a host deaf to DHCP. A write gives a DHCP client's
``sourcedir`` back where it finds one behind :data:`COMMENT_MARK`; any other one it
took out stays out, its sources would be commented out now anyway.
chronyd reads its main configuration only when it starts: a change there means a
restart.
"""
@@ -26,7 +33,12 @@ import shlex
from dataclasses import dataclass, field
from fnmatch import fnmatchcase
from napalm_device_types._time_sync_reading import SOURCE_LINE, ConfigFile
from napalm_device_types._time_sync_reading import (
CHRONY_DHCP_DIRS,
SOURCE_LINE,
ConfigFile,
in_dhcp_dir,
)
#: What a source line taken out of chrony's configuration starts with.
COMMENT_MARK = "#napalm-time-sync: "
@@ -34,6 +46,8 @@ COMMENT_MARK = "#napalm-time-sync: "
INCLUDE_COMMENT = "# napalm-time-sync: the time sources"
_DIRECTORY_LINE = re.compile(r"\s*(sourcedir|confdir|include)\s+(.+?)\s*$")
#: A ``sourcedir`` line 4.35.0 commented out.
_MARKED_SOURCEDIR = re.compile(rf"{re.escape(COMMENT_MARK)}sourcedir\s+(.+?)\s*$")
@dataclass
@@ -43,6 +57,7 @@ class ChronyPlan:
target: str # the file the sources go to
content: str
comment_out: dict[str, list[int]] = field(default_factory=dict) # path: line numbers
restore: dict[str, list[int]] = field(default_factory=dict) # path: marked sourcedir lines
include_in: str | None = None # the main configuration an include of target is appended to
reload: bool = False # True: ``chronyc reload sources`` takes the change
@@ -80,27 +95,27 @@ def _target(main: ConfigFile, sources_file: str, include_file: str) -> tuple[str
return include_file, main.path, False
def _dropped_sourcedirs(main: ConfigFile, kept: str | None) -> set[int]:
"""The ``sourcedir`` lines of *main* that do not name the directory *kept*."""
return {
number
for number, kind, args in _directives(main)
if kind == "sourcedir" and (kept is None or kept not in args)
}
def _lines_to_comment(f: ConfigFile, dropped: set[int]) -> list[int]:
def _dhcp_sourcedirs_to_restore(main: ConfigFile, dhcp_dirs: tuple[str, ...]) -> list[int]:
"""The lines of *main* where 4.35.0 commented out a DHCP client's ``sourcedir``."""
dhcp = {posixpath.normpath(d) for d in dhcp_dirs}
return [
number
for number, line in enumerate(f.lines, start=1)
if SOURCE_LINE.match(line) or (f.kind == "main" and number in dropped)
for number, line in enumerate(main.lines, start=1)
if (marked := _MARKED_SOURCEDIR.match(line))
and all(posixpath.normpath(arg) in dhcp for arg in marked[1].split())
]
def plan_chrony(
files: list[ConfigFile], servers: list[str], *, sources_file: str, include_file: str
files: list[ConfigFile],
servers: list[str],
*,
sources_file: str,
include_file: str,
dhcp_dirs: tuple[str, ...] = CHRONY_DHCP_DIRS,
) -> ChronyPlan:
"""Where *servers* go and what else is commented out.
"""Where *servers* go, what else is commented out, and which DHCP ``sourcedir``
(one of *dhcp_dirs*) comes back.
:raises RuntimeError: when the reading found no chrony configuration.
"""
@@ -108,14 +123,20 @@ def plan_chrony(
if main is None:
raise RuntimeError("chrony runs, but no chrony configuration was found to write to")
target, include_in, is_sources = _target(main, sources_file, include_file)
dropped = _dropped_sourcedirs(main, posixpath.dirname(target) if is_sources else None)
comment_out = _comment_out(files, target, dropped)
comment_out = _comment_out(files, target, dhcp_dirs)
restore = _dhcp_sourcedirs_to_restore(main, dhcp_dirs)
return ChronyPlan(
target=target,
content="".join(f"{source_line(s)}\n" for s in servers),
comment_out=comment_out,
restore={main.path: restore} if restore else {},
include_in=include_in,
reload=is_sources and include_in is None and _only_sources_files(files, comment_out),
reload=(
is_sources
and include_in is None
and not restore
and _only_sources_files(files, comment_out)
),
)
@@ -124,25 +145,36 @@ def _only_sources_files(files: list[ConfigFile], changed: dict[str, list[int]])
return all(f.kind == "sourcedir" for f in files if f.path in changed)
def _comment_out(files: list[ConfigFile], target: str, dropped: set[int]) -> dict[str, list[int]]:
"""Per file, the lines that take a source; a dropped ``sourcedir``'s files are not read."""
def _comment_out(
files: list[ConfigFile], target: str, dhcp_dirs: tuple[str, ...]
) -> dict[str, list[int]]:
"""Per file, the lines that take a source; the DHCP client's files are left alone."""
found: dict[str, list[int]] = {}
for f in files:
if f.path == target or (f.kind == "sourcedir" and f.at in dropped):
if f.path == target or in_dhcp_dir(f, dhcp_dirs):
continue
numbers = _lines_to_comment(f, dropped)
numbers = [n for n, line in enumerate(f.lines, start=1) if SOURCE_LINE.match(line)]
if numbers:
found[f.path] = numbers
return found
def comment_out_command(path: str, numbers: list[int]) -> str:
"""``sed`` that comments out those lines of *path* that still hold a source or a sourcedir."""
"""``sed`` that comments out those lines of *path* that still hold a source."""
edits = " ".join(
"-e "
+ shlex.quote(
f"{n}{{/^[[:space:]]*(server|pool|peer|sourcedir)[[:space:]]/s/^/{COMMENT_MARK}/;}}"
)
+ shlex.quote(f"{n}{{/^[[:space:]]*(server|pool|peer)[[:space:]]/s/^/{COMMENT_MARK}/;}}")
for n in numbers
)
return f"sed -i -E {edits} {shlex.quote(path)}"
def restore_command(path: str, numbers: list[int]) -> str:
"""``sed`` that takes :data:`COMMENT_MARK` off those lines of *path* that are still a
``sourcedir`` behind it. The mark holds nothing a regular expression or ``sed``'s
``s///`` would read as special."""
edits = " ".join(
"-e " + shlex.quote(f"{n}{{s/^{COMMENT_MARK}(sourcedir[[:space:]])/\\1/;}}")
for n in numbers
)
return f"sed -i -E {edits} {shlex.quote(path)}"
+85 -17
View File
@@ -14,10 +14,15 @@ One command, as the login user, prints framed sections (``@@ts <name> ...``):
line of it reads;
* ``ntpd <path>``, ``openntpd <path>`` -- their configuration;
* ``end`` -- the reading is complete.
What DHCP brought is read apart from the configured servers: chrony's files in a
DHCP client's ``sourcedir`` (:data:`CHRONY_DHCP_DIRS`), systemd-timesyncd's
``LinkNTPServers``.
"""
from __future__ import annotations
import posixpath
import re
import shlex
from dataclasses import dataclass, field
@@ -37,6 +42,11 @@ TIME_SYNC_UNITS: tuple[tuple[str, str], ...] = (
("openntpd", "openntpd"),
)
#: The ``sourcedir`` a DHCP client writes chrony's sources to, one file per interface
#: and lease: the NetworkManager dispatcher and dhclient hook of Debian, Ubuntu and
#: Red Hat. What chrony reads from there is DHCP's (option 42), not configuration.
CHRONY_DHCP_DIRS: tuple[str, ...] = ("/run/chrony-dhcp", "/var/run/chrony-dhcp")
#: A source line of chrony's or ntpd's configuration: the directive and what follows.
SOURCE_LINE = re.compile(r"\s*(server|pool|peer)\s+(.*)")
_OPENNTPD_SOURCE = re.compile(r"\s*(servers?)\s+(\S+)")
@@ -197,19 +207,47 @@ def _source_name(rest: str) -> str | None:
return next((word for word in rest.split() if not word.startswith("-")), None)
def chrony_servers(files: list[ConfigFile]) -> list[str]:
"""chrony's sources in the order it reads them: each file in place of the line naming it."""
def in_dhcp_dir(f: ConfigFile, dhcp_dirs: tuple[str, ...]) -> bool:
"""Whether *f* is a sources file a DHCP client wrote to one of *dhcp_dirs*."""
if f.kind != "sourcedir":
return False
directory = posixpath.normpath(posixpath.dirname(f.path))
return directory in {posixpath.normpath(d) for d in dhcp_dirs}
def _named_on(files: list[ConfigFile], number: int) -> list[ConfigFile]:
"""The files the main configuration's line *number* reads."""
return [f for f in files if f.at == number and f.kind != "main"]
def _read_order(files: list[ConfigFile], dhcp_dirs: tuple[str, ...]) -> list[tuple[bool, str]]:
"""Every line chrony reads, in its order -- each file in place of the line naming
it -- and whether a DHCP client wrote it."""
main = next((f for f in files if f.kind == "main"), None)
if main is None:
return []
found: list[str] = []
found: list[tuple[bool, str]] = []
for number, line in enumerate(main.lines, start=1):
found.extend(_sources([line]))
for included in (f for f in files if f.at == number and f.kind != "main"):
found.extend(_sources(included.lines))
found.append((False, line))
for included in _named_on(files, number):
dhcp = in_dhcp_dir(included, dhcp_dirs)
found.extend((dhcp, text) for text in included.lines)
return found
def chrony_servers(files: list[ConfigFile], dhcp_dirs: tuple[str, ...] = ()) -> list[str]:
"""chrony's configured sources in the order it reads them; not those in *dhcp_dirs*."""
return _sources([line for dhcp, line in _read_order(files, dhcp_dirs) if not dhcp])
def chrony_dhcp_servers(files: list[ConfigFile], dhcp_dirs: tuple[str, ...]) -> list[str]:
"""The sources chrony reads from the DHCP client's files in *dhcp_dirs*, in its order.
A ``sourcedir`` chrony does not read (commented out) prints no files: nothing then.
"""
return _sources([line for dhcp, line in _read_order(files, dhcp_dirs) if dhcp])
def _sources(lines: list[str]) -> list[str]:
names = []
for line in lines:
@@ -229,14 +267,30 @@ def openntpd_servers(lines: list[str]) -> list[str]:
return [m[2] for line in lines if (m := _OPENNTPD_SOURCE.match(line))]
def timesyncd_servers(values: dict[str, str]) -> list[str]:
"""systemd-timesyncd's servers in the order it tries them; the fallback only alone."""
def _listed(values: dict[str, str], *keys: str) -> list[str]:
found: list[str] = []
for key in ("RuntimeNTPServers", "SystemNTPServers", "LinkNTPServers"):
for key in keys:
for server in values.get(key, "").split():
if server not in found:
found.append(server)
return found or values.get("FallbackNTPServers", "").split()
return found
def timesyncd_servers(values: dict[str, str]) -> list[str]:
"""systemd-timesyncd's configured servers, runtime then system; the fallback only
while it has no other server, a link's included. A link's servers are not among
them: :func:`timesyncd_link_servers`."""
configured = _listed(values, "RuntimeNTPServers", "SystemNTPServers")
if configured or timesyncd_link_servers(values):
return configured
return _listed(values, "FallbackNTPServers")
def timesyncd_link_servers(values: dict[str, str]) -> list[str]:
"""The servers network links gave systemd-timesyncd: systemd-networkd's, from a DHCP
lease (option 42) or a ``.network`` file's ``NTP=`` -- the reading cannot tell those
apart and counts them as DHCP's."""
return _listed(values, "LinkNTPServers")
def chrony_synchronized(sources: str) -> bool | None:
@@ -263,9 +317,9 @@ def _time_zone(reading: Reading) -> str | None:
return next((z for line in reading.zone_files if (z := _zone_name(line))), None)
def _servers(reading: Reading) -> list[str]:
def _servers(reading: Reading, dhcp_dirs: tuple[str, ...]) -> list[str]:
if reading.daemon == "chrony":
return chrony_servers(reading.chrony)
return chrony_servers(reading.chrony, dhcp_dirs)
if reading.daemon == "systemd-timesyncd":
return timesyncd_servers(reading.timesync)
if reading.daemon == "ntpd" and reading.ntpd is not None:
@@ -275,6 +329,18 @@ def _servers(reading: Reading) -> list[str]:
return []
def _dhcp_servers(reading: Reading, dhcp_dirs: tuple[str, ...]) -> list[str] | None:
"""What DHCP brought: chrony's and timesyncd's; None where ntpd or OpenNTPD runs,
whose DHCP hooks keep a configuration of their own that is not read."""
if reading.daemon == "chrony":
return chrony_dhcp_servers(reading.chrony, dhcp_dirs)
if reading.daemon == "systemd-timesyncd":
return timesyncd_link_servers(reading.timesync)
if reading.daemon is None:
return []
return None
def _synchronized(reading: Reading) -> bool | None:
if reading.daemon == "chrony":
chrony = chrony_synchronized(reading.chronyc)
@@ -283,19 +349,21 @@ def _synchronized(reading: Reading) -> bool | None:
return {"yes": True, "no": False}.get(reading.timedatectl.get("NTPSynchronized", ""))
def time_sync_of(reading: Reading) -> TimeSyncDict:
"""What a reading says, as ``get_time_sync`` returns it."""
def time_sync_of(reading: Reading, dhcp_dirs: tuple[str, ...] = CHRONY_DHCP_DIRS) -> TimeSyncDict:
"""What a reading says, as ``get_time_sync`` returns it; *dhcp_dirs* are the DHCP
client's ``sourcedir`` of chrony."""
return {
"ntp_servers": _servers(reading),
"ntp_servers": _servers(reading, dhcp_dirs),
"timezone": _time_zone(reading),
"synchronized": _synchronized(reading),
"daemon": reading.daemon,
"dhcp_ntp_servers": _dhcp_servers(reading, dhcp_dirs),
}
def parse_time_sync(text: str) -> TimeSyncDict:
def parse_time_sync(text: str, dhcp_dirs: tuple[str, ...] = CHRONY_DHCP_DIRS) -> TimeSyncDict:
"""The reading's output as ``get_time_sync`` returns it.
:raises ValueError: for a reading cut short.
"""
return time_sync_of(parse_reading(text))
return time_sync_of(parse_reading(text), dhcp_dirs)
+55 -16
View File
@@ -58,6 +58,19 @@ _DHCP_SUBNET_COMPARE_FIELDS = (
_HEX_ONLY = re.compile(r"[^0-9a-f]")
class DhcpApplyError(RuntimeError):
"""Some reservations of an applied set were refused by the device.
Raised after the rest were written and committed: ``failed`` of ``attempted``
changes did not go through, and each was named in a ``[failed]`` progress line.
"""
def __init__(self, failed: int, attempted: int) -> None:
self.failed = failed
self.attempted = attempted
super().__init__(f"{failed} of {attempted} DHCP reservation change(s) were refused")
def normalize_mac(mac: str | None) -> str:
"""Reduces a MAC address to lowercase colon-separated form.
@@ -298,29 +311,55 @@ class DhcpServerMixin:
committing reloads the DHCP daemon and drops in-flight requests, which
is too high a price for a no-op run.
A change the device refuses (say, an address in a subnet its DHCP server
does not manage) does not stop the others: it gets a ``[failed]`` line,
the rest are written and committed, and ``DhcpApplyError`` is raised at
the end. Any non-empty diff commits, even one whose every write was
refused, so that writes an earlier, interrupted run saved take effect.
:param desired: The complete desired reservation set.
:yields: Progress lines, one per applied add/update, plus a final
commit line.
:yields: Progress lines, one per applied, updated or refused change,
plus a final commit line.
:raises DhcpApplyError: after the commit, when a change was refused.
"""
diff = self.diff_dhcp_reservations(desired)
for reservation in diff["add"]:
self.apply_dhcp_reservation(reservation)
yield f"[add] {reservation['mac']} -> {reservation['ip']}"
for entry in diff["update"]:
self.apply_dhcp_reservation(entry["reservation"], uuid=entry["uuid"])
fields = ", ".join(entry["changed_fields"])
yield (
f"[update] {entry['reservation']['mac']} -> {entry['reservation']['ip']} ({fields})"
)
if not diff["add"] and not diff["update"]:
attempted = len(diff["add"]) + len(diff["update"])
if not attempted:
yield "[commit] no changes"
return
added = updated = refused = 0
for reservation in diff["add"]:
line, ok = self._write_reservation(reservation, None, "[add]")
added, refused = added + ok, refused + (not ok)
yield line
for entry in diff["update"]:
fields = ", ".join(entry["changed_fields"])
line, ok = self._write_reservation(entry["reservation"], entry["uuid"], "[update]")
updated, refused = updated + ok, refused + (not ok)
yield f"{line} ({fields})" if ok else line
# Committed even when every write was refused: a run that stopped before its
# commit left saved writes behind, which the device reports as configured, so
# no later diff would ever commit them.
self.commit_dhcp_reservations()
yield (f"[commit] applied {len(diff['add'])} add(s), {len(diff['update'])} update(s)")
yield f"[commit] applied {added} add(s), {updated} update(s)"
if refused:
raise DhcpApplyError(refused, attempted)
def _write_reservation(
self, reservation: DhcpReservationDict, uuid: str | None, tag: str
) -> tuple[str, bool]:
"""Write one reservation; its progress line, and whether the device took it."""
target = f"{reservation['mac']} -> {reservation['ip']}"
try:
self.apply_dhcp_reservation(reservation, uuid=uuid)
except Exception as exc:
return f"[failed] {target}: {exc}", False
return f"{tag} {target}", True
def diff_dhcp_subnets(self, desired: list[DhcpSubnetDict]) -> DhcpSubnetDiffDict:
"""
+19 -6
View File
@@ -13,9 +13,13 @@ what ``dnf check --duplicates`` and ``package-cleanup --dupes`` report. Packages
dnf installs side by side on purpose do not count: its default ``installonlypkgs``
(``kernel``, ``installonlypkg(kernel)`` and the rest), plus any the host adds in
``dnf.conf``, asked of rpm by what they provide; and ``gpg-pubkey``, one entry
per imported key. Also interrupted is a transaction that never ended: dnf 4's
history (libdnf's ``trans`` table) holds its row with ``state`` 0 until
``finish()`` writes 1 (done) or 2 (error), so the newest row at 0 is one cut off;
per imported key. The defaults are the ones the host's libdnf was built with, asked
of its Python binding, as a distribution may add its own: Oracle Linux adds
``kernel-uek`` and its parts, whose packages provide nothing of upstream's list.
Upstream's list is the floor where libdnf cannot be asked (dnf 5, yum). Also
interrupted is a transaction that never ended: dnf 4's history (libdnf's ``trans``
table) holds its row with ``state`` 0 until ``finish()`` writes 1 (done) or 2
(error), so the newest row at 0 is one cut off;
yum leaves a ``transaction-all.*`` journal in ``/var/lib/yum``, which
``yum-complete-transaction`` reads (one renamed ``.disabled`` is given up). The
history is read with Python's sqlite3 module in read-only mode, as a stock EL host
@@ -81,6 +85,13 @@ _DNF_HISTORY_PY = (
"r = c.execute('SELECT id, state FROM trans ORDER BY id DESC LIMIT 1').fetchone(); "
"r and print(*r)"
)
#: The ``installonlypkgs`` the host's libdnf (dnf 4) was built with, space-separated;
#: nothing where there is no libdnf. ``ConfigMain`` is held in a name: an option
#: taken from a temporary one outlives it, and reading it then crashes Python.
_LIBDNF_INSTALLONLY_PY = (
"import libdnf.conf; c = libdnf.conf.ConfigMain(); "
"print(c.installonlypkgs().getValueString().replace(',', ' '))"
)
_RUNNING_PART = (
"echo '[pm-running]'; cat /proc/[0-9]*/comm 2>/dev/null | grep -xE '"
@@ -90,7 +101,10 @@ _RUNNING_PART = (
_RPM_PART = (
"if command -v rpm >/dev/null 2>&1 && { command -v dnf || command -v yum; } >/dev/null 2>&1; "
"then echo '[rpm-frontend]'; { command -v dnf || command -v yum; } | sed 's#.*/##'; "
"rpmpy=; for p in /usr/libexec/platform-python python3; do "
"if command -v $p >/dev/null 2>&1; then rpmpy=$p; break; fi; done; "
f"echo '[rpm-installonly]'; io=\"{' '.join(_INSTALLONLY)} "
f'$([ -n "$rpmpy" ] && $rpmpy -c "{_LIBDNF_INSTALLONLY_PY}" 2>/dev/null) '
f"$(sed -n 's/^[[:space:]]*installonlypkgs[[:space:]]*=//p' {_DNF_CONF} 2>/dev/null "
"| tr ',' ' ')\"; rpm -q --whatprovides $io --qf '%{NAME}\\n' 2>/dev/null "
"| grep -v ' ' | sort -u; "
@@ -98,9 +112,8 @@ _RPM_PART = (
"| grep -v '^gpg-pubkey ' | sort | uniq -d | head -n 50; "
f"echo '[yum-journal]'; ls -1 {YUM_DIR} 2>/dev/null | grep '^transaction-all\\.' "
"| head -n 20; "
"echo '[dnf-history]'; for py in /usr/libexec/platform-python python3; do "
f'if command -v $py >/dev/null 2>&1; then $py -c "{_DNF_HISTORY_PY}" 2>/dev/null; '
"break; fi; done; fi; "
"echo '[dnf-history]'; "
f'[ -n "$rpmpy" ] && $rpmpy -c "{_DNF_HISTORY_PY}" 2>/dev/null; fi; '
)
_APK_PART = (
f"if command -v apk >/dev/null 2>&1 && [ -r {APK_INSTALLED} ]; then echo '[apk-audit]'; "
+25 -8
View File
@@ -7,7 +7,7 @@ device that differs. :class:`TimeSyncMixin` is the contract for that:
"get_time_sync", None))`` is the truthful answer to "can this driver tell";
* ``configure_time_sync(ntp_servers=None, timezone=None)`` -- a fix template
(:mod:`napalm_device_types.fixes`): ``None`` leaves that value as it is, and the
device ends up with exactly the servers given, in their order. A driver
device ends up configured with exactly the servers given, in their order. A driver
implements the steps; one whose device takes the change in a single call
overrides the method;
* ``time_sync_writes`` -- which of the two values the driver can write. A BMC that
@@ -17,16 +17,24 @@ device that differs. :class:`TimeSyncMixin` is the contract for that:
A driver that inherits the mixin but cannot run it sets ``get_time_sync`` (and
``configure_time_sync``) to ``None``.
**Servers from DHCP count** (NetOrk/netork#2380). A device that takes its time
servers from a DHCP lease (option 42) gets them from whoever serves the subnet --
usually the site's time giver. ``ntp_servers`` are only the servers configured on
the device; ``dhcp_ntp_servers`` are those the device got by DHCP and uses. A write
replaces the configured servers and leaves DHCP's alone: it never switches the use of
DHCP-provided servers off, and the ``verify`` step does not count them.
A zone is an IANA name on the wire. A device that wants a POSIX rule or an offset
with a summer-time rule gets it from :mod:`napalm_device_types.posix_tz`.
"""
from __future__ import annotations
# No ``from __future__ import annotations`` here: TypedDict tells a NotRequired key
# only from an evaluated annotation (``TimeSyncDict.__optional_keys__``).
import ipaddress
import re
from collections.abc import Sequence
from typing import TYPE_CHECKING, ClassVar, TypedDict
from typing import TYPE_CHECKING, ClassVar, NotRequired, TypedDict
from napalm_device_types.fixes import FixStep, run_fix_steps
from napalm_device_types.models import FixResultDict, FixStepOutcomeDict
@@ -48,6 +56,9 @@ class TimeSyncDict(TypedDict):
timezone: str | None # IANA name ("Europe/Berlin"); None = not readable or no named zone
synchronized: bool | None # the clock is synchronised to a source right now; None = unknown
daemon: str | None # "chrony", "systemd-timesyncd", "ntpd", "openntpd", ...; None = own client
# The servers the device got by DHCP (option 42) and uses, in its order; never in
# ntp_servers. [] = none; absent or None = the driver cannot tell.
dhcp_ntp_servers: NotRequired[list[str] | None]
def _is_server(name: str) -> bool:
@@ -94,7 +105,9 @@ class TimeSyncMixin:
"""How the device keeps its clock now.
``ntp_servers`` are the sources its configuration names, in its order
(a pool by its name, not the addresses it resolved to). ``timezone``
(a pool by its name, not the addresses it resolved to);
``dhcp_ntp_servers`` those a DHCP lease gave it and it uses, apart from
them -- left out where the driver cannot tell. ``timezone``
is an IANA name, or None where the device keeps an offset and a rule
instead of a name. ``daemon`` names the client that keeps the clock on
a host that has a choice; None for a device's own.
@@ -106,6 +119,7 @@ class TimeSyncMixin:
"timezone": "Europe/Berlin",
"synchronized": True,
"daemon": "chrony",
"dhcp_ntp_servers": ["192.168.1.1"],
}
:raises Exception: when the device could not be read.
@@ -115,8 +129,9 @@ class TimeSyncMixin:
# The steps a driver implements:
def _write_ntp_servers(self, servers: list[str]) -> FixStepOutcomeDict:
"""Leave the device with exactly *servers*, in this order: sources it
had configured elsewhere stop counting (commented out, replaced)."""
"""Leave the device configured with exactly *servers*, in this order:
sources it had configured elsewhere stop counting (commented out,
replaced). Servers it gets by DHCP stay, and so does its use of them."""
...
def _write_time_zone(self, zone: str) -> FixStepOutcomeDict:
@@ -128,6 +143,8 @@ class TimeSyncMixin:
) -> FixResultDict:
"""Give the device exactly *ntp_servers*, in order, and *timezone*; None leaves one alone.
What the device gets by DHCP is not touched: it keeps using those servers too.
The steps: ``ntp_servers``, ``timezone``, then ``verify``, which reads the
device again.
@@ -157,8 +174,8 @@ class TimeSyncMixin:
def _verify_time_sync(self, servers: list[str] | None, zone: str | None) -> FixStepOutcomeDict:
"""Read the device again: does it now have what was written?
Servers compare without case. A zone is compared where the device reads
one back by name.
Servers compare without case; the servers DHCP brought are not among
them. A zone is compared where the device reads one back by name.
"""
now = self.get_time_sync()
wrong: list[str] = []
+22 -6
View File
@@ -13,17 +13,22 @@ reads them, ``timedatectl show-timesync`` for systemd-timesyncd, ``ntp.conf``,
OpenNTPD's ``ntpd.conf``. Synchronised is chronyd's own word where it runs (a
selected source in ``chronyc -n sources``), else the kernel's (``NTPSynchronized``).
What DHCP brought (option 42) is read apart, as ``dhcp_ntp_servers``, and never as a
configured server: chrony's sources from the DHCP client's ``sourcedir``
(:attr:`~PosixTimeSyncMixin.CHRONY_DHCP_DIRS`, the files as chronyd reads them),
systemd-timesyncd's ``LinkNTPServers``. Where ntpd or OpenNTPD runs it is unknown.
**Writing** runs as root through ``run_command(privileged=True)``; root never reads
stdin -- a file is staged by the login user and installed by root.
* The zone: ``timedatectl set-timezone``.
* chrony: the servers go to a file of their own and every other source is commented
out (:mod:`napalm_device_types._chrony`), then ``chronyc reload sources``, or a
restart of chronyd where its main configuration changed or it cannot reload.
* chrony: the servers go to a file of their own and every other configured source is
commented out (:mod:`napalm_device_types._chrony`); the DHCP client's ``sourcedir``
stays, and one 4.35.0 commented out comes back. Then ``chronyc reload sources``, or
a restart of chronyd where its main configuration changed or it cannot reload.
* systemd-timesyncd: :attr:`~PosixTimeSyncMixin.TIMESYNCD_DROPIN` resets ``NTP=``,
sets the servers and empties ``FallbackNTP=``, then the service restarts. Servers a
network link brings (DHCP through systemd-networkd) are not timesyncd's to drop;
they stay, and the ``verify`` step names them.
network link brings (DHCP through systemd-networkd) stay and count.
* ntpd, OpenNTPD, or no client: the servers are read, not written -- the step fails
and says so.
"""
@@ -39,9 +44,11 @@ from napalm_device_types._chrony import (
comment_out_command,
include_command,
plan_chrony,
restore_command,
)
from napalm_device_types._scheduled_reading import TIME_ZONE_SH
from napalm_device_types._time_sync_reading import (
CHRONY_DHCP_DIRS,
TIME_SYNC_UNITS,
Reading,
ReadPaths,
@@ -51,6 +58,7 @@ from napalm_device_types._time_sync_reading import (
parse_time_sync,
reading_command,
time_sync_of,
timesyncd_link_servers,
timesyncd_servers,
)
from napalm_device_types.models import FixStepOutcomeDict
@@ -91,6 +99,9 @@ class PosixTimeSyncMixin(TimeSyncMixin):
CHRONY_SOURCES_FILE: ClassVar[str] = "/etc/chrony/sources.d/netork.sources"
#: The servers' file otherwise (its name also in a ``confdir``); included if need be.
CHRONY_INCLUDE_FILE: ClassVar[str] = "/etc/chrony.d/netork.conf"
#: The ``sourcedir`` a DHCP client writes chrony's sources to: read as
#: ``dhcp_ntp_servers``, never commented out.
CHRONY_DHCP_DIRS: ClassVar[tuple[str, ...]] = CHRONY_DHCP_DIRS
TIMESYNCD_DROPIN: ClassVar[str] = "/etc/systemd/timesyncd.conf.d/netork.conf"
NTPD_CONFIGS: ClassVar[tuple[str, ...]] = ("/etc/ntpsec/ntp.conf", "/etc/ntp.conf")
OPENNTPD_CONFIGS: ClassVar[tuple[str, ...]] = ("/etc/openntpd/ntpd.conf", "/etc/ntpd.conf")
@@ -125,7 +136,7 @@ class PosixTimeSyncMixin(TimeSyncMixin):
:raises ValueError: when the reading came back cut short.
"""
return time_sync_of(self._ts_read())
return time_sync_of(self._ts_read(), self.CHRONY_DHCP_DIRS)
# -- writing ---------------------------------------------------------------------------------
@@ -175,9 +186,13 @@ class PosixTimeSyncMixin(TimeSyncMixin):
servers,
sources_file=self.CHRONY_SOURCES_FILE,
include_file=self.CHRONY_INCLUDE_FILE,
dhcp_dirs=self.CHRONY_DHCP_DIRS,
)
said = [f"wrote {plan.target}"]
self._ts_install(plan.target, plan.content)
for path, numbers in plan.restore.items():
self._ts_checked(restore_command(path, numbers))
said.append(f"gave DHCP's sourcedir back in {path}:{','.join(map(str, numbers))}")
for path, numbers in plan.comment_out.items():
self._ts_checked(comment_out_command(path, numbers))
said.append(f"commented out {path}:{','.join(map(str, numbers))}")
@@ -199,5 +214,6 @@ __all__ = [
"parse_key_values",
"parse_time_sync",
"timesyncd_dropin",
"timesyncd_link_servers",
"timesyncd_servers",
]
+1 -1
View File
@@ -4,7 +4,7 @@ build-backend = "setuptools.build_meta"
[project]
name = "napalm-device-types"
version = "4.35.0"
version = "4.36.1"
description = "Abstract device-type base classes for NAPALM drivers"
readme = "README.md"
requires-python = ">=3.12"
+90 -1
View File
@@ -14,7 +14,7 @@ from typing import Any
import pytest
from napalm_device_types import DhcpServerMixin, FirewallDriver, ResidentialGatewayDriver
from napalm_device_types.dhcp import normalize_mac
from napalm_device_types.dhcp import DhcpApplyError, normalize_mac
from napalm_device_types.models import DhcpReservationDict
@@ -209,6 +209,95 @@ class TestApplyReservationSet:
assert lines == ["[commit] no changes"]
class _RefusingDhcpServer(_FakeDhcpServer):
"""A server that refuses the reservations for the IPs in *refused*, as Kea does
for one whose subnet it does not manage."""
def __init__(self, live: list[DhcpReservationDict] | None = None, *, refused: set[str]) -> None:
super().__init__(live)
self.refused = refused
def apply_dhcp_reservation(
self, reservation: DhcpReservationDict, *, uuid: str | None = None
) -> dict[str, Any]:
if reservation["ip"] in self.refused:
raise ValueError(f"No Kea-managed subnet contains {reservation['ip']}")
return super().apply_dhcp_reservation(reservation, uuid=uuid)
def _three() -> list[DhcpReservationDict]:
return [
_reservation(mac="aa:bb:cc:dd:ee:01", ip="10.10.20.50"),
_reservation(mac="aa:bb:cc:dd:ee:02", ip="10.10.99.5", subnet=""),
_reservation(mac="aa:bb:cc:dd:ee:03", ip="10.10.20.52"),
]
class TestApplyWithARefusedReservation:
"""One reservation the server refuses must not hold back the others, and what was
written must take effect: a server that saved a write and was never committed shows
it as configured, so the next apply finds nothing to do and never commits it."""
def test_the_others_are_still_written(self):
driver = _RefusingDhcpServer(refused={"10.10.99.5"})
with pytest.raises(DhcpApplyError):
list(driver.apply_dhcp_reservationset(_three()))
assert [entry["reservation"]["ip"] for entry in driver.applied] == [
"10.10.20.50",
"10.10.20.52",
]
def test_what_was_written_is_committed(self):
driver = _RefusingDhcpServer(refused={"10.10.99.5"})
with pytest.raises(DhcpApplyError):
list(driver.apply_dhcp_reservationset(_three()))
assert driver.commits == 1
def test_a_refused_set_still_commits(self):
# An earlier apply that stopped before its commit left saved writes behind; a
# run whose every write is refused commits too, so those take effect.
driver = _RefusingDhcpServer(refused={"10.10.99.5"})
with pytest.raises(DhcpApplyError):
list(driver.apply_dhcp_reservationset([_three()[1]]))
assert driver.commits == 1
def test_progress_names_the_refused_one_and_why(self):
driver = _RefusingDhcpServer(refused={"10.10.99.5"})
lines: list[str] = []
with pytest.raises(DhcpApplyError) as raised:
for line in driver.apply_dhcp_reservationset(_three()):
lines.append(line)
failed = [line for line in lines if line.startswith("[failed]")]
assert failed == [
"[failed] aa:bb:cc:dd:ee:02 -> 10.10.99.5: No Kea-managed subnet contains 10.10.99.5"
]
assert lines[-1] == "[commit] applied 2 add(s), 0 update(s)"
assert "1 of 3" in str(raised.value)
def test_a_refused_update_is_reported_like_an_add(self):
driver = _RefusingDhcpServer(
[_reservation(uuid="u1", ip="10.10.20.9")], refused={"10.10.20.50"}
)
with pytest.raises(DhcpApplyError, match="1 of 1"):
list(driver.apply_dhcp_reservationset([_reservation()]))
assert driver.applied == []
assert driver.commits == 1
def test_the_error_is_a_runtime_error(self):
# Callers that catch RuntimeError for a failed apply keep working.
assert issubclass(DhcpApplyError, RuntimeError)
class TestKeysThatCannotNameOneReservation:
def test_a_live_reservation_without_mac_is_never_matched(self):
driver = _FakeDhcpServer([_reservation(uuid="b", mac="")])
+42 -2
View File
@@ -598,8 +598,8 @@ class TestInterruptedRpm:
The outputs are shaped after the documented formats -- ``rpm -qa --qf``,
libdnf's ``trans`` table (``state`` 0 = UNKNOWN until ``finish()`` sets DONE
1 or ERROR 2), yum's ``transaction-all.*`` journal that
``yum-complete-transaction`` reads -- and still have to be checked against a
real Oracle Linux 9 host.
``yum-complete-transaction`` reads. Checked against Oracle Linux 9 (build-arm-01,
2026-11-12): its UEK kernels are installonly only by libdnf's built-in defaults.
"""
INSTALLONLY = ("kernel-core", "kernel-modules", "kernel-uek-core")
@@ -804,6 +804,29 @@ case "$*" in
esac
"""
#: An rpm on an Oracle Linux host: its UEK kernel ``vendor-kernel`` provides nothing
#: of dnf's upstream defaults and sits in the database three times, like glibc twice.
_RPM_VENDOR_KERNEL_STUB = """#!/bin/sh
case "$*" in
*--whatprovides*) for a; do [ "$a" = vendor-kernel ] && echo vendor-kernel; done
echo 'no package provides kernel-PAE' ;;
-qa*) printf '%s\\n' 'vendor-kernel aarch64' 'vendor-kernel aarch64' 'vendor-kernel aarch64' \\
'glibc aarch64' 'glibc aarch64' ;;
esac
"""
#: libdnf's Python binding, as far as the command asks it: the installonlypkgs a
#: distribution built into its libdnf -- here one more than upstream's.
_LIBDNF_CONF = """class _Option:
def getValueString(self):
return "kernel, installonlypkg(kernel), vendor-kernel"
class ConfigMain:
def installonlypkgs(self):
return _Option()
"""
#: /lib/apk/db/installed shaped after apk-tools' database.c: busybox's script failed.
_APK_DB = """C:Q1abc=
P:alpine-baselayout
@@ -886,6 +909,23 @@ class TestThePackageManagerCommandOnARealShell:
assert status["package_manager"] == "dnf"
assert status["interrupted_packages"] == [{"name": "glibc", "state": "duplicate"}]
def test_the_installonly_packages_libdnf_was_built_with(self, tmp_path, shell):
"""Oracle builds kernel-uek into libdnf's defaults; its UEK packages provide
nothing of upstream's, so only libdnf itself tells (#107)."""
_skip_busybox_rpm(shell)
env = _stubs(tmp_path, "rpm", "dnf")
(tmp_path / "bin" / "rpm").write_text(_RPM_VENDOR_KERNEL_STUB)
libdnf = tmp_path / "py" / "libdnf"
libdnf.mkdir(parents=True)
(libdnf / "__init__.py").write_text("")
(libdnf / "conf.py").write_text(_LIBDNF_CONF)
env["PYTHONPATH"] = str(tmp_path / "py")
out = _run(PACKAGE_MANAGERS_COMMAND, env, shell)
status = parse_host_status(out)
assert status["interrupted_packages"] == [{"name": "glibc", "state": "duplicate"}]
def test_dnfs_last_transaction_that_never_ended(self, tmp_path, shell):
_skip_busybox_rpm(shell)
history = tmp_path / "history.sqlite"
+14
View File
@@ -34,9 +34,16 @@ def test_the_shape():
"timezone": str | None,
"synchronized": bool | None,
"daemon": str | None,
"dhcp_ntp_servers": list[str] | None,
}
def test_the_dhcp_servers_are_optional():
# A driver that cannot tell what DHCP brought leaves the key out (NetOrk/netork#2380).
assert TimeSyncDict.__required_keys__ == {"ntp_servers", "timezone", "synchronized", "daemon"}
assert TimeSyncDict.__optional_keys__ == {"dhcp_ntp_servers"}
def test_the_two_values_a_driver_may_write():
assert frozenset({"ntp_servers", "timezone"}) == TIME_SYNC_VALUES
@@ -190,6 +197,13 @@ class TestConfigureTimeSync:
assert Lower().configure_time_sync(["NTP1.Example.org"])["success"] is True
def test_servers_from_dhcp_do_not_fail_the_check(self):
# DHCP option 42 counts: what the device got by DHCP stays and is no mistake.
device = _Device()
device.state["dhcp_ntp_servers"] = ["192.168.1.1"]
result = device.configure_time_sync(["ntp1.example.org"])
assert result["success"] is True, result["output"]
def test_a_device_without_a_zone_name_is_not_checked_for_one(self):
# A switch keeps an offset and a summer-time rule, not a name: it reads None.
result = _Device(keeps_zone_name=False).configure_time_sync(timezone="Europe/Berlin")
+131 -18
View File
@@ -4,7 +4,8 @@ The reading is run by a real ``sh`` over a directory tree that stands in for the
host, with ``timedatectl``, ``systemctl`` and ``chronyc`` replaced by scripts that
keep a log -- so the shell itself is tested, not a hand-written copy of its output.
``sed`` and ``install`` are the real ones, so the files the writes leave are what a
host would get (NetOrk/netork#2361).
host would get (NetOrk/netork#2361). Servers that DHCP brings are read apart and left
alone by a write (NetOrk/netork#2380).
"""
from __future__ import annotations
@@ -20,6 +21,7 @@ from napalm_device_types.channel import CommandResult
from napalm_device_types.time_sync_posix import (
chrony_synchronized,
parse_key_values,
timesyncd_link_servers,
timesyncd_servers,
)
@@ -136,6 +138,7 @@ class Host(PosixTimeSyncMixin):
self.CHRONY_CONFIGS = (str(etc / "chrony/chrony.conf"), str(etc / "chrony.conf")) # type: ignore[misc]
self.CHRONY_SOURCES_FILE = str(etc / "chrony/sources.d/netork.sources") # type: ignore[misc]
self.CHRONY_INCLUDE_FILE = str(etc / "chrony.d/netork.conf") # type: ignore[misc]
self.CHRONY_DHCP_DIRS = (str(root / "run/chrony-dhcp"),) # type: ignore[misc]
self.TIMESYNCD_DROPIN = str(etc / "systemd/timesyncd.conf.d/netork.conf") # type: ignore[misc]
self.NTPD_CONFIGS = (str(etc / "ntpsec/ntp.conf"), str(etc / "ntp.conf")) # type: ignore[misc]
self.OPENNTPD_CONFIGS = (str(etc / "openntpd/ntpd.conf"),) # type: ignore[misc]
@@ -223,21 +226,43 @@ class TestParsing:
def test_timesyncd_uses_the_fallback_only_when_nothing_else_is_known(self):
assert timesyncd_servers(parse_key_values(TIMESYNC_FALLBACK)) == ["ntp.ubuntu.com"]
# runtime, then system, then link: the order timesyncd tries them in.
# runtime, then system: the configured ones; a link's servers are DHCP's.
assert timesyncd_servers(parse_key_values(TIMESYNC_CONFIGURED)) == [
"ntp1.example.org",
"ntp2.example.org",
"192.168.1.1",
]
assert timesyncd_servers({}) == []
def test_timesyncd_does_not_fall_back_while_a_link_brings_servers(self):
values = {"LinkNTPServers": "192.168.1.1", "FallbackNTPServers": "ntp.ubuntu.com"}
assert timesyncd_servers(values) == []
assert timesyncd_link_servers(values) == ["192.168.1.1"]
def test_timesyncd_link_servers(self):
assert timesyncd_link_servers(parse_key_values(TIMESYNC_CONFIGURED)) == ["192.168.1.1"]
assert timesyncd_link_servers({"LinkNTPServers": "10.0.0.1 10.0.0.1 10.0.0.2"}) == [
"10.0.0.1",
"10.0.0.2",
]
assert timesyncd_link_servers(parse_key_values(TIMESYNC_FALLBACK)) == []
def test_chrony_is_synchronised_while_it_has_selected_a_source(self):
assert chrony_synchronized(CHRONYC_SYNCED) is True
assert chrony_synchronized(CHRONYC_UNSYNCED) is False
assert chrony_synchronized("") is None
def test_chrony_without_a_main_configuration_has_no_sources(self):
assert parse_time_sync("@@ts active\nchrony chrony active\n@@ts end\n")["ntp_servers"] == []
reading = parse_time_sync("@@ts active\nchrony chrony active\n@@ts end\n")
assert (reading["ntp_servers"], reading["dhcp_ntp_servers"]) == ([], [])
def test_the_dhcp_directory_is_debian_s_and_red_hat_s_by_default(self):
reading = parse_time_sync(
"@@ts active\nchrony chrony active\n"
"@@ts chrony 0 main /etc/chrony/chrony.conf\nsourcedir /run/chrony-dhcp\n"
"@@ts chrony 1 sourcedir /run/chrony-dhcp/eth0.sources\nserver 10.0.0.1 iburst\n"
"@@ts end\n"
)
assert (reading["ntp_servers"], reading["dhcp_ntp_servers"]) == ([], ["10.0.0.1"])
def test_a_reading_cut_short_raises(self):
with pytest.raises(ValueError, match="cut short"):
@@ -251,14 +276,35 @@ class TestReading:
"ntp_servers": [
"ntp-conf.example.org",
"2.debian.pool.ntp.org",
"192.168.1.1",
"ntp-other.example.org",
],
"timezone": "Europe/Berlin",
"synchronized": True,
"daemon": "chrony",
"dhcp_ntp_servers": ["192.168.1.1"],
}
def test_dhcp_servers_in_the_order_chrony_reads_them(self, debian: Path):
_write(debian / "run/chrony-dhcp/wlan0.sources", "server 10.9.0.1 iburst\n")
_write(debian / "run/chrony-dhcp/eth0.sources", "server 192.168.1.1\npool p.example\n")
assert Host(debian).get_time_sync()["dhcp_ntp_servers"] == [
"192.168.1.1",
"p.example",
"10.9.0.1",
]
def test_a_dhcp_directory_chrony_does_not_read_brings_nothing(self, debian: Path):
# What 4.35.0 left behind: DHCP's sourcedir commented out.
conf = debian / "etc/chrony/chrony.conf"
conf.write_text(_text(conf).replace("\nsourcedir ", "\n#napalm-time-sync: sourcedir ", 1))
reading = Host(debian).get_time_sync()
assert reading["dhcp_ntp_servers"] == []
assert "192.168.1.1" not in reading["ntp_servers"]
def test_without_a_dhcp_directory_dhcp_brings_nothing(self, root: Path):
_write(root / "etc/chrony.conf", "pool 2.rhel.pool.ntp.org iburst\n")
assert Host(root, active="chronyd").get_time_sync()["dhcp_ntp_servers"] == []
def test_the_reading_is_one_round_trip_without_root(self, debian: Path):
host = Host(debian)
host.get_time_sync()
@@ -291,7 +337,8 @@ class TestReading:
(root / "fix/timesync").write_text(TIMESYNC_CONFIGURED)
reading = Host(root, active="systemd-timesyncd").get_time_sync()
assert reading["daemon"] == "systemd-timesyncd"
assert reading["ntp_servers"] == ["ntp1.example.org", "ntp2.example.org", "192.168.1.1"]
assert reading["ntp_servers"] == ["ntp1.example.org", "ntp2.example.org"]
assert reading["dhcp_ntp_servers"] == ["192.168.1.1"]
assert reading["synchronized"] is True
def test_ntpd(self, root: Path):
@@ -303,6 +350,8 @@ class TestReading:
reading = Host(root, active="ntpsec").get_time_sync()
assert reading["daemon"] == "ntpd"
assert reading["ntp_servers"] == ["0.debian.pool.ntp.org", "ntp1.example.org"]
# Debian's DHCP hook gives ntpd a configuration of its own: not read, so unknown.
assert reading["dhcp_ntp_servers"] is None
def test_openntpd(self, root: Path):
_write(
@@ -312,6 +361,7 @@ class TestReading:
reading = Host(root, active="openntpd").get_time_sync()
assert reading["daemon"] == "openntpd"
assert reading["ntp_servers"] == ["pool.ntp.org", "ntp1.example.org"]
assert reading["dhcp_ntp_servers"] is None
def test_no_client_runs(self, root: Path):
(root / "fix/timedatectl").write_text(
@@ -322,6 +372,7 @@ class TestReading:
"timezone": "Europe/Berlin",
"synchronized": False,
"daemon": None,
"dhcp_ntp_servers": [],
}
def test_without_timedatectl_the_zone_comes_from_the_files(self, root: Path):
@@ -352,7 +403,8 @@ class TestChronyServers:
)
conf = _text(debian / "etc/chrony/chrony.conf")
assert "\n#napalm-time-sync: pool 2.debian.pool.ntp.org iburst\n" in conf
assert f"\n#napalm-time-sync: sourcedir {debian}/run/chrony-dhcp\n" in conf
# DHCP option 42 counts: chrony keeps reading what DHCP brings.
assert f"\nsourcedir {debian}/run/chrony-dhcp\n" in conf
assert f"\nsourcedir {debian}/etc/chrony/sources.d\n" in conf
assert f"\nconfdir {debian}/etc/chrony/conf.d\n" in conf
assert _text(debian / "etc/chrony/conf.d/local.conf") == (
@@ -361,15 +413,72 @@ class TestChronyServers:
assert _text(debian / "etc/chrony/sources.d/other.sources") == (
"#napalm-time-sync: server ntp-other.example.org\n"
)
# DHCP's own file is left alone: it is not read any more.
# DHCP's own file is left alone: the DHCP client rewrites it on every lease.
assert _text(debian / "run/chrony-dhcp/eth0.sources") == "server 192.168.1.1 iburst\n"
# The main configuration changed: chronyd reads it only when it starts.
assert "systemctl restart chrony.service" in host.log()
assert host.get_time_sync()["ntp_servers"] == [
"ntp1.example.org",
"10.0.0.1",
"2.de.pool.ntp.org",
]
reading = host.get_time_sync()
assert reading["ntp_servers"] == ["ntp1.example.org", "10.0.0.1", "2.de.pool.ntp.org"]
assert reading["dhcp_ntp_servers"] == ["192.168.1.1"]
def test_what_4_35_0_took_from_dhcp_is_given_back(self, debian: Path):
conf = debian / "etc/chrony/chrony.conf"
Host(debian).configure_time_sync(["ntp1.example.org"])
# A host 4.35.0 aligned: DHCP's sourcedir commented out behind the mark.
dhcp = f"sourcedir {debian}/run/chrony-dhcp"
conf.write_text(_text(conf).replace(f"\n{dhcp}\n", f"\n#napalm-time-sync: {dhcp}\n"))
assert Host(debian).get_time_sync()["dhcp_ntp_servers"] == []
(debian / "log").unlink()
host = Host(debian)
result = host.configure_time_sync(["ntp1.example.org"])
assert result["success"] is True, result["output"]
assert f"\n{dhcp}\n" in _text(conf)
assert "#napalm-time-sync: sourcedir" not in _text(conf)
# The main configuration changed again: a restart, not a reload.
assert "systemctl restart chrony.service" in host.log()
assert host.get_time_sync()["dhcp_ntp_servers"] == ["192.168.1.1"]
def test_a_foreign_sourcedir_4_35_0_took_out_stays_out(self, debian: Path):
conf = debian / "etc/chrony/chrony.conf"
foreign = f"#napalm-time-sync: sourcedir {debian}/etc/chrony/extra.d"
conf.write_text(_text(conf) + foreign + "\n")
_write(debian / "etc/chrony/extra.d/x.sources", "server ntp-extra.example.org\n")
assert Host(debian).configure_time_sync(["ntp1.example.org"])["success"] is True
assert f"\n{foreign}\n" in _text(conf)
def test_a_foreign_sourcedir_stays_and_its_sources_stop_counting(self, debian: Path):
conf = debian / "etc/chrony/chrony.conf"
conf.write_text(_text(conf) + f"sourcedir {debian}/etc/chrony/extra.d\n")
_write(debian / "etc/chrony/extra.d/x.sources", "server ntp-extra.example.org\n")
host = Host(debian)
result = host.configure_time_sync(["ntp1.example.org"])
assert result["success"] is True, result["output"]
assert f"\nsourcedir {debian}/etc/chrony/extra.d\n" in _text(conf)
assert _text(debian / "etc/chrony/extra.d/x.sources") == (
"#napalm-time-sync: server ntp-extra.example.org\n"
)
assert host.get_time_sync()["ntp_servers"] == ["ntp1.example.org"]
def test_dhcp_is_kept_where_the_servers_go_to_a_confdir(self, root: Path):
_write(
root / "etc/chrony/chrony.conf",
f"confdir {root}/etc/chrony/conf.d\npool a.pool.ntp.org\n"
f"sourcedir {root}/run/chrony-dhcp\n",
)
_write(root / "run/chrony-dhcp/eth0.sources", "server 192.168.1.1 iburst\n")
host = Host(root)
assert host.configure_time_sync(["ntp1.example.org"])["success"] is True
assert f"\nsourcedir {root}/run/chrony-dhcp\n" in _text(root / "etc/chrony/chrony.conf")
reading = host.get_time_sync()
assert (reading["ntp_servers"], reading["dhcp_ntp_servers"]) == (
["ntp1.example.org"],
["192.168.1.1"],
)
def test_a_second_run_only_reloads_the_sources(self, debian: Path):
Host(debian).configure_time_sync(["ntp1.example.org"])
@@ -480,7 +589,7 @@ class TestTimesyncdServers:
assert "systemctl restart systemd-timesyncd.service" in host.log()
assert host.get_time_sync()["ntp_servers"] == ["ntp1.example.org", "10.0.0.1"]
def test_servers_from_dhcp_stay_and_the_check_says_so(self, root: Path):
def test_servers_from_dhcp_stay_and_count(self, root: Path):
(root / "fix/timesync").write_text(TIMESYNC_CONFIGURED)
tool = (root / "bin/systemctl").read_text()
(root / "bin/systemctl").write_text(
@@ -488,11 +597,15 @@ class TestTimesyncdServers:
"printf 'SystemNTPServers", "printf 'LinkNTPServers=192.168.1.1\\nSystemNTPServers"
)
)
result = Host(root, active="systemd-timesyncd").configure_time_sync(["ntp1.example.org"])
host = Host(root, active="systemd-timesyncd")
result = host.configure_time_sync(["ntp1.example.org"])
assert result["success"] is False
assert result["steps"][-1]["step"] == "verify"
assert "192.168.1.1" in result["steps"][-1]["output"]
assert result["success"] is True, result["output"]
reading = host.get_time_sync()
assert (reading["ntp_servers"], reading["dhcp_ntp_servers"]) == (
["ntp1.example.org"],
["192.168.1.1"],
)
class TestOtherClients: