aboutsummaryrefslogtreecommitdiff
path: root/examples/redis-unstable/utils/install_server.sh
diff options
context:
space:
mode:
Diffstat (limited to 'examples/redis-unstable/utils/install_server.sh')
-rwxr-xr-xexamples/redis-unstable/utils/install_server.sh291
1 files changed, 291 insertions, 0 deletions
diff --git a/examples/redis-unstable/utils/install_server.sh b/examples/redis-unstable/utils/install_server.sh
new file mode 100755
index 0000000..efda7da
--- /dev/null
+++ b/examples/redis-unstable/utils/install_server.sh
@@ -0,0 +1,291 @@
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# Service installer for redis server, runs interactively by default.
29#
30# To run this script non-interactively (for automation/provisioning purposes),
31# feed the variables into the script. Any missing variables will be prompted!
32# Tip: Environment variables also support command substitution (see REDIS_EXECUTABLE)
33#
34# Example:
35#
36# sudo REDIS_PORT=1234 \
37# REDIS_CONFIG_FILE=/etc/redis/1234.conf \
38# REDIS_LOG_FILE=/var/log/redis_1234.log \
39# REDIS_DATA_DIR=/var/lib/redis/1234 \
40# REDIS_EXECUTABLE=`command -v redis-server` ./utils/install_server.sh
41#
42# This generates a redis config file and an /etc/init.d script, and installs them.
43#
44# /!\ This script should be run as root
45#
46# NOTE: This script will not work on Mac OSX.
47# It supports Debian and Ubuntu Linux.
48#
49################################################################################
50
51die () {
52 echo "ERROR: $1. Aborting!"
53 exit 1
54}
55
56
57#Absolute path to this script
58SCRIPT=$(readlink -f $0)
59#Absolute path this script is in
60SCRIPTPATH=$(dirname $SCRIPT)
61
62#Initial defaults
63_REDIS_PORT=6379
64_MANUAL_EXECUTION=false
65
66echo "Welcome to the redis service installer"
67echo "This script will help you easily set up a running redis server"
68echo
69
70#check for root user
71if [ "$(id -u)" -ne 0 ] ; then
72 echo "You must run this script as root. Sorry!"
73 exit 1
74fi
75
76#bail if this system is managed by systemd
77_pid_1_exe="$(readlink -f /proc/1/exe)"
78if [ "${_pid_1_exe##*/}" = systemd ]
79then
80 echo "This systems seems to use systemd."
81 echo "Please take a look at the provided example service unit files in this directory, and adapt and install them. Sorry!"
82 exit 1
83fi
84unset _pid_1_exe
85
86if ! echo $REDIS_PORT | egrep -q '^[0-9]+$' ; then
87 _MANUAL_EXECUTION=true
88 #Read the redis port
89 read -p "Please select the redis port for this instance: [$_REDIS_PORT] " REDIS_PORT
90 if ! echo $REDIS_PORT | egrep -q '^[0-9]+$' ; then
91 echo "Selecting default: $_REDIS_PORT"
92 REDIS_PORT=$_REDIS_PORT
93 fi
94fi
95
96if [ -z "$REDIS_CONFIG_FILE" ] ; then
97 _MANUAL_EXECUTION=true
98 #read the redis config file
99 _REDIS_CONFIG_FILE="/etc/redis/$REDIS_PORT.conf"
100 read -p "Please select the redis config file name [$_REDIS_CONFIG_FILE] " REDIS_CONFIG_FILE
101 if [ -z "$REDIS_CONFIG_FILE" ] ; then
102 REDIS_CONFIG_FILE=$_REDIS_CONFIG_FILE
103 echo "Selected default - $REDIS_CONFIG_FILE"
104 fi
105fi
106
107if [ -z "$REDIS_LOG_FILE" ] ; then
108 _MANUAL_EXECUTION=true
109 #read the redis log file path
110 _REDIS_LOG_FILE="/var/log/redis_$REDIS_PORT.log"
111 read -p "Please select the redis log file name [$_REDIS_LOG_FILE] " REDIS_LOG_FILE
112 if [ -z "$REDIS_LOG_FILE" ] ; then
113 REDIS_LOG_FILE=$_REDIS_LOG_FILE
114 echo "Selected default - $REDIS_LOG_FILE"
115 fi
116fi
117
118if [ -z "$REDIS_DATA_DIR" ] ; then
119 _MANUAL_EXECUTION=true
120 #get the redis data directory
121 _REDIS_DATA_DIR="/var/lib/redis/$REDIS_PORT"
122 read -p "Please select the data directory for this instance [$_REDIS_DATA_DIR] " REDIS_DATA_DIR
123 if [ -z "$REDIS_DATA_DIR" ] ; then
124 REDIS_DATA_DIR=$_REDIS_DATA_DIR
125 echo "Selected default - $REDIS_DATA_DIR"
126 fi
127fi
128
129if [ ! -x "$REDIS_EXECUTABLE" ] ; then
130 _MANUAL_EXECUTION=true
131 #get the redis executable path
132 _REDIS_EXECUTABLE=`command -v redis-server`
133 read -p "Please select the redis executable path [$_REDIS_EXECUTABLE] " REDIS_EXECUTABLE
134 if [ ! -x "$REDIS_EXECUTABLE" ] ; then
135 REDIS_EXECUTABLE=$_REDIS_EXECUTABLE
136
137 if [ ! -x "$REDIS_EXECUTABLE" ] ; then
138 echo "Mmmmm... it seems like you don't have a redis executable. Did you run make install yet?"
139 exit 1
140 fi
141 fi
142fi
143
144#check the default for redis cli
145CLI_EXEC=`command -v redis-cli`
146if [ -z "$CLI_EXEC" ] ; then
147 CLI_EXEC=`dirname $REDIS_EXECUTABLE`"/redis-cli"
148fi
149
150echo "Selected config:"
151
152echo "Port : $REDIS_PORT"
153echo "Config file : $REDIS_CONFIG_FILE"
154echo "Log file : $REDIS_LOG_FILE"
155echo "Data dir : $REDIS_DATA_DIR"
156echo "Executable : $REDIS_EXECUTABLE"
157echo "Cli Executable : $CLI_EXEC"
158
159if $_MANUAL_EXECUTION == true ; then
160 read -p "Is this ok? Then press ENTER to go on or Ctrl-C to abort." _UNUSED_
161fi
162
163mkdir -p `dirname "$REDIS_CONFIG_FILE"` || die "Could not create redis config directory"
164mkdir -p `dirname "$REDIS_LOG_FILE"` || die "Could not create redis log dir"
165mkdir -p "$REDIS_DATA_DIR" || die "Could not create redis data directory"
166
167#render the templates
168TMP_FILE="/tmp/${REDIS_PORT}.conf"
169DEFAULT_CONFIG="${SCRIPTPATH}/../redis.conf"
170INIT_TPL_FILE="${SCRIPTPATH}/redis_init_script.tpl"
171INIT_SCRIPT_DEST="/etc/init.d/redis_${REDIS_PORT}"
172PIDFILE="/var/run/redis_${REDIS_PORT}.pid"
173
174if [ ! -f "$DEFAULT_CONFIG" ]; then
175 echo "Mmmmm... the default config is missing. Did you switch to the utils directory?"
176 exit 1
177fi
178
179#Generate config file from the default config file as template
180#changing only the stuff we're controlling from this script
181echo "## Generated by install_server.sh ##" > $TMP_FILE
182
183read -r SED_EXPR <<-EOF
184s#^port .\+#port ${REDIS_PORT}#; \
185s#^logfile .\+#logfile ${REDIS_LOG_FILE}#; \
186s#^dir .\+#dir ${REDIS_DATA_DIR}#; \
187s#^pidfile .\+#pidfile ${PIDFILE}#; \
188s#^daemonize no#daemonize yes#;
189EOF
190sed "$SED_EXPR" $DEFAULT_CONFIG >> $TMP_FILE
191
192#cat $TPL_FILE | while read line; do eval "echo \"$line\"" >> $TMP_FILE; done
193cp $TMP_FILE $REDIS_CONFIG_FILE || die "Could not write redis config file $REDIS_CONFIG_FILE"
194
195#Generate sample script from template file
196rm -f $TMP_FILE
197
198#we hard code the configs here to avoid issues with templates containing env vars
199#kinda lame but works!
200REDIS_INIT_HEADER=\
201"#!/bin/sh\n
202#Configurations injected by install_server below....\n\n
203EXEC=$REDIS_EXECUTABLE\n
204CLIEXEC=$CLI_EXEC\n
205PIDFILE=\"$PIDFILE\"\n
206CONF=\"$REDIS_CONFIG_FILE\"\n\n
207REDISPORT=\"$REDIS_PORT\"\n\n
208###############\n\n"
209
210REDIS_CHKCONFIG_INFO=\
211"# REDHAT chkconfig header\n\n
212# chkconfig: - 58 74\n
213# description: redis_${REDIS_PORT} is the redis daemon.\n
214### BEGIN INIT INFO\n
215# Provides: redis_6379\n
216# Required-Start: \$network \$local_fs \$remote_fs\n
217# Required-Stop: \$network \$local_fs \$remote_fs\n
218# Default-Start: 2 3 4 5\n
219# Default-Stop: 0 1 6\n
220# Should-Start: \$syslog \$named\n
221# Should-Stop: \$syslog \$named\n
222# Short-Description: start and stop redis_${REDIS_PORT}\n
223# Description: Redis daemon\n
224### END INIT INFO\n\n"
225
226if command -v chkconfig >/dev/null; then
227 #if we're a box with chkconfig on it we want to include info for chkconfig
228 echo "$REDIS_INIT_HEADER" "$REDIS_CHKCONFIG_INFO" > $TMP_FILE && cat $INIT_TPL_FILE >> $TMP_FILE || die "Could not write init script to $TMP_FILE"
229else
230 #combine the header and the template (which is actually a static footer)
231 echo "$REDIS_INIT_HEADER" > $TMP_FILE && cat $INIT_TPL_FILE >> $TMP_FILE || die "Could not write init script to $TMP_FILE"
232fi
233
234###
235# Generate sample script from template file
236# - No need to check which system we are on. The init info are comments and
237# do not interfere with update_rc.d systems. Additionally:
238# Ubuntu/debian by default does not come with chkconfig, but does issue a
239# warning if init info is not available.
240
241cat > ${TMP_FILE} <<EOT
242#!/bin/sh
243#Configurations injected by install_server below....
244
245EXEC=$REDIS_EXECUTABLE
246CLIEXEC=$CLI_EXEC
247PIDFILE=$PIDFILE
248CONF="$REDIS_CONFIG_FILE"
249REDISPORT="$REDIS_PORT"
250###############
251# SysV Init Information
252# chkconfig: - 58 74
253# description: redis_${REDIS_PORT} is the redis daemon.
254### BEGIN INIT INFO
255# Provides: redis_${REDIS_PORT}
256# Required-Start: \$network \$local_fs \$remote_fs
257# Required-Stop: \$network \$local_fs \$remote_fs
258# Default-Start: 2 3 4 5
259# Default-Stop: 0 1 6
260# Should-Start: \$syslog \$named
261# Should-Stop: \$syslog \$named
262# Short-Description: start and stop redis_${REDIS_PORT}
263# Description: Redis daemon
264### END INIT INFO
265
266EOT
267cat ${INIT_TPL_FILE} >> ${TMP_FILE}
268
269#copy to /etc/init.d
270cp $TMP_FILE $INIT_SCRIPT_DEST && \
271 chmod +x $INIT_SCRIPT_DEST || die "Could not copy redis init script to $INIT_SCRIPT_DEST"
272echo "Copied $TMP_FILE => $INIT_SCRIPT_DEST"
273
274#Install the service
275echo "Installing service..."
276if command -v chkconfig >/dev/null 2>&1; then
277 # we're chkconfig, so lets add to chkconfig and put in runlevel 345
278 chkconfig --add redis_${REDIS_PORT} && echo "Successfully added to chkconfig!"
279 chkconfig --level 345 redis_${REDIS_PORT} on && echo "Successfully added to runlevels 345!"
280elif command -v update-rc.d >/dev/null 2>&1; then
281 #if we're not a chkconfig box assume we're able to use update-rc.d
282 update-rc.d redis_${REDIS_PORT} defaults && echo "Success!"
283else
284 echo "No supported init tool found."
285fi
286
287/etc/init.d/redis_$REDIS_PORT start || die "Failed starting service..."
288
289#tada
290echo "Installation successful!"
291exit 0