Files
nixos/modules/services/switches/edgeswitch.py
JakeHillion a84cb6d2ff
commit-validation / validate-single-commit (pull_request) Successful in 8s
buildbot/nix-eval Build done. (690 warnings)
buildbot/nix-build Build done.
buildbot/nix-effects Build done.
switches: manage es-48-500w from the network registry
The VLANs and port settings of es-48-500w are configured by hand in its
web UI, and the VLAN each host's port carries is agreed with the host's
own config only by convention.

Added a switches service, on stinger, that applies the registry to the
managed switches. A device with a kind is a managed switch and has a
port table in network/ports: a port either names the registered device
cabled to it, and then carries that device's networks, or states its own
native and tagged networks. Each switch gets an activate-switch-NAME
oneshot unit that is handed the switch's desired state, so a deploy
re-runs the units of the switches whose state changed. edgeswitch.py
drives the switch over its SSH CLI: it diffs the desired state against
the running config, sends the commands that close the gap, and saves
the config only once the switch and the gateway are still reachable.

Registered es-48-500w as an edgeswitch with its full port table, taken
from the switch's current config except that ports 11 and 12 move from
the DMZ to the LAN, and marked the networks stinger and warlock carry as
tagged. warlock carries the DMZ without an address, so
a membership's address is now optional and the router only reserves
addresses for memberships that have one. The mtu9000 and cellular VLANs
are now networks in the registry, and cyclone reads the cellular VLAN ID
from it. The switch's SSH host key is in the fleet's known hosts, and
its password is an agent-tier secret.

Test plan:
- CI
- A dry run from maverick against the switch only moves ports 11 and 12
  to the LAN
- Applied and saved three test configurations to spare port 17 from
  maverick, covering joining, retagging and leaving VLANs, then restored
  the port
- Deployed to stinger with a tagged-only test configuration on port 12:
  the unit applied and saved it along with the port descriptions
- With an unreachable gateway the script applies, then exits without
  saving
- maverick's toplevel only gains the switch in its SSH known hosts; the
  router snapshot is unchanged
2026-11-19 23:39:23 +00:00

209 lines
6.8 KiB
Python

