From 971f783e4e692fcfecce513aebb194efdd5cd4e4 Mon Sep 17 00:00:00 2001 From: Vasily Evseenko Date: Fri, 20 Sep 2024 15:40:02 +0300 Subject: [PATCH] Add openwrt init script and custom node init --- openwrt/net/wfb-ng/Makefile | 4 +++- openwrt/net/wfb-ng/files/wfb-ng.init | 26 ++++++++++++++++++++++++++ wfb_ng/cluster.py | 20 ++++++++++++++++++-- wfb_ng/conf/master.cfg | 8 +++++++- wfb_ng/server.py | 2 +- 5 files changed, 55 insertions(+), 5 deletions(-) create mode 100755 openwrt/net/wfb-ng/files/wfb-ng.init diff --git a/openwrt/net/wfb-ng/Makefile b/openwrt/net/wfb-ng/Makefile index f6887496..07f575e7 100644 --- a/openwrt/net/wfb-ng/Makefile +++ b/openwrt/net/wfb-ng/Makefile @@ -2,7 +2,7 @@ include $(TOPDIR)/rules.mk PKG_NAME:=wfb-ng PKG_VERSION:=24.9.2 -PKG_RELEASE:=1 +PKG_RELEASE:=2 PKG_LICENSE:=GPL-3.0-only PKG_LICENSE_FILES:=LICENSE @@ -42,6 +42,8 @@ define Package/wfb-ng/install $(INSTALL_DIR) $(1)/usr/bin $(INSTALL_BIN) $(PKG_BUILD_DIR)/wfb_rx $(1)/usr/bin/ $(INSTALL_BIN) $(PKG_BUILD_DIR)/wfb_tx $(1)/usr/bin/ + $(INSTALL_DIR) $(1)/etc/init.d + $(INSTALL_BIN) ./files/wfb-ng.init $(1)/etc/init.d/wfb-ng endef $(eval $(call BuildPackage,wfb-ng)) diff --git a/openwrt/net/wfb-ng/files/wfb-ng.init b/openwrt/net/wfb-ng/files/wfb-ng.init new file mode 100755 index 00000000..376b87be --- /dev/null +++ b/openwrt/net/wfb-ng/files/wfb-ng.init @@ -0,0 +1,26 @@ +#!/bin/sh /etc/rc.common + +START=99 +STOP=10 + +USE_PROCD=1 +NAME=wfb-ng + +SYSUPGRADE_CONF="/etc/sysupgrade.conf" +WFB_INIT="/usr/sbin/wfb-ng.sh" + +if [ -f $SYSUPGRADE_CONF ] && [ -x $WFB_INIT ] && ! grep -q $WFB_INIT $SYSUPGRADE_CONF +then + echo $WFB_INIT >> $SYSUPGRADE_CONF +fi + +start_service() { + if [ -x $WFB_INIT ]; then + procd_open_instance wfb-ng + procd_set_param command $WFB_INIT + procd_set_param respawn + procd_set_param stdout 1 + procd_set_param stderr 1 + procd_close_instance + fi +} diff --git a/wfb_ng/cluster.py b/wfb_ng/cluster.py index aaa8d635..5b7f0b71 100644 --- a/wfb_ng/cluster.py +++ b/wfb_ng/cluster.py @@ -108,6 +108,10 @@ def get_allocator(node): trap _cleanup EXIT +{% if custom_init_script != None %} +{{ custom_init_script }} +{% endif %} + iw reg set {{ settings.common.wifi_region }} {% for wlan in wlans %} @@ -136,8 +140,10 @@ def get_allocator(node): {% endif %} {% endfor %} +{% if ssh_mode %} # Will fail in case of connection loss (sleep 1; exec cat > /dev/null) & +{% endif %} echo "WFB-ng init done" wait -n @@ -146,7 +152,11 @@ def get_allocator(node): script_template = env.from_string(script_template) -def gen_cluster_scripts(cluster_nodes): +def gen_cluster_scripts(cluster_nodes, ssh_mode=False): + """ + cluster_nodes: node_name -> service_map + """ + res = {} for node, node_attrs in cluster_nodes.items(): @@ -164,6 +174,10 @@ def gen_cluster_scripts(cluster_nodes): settings.cluster.nodes[node], settings.common.__dict__) + custom_init_script = search_attr('custom_init_script', + settings.cluster.nodes[node], + settings.cluster.__dict__) + if not isinstance(txpower, dict): txpower = dict((wlan, txpower) for wlan in wlans) @@ -171,6 +185,8 @@ def gen_cluster_scripts(cluster_nodes): ht_mode=bandwidth_map[max_bw], services=node_attrs, txpower=txpower, - channel=channel) + channel=channel, + ssh_mode=ssh_mode, + custom_init_script=custom_init_script) return res diff --git a/wfb_ng/conf/master.cfg b/wfb_ng/conf/master.cfg index 271afebe..533657e1 100644 --- a/wfb_ng/conf/master.cfg +++ b/wfb_ng/conf/master.cfg @@ -53,7 +53,11 @@ temp_overheat_warning = 60 # [*C] (8812eu only) Overheat warning threshold. nodes = { # required host attrs: 'wlans' - # optional host attrs (will override defaults): 'ssh', 'server_address', 'wifi_txpower', 'wifi_channel', 'ssh_user', 'ssh_port', 'ssh_key' + # optional host attrs (will override defaults): + # 'ssh', 'server_address', 'wifi_txpower', + # 'wifi_channel', 'ssh_user', 'ssh_port', 'ssh_key' + # 'custom_init_script' + # # If ssh_user or ssh_port is set to None then node will not be automatically initialized in ssh mode. # If ssh_key is None, then ssh_agent will be used. @@ -68,6 +72,8 @@ nodes = { ssh_user = 'root' ssh_port = 22 ssh_key = None # Path to ssh private key. If None then it will try to use ssh-agent +custom_init_script = None # Do some custom command inside of node init script before wfb-ng start + # You can specify any bash snippet here server_address = None # Set to IP address which is reachable from all cluster nodes base_port_server = 10000 # UDP ports allocated on server diff --git a/wfb_ng/server.py b/wfb_ng/server.py index f8820d41..73072e03 100644 --- a/wfb_ng/server.py +++ b/wfb_ng/server.py @@ -122,7 +122,7 @@ def _ssh_exited(x, node): if is_cluster: services, cluster_nodes = parse_cluster_services(profiles) if cluster_mode == 'ssh': - for node, setup_script in gen_cluster_scripts(cluster_nodes).items(): + for node, setup_script in gen_cluster_scripts(cluster_nodes, ssh_mode=True).items(): ssh_user = search_attr('ssh_user', settings.cluster.nodes[node], settings.cluster.__dict__)