Skip to content
Snippets Groups Projects
Commit 3584dd2a authored by Claudio Pisa's avatar Claudio Pisa
Browse files

Do not modify configuration files

The hook was looking for the default gateway in the machine's configuration
files, but this didn't work when using DHCP. Also, there is no real advantage
in modifying the machine's configuration files.

So we just change the routes at run time.
parent 3fdd7699
No related branches found
No related tags found
No related merge requests found
......@@ -2,7 +2,6 @@
# Here do anything needed to install the service
# e.g. apt install -y foo
# Make sure this hook exits cleanly and is idempotent.
set -e
set -x
status-set maintenance "Setting the default gateway"
......@@ -21,58 +20,23 @@ source /etc/lsb-release
RELEASE_MAJOR="$(echo $DISTRIB_RELEASE | awk -F '.' '{print $1}')"
if [ "$RELEASE_MAJOR" -le "16" ]; then
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."
exit 0
fi
apt install -y sipcalc
GATEWAY_NET=`sipcalc $GATEWAY | grep "Network address" | awk '{print $NF}'`
IFS=$'\n'
FOUND=0
for LINE in $(grep -s address /etc/network/interfaces /etc/network/interfaces.d/*.cfg);
do
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
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 -force route del default || true
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 blocked "Incorrect gateway IP."
fi
# copy metric 0 routes to routes with metric 1
ip route show | grep "^default" | grep -v metric | while read line; do ip route append $line metric 1 || true; done
# delete metric 0 routes
ip route show | grep "^default" | grep -v metric | while read line; do ip route del $line metric 0; done
else # release is not less or equal than trusty
# copy metric 0 routes to routes with metric 1
ip route show default metric 0 | while read line; do ip route append $line metric 1 || true; done
# delete metric 0 routes
ip route show default metric 0 | while read line; do ip route del $line metric 0; done
# add our route
if ip route replace default via $GATEWAY_IP metric 0; then
juju-log "Default gateway set"
status-set active "Unit is ready"
else
juju-log "Route change failed. Exiting..."
status-set blocked "Incorrect gateway IP."
fi
fi
# add our route
if ip route replace default via $GATEWAY_IP metric 0; then
juju-log "Default gateway set"
status-set active "Unit is ready"
else
juju-log "Route change failed. Exiting..."
status-set blocked "Incorrect gateway IP."
fi
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment