diff --git a/hooks/install b/hooks/install index a35229fa4f6a2f90a11d79e6f9ca1de75f76f02c..d96661fe35133408ce5c368f6d4eab2806762175 100644 --- a/hooks/install +++ b/hooks/install @@ -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