[PATCH 1/3] configurator: separate sync hostname

Tim Niemeyer tim.niemeyer at mastersword.de
So Nov 29 12:35:13 CET 2015


Signed-off-by: Tim Niemeyer <tim.niemeyer at mastersword.de>
---

 bsp/default/root_file_system/etc/configurator.sh | 41 ------------
 bsp/default/root_file_system/etc/crontabs/root   |  2 +-
 bsp/default/root_file_system/etc/syncHostname.sh | 80 ++++++++++++++++++++++++
 3 files changed, 81 insertions(+), 42 deletions(-)
 create mode 100755 bsp/default/root_file_system/etc/syncHostname.sh

diff --git a/bsp/default/root_file_system/etc/configurator.sh b/bsp/default/root_file_system/etc/configurator.sh
index eb434c8..c7b92bc 100644
--- a/bsp/default/root_file_system/etc/configurator.sh
+++ b/bsp/default/root_file_system/etc/configurator.sh
@@ -15,7 +15,6 @@ if [ -f /etc/config/configurator ];then
 	SCRIPT_VERSION=`uci get configurator. at script[0].version`
 	SCRIPT_ERROR_LEVEL=`uci get configurator. at script[0].error_level`
 	SCRIPT_LOGFILE=`uci get configurator. at script[0].logfile`
-	SCRIPT_SYNC_HOSTNAME=`uci get configurator. at script[0].sync_hostname`
 	CRAWL_METHOD=`uci get configurator. at crawl[0].method`
 	CRAWL_ROUTER_ID=`uci get configurator. at crawl[0].router_id`
 	CRAWL_UPDATE_HASH=`uci get configurator. at crawl[0].update_hash`
@@ -44,42 +43,6 @@ else
 	}
 fi
 
