]>
git.saurik.com Git - redis.git/blob - utils/install_server.sh
3 # Copyright 2011 Dvir Volk <dvirsk at gmail dot com>. All rights reserved.
5 # Redistribution and use in source and binary forms, with or without
6 # modification, are permitted provided that the following conditions are met:
8 # 1. Redistributions of source code must retain the above copyright notice,
9 # this list of conditions and the following disclaimer.
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.
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.
26 ################################################################################
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
33 echo "ERROR: $1. Aborting!"
40 echo "Welcome to the redis service installer"
41 echo "This script will help you easily set up a running redis server
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!"
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
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"
66 mkdir -p `dirname "$REDIS_CONFIG_FILE"` || die
"Could not create redis config directory"
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"
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"
84 mkdir -p $REDIS_DATA_DIR || die
"Could not create redis data directory"
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
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?"
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"
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"
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
120 #Generate sample script from template file
123 #we hard code the configs here to avoid issues with templates containing env vars
124 #kinda lame but works!
127 #Configurations injected by install_server below....\n\n
128 EXEC=$REDIS_EXECUTABLE\n
131 CONF=\"$REDIS_CONFIG_FILE\"\n\n
132 REDISPORT=\"$REDIS_PORT\"\n\n
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"
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"
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"
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"
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!"
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!"
172 /etc
/init.d
/redis_
$REDIS_PORT start
|| die
"Failed starting service..."
175 echo "Installation successful!"