feat(access-point): SSID settings, radio channels, VLAN names, the AP configuration and DHCP leases through declared methods (NetOrk/netork#695)
push_ssid_config, set_radio_channels and set_vlan_name run as checked uci steps (#35): a refused step raises naming itself and reverts what was staged. set_radio_channels replaces the undeclared push_radio_channel. apply_ap_config takes napalm-device-types' ApConfigDict and the parts the caller found different and writes them as one detached script: the builder netOrk had in _uci_builders.py, moved here. It answers which parts it wrote and which it could not, whether the AP restarts, and the script with every passphrase masked. A passphrase part now writes the key. get_dhcp_leases reads dnsmasq's lease file. Needs napalm-device-types 4.54.0; CI pre-installs it from its branch until it is on PyPI.
This commit is contained in:
1 parent
dd3e3f1543
commit
634bc9c793
18 files changed
+2108
-40
No files matched your search
@@ -0,0 +1,106 @@
|
||||
# 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, and the configs they stage for a commit."""
|
||||
|
||||
commands: list[str] = field(default_factory=list)
|
||||
commits: set[str] = field(default_factory=set)
|
||||
|
||||
|
||||
@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)
|
||||
Reference in new issue
Block a user