Setup virtual IP address using Pre- and Post scripts
It is possible to specify a pre- and a post script when a switch of the master/slave functionality is issued. The absolute path to these these scripts can be configured in eranger-replication.cfg
... ## scripts to be executed before and after switch [scripts] pre_script=/opt/eranger/sbin/vip-switch.sh post_script=/opt/eranger/sbin/vip-switch.sh # pre_timeout=30 # post_timeout=30
Above script does not belong to the SKOOR software distribution. However, for convenience, there is an example provided below.
One can provide the very same script for the pre_script and the post_script option, such as is the case in the example above. Pre-/Post-scripts will be terminated if they don't exit within the specified timeout (units are in seconds).
Use case:
Bind and unbind a virtual high availability IP address to the current masters network interface when switching master/slave roles. When a switch is issued on the current slave, the pre- and post-scripts are run in the following order:
Script | on system | action |
pre_script | current slave | do nothing |
pre_script | current master | unbind the HA-IP-address |
core work of eranger-server-replication.pl |
| perform the actual switch of the master/slave functionality |
post_script | new master | bind the HA-IP-address 10.1.0.90 |
post_script | new slave | do nothing |
In the above example 10.1.0.90 represents the virtual HA-IP address.
If the master is not reachable from the slave, pre- and post-scripts will be omitted on the old master.
Below example pre/post-script /opt/eranger/sbin/vip-switch.sh binds/unbinds the HA-IP address 10.1.0.90 (netmask 255.255.254.0) to/from the network interface eth0 on the current master.
#!/bin/bash
#
# vip-switch.sh
#
# A script to configure virtual hosting on the WTC eRanger platform
#
# Versions: 2016-02-09 / Port from FreeBSD to RHEL
#
# Arguments:
# $1 -> IP address of new master (not used here)
# $2 -> mode (pre/post)
# $3 -> switch direction (m2s/s2m)
#
# Functionality:
# m2s mode -> remove ip ${VIP} from interface ${BASE_INT} in pre
# -> do nothing in post
#
# s2m mode -> do nothing in pre
# -> add ip ${VIP} as alias to interface ${BASE_INT} in post
#
## CONFIG ##
VIP="10.1.0.90"
VWP="${VIP}/25"
NETMASK="255.255.255.128"
BASE_INT="eth0"
ALIAS_INT="eth0:0"
CONFFILE="/etc/sysconfig/network-scripts/ifcfg-${ALIAS_INT}"
## CONSTS ##
IF="/sbin/ip"
GREP="/bin/grep"
PING="/bin/ping"
## METHODS ##
function usage {
echo "Usage: $0 <master> <pre/post> <mode>"
echo " master : the ip address of the new master"
echo " pre/post: in which phase to run the script"
echo " mode : either \"m2s\" or \"s2m\""
exit 1
}
## CHECKS ##
for b in ${IF} ${GREP} ${PING}; do
if [ ! -x ${b} ]; then
echo "ERR> ${b} not available, exiting..."
exit 1
fi
done
if [ "x${VIP}" == "x" -o "x${BASE_INT}" == "x" ]; then
echo "ERR> Necessary config missing, exiting..."
exit 1
fi
${IF} addr list ${BASE_INT} &>/dev/null
if [ $? -ne 0 ]; then
echo "ERR> Base interface ${BASE_INT} is not available, exiting..."
exit 1
fi
# parse args
pp=$2
mode=$3
if [ "x${pp}" != "xpre" -a "x${pp}" != "xpost" ]; then
usage
fi
if [ "x${mode}" != "xs2m" -a "x${mode}" != "xm2s" ]; then
usage
fi
if [ ${mode} == "s2m" ]; then
# script is running on new master
if [ "x${pp}" == "xpre" ]; then
# only doing stuff in post
exit 0
fi
# safety check
${IF} addr list ${BASE_INT} 2>/dev/null | ${GREP} -q "inet ${VWP}" &>/dev/null
if [ $? -eq 0 ]; then
echo "OK> VIP ${VIP} is already defined on ${BASE_INT}"
exit 0
fi
# is VIP up on slave?
${PING} -c 1 -n -W 2 ${VIP} &>/dev/null
if [ $? -eq 0 ]; then
# ip is still pingable
echo "ERR> ${VIP} is still up on slave, exiting"
exit 1
fi
# we are new master, VIP is down -> add VIP
${IF} addr add ${VWP} dev ${BASE_INT} label ${ALIAS_INT} &>/dev/null
if [ $? -ne 0 ]; then
echo "ERR> Failed to add IP ${VIP} to ${BASE_INT}"
exit 1
else
echo "OK> ${VIP} added to ${BASE_INT}."
# need to create CONFFILE in case server is rebooted -> VIP would be lost then
cat << EOF > ${CONFFILE}
DEVICE=${ALIAS_INT}
IPADDR=${VIP}
NETMASK=${NETMASK}
EOF
if [ $? -eq 0 ]; then
echo "OK> ${VIP} configured in ${CONFFILE}."
else
echo "ERR> ${VIP} not configured in ${CONFFILE}."
fi
exit 0
fi
else
# script is running on new slave
if [ "x${pp}" == "xpost" ]; then
# nothing to do
exit 0
fi
# safety check
${IF} addr list ${BASE_INT} 2>/dev/null | ${GREP} -q "inet ${VWP}" &>/dev/null
if [ $? -ne 0 ]; then
echo "OK> VIP ${VIP} is not defined on ${BASE_INT}"
exit 0
fi
# eranger is about to be switched -> remove VIP
${IF} addr del ${VWP} dev ${BASE_INT} label ${ALIAS_INT} &>/dev/null
if [ $? -ne 0 ]; then
echo "ERR> Failed to remove IP ${VIP} from ${BASE_INT}"
exit 1
else
echo "OK> ${VIP} removed from ${BASE_INT}"
# need to remove CONFFILE now
rm -f ${CONFFILE}
exit 0
fi
fi
exit 0
The pre and post scripts are always called with the following three arguments:
$1 | IP address of new master |
$2 | pattern "pre" or "post" in order to determine if the script should do pre- or post-tasks. |
$3 | pattern "m2s" or "s2m" with the meaning: m2s host on which this script runs should be converted from master to slave |