]> git.saurik.com Git - redis.git/blob - utils/install_server.sh
Fix overflow in mstime() in redis-cli and benchmark.
[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
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
52 #Read the redis port
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
57 fi
58
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"
65 fi
66 #try and create it
67 mkdir -p `dirname "$REDIS_CONFIG_FILE"` || die "Could not create redis config directory"
68
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"
75 fi
76
77
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"
84 fi
85 mkdir -p $REDIS_DATA_DIR || die "Could not create redis data directory"
86
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
92
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?"
95 exit 1
96 fi
97
98 fi
99
100
101 #render the tmplates
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"
107
108
109
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"
114 fi
115
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
119
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#;"
125 echo $SED_EXPR
126 sed -r "$SED_EXPR" $DEFAULT_CONFIG >> $TMP_FILE
127
128 #cat $TPL_FILE | while read line; do eval "echo \"$line\"" >> $TMP_FILE; done
129 cp -f $TMP_FILE $REDIS_CONFIG_FILE || exit 1
130
131 #Generate sample script from template file
132 rm -f $TMP_FILE
133
134 #we hard code the configs here to avoid issues with templates containing env vars
135 #kinda lame but works!
136 REDIS_INIT_HEADER=\
137 "#/bin/sh\n
138 #Configurations injected by install_server below....\n\n
139 EXEC=$REDIS_EXECUTABLE\n
140 CLIEXEC=$CLI_EXEC\n
141 PIDFILE=$PIDFILE\n
142 CONF=\"$REDIS_CONFIG_FILE\"\n\n
143 REDISPORT=\"$REDIS_PORT\"\n\n
144 ###############\n\n"
145
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"
161
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"
165 else
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"
168 fi
169
170 #copy to /etc/init.d
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"
173
174 #Install the service
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!"
179 else
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!"
183 fi
184
185 /etc/init.d/redis_$REDIS_PORT start || die "Failed starting service..."
186
187 #tada
188 echo "Installation successful!"
189 exit 0
190