Skip to content

Commit 15229b4

Browse files
authored
feat(ns-plug): restart service if connection goes down
Make sure that ns-plug always try to connect to the controller. This fix will allow automatica re-connection in case of disaster recovery of the remote controller. #978
1 parent 27ecb48 commit 15229b4

File tree

3 files changed

+27
-10
lines changed

3 files changed

+27
-10
lines changed

packages/ns-plug/Makefile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,7 @@ define Package/ns-plug/install
7373
$(INSTALL_DIR) $(1)/usr/libexec/mwan-hooks
7474
$(INSTALL_BIN) ./files/ns-plug.init $(1)/etc/init.d/ns-plug
7575
$(INSTALL_BIN) ./files/ns-plug $(1)/usr/sbin/ns-plug
76+
$(INSTALL_BIN) ./files/restart-ns-plug $(1)/usr/libexec/restart-ns-plug
7677
$(INSTALL_BIN) ./files/distfeed-setup $(1)/usr/sbin/distfeed-setup
7778
$(INSTALL_BIN) ./files/remote-backup $(1)/usr/sbin
7879
$(INSTALL_BIN) ./files/send-backup $(1)/usr/sbin

packages/ns-plug/files/ns-plug

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -65,13 +65,11 @@ fi
6565

6666
# Register the server
6767
valid=0
68-
max_attempts=30 # 60 seconds
69-
watchdog=0
7068
response=""
71-
until [ "${valid}" -eq 1 ] || [ "${watchdog}" -ge "${max_attempts}" ]
69+
until [ "${valid}" -eq 1 ]
7270
do
7371
response=$(curl ${curl_opts} -H "Content-Type: application/json" -H "RegistrationToken: ${token}" ${server}/api/units/register -X POST --data '{"unit_id": "'${unit_id}'", "username": "'${user}'", "password": "'${secret}'", "unit_name": "'${unit_name}'", "version": "'${VERSION_ID}'", "subscription_type": "'${subscription_type}'", "system_id": "'${system_id}'"}')
74-
http_code=$(echo ${response} | jq -r .code)
72+
http_code=$(echo ${response} | jq -r '.code // empty')
7573
if [ "${http_code}" == "409" ]; then
7674
# Duplicate entry, cleanup uci config
7775
uci delete rpcd.controller
@@ -82,15 +80,11 @@ do
8280
elif [ "${http_code}" == "200" ]; then
8381
valid=1
8482
else
85-
sleep 2 # wait for controller to be reacheable
86-
watchdog=$(( watchdog + 1 ))
83+
logger -t ns-plug "Connection failed. Waiting for the controller ..."
84+
sleep 5 # wait for controller to be reacheable
8785
fi
8886
done
8987

90-
if [ "${watchdog}" -ge "${max_attempts}" ]; then
91-
exit 5
92-
fi
93-
9488
# Save the new password after successfull registration
9589
uci set rpcd.controller.password=${passwd}
9690
uci commit rpcd
@@ -112,6 +106,7 @@ dev tun-nsplug
112106
tls-client
113107
script-security 2
114108
route-up /usr/sbin/ns-controller-push-info
109+
down "/usr/libexec/restart-ns-plug"
115110
<ca>
116111
${ca}
117112
</ca>
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
#!/bin/bash
2+
3+
#
4+
# Copyright (C) 2024 Nethesis S.r.l.
5+
# SPDX-License-Identifier: GPL-2.0-only
6+
#
7+
8+
# Restart the ns-plug service when tun-nsplug goes down
9+
10+
# the service can take a while to stop
11+
if /etc/init.d/ns-plug status ; then
12+
sleep 30
13+
fi
14+
15+
# Check if the service is NOT running
16+
if ! /etc/init.d/ns-plug status ; then
17+
exit 0
18+
fi
19+
20+
# Restart the service
21+
/etc/init.d/ns-plug restart

0 commit comments

Comments
 (0)