]>
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!"
53 read -p "Please select the redis port for this instance: [$_REDIS_PORT] " REDIS_PORT
54 if [ ! `echo $REDIS_PORT | egrep "^[0-9]+\$"` ] ; then
55 echo "Selecting default: $_REDIS_PORT"
56 REDIS_PORT
=$_REDIS_PORT
59 #read the redis config file
60 _REDIS_CONFIG_FILE
="/etc/redis/$REDIS_PORT.conf"
61 read -p "Please select the redis config file name [$_REDIS_CONFIG_FILE] " REDIS_CONFIG_FILE
62 if [ !"$REDIS_CONFIG_FILE" ] ; then
63 REDIS_CONFIG_FILE
=$_REDIS_CONFIG_FILE
64 echo "Selected default - $REDIS_CONFIG_FILE"
67 mkdir -p `dirname "$REDIS_CONFIG_FILE"` || die
"Could not create redis config directory"
69 #read the redis log file path
70 _REDIS_LOG_FILE
="/var/log/redis_$REDIS_PORT.log"
71 read -p "Please select the redis log file name [$_REDIS_LOG_FILE] " REDIS_LOG_FILE
72 if [ !"$REDIS_LOG_FILE" ] ; then
73 REDIS_LOG_FILE
=$_REDIS_LOG_FILE
74 echo "Selected default - $REDIS_LOG_FILE"
78 #get the redis data directory
79 _REDIS_DATA_DIR
="/var/lib/redis/$REDIS_PORT"
80 read -p "Please select the data directory for this instance [$_REDIS_DATA_DIR] " REDIS_DATA_DIR
81 if [ !"$REDIS_DATA_DIR" ] ; then
82 REDIS_DATA_DIR
=$_REDIS_DATA_DIR
83 echo "Selected default - $REDIS_DATA_DIR"
85 mkdir -p $REDIS_DATA_DIR || die
"Could not create redis data directory"
87 #get the redis executable path
88 _REDIS_EXECUTABLE
=`which redis-server`
89 read -p "Please select the redis executable path [$_REDIS_EXECUTABLE] " REDIS_EXECUTABLE
90 if [ ! -f "$REDIS_EXECUTABLE" ] ; then
91 REDIS_EXECUTABLE
=$_REDIS_EXECUTABLE
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?"
102 TMP_FILE
="/tmp/$REDIS_PORT.conf"
103 DEFAULT_CONFIG
="../redis.conf"
104 INIT_TPL_FILE
="./redis_init_script.tpl"
105 INIT_SCRIPT_DEST
="/etc/init.d/redis_$REDIS_PORT"
106 PIDFILE
="/var/run/redis_$REDIS_PORT.pid"
110 #check the default for redis cli
111 CLI_EXEC
=`which redis-cli`
112 if [ ! "$CLI_EXEC" ] ; then
113 CLI_EXEC
=`dirname $REDIS_EXECUTABLE`"/redis-cli"
116 #Generate config file from the default config file as template
117 #changing only the stuff we're controlling from this script
118 echo "## Generated by install_server.sh ##" > $TMP_FILE
120 SED_EXPR
="s#^port [0-9]{4}\$#port ${REDIS_PORT}#;\
121 s#^logfile .+\$#logfile ${REDIS_LOG_FILE}#;\
122 s#^dir .+\$#dir ${REDIS_DATA_DIR}#;\
123 s#^pidfile .+\$#pidfile ${PIDFILE}#;\
124 s#^daemonize no\$#daemonize yes#;"
126 sed -r "$SED_EXPR" $DEFAULT_CONFIG >> $TMP_FILE
128 #cat $TPL_FILE | while read line; do eval "echo \"$line\"" >> $TMP_FILE; done
129 cp -f $TMP_FILE $REDIS_CONFIG_FILE || exit 1
131 #Generate sample script from template file
134 #we hard code the configs here to avoid issues with templates containing env vars
135 #kinda lame but works!
138 #Configurations injected by install_server below....\n\n
139 EXEC=$REDIS_EXECUTABLE\n
142 CONF=\"$REDIS_CONFIG_FILE\"\n\n
143 REDISPORT=\"$REDIS_PORT\"\n\n
146 REDIS_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"
162 if [ !`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"
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"
171 cp -f $TMP_FILE $INIT_SCRIPT_DEST && chmod +x
$INIT_SCRIPT_DEST || die
"Could not copy redis init script to $INIT_SCRIPT_DEST"
172 echo "Copied $TMP_FILE => $INIT_SCRIPT_DEST"
175 echo "Installing service..."
176 if [ !`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!"
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!"
185 /etc
/init.d
/redis_
$REDIS_PORT start
|| die
"Failed starting service..."
188 echo "Installation successful!"