From 568dfc4bd56e15c71dddb1aaba9cd8acc8406c0a Mon Sep 17 00:00:00 2001
From: Alex Barchiesi <alex.barchiesi@garr.it>
Date: Sat, 23 Jun 2018 03:52:50 +0200
Subject: [PATCH] Update to handle /etc/network/interfaces.d/*.cfg.

---
 hooks/install | 72 +++++++++++++++++++++++++--------------------------
 revision      |  2 +-
 2 files changed, 36 insertions(+), 38 deletions(-)

diff --git a/hooks/install b/hooks/install
index f4a14a3..fb36b6e 100644
--- a/hooks/install
+++ b/hooks/install
@@ -1,59 +1,57 @@
 #!/bin/bash
 # Here do anything needed to install the service
-# i.e. apt-get install -y foo  or  bzr branch http://myserver/mycode /srv/webroot
-# Make sure this hook exits cleanly and is idempotent, common problems here are
-# failing to account for a debconf question on a dependency, or trying to pull
-# from github without installing git first.
+# e.g. apt install -y foo
+# Make sure this hook exits cleanly and is idempotent.
 set -e
-status-set maintenance "Changing Default gateway"
+status-set maintenance "Setting the default gateway"
 
 GATEWAY="$(config-get gateway)"
-OLD_GATEWAY_IP=`grep gateway /etc/network/interfaces | awk '{print $2}'`
-GATEWAY_IP=`echo "$GATEWAY" | awk -F "/" {'print $1'}`
 
 if [ -z "$GATEWAY" ]; then
-		juju-log "Gateway option is empty. Exiting..."
-		status-set active "Unit is ready. Gateway IP option empty"
-        exit 0
+    juju-log "Gateway option is empty. Exiting..."
+    status-set blocked "No gateway specified."
+    exit 0
 fi
 
+GATEWAY_IP=`echo "$GATEWAY" | awk -F "/" '{print $1}'`
+OLD_GATEWAY_IP=`grep -s gateway /etc/network/interfaces /etc/network/interfaces.d/*.cfg | head -1 | awk '{print $NF}'`
+
 if [ "$GATEWAY_IP" == "$OLD_GATEWAY_IP" ]; then
-	juju-log "Default gateway is already set. Exiting..."
-	status-set active "Unit is ready. Gateway ok"
-	exit 0
+    juju-log "Default gateway is already set. Exiting..."
+    status-set active "Unit is ready."
+    exit 0
 fi
 
 apt install -y sipcalc
 
 GATEWAY_NET=`sipcalc $GATEWAY | grep "Network address" | awk '{print $NF}'`
 
-old_IFS=$IFS
 IFS=$'\n'
-LINE=0
-for INTERFACE in $(cat -n /etc/network/interfaces | grep -w address); 
+FOUND=0
+for LINE in $(grep -s address /etc/network/interfaces /etc/network/interfaces.d/*.cfg); 
 do
-	ADDRESS=`echo $INTERFACE | awk '{ print $NF}'`
-	ADDRESS_NET=`sipcalc $ADDRESS | grep "Network address" | awk '{print $NF}'`
-	if [ "$ADDRESS_NET" == "$GATEWAY_NET" ]; then
-		LINE=`echo $INTERFACE | awk '{ print $1}'`
-	fi
+    FILE=`echo $LINE | awk '{print substr($1, 0, length($1)-1)}'`  # drop ':'
+    ADDRESS=`echo $LINE | awk '{print $NF}'`
+    ADDRESS_NET=`sipcalc $ADDRESS | grep "Network address" | awk '{print $NF}'`
+    if [ "$ADDRESS_NET" == "$GATEWAY_NET" ]; then
+	FOUND=1
+    fi
 done
-IFS=$old_IFS
-
-if [ "$LINE" -gt 0 ]; then
-	juju-log "Default gateway $GATEWAY_IP is setting..."
-	sed -i "${LINE}i\  gateway $GATEWAY_IP" /etc/network/interfaces
-	if [ ! -z "$OLD_GATEWAY_IP" ]; then
-		juju-log "Old default gateway $OLD_GATEWAY_IP removing..."
-		sed -i "/${OLD_GATEWAY_IP}/d" /etc/network/interfaces
-	fi
-	ip route del default
-	ip route add default via $GATEWAY_IP
-	juju-log "Default gateway changed"
+unset IFS
+
+if [ "$FOUND" -gt 0 ]; then
+    juju-log "Setting default gateway: $GATEWAY_IP ..."
+    sed -i "/${GATEWAY_IP}/a\    gateway $GATEWAY_IP" $FILE
+    if [ ! -z "$OLD_GATEWAY_IP" ]; then
+	juju-log "Removing old default gateway $OLD_GATEWAY_IP..."
+	sed -i "/${OLD_GATEWAY_IP}/d" $FILE
+    fi
+    ip route del default
+    ip route add default via $GATEWAY_IP
+    juju-log "Default gateway changed"
+    status-set active "Unit is ready"
 else
-	juju-log "Network not found. Exiting..."
-	status-set active "Unit is ready. Incorrect gateway IP"
-	exit 0
+    juju-log "Network not found. Exiting..."
+    status-set blocked "Incorrect gateway IP."
 fi
 
-status-set active "Unit is ready. Gateway ok"
diff --git a/revision b/revision
index 45a4fb7..ec63514 100644
--- a/revision
+++ b/revision
@@ -1 +1 @@
-8
+9
-- 
GitLab