feat(time-sync): w32time's servers and the time zone through TimeSyncMixin #10

Merged
christianmanivong merged 1 commits from feature/time-sync-2367 into main 2026-11-11 14:51:44 +00:00
Owner

Summary

Windows hosts take part in netOrk's time sync (NetOrk/netork#2360) through napalm-device-types' TimeSyncMixin. The driver writes both the servers and the zone, over PowerShell Remoting with one script per read, like every other getter.

Changes

  • Reading (napalm_windows/time_sync.py):
    • The servers and the mode come from w32time's registry values NtpServer and Type, with group policy's values first. They are not parsed from w32tm /query /configuration, whose text is in the host's language. The ,0x flags are dropped, and Type NoSync has no servers.
    • synchronized is the leap indicator on the first line of w32tm /query /status (3 = not synchronised), which reads the same in English and German. A stopped service counts as not synchronised. daemon is w32time.
    • The zone is the registry's TimeZoneInformation (what tzutil /g reads), read fresh each time: [TimeZoneInfo]::Local is cached for the life of the remote session.
  • Writing the servers: NtpServer = a,0x8 b,0x8 (client mode) and Type = NTP, which is what w32tm /config /manualpeerlist /syncfromflags:manual writes, then w32tm /config /update. A stopped Windows Time service is started first. No value goes through a native command line, because Windows PowerShell 5.1 mangles embedded quotes there. Two cases are refused before anything is written:
    • a domain member on the domain hierarchy (NT5DS, AllSync), which reports its configured servers;
    • a host whose w32time settings come from group policy.
  • Writing the zone: Set-TimeZone -Id <Windows zone>, which also switches summer time back on.
  • Read-back: Windows keeps a Windows zone, and one Windows zone stands for several IANA zones. Without help, Europe/Vienna would read back as Europe/Berlin, and netOrk would realign the host on every poll. So the IANA name written is kept in HKLM\SOFTWARE\napalm-windows (IanaTimeZone) and read back while the host is still in that Windows zone. A zone changed by hand since then reads as its CLDR name. Summer time switched off reads as None.
  • Writes the host already has are skipped.
  • Zone table: the hand-picked table of common zones is replaced by CLDR's windowsZones.xml (release 49, sha256 in the header) in both directions: 142 Windows zones and 578 IANA names. tools/windows_zones.py generates it into napalm_windows/_windows_zones.py with tzdata's link lines, and nothing is fetched at runtime. CLDR keeps old names (Asia/Calcutta); one that tzdata renamed reads as its current name (Asia/Kolkata). A merge is not a rename (Atlantic/Reykjavik stays). Every Windows zone reads back as itself (test). For the common zones the 001 names are the same as the old table's, so the scheduled-jobs reading is unchanged there.
  • napalm-device-types>=4.35.0. The README has a Time sync section, the CHANGELOG is updated, and tools/harvest.py records the new script.

Related Issue

Part of NetOrk/netork#2367 (epic NetOrk/netork#2360)

Testing

  • pytest: 251 passed, coverage 98.4% (floor 95). tests/test_time_sync.py runs against a fake host that answers the reading from tests/fixtures/synthetic/time_sync.json and applies the write scripts, so verify reads back what a host would. It covers:
    • workgroup, domain member (NT5DS, AllSync), group policy, NoSync;
    • English and German status, a stopped service, w32tm errors;
    • the zone marker, a zone changed by hand, _dstoff, NUL-padded key names;
    • no rewrite, failing scripts.
      tests/test_windows_zones_tool.py covers the generator.
  • ruff format, ruff check, mypy, napalm-standard check and ratchet, and bandit are clean.
  • Not run against a Windows host: the fixture is the shape the script is designed to emit.
