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#Initial defaults 38_REDIS_PORT=6379 39 40echo "Welcome to the redis service installer" 41echo "This script will help you easily set up a running redis server 42 43" 44 45#check for root user TODO: replace this with a call to "id" 46if [ `whoami` != "root" ] ; then 47 echo "You must run this script as root. Sorry!" 48 exit 1 49fi 50 51 52#Read the redis port 53read -p "Please select the redis port for this instance: [$_REDIS_PORT] " REDIS_PORT 54if [ ! `echo $REDIS_PORT | egrep "^[0-9]+\$"` ] ; then 55 echo "Selecting default: $_REDIS_PORT" 56 REDIS_PORT=$_REDIS_PORT 57fi 58 59#read the redis config file 60_REDIS_CONFIG_FILE="/etc/redis/$REDIS_PORT.conf" 61read -p "Please select the redis config file name [$_REDIS_CONFIG_FILE] " REDIS_CONFIG_FILE 62if [ !"$REDIS_CONFIG_FILE" ] ; then 63 REDIS_CONFIG_FILE=$_REDIS_CONFIG_FILE 64 echo "Selected default - $REDIS_CONFIG_FILE" 65fi 66#try and create it 67mkdir -p `dirname "$REDIS_CONFIG_FILE"` || die "Could not create redis config directory" 68 69#read the redis log file path 70_REDIS_LOG_FILE="/var/log/redis_$REDIS_PORT.log" 71read -p "Please select the redis log file name [$_REDIS_LOG_FILE] " REDIS_LOG_FILE 72if [ !"$REDIS_LOG_FILE" ] ; then 73 REDIS_LOG_FILE=$_REDIS_LOG_FILE 74 echo "Selected default - $REDIS_LOG_FILE" 75fi 76 77 78#get the redis data directory 79_REDIS_DATA_DIR="/var/lib/redis/$REDIS_PORT" 80read -p "Please select the data directory for this instance [$_REDIS_DATA_DIR] " REDIS_DATA_DIR 81if [ !"$REDIS_DATA_DIR" ] ; then 82 REDIS_DATA_DIR=$_REDIS_DATA_DIR 83 echo "Selected default - $REDIS_DATA_DIR" 84fi 85mkdir -p $REDIS_DATA_DIR || die "Could not create redis data directory" 86 87#get the redis executable path 88_REDIS_EXECUTABLE=`which redis-server` 89read -p "Please select the redis executable path [$_REDIS_EXECUTABLE] " REDIS_EXECUTABLE 90if [ ! -f "$REDIS_EXECUTABLE" ] ; then 91 REDIS_EXECUTABLE=$_REDIS_EXECUTABLE 92 93 if [ ! -f "$REDIS_EXECUTABLE" ] ; then 94 echo "Mmmmm... it seems like you don't have a redis executable. Did you run make install yet?" 95 exit 1 96 fi 97 98fi 99 100 101#render the tmplates 102TMP_FILE="/tmp/$REDIS_PORT.conf" 103DEFAULT_CONFIG="../redis.conf" 104INIT_TPL_FILE="./redis_init_script.tpl" 105INIT_SCRIPT_DEST="/etc/init.d/redis_$REDIS_PORT" 106PIDFILE="/var/run/redis_$REDIS_PORT.pid" 107 108 109 110#check the default for redis cli 111CLI_EXEC=`which redis-cli` 112if [ ! "$CLI_EXEC" ] ; then 113 CLI_EXEC=`dirname $REDIS_EXECUTABLE`"/redis-cli" 114fi 115 116#Generate config file from the default config file as template 117#changing only the stuff we're controlling from this script 118echo "## Generated by install_server.sh ##" > $TMP_FILE 119 120SED_EXPR="s#^port [0-9]{4}\$#port ${REDIS_PORT}#;\ 121s#^logfile .+\$#logfile ${REDIS_LOG_FILE}#;\ 122s#^dir .+\$#dir ${REDIS_DATA_DIR}#;\ 123s#^pidfile .+\$#pidfile ${PIDFILE}#;\ 124s#^daemonize no\$#daemonize yes#;" 125echo $SED_EXPR 126sed -r "$SED_EXPR" $DEFAULT_CONFIG >> $TMP_FILE 127 128#cat $TPL_FILE | while read line; do eval "echo \"$line\"" >> $TMP_FILE; done 129cp -f $TMP_FILE $REDIS_CONFIG_FILE || exit 1 130 131#Generate sample script from template file 132rm -f $TMP_FILE 133 134#we hard code the configs here to avoid issues with templates containing env vars 135#kinda lame but works! 136REDIS_INIT_HEADER=\ 137"#/bin/sh\n 138#Configurations injected by install_server below....\n\n 139EXEC=$REDIS_EXECUTABLE\n 140CLIEXEC=$CLI_EXEC\n 141PIDFILE=$PIDFILE\n 142CONF=\"$REDIS_CONFIG_FILE\"\n\n 143REDISPORT=\"$REDIS_PORT\"\n\n 144###############\n\n" 145 146REDIS_CHKCONFIG_INFO=\ 147"# REDHAT chkconfig header\n\n 148# chkconfig: - 58 74\n 149# description: redis_6379 is the redis daemon.\n 150### BEGIN INIT INFO\n 151# Provides: redis_6379\n 152# Required-Start: $network $local_fs $remote_fs\n 153# Required-Stop: $network $local_fs $remote_fs\n 154# Default-Start: 2 3 4 5\n 155# Default-Stop: 0 1 6\n 156# Should-Start: $syslog $named\n 157# Should-Stop: $syslog $named\n 158# Short-Description: start and stop redis_6379\n 159# Description: Redis daemon\n 160### END INIT INFO\n\n" 161 162if [ !`which chkconfig` ] ; then 163 #combine the header and the template (which is actually a static footer) 164 echo $REDIS_INIT_HEADER > $TMP_FILE && cat $INIT_TPL_FILE >> $TMP_FILE || die "Could not write init script to $TMP_FILE" 165else 166 #if we're a box with chkconfig on it we want to include info for chkconfig 167 echo -e $REDIS_INIT_HEADER $REDIS_CHKCONFIG_INFO > $TMP_FILE && cat $INIT_TPL_FILE >> $TMP_FILE || die "Could not write init script to $TMP_FILE" 168fi 169 170#copy to /etc/init.d 171cp -f $TMP_FILE $INIT_SCRIPT_DEST && chmod +x $INIT_SCRIPT_DEST || die "Could not copy redis init script to $INIT_SCRIPT_DEST" 172echo "Copied $TMP_FILE => $INIT_SCRIPT_DEST" 173 174#Install the service 175echo "Installing service..." 176if [ !`which chkconfig` ] ; then 177 #if we're not a chkconfig box assume we're able to use update-rc.d 178 update-rc.d redis_$REDIS_PORT defaults && echo "Success!" 179else 180 # we're chkconfig, so lets add to chkconfig and put in runlevel 345 181 chkconfig --add redis_$REDIS_PORT && echo "Successfully added to chkconfig!" 182 chkconfig --level 345 redis_$REDIS_PORT on && echo "Successfully added to runlevels 345!" 183fi 184 185/etc/init.d/redis_$REDIS_PORT start || die "Failed starting service..." 186 187#tada 188echo "Installation successful!" 189exit 0 190 191