# 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.
.