diff --git a/CHANGELOG.md b/CHANGELOG.md index 7912d4d..1aeed11 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -70,6 +70,15 @@ recorded here; their history is the git log. `manage_service("lldpd", "enable")` and `manage_service("lldpd", "start")` (#8). ### Fixed +- `push_mac_acl()` and `push_radio_channel()` raise when the router refuses a step + (`CommandErrorException`, naming the `uci` command or the wifi restart and what the + router said). They used to return normally after a refused `uci set`, `add_list`, + `commit` or `wifi reload`, so an ACL that never landed looked applied. Each step runs + on an SSH exec channel of its own and as root, with its exit status checked; a + refused change reverts what was staged, so nothing of it is committed by a later + write, and wifi is not restarted for it. `push_mac_acl()` raises when no wifi-iface + carries the SSID, where it did nothing, and deletes `macfilter`/`maclist` only where + a section has them instead of hiding every delete's status behind `|| true` (#35). - `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 diff --git a/README.md b/README.md index 9bb4051..f117a2c 100644 --- a/README.md +++ b/README.md @@ -116,6 +116,16 @@ 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. +## Wireless writes + +`push_mac_acl(ssid_name, mode, macs)` (napalm-device-types `MacAclMixin`) rewrites the +MAC filter of every wifi-iface carrying the SSID and reloads wifi; +`push_radio_channel(radio, channel)` sets a radio's channel (`0` is `auto`) and restarts +wifi in full. Each `uci` step runs on an SSH exec channel of its own, as root, with its +exit status checked: a refused step raises `CommandErrorException` naming it, what was +staged is reverted, and nothing is committed or restarted. An SSID no wifi-iface +carries raises too. + ## Time sync `configure_time_sync(ntp_servers=, timezone=)` (napalm-device-types `TimeSyncMixin`) diff --git a/napalm_openwrt/uci_steps.py b/napalm_openwrt/uci_steps.py new file mode 100644 index 0000000..68ea771 --- /dev/null +++ b/napalm_openwrt/uci_steps.py @@ -0,0 +1,147 @@ +# 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. + +"""UCI writes run step by step, each step's exit status checked (#35). + +A write stages its changes with one ``uci`` command each, commits the config, then +restarts what reads it. Every step runs on an SSH exec channel of its own, so its exit +status is the command's own -- nothing is hidden behind ``|| true`` or a shell prompt. + +A refused step raises :class:`CommandErrorException` naming the step and what the +device said. A refusal before the commit reverts what was staged, so the next commit +of the config -- by any write -- does not apply half a change; nothing is restarted. +A refused restart comes after the commit: the change is saved, and the error says so. +""" + +from __future__ import annotations + +import logging +import re +import shlex +from collections.abc import Sequence +from dataclasses import dataclass, field +from typing import TYPE_CHECKING + +from napalm.base.exceptions import CommandErrorException + +if TYPE_CHECKING: # pragma: no cover - typing only + from napalm_device_types import CommandResult + +logger = logging.getLogger(__name__) + +#: Seconds one step may take; ``wifi`` brings every radio down and up again. +UCI_STEP_TIMEOUT = 60 + +#: ``config.section=type`` or ``config.section.option=value`` of ``uci show``. +_SHOW_LINE = re.compile( + r"^(?P[^.=\s]+)\.(?P
[^.=\s]+)(?:\.(?P