Files
napalm-openwrt/napalm_openwrt/ap_config/_common.py
T
Christian Manivong 9bac9bd1af
driver / standard (pull_request) Successful in 16s
driver / lint (pull_request) Successful in 11s
driver / lowest (pull_request) Successful in 28s
driver / types (pull_request) Successful in 1m4s
driver / test (3.13) (pull_request) Successful in 54s
driver / test (3.12) (pull_request) Successful in 1m8s
driver / build (pull_request) Successful in 28s
driver / security (pull_request) Successful in 59s
CI / driver (pull_request) Successful in 2m8s
driver / publish (pull_request) Skipped
fix(access-point): a change the AP refuses in apply_ap_config is raised, and nothing applied (#54)
The configuration ran as one detached script, so a refused uci step
only reached its log while the call reported the part as applied. The
changes are now staged by a script the call waits for: a uci wrapper
records each refused set, add, add_list or delete with its part, and at
the end every config is reverted and the script exits 3, which raises
CommandErrorException naming each part, passphrases masked. Quiet -q
calls stay tests. The commits, LuCI's service, the reloads and the
restart run detached afterwards, as before.

Closes #54
2026-11-29 22:43:34 +01:00

112 lines
3.9 KiB
Python

# 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.
"""What the AP-configuration builders share: quoting, the plan they fill, the radios.
No I/O: every function here turns values into shell text.
"""
from __future__ import annotations
from collections.abc import Iterable
from dataclasses import dataclass, field
from napalm_device_types import ApConfigDict, ApSsidConfigDict
from napalm_openwrt.uci_steps import UciSection
#: The bridge every managed AP's SSID VLANs share, and the port it is trunked on.
AP_BRIDGE = "br-ap"
AP_UPLINK_PORT = "eth0"
#: ``band`` (or the old ``hwmode``) of a wifi-device, by the band it names.
_BANDS = {
"2.4GHz": ("2g", "11g", "b", "g", "bg", "bgn"),
"5GHz": ("5g", "11a", "a", "ac", "ax5"),
}
def sq(value: object) -> str:
"""*value* as one single-quoted shell word, whatever it contains.
An SSID name, a passphrase or a MAC goes into the script; a ``'`` in it
would end the quoting and run the rest as root on the AP.
"""
return "'" + str(value).replace("'", "'\\''") + "'"
@dataclass
class Plan:
"""The commands built so far, the configs they stage for a commit, and what runs live.
``commands`` only stage UCI changes, which the device does not run until they are
committed; ``actions`` change the running device and come after the commits.
"""
commands: list[str] = field(default_factory=list)
commits: set[str] = field(default_factory=set)
actions: list[str] = field(default_factory=list)
@dataclass(frozen=True)
class Radios:
"""The AP's 5 GHz and 2.4 GHz radios, by their UCI section name; None without one."""
r5g: str | None = None
r2g: str | None = None
@property
def all(self) -> list[str]:
return [r for r in (self.r2g, self.r5g) if r]
def radios_of(sections: Iterable[UciSection]) -> Radios:
"""The first radio of each band among the ``wifi-device`` sections of ``uci show wireless``."""
found: dict[str, str] = {}
for sec in sections:
if sec.type != "wifi-device":
continue
raw = (sec.options.get("band") or sec.options.get("hwmode") or [""])[0]
for band, words in _BANDS.items():
if raw in words:
found.setdefault(band, sec.name)
return Radios(r5g=found.get("5GHz"), r2g=found.get("2.4GHz"))
def ssid_of(config: ApConfigDict, name: str) -> ApSsidConfigDict | None:
"""The SSID *name* of *config*; None when it has none of that name."""
return next((s for s in config.get("ssids") or [] if s.get("name") == name), None)
def needs_key(encryption: str) -> bool:
"""Whether hostapd needs a local key for *encryption*: every PSK and SAE mode."""
enc = encryption.lower()
return "psk" in enc or "sae" in enc
def keyed_without_key(ssid: ApSsidConfigDict) -> bool:
"""A keyed mode with no key on record: writing it would take the SSID off the
air, and writing ``none`` instead would put it on the air unprotected."""
return needs_key(ssid.get("encryption") or "none") and not ssid.get("key")
def ssid_loop(ssid_name: str, body: Iterable[str]) -> str:
"""A loop over each wireless section that carries *ssid_name*, *body* run as ``$_s``."""
lines = [
"for _s in $(uci show wireless | grep -oE '^wireless\\.[^.]+' | sort -u); do",
f' [ "$(uci -q get $_s.ssid 2>/dev/null)" = {sq(ssid_name)} ] || continue',
*(f" {line}" for line in body),
"done",
]
return "\n".join(lines)