changed how network config is done

We now use NetworkD() to configure systemd-networkd.
Path.glob doesn't return things in alphabetical order, which is needed
to configure networkd without any error. Need a fix for that.
This commit is contained in:
Pranav Jerry 2021-09-27 10:00:59 +05:30
parent ab3252683a
commit a81449b898
No known key found for this signature in database
GPG Key ID: F1DCDC4FED0A0C5B
5 changed files with 46 additions and 27 deletions

View File

@ -7,7 +7,9 @@ DESTDIR:= /
all: build all: build
build: naxalnet/__init__.py build: build/lib/naxalnet/__init__.py
build/lib/naxalnet/__init__.py: naxalnet/__init__.py
$(PYTHON) setup.py build $(PYTHON) setup.py build
install: build install: build

View File

@ -23,7 +23,7 @@ Restart=on-failure
RestartSec=2sec RestartSec=2sec
ExecStart=/usr/bin/naxalnet --systemd ExecStart=/usr/bin/naxalnet --systemd
# Reload systemd-networkd after naxalnet signals it is ready # 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 # When naxalnet exits, delete all files starting
# with mesh.* in /run/systemd/network # with mesh.* in /run/systemd/network
ExecStopPost=/usr/bin/find /run/systemd/network -type f -delete -name "mesh.*" ExecStopPost=/usr/bin/find /run/systemd/network -type f -delete -name "mesh.*"

View File

@ -35,4 +35,4 @@ See README.md for documentation.
# #
# In case you forgot to change the version, skip the number # In case you forgot to change the version, skip the number
# and put the next number in the next commit. # and put the next number in the next commit.
__version__ = "0.4.0a3" __version__ = "0.4.0a4"

View File

@ -25,7 +25,7 @@ TODO: Add more details
from pathlib import Path from pathlib import Path
from shutil import copy from shutil import copy
from dasbus.connection import SystemMessageBus from dasbus.connection import SystemMessageBus
from naxalnet.config import args
NETWORKD_BUS = "org.freedesktop.network1" NETWORKD_BUS = "org.freedesktop.network1"
NETWORKD_PATH = "/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) copy(i, destination)
class Networkd: class NetworkD:
"""control systemd-networkd""" """control systemd-networkd"""
def __init__(self, bus=SystemMessageBus()): def __init__(self, runtime_dir="/run/systemd/network", bus=SystemMessageBus()):
self._bus = bus self._bus = bus
self.proxy_reload() self.proxy_reload()
self.files = [] 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: def proxy_reload(self) -> None:
"""reload the proxy""" """reload the proxy"""
self.proxy = self._bus.get_proxy(NETWORKD_BUS, NETWORKD_PATH) 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: def add_config(self, name: str) -> None:
"""add config file to runtime directory and reload networkd""" """add config file to runtime directory and reload networkd"""
text = Path(name).read_text(encoding="utf-8") source = Path(name)
contents = text.format(**self.variables) 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) 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"

View File

@ -32,32 +32,30 @@ from shutil import copy
from dasbus.error import DBusError from dasbus.error import DBusError
from systemd.daemon import notify from systemd.daemon import notify
from naxalnet import __version__ 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.log import logger
from naxalnet.iwd import Adapter, Device, IWD from naxalnet.iwd import Adapter, Device, IWD
from naxalnet.config import args from naxalnet.config import args
from naxalnet.daemon import Daemon from naxalnet.daemon import Daemon
from naxalnet.network import NetworkD
def copy_files(): def setup_mesh():
""" """configure networkd to setup the mesh"""
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)
"""
try: try:
notify("STATUS=Configuring the network...") notify("STATUS=Configuring the network...")
logger.info("Copying network config files") logger.info("Copying network config files")
dest = Path(args.networkd_runtime_dir) dest = Path(args.networkd_runtime_dir)
src = Path(args.networkd_config_dir)
# Create the volatile directory if it doesn't exist # Create the volatile directory if it doesn't exist
dest.mkdir(parents=True, exist_ok=True) dest.mkdir(parents=True, exist_ok=True)
# Copy all files in src to dest networkd = NetworkD(runtime_dir=args.networkd_runtime_dir)
for i in src.iterdir(): # TODO: replace with valus from args
copy(i, dest) 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: except PermissionError:
logger.exception("A PermissionError occured while copying files") logger.exception("A PermissionError occured while copying files")
logger.error(REPORT_BUG_INFO) logger.error(REPORT_BUG_INFO)
@ -171,12 +169,12 @@ def main():
elif args.version: elif args.version:
print_version() print_version()
sys.exit(0) sys.exit(0)
copy_files()
setup_devices()
# Notify systemd that naxalnet is ready. # Notify systemd that naxalnet is ready.
# see man:sd_notify(3) # see man:sd_notify(3)
notify("READY=1") notify("READY=1")
setup_devices()
setup_mesh()
# Start the daemon so that setup_devices() is called every # Start the daemon so that setup_devices() is called every
# time a device is connected or removed. # time a device is connected or removed.