Skip to content

Instantly share code, notes, and snippets.

@rhoml
Last active August 29, 2015 14:07
Show Gist options
  • Save rhoml/fecb88ce4500ef22b05f to your computer and use it in GitHub Desktop.
Save rhoml/fecb88ce4500ef22b05f to your computer and use it in GitHub Desktop.
Reconfigure script for Sentinel and Twemproxy
#!/bin/bash
# Simple reconfigure script to modify multiple nutcracker(twemproxy) servers in case of Redis
# Failover.
export PATH=/sbin:/usr/local/bin:$PATH
NAME=sentinel
LOGFILE=/opt/sentinel/logs/reconfigure.log
ERRORLOGFILE=/opt/sentinel/logs/reconfigure.error.log
NUTCRACKER_CONF=/opt/nutcracker/etc/nutcracker.yml
NUTCRACKERFAILOVER_CONF=/opt/nutcracker/etc/nutcrackerfailover.yml
NUTCRACKER_PORTs='22121,22123'
MASTER_NAME=$1
ROLE=$2
STATE=$3
FROM_IP=$4
FROM_PORT=$5
TO_IP=$6
TO_PORT=$7
date="$(date -R)"
readonly PROGNAME=sentinel
readonly LOCKFILE_DIR=/tmp
readonly LOCK_FD=200
lock() {
local prefix=$1
local fd=${2:-$LOCK_FD}
local lock_file=$LOCKFILE_DIR/$prefix.lock
# create lock file
eval "exec $fd>$lock_file"
# acquier the lock
flock -n $fd \
&& return 0 \
|| return 1
}
eexit() {
local error_str="$@"
echo $error_str >> ${ERRORLOGFILE}
exit 1
}
do_nutcracker_reconfig() {
local old_master_ip=$1
local old_master_port=$2
local new_master_ip=$3
local new_master_port=$4
local nutcracker_yml=$5
sed -i s/${old_master_ip}:${old_master_port}/${new_master_ip}:${new_master_port}/g ${nutcracker_yml}
}
do_pause_traffic() {
local destination_ports=$1
iptables -I INPUT -p tcp -m multiport --dports ${destination_ports} --syn -j DROP
}
do_release_traffic() {
local destination_ports=$1
iptables -D INPUT -p tcp -m multiport --dports ${destination_port} --syn -j DROP
}
do_reload_nutcracker() {
local service=$1
local action=$2
/etc/init.d/$service $action > /dev/null 2>&1
}
check_lock() {
while : ; do
[[ -f "/tmp/${PROGNAME}.lock" ]] && break
lock $PROGNAME \
|| eexit "Only one instance of $PROGNAME can run at one time."
sleep 0.5
done
}
main() {
check_lock
echo "${date} NUTCRACKER ${MASTER_NAME} PHASE 01: failover triggered from ${FROM_IP}:${FROM_PORT} > ${TO_IP}:${TO_PORT} " >> ${LOGFILE}
do_nutcracker_reconfig $FROM_IP $FROM_PORT $TO_IP $TO_PORT $NUTCRACKER_CONF
echo "${date} NUTCRACKER-FAILOVER ${MASTER_NAME} PHASE 02: failover triggered from ${FROM_IP}:${FROM_PORT} > ${TO_IP}:${TO_PORT} " >> ${LOGFILE}
do_nutcracker_reconfig $FROM_IP $FROM_PORT $TO_IP $TO_PORT $NUTCRACKERFAILOVER_CONF
do_pause_traffic ${NUTCRACKER_PORTS}
echo "${date} ${MASTER_NAME} PHASE 03: restarting Nutcracker " >> ${LOGFILE}
do_reload_nutcracker nutcracker restart
echo "${date} ${MASTER_NAME} PHASE 04: restarting Nutcracker FAILOVER " >> ${LOGFILE}
do_reload_nutcracker nutcrackerfailover restart
do_release_traffic ${NUTCRACKER_PORT}
echo "${date} ${MASTER_NAME} Failover ended." >> ${LOGFILE}
# Unlock script
rm -rf /tmp/${PROGNAME}.lock
exit 0
}
main
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment