#!/usr/bin/env bash

# Script to take control of a previously defined VIP by connecting to the
# current VIP location and destroying it. Then create the VIP locally and
# then promote the current node to Primary status.

VIP=10.0.30.50
IFACE=eth0
PATH=/usr/pgsql-12/bin:$PATH
VIP_CONF="${VIP}/32 dev ${IFACE} label ${IFACE}:pg"

ssh -o StrictHostKeyChecking=no \
    -o UserKnownHostsFile=/dev/null \
    -o ConnectTimeout=5 \
    -o ServerAliveInterval=1 \
    postgres@${VIP} sudo ip addr del ${VIP_CONF}

sudo /sbin/ip addr add ${VIP_CONF}
sudo /usr/sbin/arping -b -A -c 3 -I ${IFACE} ${VIP}

repmgr standby promote
exit 0
