Skip to content

Instantly share code, notes, and snippets.

@chrisboulton
Created May 27, 2013 07:01
Show Gist options
  • Select an option

  • Save chrisboulton/5655559 to your computer and use it in GitHub Desktop.

Select an option

Save chrisboulton/5655559 to your computer and use it in GitHub Desktop.
#!/bin/sh
set -x
REDIS_CLI="redis-cli"
REDIS_SERVER="/usr/bin/redis-server"
START_STOP="/sbin/start-stop-daemon"
E_INVALID_ARGS=65
E_INVALID_COMMAND=66
E_NO_SLAVES=67
E_DB_PROBLEM=68
E_BAD_CONFIG=69
SYSLOG_TAG="keepalived-redis"
SYSLOG_PRI="user.notice"
error() {
echo "Exiting: ERROR $1: $2"
exit $1
}
get_pidfile() {
config_file=$1
local pidfile=`grep -E "^\s*pidfile " $config_file | cut -d' ' -f2`
echo $pidfile
}
get_port() {
config_file=$1
local port=`grep -E "^\s*port " $config_file | cut -d' ' -f2`
echo $port
}
output() {
echo $@
echo $@ | logger -t $SYSLOG_TAG -p $SYSLOG_PRI
}
# start_redis $config
start_redis() {
config_file=$1
port=`get_port $config_file`
alive=`$REDIS_CLI -p $port INFO`
if [ $? -ne 0 ]; then
$START_STOP --start --quiet --umask 007 --pidfile `get_pidfile ${config_file}` --chuid redis:redis --exec $REDIS_SERVER -- $config_file
sleep 1
fi
}
start_master() {
config_file=$1
port=`get_port $config_file`
$REDIS_CLI -p $port SLAVEOF no one
}
start_slave() {
config_file=$1
master_ip=$2
master_port=$3
port=`get_port $config_file`
$REDIS_CLI -p $port SLAVEOF $master_ip $master_port
}
check_health() {
config_file=$1
port=`get_port $config_file`
alive=`$REDIS_CLI -p $port INFO`
if [ $? -ne 0 ]; then
local result=1
else
local result=0
fi
echo $result
}
usage() {
echo "Start Redis and promote to MASTER/SLAVE"
echo "\nOptions: "
echo "\t--master -c=REDIS_CONFIG promote the redis-server to MASTER using the supplied redis.conf at REDIS_CONFIG"
echo "\t--slave -c=REDIS_CONFIG -h=IP:PORT promote the redis-server to SLAVE of IP:PORT using the supplied redis.conf at REDIS_CONFIG"
echo "\t--health -c=REDIS_CONFIG check the health of the redis-server process configured for REDIS_CONFIG"
echo "\t--fault -c=REDIS_CONFIG put the redis at REDIS_CONFIG in to a fault state"
echo ""
exit $E_INVALID_ARGS
}
for arg in "$@"; do
case $arg in
--master) arg_m=true;;
--slave) arg_s=true;;
--fault) arg_k=true;;
--health) arg_h=true;;
--config=*) arg_config=${arg#*=};;
--host=*) arg_host=${arg#*=};;
*) usage;;
esac
done
if [ $arg_m ]; then
if [ "$arg_config" == "" ] || [ ! -f $arg_config ]; then
echo "Must pass --config with the path to a redis configuration file"
exit $E_INVALID_ARGS
fi
if [ "`get_port $arg_config`" == "" ]; then
error $E_BAD_CONFIG "Could not find redis port in ${arg_config}"
fi
output "Promoting redis-server to MASTER using ${arg_config}\n"
start_redis $arg_config
wait
start_master $arg_config
elif [ $arg_h ]; then
if [ "$arg_config" == "" ] || [ ! -f $arg_config ]; then
echo "Must pass --config with the path to a redis configuration file"
exit $E_INVALID_ARGS
fi
if [ "`get_port $arg_config`" == "" ]; then
error $E_BAD_CONFIG "Could not find redis port in ${arg_config}"
fi
output "Checking health of redis using ${arg_config}\n"
result=`check_health $arg_config`
if [ $result -eq 0 ]; then
output "Redis with ${arg_config} is running"
exit 0
else
output "Redis with ${arg_config} is not running"
exit 1
fi
elif [ $arg_f ]; then
if [ "$arg_config" == "" ] || [ ! -f $arg_config ]; then
echo "Must pass --config with the path to a redis configuration file"
exit $E_INVALID_ARGS
fi
if [ "`get_port $arg_config`" == "" ]; then
error $E_BAD_CONFIG "Could not find redis port in ${arg_config}"
fi
output "Putting redis-server using ${arg_config} in to FAULT state"
elif [ $arg_s ]; then
if [ "$arg_config" == "" ] || [ ! -f $arg_config ]; then
echo "Must pass --config with the path to a redis configuration file"
exit $E_INVALID_ARGS
fi
if [ "`get_port $arg_config`" == "" ]; then
error $E_BAD_CONFIG "Could not find redis port in ${arg_config}"
fi
master_ip=`echo $arg_host | cut -d: -f1`
master_port=`echo $arg_host | cut -d: -f2`
if [ "$master_ip" == "" ] || [ "$master_port" == "" ]; then
echo "Must pass --host as IP:PORT combination of redis master"
exit $E_INVALID_ARGS
fi
output "Promoting redis-server to SLAVE of ${arg_host} using ${arg_config}"
start_redis $arg_config
wait
start_slave $arg_config $master_ip $master_port
else
usage
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment