Back to home page

Redis cross reference

 
 

    


0001 #!/bin/sh
0002 
0003 # Copyright 2011 Dvir Volk <dvirsk at gmail dot com>. All rights reserved.
0004 #
0005 # Redistribution and use in source and binary forms, with or without
0006 # modification, are permitted provided that the following conditions are met:
0007 #
0008 #   1. Redistributions of source code must retain the above copyright notice,
0009 #   this list of conditions and the following disclaimer.
0010 #
0011 #   2. Redistributions in binary form must reproduce the above copyright
0012 #   notice, this list of conditions and the following disclaimer in the
0013 #   documentation and/or other materials provided with the distribution.
0014 #
0015 # THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED
0016 # WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
0017 # MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
0018 # EVENT SHALL Dvir Volk OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
0019 # INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
0020 # LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
0021 # OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
0022 # LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
0023 # NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
0024 # EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
0025 #
0026 ################################################################################
0027 #
0028 # Interactive service installer for redis server
0029 # this generates a redis config file and an /etc/init.d script, and installs them
0030 # this scripts should be run as root
0031 
0032 die () {
0033         echo "ERROR: $1. Aborting!" 
0034         exit 1
0035 }
0036 
0037 #Initial defaults
0038 _REDIS_PORT=6379
0039 
0040 echo "Welcome to the redis service installer"
0041 echo "This script will help you easily set up a running redis server
0042 
0043 "
0044 
0045 #check for root user TODO: replace this with a call to "id"
0046 if [ `whoami` != "root" ] ; then
0047         echo "You must run this script as root. Sorry!"
0048         exit 1
0049 fi
0050 
0051 
0052 #Read the redis port
0053 read  -p "Please select the redis port for this instance: [$_REDIS_PORT] " REDIS_PORT 
0054 if [ ! `echo $REDIS_PORT | egrep "^[0-9]+\$"`  ] ; then
0055         echo "Selecting default: $_REDIS_PORT"
0056         REDIS_PORT=$_REDIS_PORT 
0057 fi
0058 
0059 #read the redis config file
0060 _REDIS_CONFIG_FILE="/etc/redis/$REDIS_PORT.conf"
0061 read -p "Please select the redis config file name [$_REDIS_CONFIG_FILE] " REDIS_CONFIG_FILE
0062 if [ !"$REDIS_CONFIG_FILE" ] ; then
0063         REDIS_CONFIG_FILE=$_REDIS_CONFIG_FILE
0064         echo "Selected default - $REDIS_CONFIG_FILE"
0065 fi
0066 #try and create it
0067 mkdir -p `dirname "$REDIS_CONFIG_FILE"` || die "Could not create redis config directory"
0068 
0069 #read the redis log file path
0070 _REDIS_LOG_FILE="/var/log/redis_$REDIS_PORT.log"
0071 read -p "Please select the redis log file name [$_REDIS_LOG_FILE] " REDIS_LOG_FILE
0072 if [ !"$REDIS_LOG_FILE" ] ; then
0073         REDIS_LOG_FILE=$_REDIS_LOG_FILE
0074         echo "Selected default - $REDIS_LOG_FILE"
0075 fi
0076 
0077 
0078 #get the redis data directory
0079 _REDIS_DATA_DIR="/var/lib/redis/$REDIS_PORT"
0080 read -p "Please select the data directory for this instance [$_REDIS_DATA_DIR] " REDIS_DATA_DIR
0081 if [ !"$REDIS_DATA_DIR" ] ; then
0082         REDIS_DATA_DIR=$_REDIS_DATA_DIR
0083         echo "Selected default - $REDIS_DATA_DIR"
0084 fi
0085 mkdir -p $REDIS_DATA_DIR || die "Could not create redis data directory"
0086 
0087 #get the redis executable path
0088 _REDIS_EXECUTABLE=`which redis-server`
0089 read -p "Please select the redis executable path [$_REDIS_EXECUTABLE] " REDIS_EXECUTABLE
0090 if [ ! -f "$REDIS_EXECUTABLE" ] ; then
0091         REDIS_EXECUTABLE=$_REDIS_EXECUTABLE
0092         
0093         if [ ! -f "$REDIS_EXECUTABLE" ] ; then
0094                 echo "Mmmmm...  it seems like you don't have a redis executable. Did you run make install yet?"
0095                 exit 1
0096         fi
0097         
0098 fi
0099 
0100 
0101 #render the tmplates
0102 TMP_FILE="/tmp/$REDIS_PORT.conf"
0103 DEFAULT_CONFIG="../redis.conf"
0104 INIT_TPL_FILE="./redis_init_script.tpl"
0105 INIT_SCRIPT_DEST="/etc/init.d/redis_$REDIS_PORT"
0106 PIDFILE="/var/run/redis_$REDIS_PORT.pid"
0107 
0108 
0109 
0110 #check the default for redis cli
0111 CLI_EXEC=`which redis-cli`
0112 if [ ! "$CLI_EXEC" ] ; then 
0113         CLI_EXEC=`dirname $REDIS_EXECUTABLE`"/redis-cli"
0114 fi
0115 
0116 #Generate config file from the default config file as template
0117 #changing only the stuff we're controlling from this script
0118 echo "## Generated by install_server.sh ##" > $TMP_FILE
0119 
0120 SED_EXPR="s#^port [0-9]{4}\$#port ${REDIS_PORT}#;\
0121 s#^logfile .+\$#logfile ${REDIS_LOG_FILE}#;\
0122 s#^dir .+\$#dir ${REDIS_DATA_DIR}#;\
0123 s#^pidfile .+\$#pidfile ${PIDFILE}#;\
0124 s#^daemonize no\$#daemonize yes#;" 
0125 echo $SED_EXPR
0126 sed -r "$SED_EXPR" $DEFAULT_CONFIG  >> $TMP_FILE
0127 
0128 #cat $TPL_FILE | while read line; do eval "echo \"$line\"" >> $TMP_FILE; done
0129 cp -f $TMP_FILE $REDIS_CONFIG_FILE || exit 1
0130 
0131 #Generate sample script from template file
0132 rm -f $TMP_FILE
0133 
0134 #we hard code the configs here to avoid issues with templates containing env vars
0135 #kinda lame but works!
0136 REDIS_INIT_HEADER=\
0137 "#/bin/sh\n
0138 #Configurations injected by install_server below....\n\n
0139 EXEC=$REDIS_EXECUTABLE\n
0140 CLIEXEC=$CLI_EXEC\n
0141 PIDFILE=$PIDFILE\n
0142 CONF=\"$REDIS_CONFIG_FILE\"\n\n
0143 REDISPORT=\"$REDIS_PORT\"\n\n
0144 ###############\n\n"
0145 
0146 REDIS_CHKCONFIG_INFO=\
0147 "# REDHAT chkconfig header\n\n
0148 # chkconfig: - 58 74\n
0149 # description: redis_6379 is the redis daemon.\n
0150 ### BEGIN INIT INFO\n
0151 # Provides: redis_6379\n
0152 # Required-Start: $network $local_fs $remote_fs\n
0153 # Required-Stop: $network $local_fs $remote_fs\n
0154 # Default-Start: 2 3 4 5\n
0155 # Default-Stop: 0 1 6\n
0156 # Should-Start: $syslog $named\n
0157 # Should-Stop: $syslog $named\n
0158 # Short-Description: start and stop redis_6379\n
0159 # Description: Redis daemon\n
0160 ### END INIT INFO\n\n"
0161 
0162 if [ !`which chkconfig` ] ; then 
0163         #combine the header and the template (which is actually a static footer)
0164         echo $REDIS_INIT_HEADER > $TMP_FILE && cat $INIT_TPL_FILE >> $TMP_FILE || die "Could not write init script to $TMP_FILE"
0165 else
0166         #if we're a box with chkconfig on it we want to include info for chkconfig
0167         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"
0168 fi
0169 
0170 #copy to /etc/init.d
0171 cp -f $TMP_FILE $INIT_SCRIPT_DEST && chmod +x $INIT_SCRIPT_DEST || die "Could not copy redis init script to  $INIT_SCRIPT_DEST"
0172 echo "Copied $TMP_FILE => $INIT_SCRIPT_DEST"
0173 
0174 #Install the service
0175 echo "Installing service..."
0176 if [ !`which chkconfig` ] ; then 
0177         #if we're not a chkconfig box assume we're able to use update-rc.d
0178         update-rc.d redis_$REDIS_PORT defaults && echo "Success!"
0179 else
0180         # we're chkconfig, so lets add to chkconfig and put in runlevel 345
0181         chkconfig --add redis_$REDIS_PORT && echo "Successfully added to chkconfig!"
0182         chkconfig --level 345 redis_$REDIS_PORT on && echo "Successfully added to runlevels 345!"
0183 fi
0184         
0185 /etc/init.d/redis_$REDIS_PORT start || die "Failed starting service..."
0186 
0187 #tada
0188 echo "Installation successful!"
0189 exit 0
0190