]> git.saurik.com Git - redis.git/blame - utils/install_server.sh
fixes to install script and template
[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
001f8da2 44#try and create it
9210e701 45mkdir -p `dirname "$REDIS_CONFIG_FILE"` || die "Could not create redis config directory"
46
001f8da2 47#read the redis log file path
48_REDIS_LOG_FILE="/var/log/redis_$REDIS_PORT.log"
49read -p "Please select the redis log file name [$_REDIS_LOG_FILE] " REDIS_LOG_FILE
50if [ !"$REDIS_LOG_FILE" ] ; then
51 REDIS_LOG_FILE=$_REDIS_LOG_FILE
52 echo "Selected default - $REDIS_LOG_FILE"
53fi
54
55
9210e701 56#get the redis data directory
57_REDIS_DATA_DIR="/var/lib/redis/$REDIS_PORT"
58read -p "Please select the data directory for this instance [$_REDIS_DATA_DIR] " REDIS_DATA_DIR
59if [ !"$REDIS_DATA_DIR" ] ; then
60 REDIS_DATA_DIR=$_REDIS_DATA_DIR
61 echo "Selected default - $REDIS_DATA_DIR"
62fi
001f8da2 63mkdir -p $REDIS_DATA_DIR || die "Could not create redis data directory"
9210e701 64
65#get the redis executable path
66_REDIS_EXECUTABLE=`which redis-server`
67read -p "Please select the redis executable path [$_REDIS_EXECUTABLE] " REDIS_EXECUTABLE
68if [ ! -f "$REDIS_EXECUTABLE" ] ; then
69 REDIS_EXECUTABLE=$_REDIS_EXECUTABLE
70
71 if [ ! -f "$REDIS_EXECUTABLE" ] ; then
72 echo "Mmmmm... it seems like you don't have a redis executable. Did you run make install yet?"
73 exit 1
74 fi
75
76fi
77
78#render the tmplates
79TMP_FILE="/tmp/$REDIS_PORT.conf"
80TPL_FILE="./redis.conf.tpl"
81INIT_TPL_FILE="./redis_init_script.tpl"
82INIT_SCRIPT_DEST="/etc/init.d/redis_$REDIS_PORT"
83
84#check the default for redis cli
85CLI_EXEC=`which redis-cli`
86if [ ! "$CLI_EXEC" ] ; then
87 CLI_EXEC=`dirname $REDIS_EXECUTABLE`"/redis-cli"
88fi
89
90#Generate config file from template
91echo "## Generated by install_server.sh ##" > $TMP_FILE
92cat $TPL_FILE | while read line; do eval "echo \"$line\"" >> $TMP_FILE; done
93cp -f $TMP_FILE $REDIS_CONFIG_FILE || exit 1
94
95#Generate sample script from template file
96rm -f $TMP_FILE
97
98#we hard code the configs here to avoid issues with templates containing env vars
99#kinda lame but works!
100REDIS_INIT_HEADER=\
101"#/bin/sh\n
102#Configurations injected by install_server below....\n\n
103EXEC=$REDIS_EXECUTABLE\n
104CLIEXEC=$CLI_EXEC\n
105PIDFILE=/var/run/redis_${REDIS_PORT}.pid\n
106CONF=\"$REDIS_CONFIG_FILE\"\n\n
107###############\n\n"
108
109#combine the header and the template (which is actually a static footer)
110echo $REDIS_INIT_HEADER > $TMP_FILE && cat $INIT_TPL_FILE >> $TMP_FILE || die "Could not write init script to $TMP_FILE"
111
112#copy to /etc/init.d
001f8da2 113cp -f $TMP_FILE $INIT_SCRIPT_DEST && chmod +x $INIT_SCRIPT_DEST || die "Could not copy redis init script to $INIT_SCRIPT_DEST"
9210e701 114echo "Copied $TMP_FILE => $INIT_SCRIPT_DEST"
115
116#Install the service
117echo "Installing service..."
118update-rc.d redis_$REDIS_PORT defaults && echo "Success!"
119/etc/init.d/redis_$REDIS_PORT start || die "Failed starting service..."
120
121#tada
122echo "Installation successful!"
123exit 0
124
125
126
127