From 2e4c3a70a4bcdfabdae2fe73e32da8ee590a942e Mon Sep 17 00:00:00 2001 From: Pranav Jerry Date: Mon, 27 Sep 2021 22:30:07 +0530 Subject: [PATCH] cleanup before exit This was previously done by the systemd service. But since we allowed changing the name of bat0 and bridge0, we can't expect systemd to still do that job. Commented out some lines in the systemd service and made it send SIGINT instead of the default SIGTERM to kill naxalnet. I accidentally used to increment a2, a3, a4, ... in the __version__ instead of dev1, dev2, &c. a few commits back. Removed some print() lines lurking somewhere in the code. The cleanup is still not completed. We still have to delete the interfaces bridge0 and bat0, or whatever name the user gives it. Just out of boredom, I added some description to some network configuration, though so I doubt if anyone will understand it more because of that. --- CHANGELOG.md | 4 +- naxalnet.service | 7 +-- naxalnet/__init__.py | 2 +- naxalnet/daemon.py | 1 + naxalnet/network.py | 23 ++++---- naxalnet/scripts.py | 57 +++++++++++++++----- systemd-networkd/mesh.04-batman.network | 2 +- systemd-networkd/mesh.05-wireless-ap.network | 2 +- systemd-networkd/mesh.06-eth.network | 1 + systemd-networkd/mesh.07-bridge.network | 1 + systemd-networkd/tmp.01-eth.network | 1 + 11 files changed, 66 insertions(+), 35 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 6b02b14..7430f6f 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,8 +1,10 @@ # Changelog -## [Unreleased][] - 2021-09-21 +## [Unreleased][] - 2021-09-27 - Better error messages +- Sets gateway mode automatically. **This might cause problems with nodes running previous version of naxalnet** +- Cleanup before exit ## [v0.4.0][] - 2021-09-20 diff --git a/naxalnet.service b/naxalnet.service index e923d81..340c1be 100644 --- a/naxalnet.service +++ b/naxalnet.service @@ -22,15 +22,16 @@ NotifyAccess=all Restart=on-failure RestartSec=2sec ExecStart=/usr/bin/naxalnet --systemd +KillSignal=SIGINT # Reload systemd-networkd after naxalnet signals it is ready #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.*" +#ExecStopPost=/usr/bin/find /run/systemd/network -type f -delete -name "mesh.*" # Then delete the two interfaces created... -ExecStopPost=/usr/bin/networkctl delete bridge0 bat0 +#ExecStopPost=/usr/bin/networkctl delete bridge0 bat0 # ... and reload the configuration files. -ExecStopPost=/usr/bin/networkctl reload +#ExecStopPost=/usr/bin/networkctl reload # naxalnet already logs to systemd journal so we don't need # stdout and stderr. StandardOutput=null diff --git a/naxalnet/__init__.py b/naxalnet/__init__.py index 78343da..0d57703 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.0a5.dev2" +__version__ = "0.4.0a5.dev3" diff --git a/naxalnet/daemon.py b/naxalnet/daemon.py index f935357..f7f4727 100644 --- a/naxalnet/daemon.py +++ b/naxalnet/daemon.py @@ -32,6 +32,7 @@ class Daemon: def __init__(self): self.loop = EventLoop() self.iwd = IWD() + self.callback = None def on_device_add(self, path, data): """ diff --git a/naxalnet/network.py b/naxalnet/network.py index bc5bdff..1529215 100644 --- a/naxalnet/network.py +++ b/naxalnet/network.py @@ -36,15 +36,12 @@ class NetworkD: def __init__(self, runtime_dir="/run/systemd/network", bus=SystemMessageBus()): self._bus = bus self.proxy_reload() - self.files = [] + self.variables = {} - print(runtime_dir) self.runtime_path = Path(runtime_dir) # Create the runtime directory if it doesn't exist self.runtime_path.mkdir(parents=True, exist_ok=True) - # print(self.runtime_path) - def set_vars(self, **variables): """set the variables to replace with str.format""" self.variables = variables @@ -64,17 +61,17 @@ class NetworkD: # 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) + # now write it to a runtime config of the same name destination.write_text(text, encoding="utf-8") self.reload() - # This allows to delete all configs later - self.files.append(destination.name) def is_routable(self) -> bool: """returns true if any interface is routable""" return self.proxy.AddressState == "routable" + def delete_interface(self, name: str) -> None: + """delete the given interface""" + def remove_config(self, name: str) -> None: """ remove the file called 'name' from the runtime dir and reload @@ -85,10 +82,8 @@ class NetworkD: def remove_all_configs(self) -> None: """ - Remove all configs added by add_config(). - The configs will be removed only if they were added in the same - instance of NetworkD, that is, only files in self.files will be - removed. + Remove all configs in runtime_path. This will remove all files + in runtime_path without checking who put them there. """ - for i in self.files: - self.remove_config(i) + for i in self.runtime_path.iterdir(): + self.remove_config(i.name) diff --git a/naxalnet/scripts.py b/naxalnet/scripts.py index 1390270..1aa3ea4 100644 --- a/naxalnet/scripts.py +++ b/naxalnet/scripts.py @@ -39,8 +39,14 @@ from naxalnet.config import args from naxalnet.daemon import Daemon from naxalnet.network import NetworkD +# List of wireless devices used as part of the mesh. +# Used to poweroff devices during cleanup. +# Though used as a variable, thi e name is +# capitalised to shut up pylint and co. +USED_DEVICES = [] -def get_sorted_glob(directory: str, glob: str): + +def get_sorted_glob(directory: str, glob: str) -> list: """return sorted list of filenames matching glob""" path = Path(directory) glob_list = path.glob(glob) @@ -79,7 +85,6 @@ def setup_mesh(gateway_mode: str = "off"): try: notify("STATUS=Configuring the network...") logger.info("Copying network config files") - dest = Path(args.networkd_runtime_dir) networkd = NetworkD(runtime_dir=args.networkd_runtime_dir) networkd.set_vars( @@ -87,6 +92,7 @@ def setup_mesh(gateway_mode: str = "off"): bridgedev=args.bridge_device, gateway_mode=gateway_mode, ) + for i in get_sorted_glob(args.networkd_config_dir, MESH_GLOB): logger.debug("Adding network config %s", i) networkd.add_config(i) @@ -113,6 +119,9 @@ def setup_devices(): adhoc_devices = [] ap_devices = [] + global USED_DEVICES + USED_DEVICES = [] + # Find devices supporting ad-hoc and ap for i in devices: # For each device, check if its adapter supports @@ -143,8 +152,11 @@ def setup_devices(): if not adhoc_adapter.is_powered_on(): logger.debug("Adapter %s is off. Turning on", adhoc_adapter.name) adhoc_adapter.power_on() + logger.info("Starting mesh on %s", adhoc_device.name) adhoc_device.start_adhoc_open(args.adhoc_name) + USED_DEVICES.append(adhoc_device.name) + # Start Access point if ap_device is not empty, # ie, we have more devices if len(ap_devices) != 0: @@ -158,6 +170,7 @@ def setup_devices(): logger.debug("Adapter %s is off. Turning on", ap_adapter.name) ap_adapter.power_on() ap_device.start_ap(args.ap_ssid, args.ap_passwd) + USED_DEVICES.append(ap_device.name) else: logger.warning("Not setting up WiFi AP.") else: @@ -175,6 +188,22 @@ def setup_devices(): sys.exit(4) +def cleanup(signal=None, frame=None): + """ + Remove all network config, poweroff used wireless devices and + exit with 0. + + signal and frame are ununsed + """ + networkd = NetworkD(runtime_dir=args.networkd_runtime_dir) + logger.info("Exiting gracefully") + networkd.remove_all_configs() + for i in USED_DEVICES: + device = Device(i) + device.set_mode("station") + device.power_off() + + def print_wifi(): """ Prints the name and password of the adhoc, and ap @@ -221,18 +250,18 @@ def main(): gateway_mode = args.gateway_mode else: logger.error("gateway-mode has an illegal value") - exit(5) + sys.exit(5) - setup_devices() - setup_mesh() + try: + setup_devices() + setup_mesh() - # Start the daemon so that setup_devices() is called every - # time a device is connected or removed. - daemon = Daemon() - daemon.add_callback(setup_devices) + # Start the daemon so that setup_devices() is called every + # time a device is connected or removed. + daemon = Daemon() + daemon.add_callback(setup_devices) - notify("STATUS=Waiting for changes") - daemon.start() - - # naxalnet prints Bye while exiting. - logger.info("Bye") + notify("STATUS=Waiting for changes") + daemon.start() + except KeyboardInterrupt: + cleanup() diff --git a/systemd-networkd/mesh.04-batman.network b/systemd-networkd/mesh.04-batman.network index f835808..fb06808 100644 --- a/systemd-networkd/mesh.04-batman.network +++ b/systemd-networkd/mesh.04-batman.network @@ -2,7 +2,7 @@ Name={batdev} [Network] -Description=The BATMAN interface +Description=Configuration for the BATMAN interface Bridge={bridgedev} # like in 03-wireless-ad-hoc.network, this interface diff --git a/systemd-networkd/mesh.05-wireless-ap.network b/systemd-networkd/mesh.05-wireless-ap.network index 25278e1..f1351c2 100644 --- a/systemd-networkd/mesh.05-wireless-ap.network +++ b/systemd-networkd/mesh.05-wireless-ap.network @@ -8,7 +8,7 @@ WLANInterfaceType=ap [Network] -Description=Wireless AP +Description=Configuration for Wireless AP # link the interface to the bridge Bridge={bridgedev} diff --git a/systemd-networkd/mesh.06-eth.network b/systemd-networkd/mesh.06-eth.network index 3e27b9a..6a06d90 100644 --- a/systemd-networkd/mesh.06-eth.network +++ b/systemd-networkd/mesh.06-eth.network @@ -5,4 +5,5 @@ Name=en* Name=eth* [Network] +Description=Connect ethernet to the bridge Bridge={bridgedev} diff --git a/systemd-networkd/mesh.07-bridge.network b/systemd-networkd/mesh.07-bridge.network index 6214a9d..67b558f 100644 --- a/systemd-networkd/mesh.07-bridge.network +++ b/systemd-networkd/mesh.07-bridge.network @@ -6,6 +6,7 @@ Name={bridgedev} [Network] +Description=bridge to link non-batman machines # Use DHCP to assign an IP DHCP=yes diff --git a/systemd-networkd/tmp.01-eth.network b/systemd-networkd/tmp.01-eth.network index 9372561..a719be5 100644 --- a/systemd-networkd/tmp.01-eth.network +++ b/systemd-networkd/tmp.01-eth.network @@ -6,4 +6,5 @@ Name=eth* Name=en* [Network] +Description=Check for ethernet connection DHCP=yes