Skip to content

Commit

Permalink
Parse network configuration from a team.toml (#17)
Browse files Browse the repository at this point in the history
* Parse network configuration from a `team.toml`

* make hostname configurable

* increase os version

* add env to RDEPENDS

* fix some minor bugs

* fix some more minor bugs
  • Loading branch information
schmidma authored Oct 23, 2024
1 parent f81bcd5 commit 36f5d24
Show file tree
Hide file tree
Showing 3 changed files with 135 additions and 18 deletions.
2 changes: 1 addition & 1 deletion meta-hulks/conf/distro/HULKs-OS.conf
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,5 @@ SUMMARY = "HULKs flavoured Poky"

DISTRO = "HULKs-OS"
DISTRO_NAME = "HULKs-OS"
DISTRO_VERSION = "7.5.6"
DISTRO_VERSION = "7.5.7"
SDKIMAGE_FEATURES:remove = "dbg-pkgs src-pkgs"
4 changes: 3 additions & 1 deletion meta-hulks/recipes-hulks/network-config/network-config.bb
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
DEPENDS += "nao-wifi-conf"
DEPENDS += "\
nao-wifi-conf\
"

SUMMARY = "Add systemd services to initially set ip address config"
LICENSE = "GPL-3.0-only"
Expand Down
147 changes: 131 additions & 16 deletions meta-hulks/recipes-hulks/network-config/network-config/configure_network
100644 → 100755
Original file line number Diff line number Diff line change
@@ -1,36 +1,151 @@
#!/bin/sh
#!/usr/bin/env python3
"""Configure the network of the NAO.
set -e
This script configures the network of the NAO based on the hardware IDs and the
team number. It then composes the configuration for the WLAN and wired network
based on the team number and the number of the NAO, and sets the hostname.
"""

HEAD_ID="$(/opt/aldebaran/bin/head_id)"
QUERY_STRING="to_entries | map(select(.value.head_id == \"${HEAD_ID}\")) | .[0].key"
NAO_NUMBER="$(jq -r "${QUERY_STRING}" /media/internal/hardware_ids.json)"
from __future__ import annotations

if [ -z "${NAO_NUMBER}" ]; then
exit
fi
from pathlib import Path
from subprocess import check_call, check_output
from typing import Any

HOSTNAME="tuhhnao${NAO_NUMBER}"
import tomllib

echo "[Match]
TEAM_TOML = "/media/internal/team.toml"
HEAD_ID_SCRIPT = "/opt/aldebaran/bin/head_id"
WLAN_NETWORK_PATH = "/etc/systemd/network/80-wlan.network"
WIRED_NETWORK_PATH = "/etc/systemd/network/80-wired.network"


def load_configuration(path: Path | str) -> dict[str, Any]:
"""Load the configuration from the given path.
Args:
path: Path to the configuration file.
Returns:
The configuration
"""
path = Path(path)
with path.open("rb") as file:
return tomllib.load(file)


def query_head_id() -> str:
"""Query the head ID of the NAO.
Returns:
The head ID of the NAO.
"""
command = HEAD_ID_SCRIPT.split()
output = check_output(command, encoding="UTF-8")
return output.strip()


def find_nao_with_head_id(
configuration: dict[str, Any],
head_id: str,
) -> dict[str, Any]:
"""Find the NAO with the given head ID in the configuration.
Args:
configuration: The configuration.
head_id: The head ID of the NAO.
Returns:
The NAO with the given head ID.
"""
try:
return next(x for x in configuration["naos"] if x["head_id"] == head_id)
except StopIteration as error:
msg = f"Could not find NAO with head_id {head_id}"
raise ValueError(msg) from error


def compose_wlan_network(nao: dict[str, Any], team_number: int) -> str:
"""Compose the configuration for the WLAN network.
Args:
nao: The NAO.
team_number: The team number.
Returns:
The configuration for the WLAN network.
"""
return f"""\
[Match]
Name=wlan*
[Network]
Address=10.0.24.${NAO_NUMBER}/16
Address=10.0.{team_number}.{nao["number"]}/16
[Route]
Gateway=0.0.0.0
" > /etc/systemd/network/80-wlan.network
"""


def compose_wired_network(nao: dict[str, Any], team_number: int) -> str:
"""Compose the configuration for the wired network.
Args:
nao: The NAO.
team_number: The team number.
echo "[Match]
Returns:
The configuration for the wired network.
"""
return f"""\
[Match]
Name=en* eth*
[Network]
Address=10.1.24.${NAO_NUMBER}/16
Address=10.1.{team_number}.{nao["number"]}/16
[Route]
Gateway=10.1.24.1
Destination=10.2.24.0/24
" > /etc/systemd/network/80-wired.network
"""


def write_to_file(path: str | Path, content: str) -> None:
"""Write the content to the file at the given path.
Args:
path: Path to the file.
content: Content to write.
"""
path = Path(path)
with path.open("w") as file:
file.write(content)


def set_hostname(hostname: str) -> None:
"""Set the hostname of the NAO.
Args:
hostname: The hostname.
"""
check_call(["/usr/bin/hostnamectl", "set-hostname", hostname])


if __name__ == "__main__":
configuration = load_configuration(TEAM_TOML)
head_id = query_head_id()
nao = find_nao_with_head_id(configuration, head_id)

wlan_configuration = compose_wlan_network(nao, configuration["team_number"])
wired_configuration = compose_wired_network(nao, configuration["team_number"])

hostnamectl set-hostname ${HOSTNAME}
write_to_file(WLAN_NETWORK_PATH, wlan_configuration)
write_to_file(WIRED_NETWORK_PATH, wired_configuration)
set_hostname(nao["hostname"])

0 comments on commit 36f5d24

Please sign in to comment.