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