#!/bin/bash
# This script is run by wd_escalation_command to bring down the virtual IP on other pgpool nodes
# before bringing up the virtual IP on the new active pgpool node.

set -o xtrace

POSTGRESQL_STARTUP_USER=postgres
SSH_KEY_FILE=id_rsa_pgpool
SSH_OPTIONS="-o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null -i ~/.ssh/${SSH_KEY_FILE}"
SSH_TIMEOUT=5
PGPOOLS=(server1 server2 server3)

VIP=192.168.100.50
DEVICE=enp0s8
CIDR_NETMASK=24

for pgpool in "${PGPOOLS[@]}"; do
    [ "$HOSTNAME" = "${pgpool}" ] && continue

    timeout ${SSH_TIMEOUT} ssh -T ${SSH_OPTIONS} ${POSTGRESQL_STARTUP_USER}@${pgpool} "
        /sbin/ip addr show dev ${DEVICE} | grep ${VIP} > /dev/null 2>&1
    "

    if [ $? -eq 0 ]; then
        timeout ${SSH_TIMEOUT} ssh -T ${SSH_OPTIONS} ${POSTGRESQL_STARTUP_USER}@${pgpool} "
            /usr/bin/sudo /sbin/ip addr del ${VIP}/${CIDR_NETMASK} dev ${DEVICE}
        "

        if [ $? -ne 0 ]; then
            echo ERROR: escalation.sh: failed to release VIP on ${pgpool}.
        fi
    fi
done
exit 0
