1#!/bin/sh 2 3# Copyright 2011 Dvir Volk <dvirsk at gmail dot com>. All rights reserved. 4# 5# Redistribution and use in source and binary forms, with or without 6# modification, are permitted provided that the following conditions are met: 7# 8# 1. Redistributions of source code must retain the above copyright notice, 9# this list of conditions and the following disclaimer. 10# 11# 2. Redistributions in binary form must reproduce the above copyright 12# notice, this list of conditions and the following disclaimer in the 13# documentation and/or other materials provided with the distribution. 14# 15# THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED 16# WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF 17# MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO 18# EVENT SHALL Dvir Volk OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, 19# INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 20# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, 21# OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF 22# LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING 23# NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, 24# EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 25# 26################################################################################ 27# 28# Interactive service installer for redis server 29# this generates a redis config file and an /etc/init.d script, and installs them 30# this scripts should be run as root 31 32die () { 33 echo "ERROR: $1. Aborting!" 34 exit 1 35} 36 37 38#Absolute path to this script 39SCRIPT=$(readlink -f $0) 40#Absolute path this script is in 41SCRIPTPATH=$(dirname $SCRIPT) 42 43#Initial defaults 44_REDIS_PORT=6379 45 46echo "Welcome to the redis service installer" 47echo "This script will help you easily set up a running redis server" 48echo 49 50#check for root user 51if [ "$(id -u)" -ne 0 ] ; then 52 echo "You must run this script as root. Sorry!" 53 exit 1 54fi 55 56#Read the redis port 57read -p "Please select the redis port for this instance: [$_REDIS_PORT] " REDIS_PORT 58if ! echo $REDIS_PORT | egrep -q '^[0-9]+$' ; then 59 echo "Selecting default: $_REDIS_PORT" 60 REDIS_PORT=$_REDIS_PORT 61fi 62 63#read the redis config file 64_REDIS_CONFIG_FILE="/etc/redis/$REDIS_PORT.conf" 65read -p "Please select the redis config file name [$_REDIS_CONFIG_FILE] " REDIS_CONFIG_FILE 66if [ -z "$REDIS_CONFIG_FILE" ] ; then 67 REDIS_CONFIG_FILE=$_REDIS_CONFIG_FILE 68 echo "Selected default - $REDIS_CONFIG_FILE" 69fi 70 71#read the redis log file path 72_REDIS_LOG_FILE="/var/log/redis_$REDIS_PORT.log" 73read -p "Please select the redis log file name [$_REDIS_LOG_FILE] " REDIS_LOG_FILE 74if [ -z "$REDIS_LOG_FILE" ] ; then 75 REDIS_LOG_FILE=$_REDIS_LOG_FILE 76 echo "Selected default - $REDIS_LOG_FILE" 77fi 78 79 80#get the redis data directory 81_REDIS_DATA_DIR="/var/lib/redis/$REDIS_PORT" 82read -p "Please select the data directory for this instance [$_REDIS_DATA_DIR] " REDIS_DATA_DIR 83if [ -z "$REDIS_DATA_DIR" ] ; then 84 REDIS_DATA_DIR=$_REDIS_DATA_DIR 85 echo "Selected default - $REDIS_DATA_DIR" 86fi 87 88#get the redis executable path 89_REDIS_EXECUTABLE=`command -v redis-server` 90read -p "Please select the redis executable path [$_REDIS_EXECUTABLE] " REDIS_EXECUTABLE 91if [ ! -x "$REDIS_EXECUTABLE" ] ; then 92 REDIS_EXECUTABLE=$_REDIS_EXECUTABLE 93 94 if [ ! -x "$REDIS_EXECUTABLE" ] ; then 95 echo "Mmmmm... it seems like you don't have a redis executable. Did you run make install yet?" 96 exit 1 97 fi 98fi 99 100#check the default for redis cli 101CLI_EXEC=`command -v redis-cli` 102if [ -z "$CLI_EXEC" ] ; then 103 CLI_EXEC=`dirname $REDIS_EXECUTABLE`"/redis-cli" 104fi 105 106echo "Selected config:" 107 108echo "Port : $REDIS_PORT" 109echo "Config file : $REDIS_CONFIG_FILE" 110echo "Log file : $REDIS_LOG_FILE" 111echo "Data dir : $REDIS_DATA_DIR" 112echo "Executable : $REDIS_EXECUTABLE" 113echo "Cli Executable : $CLI_EXEC" 114 115read -p "Is this ok? Then press ENTER to go on or Ctrl-C to abort." _UNUSED_ 116 117mkdir -p `dirname "$REDIS_CONFIG_FILE"` || die "Could not create redis config directory" 118mkdir -p `dirname "$REDIS_LOG_FILE"` || die "Could not create redis log dir" 119mkdir -p "$REDIS_DATA_DIR" || die "Could not create redis data directory" 120 121#render the templates 122TMP_FILE="/tmp/${REDIS_PORT}.conf" 123DEFAULT_CONFIG="${SCRIPTPATH}/../redis.conf" 124INIT_TPL_FILE="${SCRIPTPATH}/redis_init_script.tpl" 125INIT_SCRIPT_DEST="/etc/init.d/redis_${REDIS_PORT}" 126PIDFILE="/var/run/redis_${REDIS_PORT}.pid" 127 128if [ ! -f "$DEFAULT_CONFIG" ]; then 129 echo "Mmmmm... the default config is missing. Did you switch to the utils directory?" 130 exit 1 131fi 132 133#Generate config file from the default config file as template 134#changing only the stuff we're controlling from this script 135echo "## Generated by install_server.sh ##" > $TMP_FILE 136 137read -r SED_EXPR <<-EOF 138s#^port [0-9]{4}\$#port ${REDIS_PORT}#; \ 139s#^logfile .+\$#logfile ${REDIS_LOG_FILE}#; \ 140s#^dir .+\$#dir ${REDIS_DATA_DIR}#; \ 141s#^pidfile .+\$#pidfile ${PIDFILE}#; \ 142s#^daemonize no\$#daemonize yes#; 143EOF 144sed -r "$SED_EXPR" $DEFAULT_CONFIG >> $TMP_FILE 145 146#cat $TPL_FILE | while read line; do eval "echo \"$line\"" >> $TMP_FILE; done 147cp $TMP_FILE $REDIS_CONFIG_FILE || die "Could not write redis config file $REDIS_CONFIG_FILE" 148 149#Generate sample script from template file 150rm -f $TMP_FILE 151 152#we hard code the configs here to avoid issues with templates containing env vars 153#kinda lame but works! 154REDIS_INIT_HEADER=\ 155"#!/bin/sh\n 156#Configurations injected by install_server below....\n\n 157EXEC=$REDIS_EXECUTABLE\n 158CLIEXEC=$CLI_EXEC\n 159PIDFILE=\"$PIDFILE\"\n 160CONF=\"$REDIS_CONFIG_FILE\"\n\n 161REDISPORT=\"$REDIS_PORT\"\n\n 162###############\n\n" 163 164REDIS_CHKCONFIG_INFO=\ 165"# REDHAT chkconfig header\n\n 166# chkconfig: - 58 74\n 167# description: redis_${REDIS_PORT} is the redis daemon.\n 168### BEGIN INIT INFO\n 169# Provides: redis_6379\n 170# Required-Start: \$network \$local_fs \$remote_fs\n 171# Required-Stop: \$network \$local_fs \$remote_fs\n 172# Default-Start: 2 3 4 5\n 173# Default-Stop: 0 1 6\n 174# Should-Start: \$syslog \$named\n 175# Should-Stop: \$syslog \$named\n 176# Short-Description: start and stop redis_${REDIS_PORT}\n 177# Description: Redis daemon\n 178### END INIT INFO\n\n" 179 180if command -v chkconfig >/dev/null; then 181 #if we're a box with chkconfig on it we want to include info for chkconfig 182 echo "$REDIS_INIT_HEADER" "$REDIS_CHKCONFIG_INFO" > $TMP_FILE && cat $INIT_TPL_FILE >> $TMP_FILE || die "Could not write init script to $TMP_FILE" 183else 184 #combine the header and the template (which is actually a static footer) 185 echo "$REDIS_INIT_HEADER" > $TMP_FILE && cat $INIT_TPL_FILE >> $TMP_FILE || die "Could not write init script to $TMP_FILE" 186fi 187 188### 189# Generate sample script from template file 190# - No need to check which system we are on. The init info are comments and 191# do not interfere with update_rc.d systems. Additionally: 192# Ubuntu/debian by default does not come with chkconfig, but does issue a 193# warning if init info is not available. 194 195cat > ${TMP_FILE} <<EOT 196#!/bin/sh 197#Configurations injected by install_server below.... 198 199EXEC=$REDIS_EXECUTABLE 200CLIEXEC=$CLI_EXEC 201PIDFILE=$PIDFILE 202CONF="$REDIS_CONFIG_FILE" 203REDISPORT="$REDIS_PORT" 204############### 205# SysV Init Information 206# chkconfig: - 58 74 207# description: redis_${REDIS_PORT} is the redis daemon. 208### BEGIN INIT INFO 209# Provides: redis_${REDIS_PORT} 210# Required-Start: \$network \$local_fs \$remote_fs 211# Required-Stop: \$network \$local_fs \$remote_fs 212# Default-Start: 2 3 4 5 213# Default-Stop: 0 1 6 214# Should-Start: \$syslog \$named 215# Should-Stop: \$syslog \$named 216# Short-Description: start and stop redis_${REDIS_PORT} 217# Description: Redis daemon 218### END INIT INFO 219 220EOT 221cat ${INIT_TPL_FILE} >> ${TMP_FILE} 222 223#copy to /etc/init.d 224cp $TMP_FILE $INIT_SCRIPT_DEST && \ 225 chmod +x $INIT_SCRIPT_DEST || die "Could not copy redis init script to $INIT_SCRIPT_DEST" 226echo "Copied $TMP_FILE => $INIT_SCRIPT_DEST" 227 228#Install the service 229echo "Installing service..." 230if command -v chkconfig >/dev/null 2>&1; then 231 # we're chkconfig, so lets add to chkconfig and put in runlevel 345 232 chkconfig --add redis_${REDIS_PORT} && echo "Successfully added to chkconfig!" 233 chkconfig --level 345 redis_${REDIS_PORT} on && echo "Successfully added to runlevels 345!" 234elif command -v update-rc.d >/dev/null 2>&1; then 235 #if we're not a chkconfig box assume we're able to use update-rc.d 236 update-rc.d redis_${REDIS_PORT} defaults && echo "Success!" 237else 238 echo "No supported init tool found." 239fi 240 241/etc/init.d/redis_$REDIS_PORT start || die "Failed starting service..." 242 243#tada 244echo "Installation successful!" 245exit 0 246