78ee1b
#!/bin/bash
78ee1b
#
78ee1b
# Wrapper to close properly redis and sentinel
78ee1b
test x"$REDIS_DEBUG" != x && set -x
78ee1b
78ee1b
REDIS_CLI=/usr/bin/redis-cli
78ee1b
78ee1b
# Retrieve service name
78ee1b
SERVICE_NAME="$1"
78ee1b
if [ -z "$SERVICE_NAME" ]; then
78ee1b
   SERVICE_NAME=redis
78ee1b
fi
78ee1b
78ee1b
# Get the proper config file based on service name
78ee1b
CONFIG_FILE="/etc/$SERVICE_NAME.conf"
78ee1b
78ee1b
# Use awk to retrieve host, port from config file
78ee1b
HOST=`awk '/^[[:blank:]]*bind/ { print $2 }' $CONFIG_FILE | tail -n1`
78ee1b
PORT=`awk '/^[[:blank:]]*port/ { print $2 }' $CONFIG_FILE | tail -n1`
78ee1b
PASS=`awk '/^[[:blank:]]*requirepass/ { print $2 }' $CONFIG_FILE | tail -n1`
78ee1b
SOCK=`awk '/^[[:blank:]]*unixsocket\s/ { print $2 }' $CONFIG_FILE | tail -n1`
78ee1b
78ee1b
# Just in case, use default host, port
78ee1b
HOST=${HOST:-127.0.0.1}
78ee1b
if [ "$SERVICE_NAME" = redis ]; then
78ee1b
    PORT=${PORT:-6379}
78ee1b
else
78ee1b
    PORT=${PORT:-26739}
78ee1b
fi
78ee1b
78ee1b
# Setup additional parameters
78ee1b
# e.g password-protected redis instances
78ee1b
[ -z "$PASS"  ] || ADDITIONAL_PARAMS="-a $PASS"
78ee1b
78ee1b
# shutdown the service properly
78ee1b
if [ -e "$SOCK" ] ; then
78ee1b
	$REDIS_CLI -s $SOCK $ADDITIONAL_PARAMS shutdown
78ee1b
else
78ee1b
	$REDIS_CLI -h $HOST -p $PORT $ADDITIONAL_PARAMS shutdown
78ee1b
fi