-sync_hostname() {
-	err "Syncing hostname"
-	api_return=$(wget -T $API_TIMEOUT -q -O - "http://$netmon_api/api_csv_configurator.php?section=get_hostname&authentificationmethod=$CRAWL_METHOD&nickname=$CRAWL_NICKNAME&password=$CRAWL_PASSWORD&router_auto_update_hash=$CRAWL_UPDATE_HASH&router_id=$CRAWL_ROUTER_ID")
-	ret=${api_return%%,*}
-	if [ "$ret" != "success" ]; then
-		err "Ther was an error fetching the hostname"
-		exit 0
-	elif [ "$ret" = "success" ]; then
-		netmon_hostname=${api_return%,*}
-		netmon_hostname=${netmon_hostname#*,}
-		
-		#check for valid hostname as specified in rfc 1123
-		#see http://stackoverflow.com/a/3824105
-		regex='^([a-zA-Z0-9]|[a-zA-Z0-9][a-zA-Z0-9\-]{0,61}[a-zA-Z0-9])'
-		regex=$regex'(\.([a-zA-Z0-9]|[a-zA-Z0-9][a-zA-Z0-9\-]{0,61}[a-zA-Z0-9]))*$'
-		if [ "${#netmon_hostname}" -le "255" ]; then
-			if echo -n $netmon_hostname | egrep -q "$regex"; then
-				if [ "$netmon_hostname" != "`cat /proc/sys/kernel/hostname`" ]; then
-					err "Setting new hostname: $netmon_hostname"
-					uci set system. at system[0].hostname=$netmon_hostname
-					uci commit
-					echo $netmon_hostname > /proc/sys/kernel/hostname
-				else
-					err "Hostname is up to date"
-				fi
-			else
-				err "Hostname ist malformed"
-				exit 0
-			fi
-		else
-			err "Hostname exceeds the maximum length of 255 characters"
-			exit 0
-		fi
-	fi
-}
-
 assign_router() {
 	hostname=`cat /proc/sys/kernel/hostname`
 	
@@ -168,7 +131,3 @@ fi
 if [ "$AUTOADD_IPV6_ADDRESS" = "1" ]; then
 	autoadd_ipv6_address
 fi
-
-if [ "$SCRIPT_SYNC_HOSTNAME" = "1" ]; then
-	sync_hostname
-fi
\ No newline at end of file
diff --git a/bsp/default/root_file_system/etc/crontabs/root b/bsp/default/root_file_system/etc/crontabs/root
index 630c358..6cb8f9f 100644
--- a/bsp/default/root_file_system/etc/crontabs/root
+++ b/bsp/default/root_file_system/etc/crontabs/root
@@ -1,4 +1,4 @@
-*/5 * * * *	sh /etc/fastdstart.sh; sh /etc/nodewatcher.sh; sh /etc/configurator.sh
+*/5 * * * *	sh /etc/fastdstart.sh; sh /etc/nodewatcher.sh; sh /etc/configurator.sh; /etc/syncHostname.sh
 */1 * * * * /usr/sbin/batctl gw off; sleep 1; /usr/sbin/batctl gw client
 
 #15 01 * * *	rdate -s time.fu-berlin.de > /dev/null
diff --git a/bsp/default/root_file_system/etc/syncHostname.sh b/bsp/default/root_file_system/etc/syncHostname.sh
new file mode 100755
index 0000000..0147b78
--- /dev/null
+++ b/bsp/default/root_file_system/etc/syncHostname.sh
@@ -0,0 +1,80 @@
+#!/bin/sh
+# Netmon Configurator (C) 2010-2012 Freifunk Oldenburg
+# Lizenz: GPL v3
+
+test -f /tmp/started || exit
+
+#Get the configuration from the uci configuration file
+#If it does not exists, then get it from a normal bash file with variables.
+if [ -f /etc/config/configurator ];then
+	API_IPV4_ADRESS=`uci get configurator. at api[0].ipv4_address`
+	API_IPV6_ADRESS=`uci get configurator. at api[0].ipv6_address`
+	API_IPV6_INTERFACE=`uci get configurator. at api[0].ipv6_interface`
+	API_TIMEOUT=`uci get configurator. at api[0].timeout`
+	SCRIPT_ERROR_LEVEL=`uci get configurator. at script[0].error_level`
+	SCRIPT_LOGFILE=`uci get configurator. at script[0].logfile`
+	SCRIPT_SYNC_HOSTNAME=`uci get configurator. at script[0].sync_hostname`
+	CRAWL_METHOD=`uci get configurator. at crawl[0].method`
+	CRAWL_ROUTER_ID=`uci get configurator. at crawl[0].router_id`
+	CRAWL_UPDATE_HASH=`uci get configurator. at crawl[0].update_hash`
+	CRAWL_NICKNAME=`uci get configurator. at crawl[0].nickname`
+	CRAWL_PASSWORD=`uci get configurator. at crawl[0].password`
+else
+	. `dirname $0`/configurator_config
+fi
+
+if [ "$API_IPV4_ADRESS" != "1" ]; then
+	netmon_api=$API_IPV4_ADRESS
+else
+	netmon_api="[$API_IPV6_ADRESS"%"$API_IPV6_INTERFACE]"
+fi
+
+if [ "$SCRIPT_ERROR_LEVEL" -gt "1" ]; then
+	err() {
+		echo "$(date) [configurator]: $1" >> $SCRIPT_LOGFILE
+	}
+else
+	err() {
+		:
+	}
+fi
+
+sync_hostname() {
+	err "Syncing hostname"
+	api_return=$(wget -T $API_TIMEOUT -q -O - "http://$netmon_api/api_csv_configurator.php?section=get_hostname&authentificationmethod=$CRAWL_METHOD&nickname=$CRAWL_NICKNAME&password=$CRAWL_PASSWORD&router_auto_update_hash=$CRAWL_UPDATE_HASH&router_id=$CRAWL_ROUTER_ID")
+	ret=${api_return%%,*}
+	if [ "$ret" != "success" ]; then
+		err "Ther was an error fetching the hostname"
+		exit 0
+	elif [ "$ret" = "success" ]; then
+		netmon_hostname=${api_return%,*}
+		netmon_hostname=${netmon_hostname#*,}
+		
+		#check for valid hostname as specified in rfc 1123
+		#see http://stackoverflow.com/a/3824105
+		regex='^([a-zA-Z0-9]|[a-zA-Z0-9][a-zA-Z0-9\-]{0,61}[a-zA-Z0-9])'
+		regex=$regex'(\.([a-zA-Z0-9]|[a-zA-Z0-9][a-zA-Z0-9\-]{0,61}[a-zA-Z0-9]))*$'
+		if [ "${#netmon_hostname}" -le "255" ]; then
+			if echo -n $netmon_hostname | egrep -q "$regex"; then
+				if [ "$netmon_hostname" != "`cat /proc/sys/kernel/hostname`" ]; then
+					err "Setting new hostname: $netmon_hostname"
+					uci set system. at system[0].hostname=$netmon_hostname
+					uci commit
+					echo $netmon_hostname > /proc/sys/kernel/hostname
+				else
+					err "Hostname is up to date"
+				fi
+			else
+				err "Hostname ist malformed"
+				exit 0
+			fi
+		else
+			err "Hostname exceeds the maximum length of 255 characters"
+			exit 0
+		fi
+	fi
+}
+
+if [ "$SCRIPT_SYNC_HOSTNAME" = "1" ]; then
+	sync_hostname
+fi
-- 
2.1.4




Mehr Informationen über die Mailingliste franken-dev