driver / standard (pull_request) Successful in 17s
driver / lint (pull_request) Successful in 17s
driver / types (pull_request) Successful in 1m15s
driver / test (3.12) (pull_request) Successful in 1m11s
driver / test (3.13) (pull_request) Successful in 1m0s
driver / lowest (pull_request) Successful in 30s
driver / build (pull_request) Successful in 34s
driver / security (pull_request) Successful in 1m12s
CI / driver (pull_request) Successful in 2m58s
driver / publish (pull_request) Skipped
napalm-linux now brings napalm-device-types' PosixTimeSyncMixin (4.35.0). Its write -- a chrony drop-in with every other source commented out, timedatectl set-timezone -- is the wrong way in on OpenMediaVault: OMV generates chrony.conf and the zone from conf.system.time through Salt and would take the change back on its next deploy, the web UI would keep showing its own servers, and netOrk's automatic alignment would rewrite on every poll (NetOrk/netork#2370). OmvTimeSyncMixin reads the servers and the zone from System.getTimeSettings (no servers while NTP is off) and keeps the host's reading for whether the clock is synchronised and by which client. Both values are written through System.setTimeSettings, NTP switched on for servers, and only the modules that change made dirty are applied, plus chrony and timezone if already dirty, as set_hostname does. A value OMV already has is not written again, so a second write changes nothing. The floor rises to napalm-device-types 4.35.0.
128 lines
5.5 KiB
Python
128 lines
5.5 KiB
Python
"""Time sync on OpenMediaVault: its servers and zone through its own database (NetOrk/netork#2370).
|
|
|
|
napalm-linux brings napalm-device-types' ``PosixTimeSyncMixin``: it reads chrony's
|
|
files and ``timedatectl``, and writes a drop-in under ``/etc/chrony/sources.d``
|
|
(commenting out every other source) and ``timedatectl set-timezone``. OMV generates
|
|
``/etc/chrony/chrony.conf`` and the zone from ``conf.system.time`` (``ntp.enable``,
|
|
``ntp.timeservers``, ``timezone``) through Salt, so such a write would be taken back
|
|
the next time OMV deploys its time settings -- and the web UI would go on showing
|
|
its own servers.
|
|
|
|
So the servers and the zone are the web UI's: read with ``System.getTimeSettings``,
|
|
written with ``System.setTimeSettings`` -- NTP switched on, the servers as OMV keeps
|
|
them, one comma-separated string -- and deployed with ``Config.applyChanges`` for
|
|
the modules that change made dirty, plus ``chrony`` and ``timezone`` if they already
|
|
were; changes left pending in the web UI for other modules stay pending. With NTP
|
|
switched off OMV stops chronyd, and the servers it still lists count for nothing:
|
|
none are read. A value OMV already has is not written again. Whether the clock is
|
|
synchronised, and by which client, is still the host's own reading.
|
|
"""
|
|
|
|
from __future__ import annotations
|
|
|
|
import re
|
|
from typing import TYPE_CHECKING, Any
|
|
|
|
from napalm_device_types import PosixTimeSyncMixin
|
|
|
|
if TYPE_CHECKING:
|
|
from napalm_device_types import TimeSyncDict
|
|
from napalm_device_types.models import FixStepOutcomeDict
|
|
|
|
#: The fields of ``getTimeSettings`` that ``setTimeSettings`` takes back (not ``date``).
|
|
_SET_FIELDS = ("timezone", "ntpenable", "ntptimeservers", "ntpclients")
|
|
#: The modules OMV regenerates from ``conf.system.time``.
|
|
_TIME_MODULES = frozenset({"chrony", "timezone"})
|
|
_SERVER_SEPARATOR = re.compile(r"[,\s]+")
|
|
_APPLY_TIMEOUT = 600
|
|
|
|
|
|
def omv_servers(settings: dict[str, Any]) -> list[str]:
|
|
"""The servers OMV keeps time with: none while NTP is switched off."""
|
|
if not _enabled(settings.get("ntpenable")):
|
|
return []
|
|
return [s for s in _SERVER_SEPARATOR.split(str(settings.get("ntptimeservers") or "")) if s]
|
|
|
|
|
|
def _enabled(value: object) -> bool:
|
|
if isinstance(value, str):
|
|
return value.strip().lower() in {"1", "true", "yes", "on"}
|
|
return bool(value)
|
|
|
|
|
|
class OmvTimeSyncMixin(PosixTimeSyncMixin):
|
|
"""The POSIX time sync, with the servers and the zone of OMV's web UI (module docstring)."""
|
|
|
|
if TYPE_CHECKING:
|
|
|
|
def _omv_rpc(
|
|
self,
|
|
service: str,
|
|
method: str,
|
|
params: dict[str, Any] | None = None,
|
|
timeout: float = 60,
|
|
) -> Any: ...
|
|
|
|
def _omv_dirty_modules(self) -> list[str]: ...
|
|
|
|
def _omv_time_settings(self) -> dict[str, Any]:
|
|
"""``System.getTimeSettings``.
|
|
|
|
:raises RuntimeError: when OMV refuses, or answers with something else.
|
|
"""
|
|
settings = self._omv_rpc("System", "getTimeSettings")
|
|
if not isinstance(settings, dict):
|
|
raise RuntimeError(f"System.getTimeSettings answered {settings!r}")
|
|
return settings
|
|
|
|
def get_time_sync(self) -> TimeSyncDict:
|
|
"""OMV's servers and zone, the host's word on whether the clock is synchronised.
|
|
|
|
:raises RuntimeError: when ``System.getTimeSettings`` failed.
|
|
:raises ValueError: when the host's reading came back cut short.
|
|
"""
|
|
settings = self._omv_time_settings()
|
|
host = super().get_time_sync()
|
|
return {
|
|
"ntp_servers": omv_servers(settings),
|
|
"timezone": str(settings.get("timezone") or "") or None,
|
|
"synchronized": host["synchronized"],
|
|
"daemon": host["daemon"],
|
|
}
|
|
|
|
def _write_ntp_servers(self, servers: list[str]) -> FixStepOutcomeDict:
|
|
settings = self._omv_time_settings()
|
|
if [s.lower() for s in omv_servers(settings)] == [s.lower() for s in servers]:
|
|
return {"ok": True, "output": "OpenMediaVault already keeps time with these servers"}
|
|
applied = self._omv_save_time(settings, ntpenable=True, ntptimeservers=",".join(servers))
|
|
return {"ok": True, "output": f"set OpenMediaVault's time servers; applied {applied}"}
|
|
|
|
def _write_time_zone(self, zone: str) -> FixStepOutcomeDict:
|
|
settings = self._omv_time_settings()
|
|
if settings.get("timezone") == zone:
|
|
return {"ok": True, "output": f"OpenMediaVault is already in {zone}"}
|
|
applied = self._omv_save_time(settings, timezone=zone)
|
|
return {
|
|
"ok": True,
|
|
"output": f"set OpenMediaVault's time zone to {zone}; applied {applied}",
|
|
}
|
|
|
|
def _omv_save_time(self, settings: dict[str, Any], **changes: Any) -> str:
|
|
"""``setTimeSettings`` with *changes* over *settings*, then apply what that made
|
|
dirty, and the time modules already dirty; what was applied.
|
|
|
|
:raises RuntimeError: when OMV refuses.
|
|
"""
|
|
params = {field: settings[field] for field in _SET_FIELDS if field in settings}
|
|
params.update(changes)
|
|
before = set(self._omv_dirty_modules())
|
|
self._omv_rpc("System", "setTimeSettings", params)
|
|
after = set(self._omv_dirty_modules())
|
|
modules = sorted((after - before) | (after & _TIME_MODULES))
|
|
if modules:
|
|
# Salt restarts chronyd and sets the zone: give it time.
|
|
self._omv_rpc(
|
|
"Config", "applyChanges", {"modules": modules, "force": False}, _APPLY_TIMEOUT
|
|
)
|
|
return ", ".join(modules) or "nothing"
|