diff --git a/hooks/install b/hooks/install index 227d4df879fd4c82eae9e4384463498887e52ddb..9d1e072059ae7baf09c6774d4ce308ad83fe571c 100644 --- a/hooks/install +++ b/hooks/install @@ -3,6 +3,9 @@ # e.g. apt install -y foo # Make sure this hook exits cleanly and is idempotent. set -x + +source inc/common + status-set maintenance "Setting the default gateway" GATEWAY="$( config-get gateway )" @@ -15,29 +18,16 @@ fi GATEWAY_IP="$( echo "$GATEWAY" | awk -F "/" '{print $1}' )" -source /etc/lsb-release - -RELEASE_MAJOR="$( echo $DISTRIB_RELEASE | awk -F '.' '{print $1}' )" - -if [ "$RELEASE_MAJOR" -le "16" ]; then - # Ubuntu release is less or equal than trusty - defaultroutecommand="ip route show | grep ^default | grep -v metric" -else - defaultroutecommand="ip route show default metric 0" -fi +DEFAULTROUTECOMMAND="$( getdefaultroutecommand )" # copy metric 0 routes to routes with metric 1 -eval $defaultroutecommand | while read line; do - ip route append $line metric 1 || true -done +copy0metricroutes "$DEFAULTROUTECOMMAND" # delete metric 0 routes -eval $defaultroutecommand | while read line; do - ip route del $line metric 0 -done +delete0metricroutes "$DEFAULTROUTECOMMAND" # add our route -if ip route replace default via $GATEWAY_IP metric 0; then +if setdefaultgateway "$GATEWAY_IP"; then juju-log "Default gateway set" status-set active "Unit is ready" else diff --git a/hooks/update-status b/hooks/update-status new file mode 100644 index 0000000000000000000000000000000000000000..d38f17bf6f1aeee9a3288d3184da2ef15a4aba80 --- /dev/null +++ b/hooks/update-status @@ -0,0 +1,43 @@ +#!/bin/bash +# Here do anything needed to install the service +# e.g. apt install -y foo +# Make sure this hook exits cleanly and is idempotent. +set -x + +source inc/common + +GATEWAY="$( config-get gateway )" + +if [ -z "$GATEWAY" ]; then + juju-log "Gateway option is currently empty." + exit 0 +fi + +GATEWAY_IP="$( echo "$GATEWAY" | awk -F "/" '{print $1}' )" + +DEFAULTROUTECOMMAND="$( getdefaultroutecommand )" + +if eval $DEFAULTROUTECOMMAND | grep "\<${GATEWAY_IP}\>"; then + juju-log "Default gateway looks OK. Doing nothing." + exit 0 +fi + +juju-log "Default gateway doesn't look good. Setting the default gateway.." + +status-set maintenance "Setting the default gateway" + +# copy metric 0 routes to routes with metric 1 +copy0metricroutes "$DEFAULTROUTECOMMAND" + +# delete metric 0 routes +delete0metricroutes "$DEFAULTROUTECOMMAND" + +# add our route +if setdefaultgateway "$GATEWAY_IP"; 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 + diff --git a/inc/common b/inc/common new file mode 100644 index 0000000000000000000000000000000000000000..c6a96e669af43610a88a97f659df12e486f22c63 --- /dev/null +++ b/inc/common @@ -0,0 +1,38 @@ +# common functions + +getdefaultroutecommand() { + source /etc/lsb-release + + RELEASE_MAJOR="$( echo $DISTRIB_RELEASE | awk -F '.' '{print $1}' )" + + if [ "$RELEASE_MAJOR" -le "16" ]; then + # Ubuntu release is less or equal than trusty + DEFAULTROUTECOMMAND="ip route show | grep ^default | grep -v metric" + else + DEFAULTROUTECOMMAND="ip route show default metric 0" + fi + + echo "$DEFAULTROUTECOMMAND" +} + +copy0metricroutes() { + # copy metric 0 routes to routes with metric 1 + DEFAULTROUTECOMMAND="$1" + eval $DEFAULTROUTECOMMAND | while read line; do + ip route append $line metric 1 || true + done +} + +delete0metricroutes() { + # delete metric 0 routes + DEFAULTROUTECOMMAND="$1" + eval $DEFAULTROUTECOMMAND | while read line; do + ip route del $line metric 0 + done +} + +setdefaultgateway() { + GATEWAY_IP="$1" + ip route replace default via $GATEWAY_IP metric 0 +} +