Files
napalm-openwrt/napalm_openwrt/ap_config/_system.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

107 lines
3.8 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.
"""The radio and system parts of an AP configuration, as UCI commands."""
from __future__ import annotations
from collections.abc import Mapping
from napalm_device_types import ApConfigDict
from napalm_openwrt.ap_config._common import Plan, Radios, sq
_DEFAULT_SYSLOG_PORT = 514
def _country(config: ApConfigDict, radios: Radios, plan: Plan) -> None:
if not config.get("country_code") or not radios.all:
return
for radio in (radios.r5g, radios.r2g):
if radio:
plan.commands.append(f"uci set wireless.{radio}.country={sq(config['country_code'])}")
plan.commits.add("wireless")
def _htmode(field: str, radio: str | None, config: ApConfigDict, plan: Plan) -> None:
values: Mapping[str, object] = config
if values.get(field) and radio:
plan.commands.append(f"uci set wireless.{radio}.htmode={sq(values[field])}")
plan.commits.add("wireless")
def radio_part(part: str, config: ApConfigDict, radios: Radios, plan: Plan) -> None:
"""``radio/country`` on every radio, ``radio/5g_htmode`` and ``radio/2g_htmode`` on theirs."""
if part == "radio/country":
_country(config, radios, plan)
elif part == "radio/5g_htmode":
_htmode("htmode_5g", radios.r5g, config, plan)
elif part == "radio/2g_htmode":
_htmode("htmode_2g", radios.r2g, config, plan)
def _syslog(config: ApConfigDict, plan: Plan) -> None:
host = config.get("syslog_host")
if not host:
return
port = config.get("syslog_port") or _DEFAULT_SYSLOG_PORT
plan.commands += [
f"uci set system.@system[0].log_ip={sq(host)}",
f"uci set system.@system[0].log_port={sq(port)}",
"uci set system.@system[0].log_proto='udp'",
"uci set system.@system[0].log_remote='1'",
]
plan.commits.add("system")
def _web_ui(config: ApConfigDict, plan: Plan) -> None:
"""LuCI is served by uhttpd, so the policy is the service state; the listener
config stays as it is, and enabling it again restores exactly what was there."""
if "web_ui" not in config:
return
if config["web_ui"]:
plan.actions.append("/etc/init.d/uhttpd enable && /etc/init.d/uhttpd start")
else:
plan.actions.append("/etc/init.d/uhttpd stop; /etc/init.d/uhttpd disable")
def _ssh_port(config: ApConfigDict, plan: Plan) -> None:
if not config.get("ssh_port"):
return
plan.commands.append(f"uci set dropbear.@dropbear[0].Port={sq(config['ssh_port'])}")
plan.commits.add("dropbear")
def _password_auth(config: ApConfigDict, plan: Plan) -> None:
if "ssh_password_auth" not in config:
return
value = "on" if config["ssh_password_auth"] else "off"
plan.commands.append(f"uci set dropbear.@dropbear[0].PasswordAuth={sq(value)}")
plan.commands.append(f"uci set dropbear.@dropbear[0].RootPasswordAuth={sq(value)}")
plan.commits.add("dropbear")
_SYSTEM_PARTS = {
"system/syslog": _syslog,
"system/web_ui": _web_ui,
"system/ssh_port": _ssh_port,
"system/password_auth": _password_auth,
}
def system_part(part: str, config: ApConfigDict, plan: Plan) -> None:
"""Remote syslog, the web interface, the SSH port and SSH password logins."""
build = _SYSTEM_PARTS.get(part)
if build is not None:
build(config, plan)