"""Applies VLAN and port configuration to an EdgeSwitch over its SSH CLI.
Reads the desired state rendered from the network registry, diffs it against
the switch's running config and sends the commands that close the gap. The
running config is only saved once the switch and the gateway are still
reachable through it, so power-cycling the switch undoes a change that cuts
this host off.
"""
import argparse
import json
import re
import subprocess
import sys
PROMPT = re.compile(r"^\(.*\) [>#]")
def expand(spec):
"""Expands a VLAN list such as "2,4-7" into a set of IDs."""
ids = set()
for part in spec.split(","):
first, _, last = part.partition("-")
ids.update(range(int(first), int(last or first) + 1))
return ids
def run(state, password_file, commands):
"""Runs CLI commands in privileged exec mode and returns the output."""
with open(password_file) as f:
password = f.read().strip()
result = subprocess.run(
[
"sshpass",
"-f",
password_file,
"ssh",
"-T",
"-o",
"LogLevel=ERROR",
"-o",
"StrictHostKeyChecking=yes",
"-o",
"PubkeyAuthentication=no",
"-o",
"ConnectTimeout=10",
f"{state['user']}@{state['address']}",
],
input="\n".join(
["enable", password, "terminal length 0", *commands, ""]
),
capture_output=True,
text=True,
timeout=120,
)
if "#terminal length 0" not in result.stdout:
sys.exit(
f"could not reach privileged exec mode on {state['address']}:\n"
f"{result.stdout}{result.stderr}"
)
return result.stdout
def running_config(state, password_file):
"""Reads the VLAN database and each port's VLAN settings."""
output = run(state, password_file, ["show running-config"])
lines = output.split("#show running-config", 1)[1].splitlines()
vlans = {1}
ports = {}
port = None
for line in map(str.strip, lines):
if PROMPT.match(line):
break
if match := re.fullmatch(r"vlan ([\d,-]+)", line):
vlans |= expand(match[1])
elif match := re.fullmatch(r"interface +0/(\d+)", line):
port = ports[match[1]] = {
"description": None,
"pvid": 1,
"include": set(),
"exclude": set(),
"tagging": set(),
}
elif line == "exit":
port = None
elif port is None:
continue
elif match := re.fullmatch(r"description '(.*)'", line):
port["description"] = match[1]
elif match := re.fullmatch(r"vlan pvid (\d+)", line):
port["pvid"] = int(match[1])
elif match := re.fullmatch(
r"vlan participation (include|exclude) ([\d,-]+)", line
):
port[match[1]] |= expand(match[2])
elif match := re.fullmatch(r"vlan tagging ([\d,-]+)", line):
port["tagging"] |= expand(match[1])
return vlans, ports
def is_member(port, vlan):
# Every port is in the default VLAN unless excluded. Other VLANs need an
# explicit include.
if vlan == 1:
return vlan not in port["exclude"]
return vlan in port["include"]
def diff(state, vlans, ports):
"""Returns the commands that bring the switch to the desired state."""
wanted_vlans = {int(vlan): name for vlan, name in state["vlans"].items()}
commands = []
missing = sorted(set(wanted_vlans) - vlans)
if missing:
commands.append("vlan database")
for vlan in missing:
commands.append(f"vlan {vlan}")
commands.append(f'vlan name {vlan} "{wanted_vlans[vlan]}"')
commands.append("exit")
interfaces = []
for number, want in sorted(
state["ports"].items(), key=lambda p: int(p[0])
):
if number not in ports:
sys.exit(f"{state['address']} has no port 0/{number}")
have = ports[number]
native = want["native"]
if want["tagged"] == "all":
tagged = set(wanted_vlans) - {native}
else:
tagged = set(want["tagged"])
changes = []
if want["description"] not in (None, have["description"]):
changes.append(f"description '{want['description']}'")
# Join VLANs and move the PVID before leaving any, so the port is
# never left without its native VLAN.
for vlan in sorted(tagged | {native} - {None}):
if not is_member(have, vlan):
changes.append(f"vlan participation include {vlan}")
if vlan in tagged and vlan not in have["tagging"]:
changes.append(f"vlan tagging {vlan}")
if vlan == native and vlan in have["tagging"]:
changes.append(f"no vlan tagging {vlan}")
if native is not None and have["pvid"] != native:
changes.append(f"vlan pvid {native}")
for vlan in sorted((vlans | set(wanted_vlans)) - tagged - {native}):
if is_member(have, vlan):
changes.append(f"vlan participation exclude {vlan}")
if changes:
interfaces += [f"interface 0/{number}", *changes, "exit"]
if interfaces:
commands += ["configure", *interfaces, "exit"]
return commands
def main():
parser = argparse.ArgumentParser(description=__doc__)
parser.add_argument("state", help="desired state, as JSON")
parser.add_argument("--password-file", required=True)
parser.add_argument(
"--dry-run",
action="store_true",
help="print the commands that would be sent and change nothing",
)
args = parser.parse_args()
with open(args.state) as f:
state = json.load(f)
commands = diff(state, *running_config(state, args.password_file))
if not commands:
print(f"{state['address']}: up to date")
return
print("\n".join(commands))
if args.dry_run:
return
run(state, args.password_file, commands)
# Both checks go through the new config: a fresh session to the switch,
# and a ping to the gateway beyond it.
remaining = diff(state, *running_config(state, args.password_file))
if remaining:
sys.exit(
"the switch did not take these commands, not saving:\n"
+ "\n".join(remaining)
)
ping = subprocess.run(
["ping", "-c", "3", "-W", "2", state["gateway"]],
capture_output=True,
)
if ping.returncode != 0:
sys.exit(f"{state['gateway']} is unreachable, not saving")
output = run(state, args.password_file, ["write memory", "y"])
if "Configuration Saved!" not in output:
sys.exit(f"saving the config failed:\n{output}")
print(f"{state['address']}: applied and saved")
if __name__ == "__main__":
main()