Skip to content
Snippets Groups Projects
Commit aebb2fb0 authored by Claudio Pisa's avatar Claudio Pisa Committed by Alberto Colla
Browse files

2019-02-04: CP;AC; update to bionic (which is not using ifupdown)

parent 568dfc4b
No related branches found
No related tags found
No related merge requests found
......@@ -3,6 +3,7 @@
# 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"
GATEWAY="$(config-get gateway)"
......@@ -14,44 +15,64 @@ if [ -z "$GATEWAY" ]; then
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."
exit 0
fi
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
apt install -y sipcalc
GATEWAY_NET=`sipcalc $GATEWAY | grep "Network address" | awk '{print $NF}'`
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
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
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 blocked "Incorrect gateway IP."
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
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
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 blocked "Incorrect gateway IP."
fi
......@@ -7,6 +7,7 @@ description: |
tags:
- network
series:
- bionic
- xenial
- trusty
requires:
......
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