## Summary Windows hosts take part in netOrk's time sync (NetOrk/netork#2360) through napalm-device-types' `TimeSyncMixin`. The driver writes both the servers and the zone, over PowerShell Remoting with one script per read, like every other getter. ## Changes - Reading (`napalm_windows/time_sync.py`): - The servers and the mode come from w32time's registry values `NtpServer` and `Type`, with group policy's values first. They are not parsed from `w32tm /query /configuration`, whose text is in the host's language. The `,0x` flags are dropped, and `Type NoSync` has no servers. - `synchronized` is the leap indicator on the first line of `w32tm /query /status` (`3` = not synchronised), which reads the same in English and German. A stopped service counts as not synchronised. `daemon` is `w32time`. - The zone is the registry's `TimeZoneInformation` (what `tzutil /g` reads), read fresh each time: `[TimeZoneInfo]::Local` is cached for the life of the remote session. - Writing the servers: `NtpServer` = `a,0x8 b,0x8` (client mode) and `Type` = `NTP`, which is what `w32tm /config /manualpeerlist /syncfromflags:manual` writes, then `w32tm /config /update`. A stopped Windows Time service is started first. No value goes through a native command line, because Windows PowerShell 5.1 mangles embedded quotes there. Two cases are refused before anything is written: - a domain member on the domain hierarchy (`NT5DS`, `AllSync`), which reports its configured servers; - a host whose w32time settings come from group policy. - Writing the zone: `Set-TimeZone -Id <Windows zone>`, which also switches summer time back on. - Read-back: Windows keeps a Windows zone, and one Windows zone stands for several IANA zones. Without help, `Europe/Vienna` would read back as `Europe/Berlin`, and netOrk would realign the host on every poll. So the IANA name written is kept in `HKLM\SOFTWARE\napalm-windows` (`IanaTimeZone`) and read back while the host is still in that Windows zone. A zone changed by hand since then reads as its CLDR name. Summer time switched off reads as `None`. - Writes the host already has are skipped. - Zone table: the hand-picked table of common zones is replaced by CLDR's `windowsZones.xml` (release 49, sha256 in the header) in both directions: 142 Windows zones and 578 IANA names. `tools/windows_zones.py` generates it into `napalm_windows/_windows_zones.py` with tzdata's link lines, and nothing is fetched at runtime. CLDR keeps old names (`Asia/Calcutta`); one that tzdata renamed reads as its current name (`Asia/Kolkata`). A merge is not a rename (`Atlantic/Reykjavik` stays). Every Windows zone reads back as itself (test). For the common zones the 001 names are the same as the old table's, so the scheduled-jobs reading is unchanged there. - `napalm-device-types>=4.35.0`. The README has a *Time sync* section, the CHANGELOG is updated, and `tools/harvest.py` records the new script. ## Related Issue Part of NetOrk/netork#2367 (epic NetOrk/netork#2360) ## Testing - `pytest`: 251 passed, coverage 98.4% (floor 95). `tests/test_time_sync.py` runs against a fake host that answers the reading from `tests/fixtures/synthetic/time_sync.json` and applies the write scripts, so `verify` reads back what a host would. It covers: - workgroup, domain member (`NT5DS`, `AllSync`), group policy, `NoSync`; - English and German status, a stopped service, `w32tm` errors; - the zone marker, a zone changed by hand, `_dstoff`, NUL-padded key names; - no rewrite, failing scripts. `tests/test_windows_zones_tool.py` covers the generator. - `ruff format`, `ruff check`, `mypy`, `napalm-standard check` and `ratchet`, and `bandit` are clean. - Not run against a Windows host: the fixture is the shape the script is designed to emit.
christianmanivong added 1 commit 2026-11-11 14:28:42 +00:00
feat(time-sync): w32time's servers and the time zone through TimeSyncMixin
driver / standard (pull_request) Successful in 14s
driver / lint (pull_request) Successful in 14s
driver / types (pull_request) Successful in 57s
driver / test (3.12) (pull_request) Successful in 58s
driver / test (3.13) (pull_request) Successful in 51s
driver / lowest (pull_request) Successful in 26s
driver / build (pull_request) Successful in 27s
driver / security (pull_request) Successful in 57s
CI / driver (pull_request) Successful in 2m23s
driver / publish (pull_request) Skipped
0b30ab00a5
netOrk keeps one list of NTP servers and a time zone per site and aligns
a device that differs (NetOrk/netork#2360). A Windows host takes part
through w32time and its time zone, in one PowerShell script per read.

The servers and the mode are read from w32time's registry values, group
policy's first, not from w32tm /query /configuration, whose text is in
the host's language; synchronised is the leap indicator on the first
line of w32tm /query /status, which reads the same in any language. The
servers are written the way w32tm /config /manualpeerlist
/syncfromflags:manual writes them (NtpServer with 0x8, Type NTP),
followed by w32tm /config /update: no value passes a native command
line, whose quoting Windows PowerShell 5.1 mangles. A domain member that
takes its time from the domain hierarchy (NT5DS, AllSync) and a host
whose w32time comes from group policy refuse the write: their time is
set elsewhere, and a local write would be overridden or break the
domain's time.

Windows keeps a Windows zone, which stands for several IANA zones, so
Europe/Vienna written would read back as Europe/Berlin and the poll
would align the host forever. The IANA name written is kept beside it
in HKLM\SOFTWARE\napalm-windows and read back while the host is still
in that Windows zone. The zone is read from the registry, which
[TimeZoneInfo]::Local caches for the life of the remote session.

The zone table is now CLDR's windowsZones.xml (release 49) both ways,
generated by tools/windows_zones.py with tzdata's links and committed
with its source and checksum; nothing is fetched at runtime. CLDR's old
names (Asia/Calcutta) read as their current ones (Asia/Kolkata).

Writes the host already has are skipped. Needs napalm-device-types
4.35.0.

Refs NetOrk/netork#2367
christianmanivong merged commit 442541d30a into main 2026-11-11 14:51:44 +00:00
christianmanivong deleted branch feature/time-sync-2367 2026-11-11 14:51:44 +00:00
Sign in to join this conversation.