]> git.saurik.com Git - redis.git/blob - utils/install_server.sh
Merge pull request #304 from bradvoth/unstable
[redis.git] / utils / install_server.sh
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 modification, are
6 # permitted provided that the following conditions are met:
7 #
8 # 1. Redistributions of source code must retain the above copyright notice, this list of
9 # conditions and the following disclaimer.
10 #
11 # 2. Redistributions in binary form must reproduce the above copyright notice, this list
12 # of conditions and the following disclaimer in the documentation and/or other materials
13 # 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 MERCHANTABILITY AND
17 # FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL Dvir Volk OR
18 # CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
19 # CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
20 # SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
21 # ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
22 # NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
23 # ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24 #
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
33
34 die () {
35 echo "ERROR: $1. Aborting!"
36 exit 1
37 }
38
39
40 #Initial defaults
41 _REDIS_PORT=6379
42
43 echo "Welcome to the redis service installer"
44 echo "This script will help you easily set up a running redis server
45
46 "
47
48 #check for root user TODO: replace this with a call to "id"
49 if [ `whoami` != "root" ] ; then
50 echo "You must run this script as root. Sorry!"
51 exit 1
52 fi
53
54 #Read the redis port
55 read -p "Please select the redis port for this instance: [$_REDIS_PORT] " REDIS_PORT
56 if [ ! `echo $REDIS_PORT | egrep "^[0-9]+\$"` ] ; then
57 echo "Selecting default: $_REDIS_PORT"
58 REDIS_PORT=$_REDIS_PORT
59 fi
60
61 #read the redis config file
62 _REDIS_CONFIG_FILE="/etc/redis/$REDIS_PORT.conf"
63 read -p "Please select the redis config file name [$_REDIS_CONFIG_FILE] " REDIS_CONFIG_FILE
64 if [ !"$REDIS_CONFIG_FILE" ] ; then
65 REDIS_CONFIG_FILE=$_REDIS_CONFIG_FILE
66 echo "Selected default - $REDIS_CONFIG_FILE"
67 fi
68 #try and create it
69 mkdir -p `dirname "$REDIS_CONFIG_FILE"` || die "Could not create redis config directory"
70
71 #read the redis log file path
72 _REDIS_LOG_FILE="/var/log/redis_$REDIS_PORT.log"
73 read -p "Please select the redis log file name [$_REDIS_LOG_FILE] " REDIS_LOG_FILE
74 if [ !"$REDIS_LOG_FILE" ] ; then
75 REDIS_LOG_FILE=$_REDIS_LOG_FILE
76 echo "Selected default - $REDIS_LOG_FILE"
77 fi
78
79
80 #get the redis data directory
81 _REDIS_DATA_DIR="/var/lib/redis/$REDIS_PORT"
82 read -p "Please select the data directory for this instance [$_REDIS_DATA_DIR] " REDIS_DATA_DIR
83 if [ !"$REDIS_DATA_DIR" ] ; then
84 REDIS_DATA_DIR=$_REDIS_DATA_DIR
85 echo "Selected default - $REDIS_DATA_DIR"
86 fi
87 mkdir -p $REDIS_DATA_DIR || die "Could not create redis data directory"
88
89 #get the redis executable path
90 _REDIS_EXECUTABLE=`which redis-server`
91 read -p "Please select the redis executable path [$_REDIS_EXECUTABLE] " REDIS_EXECUTABLE
92 if [ ! -f "$REDIS_EXECUTABLE" ] ; then
93 REDIS_EXECUTABLE=$_REDIS_EXECUTABLE
94
95 if [ ! -f "$REDIS_EXECUTABLE" ] ; then
96 echo "Mmmmm... it seems like you don't have a redis executable. Did you run make install yet?"
97 exit 1
98 fi
99
100 fi
101
102
103 #render the tmplates
104 TMP_FILE="/tmp/$REDIS_PORT.conf"
105 TPL_FILE="./redis.conf.tpl"
106 INIT_TPL_FILE="./redis_init_script.tpl"
107 INIT_SCRIPT_DEST="/etc/init.d/redis_$REDIS_PORT"
108 PIDFILE="/var/run/redis_$REDIS_PORT.pid"
109
110
111
112 #check the default for redis cli
113 CLI_EXEC=`which redis-cli`
114 if [ ! "$CLI_EXEC" ] ; then
115 CLI_EXEC=`dirname $REDIS_EXECUTABLE`"/redis-cli"
116 fi
117
118 #Generate config file from template
119 echo "## Generated by install_server.sh ##" > $TMP_FILE
120 cat $TPL_FILE | while read line; do eval "echo \"$line\"" >> $TMP_FILE; done
121 cp -f $TMP_FILE $REDIS_CONFIG_FILE || exit 1
122
123 #Generate sample script from template file
124 rm -f $TMP_FILE
125
126 #we hard code the configs here to avoid issues with templates containing env vars
127 #kinda lame but works!
128 REDIS_INIT_HEADER=\
129 "#/bin/sh\n
130 #Configurations injected by install_server below....\n\n
131 EXEC=$REDIS_EXECUTABLE\n
132 CLIEXEC=$CLI_EXEC\n
133 PIDFILE=$PIDFILE\n
134 CONF=\"$REDIS_CONFIG_FILE\"\n\n
135 REDISPORT=\"$REDIS_PORT\"\n\n
136 ###############\n\n"
137
138 REDIS_CHKCONFIG_INFO=\
139 "# REDHAT chkconfig header\n\n
140 # chkconfig: - 58 74\n
141 # description: redis_6379 is the redis daemon.\n
142 ### BEGIN INIT INFO\n
143 # Provides: redis_6379\n
144 # Required-Start: $network $local_fs $remote_fs\n
145 # Required-Stop: $network $local_fs $remote_fs\n
146 # Should-Start: $syslog $named\n
147 # Should-Stop: $syslog $named\n
148 # Short-Description: start and stop redis_6379\n
149 # Description: Redis daemon\n
150 ### END INIT INFO\n\n"
151
152 if [[ ! `which chkconfig` ]] ; then
153 #combine the header and the template (which is actually a static footer)
154 echo -e $REDIS_INIT_HEADER > $TMP_FILE && cat $INIT_TPL_FILE >> $TMP_FILE || die "Could not write init script to $TMP_FILE"
155 else
156 #if we're a box with chkconfig on it we want to include info for chkconfig
157 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 fi
159
160 #copy to /etc/init.d
161 cp -f $TMP_FILE $INIT_SCRIPT_DEST && chmod +x $INIT_SCRIPT_DEST || die "Could not copy redis init script to $INIT_SCRIPT_DEST"
162 echo "Copied $TMP_FILE => $INIT_SCRIPT_DEST"
163
164 #Install the service
165 echo "Installing service..."
166 if [[ ! `which chkconfig` ]] ; then
167 #if we're not a chkconfig box assume we're able to use update-rc.d
168 update-rc.d redis_$REDIS_PORT defaults && echo "Success!"
169 else
170 # we're chkconfig, so lets add to chkconfig and put in runlevel 345
171 chkconfig --add redis_$REDIS_PORT && echo "Successfully added to chkconfig!"
172 chkconfig--level 345 redis_$REDIS_PORT on && echo "Successfully added to runlevels 345!"
173 fi
174
175 /etc/init.d/redis_$REDIS_PORT start || die "Failed starting service..."
176
177 #tada
178 echo "Installation successful!"
179 exit 0
180
181
182
183