]> git.saurik.com Git - redis.git/blame - utils/install_server.sh
Added installer and config template renderer
[redis.git] / utils / install_server.sh
CommitLineData
9210e701 1#! /bin/sh
2# Interactive service installer for redis server
3# this generates a redis config file and an /etc/init.d script, and installs them
4# this scripts should be run as root
5#
6# Contributed by Dvir Volk dvirsky at gmail dot com 2011
7# This code is contributed to public domain
8
9
10die () {
11 echo "ERROR: $1. Aborting!"
12 exit 1
13}
14
15
16#Initial defaults
17_REDIS_PORT=6379
18
19echo "Welcome to the redis service installer"
20echo "This script will help you easily set up a running redis server
21
22"
23
24#check for root user TODO: replace this with a call to "id"
25if [ `whoami` != "root" ] ; then
26 echo "You must run this script as root. Sorry!"
27 exit 1
28fi
29
30#Read the redis port
31read -p "Please select the redis port for this instance: [$_REDIS_PORT] " REDIS_PORT
32if [ ! `echo $REDIS_PORT | egrep "^[0-9]+\$"` ] ; then
33 echo "Selecting default: $_REDIS_PORT"
34 REDIS_PORT=$_REDIS_PORT
35fi
36
37#read the redis config file
38_REDIS_CONFIG_FILE="/etc/redis/$REDIS_PORT.conf"
39read -p "Please select the redis config file name [$_REDIS_CONFIG_FILE] " REDIS_CONFIG_FILE
40if [ !"$REDIS_CONFIG_FILE" ] ; then
41 REDIS_CONFIG_FILE=$_REDIS_CONFIG_FILE
42 echo "Selected default - $REDIS_CONFIG_FILE"
43fi
44
45#try and create
46mkdir -p `dirname "$REDIS_CONFIG_FILE"` || die "Could not create redis config directory"
47
48#get the redis data directory
49_REDIS_DATA_DIR="/var/lib/redis/$REDIS_PORT"
50read -p "Please select the data directory for this instance [$_REDIS_DATA_DIR] " REDIS_DATA_DIR
51if [ !"$REDIS_DATA_DIR" ] ; then
52 REDIS_DATA_DIR=$_REDIS_DATA_DIR
53 echo "Selected default - $REDIS_DATA_DIR"
54fi
55#try the data directory setting
56mkdir -p `dirname "$REDIS_DATA_DIR"` || die "Could not create redis data directory"
57
58
59#get the redis executable path
60_REDIS_EXECUTABLE=`which redis-server`
61read -p "Please select the redis executable path [$_REDIS_EXECUTABLE] " REDIS_EXECUTABLE
62if [ ! -f "$REDIS_EXECUTABLE" ] ; then
63 REDIS_EXECUTABLE=$_REDIS_EXECUTABLE
64
65 if [ ! -f "$REDIS_EXECUTABLE" ] ; then
66 echo "Mmmmm... it seems like you don't have a redis executable. Did you run make install yet?"
67 exit 1
68 fi
69
70fi
71
72#render the tmplates
73TMP_FILE="/tmp/$REDIS_PORT.conf"
74TPL_FILE="./redis.conf.tpl"
75INIT_TPL_FILE="./redis_init_script.tpl"
76INIT_SCRIPT_DEST="/etc/init.d/redis_$REDIS_PORT"
77
78#check the default for redis cli
79CLI_EXEC=`which redis-cli`
80if [ ! "$CLI_EXEC" ] ; then
81 CLI_EXEC=`dirname $REDIS_EXECUTABLE`"/redis-cli"
82fi
83
84#Generate config file from template
85echo "## Generated by install_server.sh ##" > $TMP_FILE
86cat $TPL_FILE | while read line; do eval "echo \"$line\"" >> $TMP_FILE; done
87cp -f $TMP_FILE $REDIS_CONFIG_FILE || exit 1
88
89#Generate sample script from template file
90rm -f $TMP_FILE
91
92#we hard code the configs here to avoid issues with templates containing env vars
93#kinda lame but works!
94REDIS_INIT_HEADER=\
95"#/bin/sh\n
96#Configurations injected by install_server below....\n\n
97EXEC=$REDIS_EXECUTABLE\n
98CLIEXEC=$CLI_EXEC\n
99PIDFILE=/var/run/redis_${REDIS_PORT}.pid\n
100CONF=\"$REDIS_CONFIG_FILE\"\n\n
101###############\n\n"
102
103#combine the header and the template (which is actually a static footer)
104echo $REDIS_INIT_HEADER > $TMP_FILE && cat $INIT_TPL_FILE >> $TMP_FILE || die "Could not write init script to $TMP_FILE"
105
106#copy to /etc/init.d
107cp -f $TMP_FILE $INIT_SCRIPT_DEST || die "Could not copy redis init script to $INIT_SCRIPT_DEST"
108echo "Copied $TMP_FILE => $INIT_SCRIPT_DEST"
109
110#Install the service
111echo "Installing service..."
112update-rc.d redis_$REDIS_PORT defaults && echo "Success!"
113/etc/init.d/redis_$REDIS_PORT start || die "Failed starting service..."
114
115#tada
116echo "Installation successful!"
117exit 0
118
119
120
121