From aebb2fb0e7561b64709abebc5ea940eb4815f999 Mon Sep 17 00:00:00 2001 From: Claudio Pisa <claudio.pisa@garr.it> Date: Mon, 4 Feb 2019 14:41:12 +0100 Subject: [PATCH] 2019-02-04: CP;AC; update to bionic (which is not using ifupdown) --- hooks/install | 87 ++++++++++++++++++++++++++++++++------------------- metadata.yaml | 1 + 2 files changed, 55 insertions(+), 33 deletions(-) diff --git a/hooks/install b/hooks/install index fb36b6e..1dbbed4 100644 --- a/hooks/install +++ b/hooks/install @@ -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 diff --git a/metadata.yaml b/metadata.yaml index aaee11c..65e741f 100644 --- a/metadata.yaml +++ b/metadata.yaml @@ -7,6 +7,7 @@ description: | tags: - network series: + - bionic - xenial - trusty requires: -- GitLab