diff --git a/Makefile b/Makefile index c5b5f46..4f8e5f3 100644 --- a/Makefile +++ b/Makefile @@ -7,7 +7,9 @@ DESTDIR:= / all: build -build: naxalnet/__init__.py +build: build/lib/naxalnet/__init__.py + +build/lib/naxalnet/__init__.py: naxalnet/__init__.py $(PYTHON) setup.py build install: build diff --git a/naxalnet.service b/naxalnet.service index 94702ed..e923d81 100644 --- a/naxalnet.service +++ b/naxalnet.service @@ -23,7 +23,7 @@ Restart=on-failure RestartSec=2sec ExecStart=/usr/bin/naxalnet --systemd # Reload systemd-networkd after naxalnet signals it is ready -ExecStartPost=/usr/bin/networkctl reload +#ExecStartPost=/usr/bin/networkctl reload # When naxalnet exits, delete all files starting # with mesh.* in /run/systemd/network ExecStopPost=/usr/bin/find /run/systemd/network -type f -delete -name "mesh.*" diff --git a/naxalnet/__init__.py b/naxalnet/__init__.py index 8997fb4..f35e3e6 100644 --- a/naxalnet/__init__.py +++ b/naxalnet/__init__.py @@ -35,4 +35,4 @@ See README.md for documentation. # # In case you forgot to change the version, skip the number # and put the next number in the next commit. -__version__ = "0.4.0a3" +__version__ = "0.4.0a4" diff --git a/naxalnet/network.py b/naxalnet/network.py index 16bd6c0..fae8df6 100644 --- a/naxalnet/network.py +++ b/naxalnet/network.py @@ -25,7 +25,7 @@ TODO: Add more details from pathlib import Path from shutil import copy from dasbus.connection import SystemMessageBus -from naxalnet.config import args + NETWORKD_BUS = "org.freedesktop.network1" NETWORKD_PATH = "/org/freedesktop/network1" @@ -38,24 +38,43 @@ def copy_glob(directory: str, glob: str, destination: str) -> None: copy(i, destination) -class Networkd: +class NetworkD: """control systemd-networkd""" - def __init__(self, bus=SystemMessageBus()): + def __init__(self, runtime_dir="/run/systemd/network", bus=SystemMessageBus()): self._bus = bus self.proxy_reload() self.files = [] - self.variables = {"batdev": "bat0", "bridgedev": "bridge0"} + self.variables = {} + print(runtime_dir) + self.runtime_path = Path(runtime_dir) + print(self.runtime_path) + + def set_vars(self, **variables): + """set the variables to replace with str.format""" + self.variables = variables def proxy_reload(self) -> None: """reload the proxy""" self.proxy = self._bus.get_proxy(NETWORKD_BUS, NETWORKD_PATH) + def reload(self) -> None: + """reload the systemd-networkd configuration""" + self.proxy.Reload() + def add_config(self, name: str) -> None: """add config file to runtime directory and reload networkd""" - text = Path(name).read_text(encoding="utf-8") - contents = text.format(**self.variables) + source = Path(name) + destination = self.runtime_path / source.name + + # Substitute variables in the config + text = source.read_text(encoding="utf-8").format(**self.variables) + # now write it to a runtime config + print(self.runtime_path / name) + destination.write_text(text, encoding="utf-8") + self.reload() self.files.append(name) - Path(args.networkd_runtime_dir + "/" + name).write_text( - contents, encoding="utf-8" - ) + + def is_routable(self) -> bool: + """returns true if any interface is routable""" + return self.proxy.AddressState == "routable" diff --git a/naxalnet/scripts.py b/naxalnet/scripts.py index b448ac5..c243357 100644 --- a/naxalnet/scripts.py +++ b/naxalnet/scripts.py @@ -32,32 +32,30 @@ from shutil import copy from dasbus.error import DBusError from systemd.daemon import notify from naxalnet import __version__ -from naxalnet.default import REPORT_BUG_INFO +from naxalnet.default import REPORT_BUG_INFO, MESH_GLOB, TMP_NET_GLOB from naxalnet.log import logger from naxalnet.iwd import Adapter, Device, IWD from naxalnet.config import args from naxalnet.daemon import Daemon +from naxalnet.network import NetworkD -def copy_files(): - """ - Copy networkd configs to volatile dir. - The D-Bus API does not support creating new interfaces - or linking to bridges. So we use config files. - See man:systemd.network(5) - """ +def setup_mesh(): + """configure networkd to setup the mesh""" try: notify("STATUS=Configuring the network...") logger.info("Copying network config files") dest = Path(args.networkd_runtime_dir) - src = Path(args.networkd_config_dir) - # Create the volatile directory if it doesn't exist dest.mkdir(parents=True, exist_ok=True) - # Copy all files in src to dest - for i in src.iterdir(): - copy(i, dest) + networkd = NetworkD(runtime_dir=args.networkd_runtime_dir) + # TODO: replace with valus from args + networkd.set_vars(batdev="bat0", bridgedev="bridge0") + for i in Path(args.networkd_config_dir).glob(MESH_GLOB): + path = str(i) + logger.debug("Adding network config %s", path) + networkd.add_config(path) except PermissionError: logger.exception("A PermissionError occured while copying files") logger.error(REPORT_BUG_INFO) @@ -171,12 +169,12 @@ def main(): elif args.version: print_version() sys.exit(0) - copy_files() - setup_devices() # Notify systemd that naxalnet is ready. # see man:sd_notify(3) notify("READY=1") + setup_devices() + setup_mesh() # Start the daemon so that setup_devices() is called every # time a device is connected or removed.