]> git.saurik.com Git - redis.git/blob - utils/install_server.sh
7f4d2672ddf69d855c068311bb8c30f02bfeab26
[redis.git] / utils / install_server.sh
1 #! /bin/sh
2 #
3 # Copyright (c) 2011, Dvir Volk <dvirsky at gmail dot com >
4 #
5 # This program is free software: you can redistribute it and/or modify
6 # it under the terms of the GNU General Public License as published by
7 # the Free Software Foundation, either version 3 of the License, or
8 # (at your option) any later version.
9 #
10 # This program is distributed in the hope that it will be useful,
11 # but WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 # GNU General Public License for more details.
14 #
15 # You should have received a copy of the GNU General Public License
16 # along with this program. If not, see <http://www.gnu.org/licenses/>.
17 #
18 # # # # # # # # # # # # # # # # # # # # # # # # # #
19 #
20 # Interactive service installer for redis server
21 # this generates a redis config file and an /etc/init.d script, and installs them
22 # this scripts should be run as root
23 #
24
25
26 die () {
27 echo "ERROR: $1. Aborting!"
28 exit 1
29 }
30
31
32 #Initial defaults
33 _REDIS_PORT=6379
34
35 echo "Welcome to the redis service installer"
36 echo "This script will help you easily set up a running redis server
37
38 "
39
40 #check for root user TODO: replace this with a call to "id"
41 if [ `whoami` != "root" ] ; then
42 echo "You must run this script as root. Sorry!"
43 exit 1
44 fi
45
46 #Read the redis port
47 read -p "Please select the redis port for this instance: [$_REDIS_PORT] " REDIS_PORT
48 if [ ! `echo $REDIS_PORT | egrep "^[0-9]+\$"` ] ; then
49 echo "Selecting default: $_REDIS_PORT"
50 REDIS_PORT=$_REDIS_PORT
51 fi
52
53 #read the redis config file
54 _REDIS_CONFIG_FILE="/etc/redis/$REDIS_PORT.conf"
55 read -p "Please select the redis config file name [$_REDIS_CONFIG_FILE] " REDIS_CONFIG_FILE
56 if [ !"$REDIS_CONFIG_FILE" ] ; then
57 REDIS_CONFIG_FILE=$_REDIS_CONFIG_FILE
58 echo "Selected default - $REDIS_CONFIG_FILE"
59 fi
60 #try and create it
61 mkdir -p `dirname "$REDIS_CONFIG_FILE"` || die "Could not create redis config directory"
62
63 #read the redis log file path
64 _REDIS_LOG_FILE="/var/log/redis_$REDIS_PORT.log"
65 read -p "Please select the redis log file name [$_REDIS_LOG_FILE] " REDIS_LOG_FILE
66 if [ !"$REDIS_LOG_FILE" ] ; then
67 REDIS_LOG_FILE=$_REDIS_LOG_FILE
68 echo "Selected default - $REDIS_LOG_FILE"
69 fi
70
71
72 #get the redis data directory
73 _REDIS_DATA_DIR="/var/lib/redis/$REDIS_PORT"
74 read -p "Please select the data directory for this instance [$_REDIS_DATA_DIR] " REDIS_DATA_DIR
75 if [ !"$REDIS_DATA_DIR" ] ; then
76 REDIS_DATA_DIR=$_REDIS_DATA_DIR
77 echo "Selected default - $REDIS_DATA_DIR"
78 fi
79 mkdir -p $REDIS_DATA_DIR || die "Could not create redis data directory"
80
81 #get the redis executable path
82 _REDIS_EXECUTABLE=`which redis-server`
83 read -p "Please select the redis executable path [$_REDIS_EXECUTABLE] " REDIS_EXECUTABLE
84 if [ ! -f "$REDIS_EXECUTABLE" ] ; then
85 REDIS_EXECUTABLE=$_REDIS_EXECUTABLE
86
87 if [ ! -f "$REDIS_EXECUTABLE" ] ; then
88 echo "Mmmmm... it seems like you don't have a redis executable. Did you run make install yet?"
89 exit 1
90 fi
91
92 fi
93
94 #render the tmplates
95 TMP_FILE="/tmp/$REDIS_PORT.conf"
96 TPL_FILE="./redis.conf.tpl"
97 INIT_TPL_FILE="./redis_init_script.tpl"
98 INIT_SCRIPT_DEST="/etc/init.d/redis_$REDIS_PORT"
99
100 #check the default for redis cli
101 CLI_EXEC=`which redis-cli`
102 if [ ! "$CLI_EXEC" ] ; then
103 CLI_EXEC=`dirname $REDIS_EXECUTABLE`"/redis-cli"
104 fi
105
106 #Generate config file from template
107 echo "## Generated by install_server.sh ##" > $TMP_FILE
108 cat $TPL_FILE | while read line; do eval "echo \"$line\"" >> $TMP_FILE; done
109 cp -f $TMP_FILE $REDIS_CONFIG_FILE || exit 1
110
111 #Generate sample script from template file
112 rm -f $TMP_FILE
113
114 #we hard code the configs here to avoid issues with templates containing env vars
115 #kinda lame but works!
116 REDIS_INIT_HEADER=\
117 "#/bin/sh\n
118 #Configurations injected by install_server below....\n\n
119 EXEC=$REDIS_EXECUTABLE\n
120 CLIEXEC=$CLI_EXEC\n
121 PIDFILE=/var/run/redis_${REDIS_PORT}.pid\n
122 CONF=\"$REDIS_CONFIG_FILE\"\n\n
123 ###############\n\n"
124
125 #combine the header and the template (which is actually a static footer)
126 echo $REDIS_INIT_HEADER > $TMP_FILE && cat $INIT_TPL_FILE >> $TMP_FILE || die "Could not write init script to $TMP_FILE"
127
128 #copy to /etc/init.d
129 cp -f $TMP_FILE $INIT_SCRIPT_DEST && chmod +x $INIT_SCRIPT_DEST || die "Could not copy redis init script to $INIT_SCRIPT_DEST"
130 echo "Copied $TMP_FILE => $INIT_SCRIPT_DEST"
131
132 #Install the service
133 echo "Installing service..."
134 update-rc.d redis_$REDIS_PORT defaults && echo "Success!"
135 /etc/init.d/redis_$REDIS_PORT start || die "Failed starting service..."
136
137 #tada
138 echo "Installation successful!"
139 exit 0
140
141
142
143