]> git.saurik.com Git - redis.git/blame - utils/install_server.sh
Merge pull request #114 from pietern/unstable-zcount
[redis.git] / utils / install_server.sh
CommitLineData
9210e701 1#! /bin/sh
78f56a5a 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 modification, are
6# permitted provided that the following conditions are met:
c01043ba 7#
78f56a5a 8# 1. Redistributions of source code must retain the above copyright notice, this list of
9# conditions and the following disclaimer.
c01043ba 10#
78f56a5a 11# 2. Redistributions in binary form must reproduce the above copyright notice, this list
12# of conditions and the following disclaimer in the documentation and/or other materials
13# provided with the distribution.
c01043ba 14#
78f56a5a 15# THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED
16# WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
17# FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL Dvir Volk OR
18# CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
19# CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
20# SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
21# ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
22# NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
23# ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
c01043ba 24#
c01043ba 25#
26# # # # # # # # # # # # # # # # # # # # # # # # # #
27#
9210e701 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#
9210e701 32
33
34die () {
35 echo "ERROR: $1. Aborting!"
36 exit 1
37}
38
39
40#Initial defaults
41_REDIS_PORT=6379
42
43echo "Welcome to the redis service installer"
44echo "This script will help you easily set up a running redis server
45
46"
47
48#check for root user TODO: replace this with a call to "id"
49if [ `whoami` != "root" ] ; then
f1e60d75 50 echo "ERROR: You must run this installation script as root. Sorry!\n"
9210e701 51 exit 1
52fi
53
54#Read the redis port
55read -p "Please select the redis port for this instance: [$_REDIS_PORT] " REDIS_PORT
56if [ ! `echo $REDIS_PORT | egrep "^[0-9]+\$"` ] ; then
57 echo "Selecting default: $_REDIS_PORT"
58 REDIS_PORT=$_REDIS_PORT
59fi
60
61#read the redis config file
62_REDIS_CONFIG_FILE="/etc/redis/$REDIS_PORT.conf"
63read -p "Please select the redis config file name [$_REDIS_CONFIG_FILE] " REDIS_CONFIG_FILE
64if [ !"$REDIS_CONFIG_FILE" ] ; then
65 REDIS_CONFIG_FILE=$_REDIS_CONFIG_FILE
66 echo "Selected default - $REDIS_CONFIG_FILE"
67fi
001f8da2 68#try and create it
9210e701 69mkdir -p `dirname "$REDIS_CONFIG_FILE"` || die "Could not create redis config directory"
70
001f8da2 71#read the redis log file path
72_REDIS_LOG_FILE="/var/log/redis_$REDIS_PORT.log"
73read -p "Please select the redis log file name [$_REDIS_LOG_FILE] " REDIS_LOG_FILE
74if [ !"$REDIS_LOG_FILE" ] ; then
75 REDIS_LOG_FILE=$_REDIS_LOG_FILE
76 echo "Selected default - $REDIS_LOG_FILE"
77fi
78
79
9210e701 80#get the redis data directory
81_REDIS_DATA_DIR="/var/lib/redis/$REDIS_PORT"
82read -p "Please select the data directory for this instance [$_REDIS_DATA_DIR] " REDIS_DATA_DIR
83if [ !"$REDIS_DATA_DIR" ] ; then
84 REDIS_DATA_DIR=$_REDIS_DATA_DIR
85 echo "Selected default - $REDIS_DATA_DIR"
86fi
001f8da2 87mkdir -p $REDIS_DATA_DIR || die "Could not create redis data directory"
9210e701 88
89#get the redis executable path
90_REDIS_EXECUTABLE=`which redis-server`
91read -p "Please select the redis executable path [$_REDIS_EXECUTABLE] " REDIS_EXECUTABLE
92if [ ! -f "$REDIS_EXECUTABLE" ] ; then
93 REDIS_EXECUTABLE=$_REDIS_EXECUTABLE
94
95 if [ ! -f "$REDIS_EXECUTABLE" ] ; then
96 echo "Mmmmm... it seems like you don't have a redis executable. Did you run make install yet?"
97 exit 1
98 fi
99
100fi
101
102#render the tmplates
103TMP_FILE="/tmp/$REDIS_PORT.conf"
104TPL_FILE="./redis.conf.tpl"
105INIT_TPL_FILE="./redis_init_script.tpl"
106INIT_SCRIPT_DEST="/etc/init.d/redis_$REDIS_PORT"
107
108#check the default for redis cli
109CLI_EXEC=`which redis-cli`
110if [ ! "$CLI_EXEC" ] ; then
111 CLI_EXEC=`dirname $REDIS_EXECUTABLE`"/redis-cli"
112fi
113
114#Generate config file from template
115echo "## Generated by install_server.sh ##" > $TMP_FILE
116cat $TPL_FILE | while read line; do eval "echo \"$line\"" >> $TMP_FILE; done
117cp -f $TMP_FILE $REDIS_CONFIG_FILE || exit 1
118
119#Generate sample script from template file
120rm -f $TMP_FILE
121
122#we hard code the configs here to avoid issues with templates containing env vars
123#kinda lame but works!
124REDIS_INIT_HEADER=\
125"#/bin/sh\n
126#Configurations injected by install_server below....\n\n
127EXEC=$REDIS_EXECUTABLE\n
128CLIEXEC=$CLI_EXEC\n
129PIDFILE=/var/run/redis_${REDIS_PORT}.pid\n
130CONF=\"$REDIS_CONFIG_FILE\"\n\n
131###############\n\n"
132
133#combine the header and the template (which is actually a static footer)
134echo $REDIS_INIT_HEADER > $TMP_FILE && cat $INIT_TPL_FILE >> $TMP_FILE || die "Could not write init script to $TMP_FILE"
135
136#copy to /etc/init.d
001f8da2 137cp -f $TMP_FILE $INIT_SCRIPT_DEST && chmod +x $INIT_SCRIPT_DEST || die "Could not copy redis init script to $INIT_SCRIPT_DEST"
9210e701 138echo "Copied $TMP_FILE => $INIT_SCRIPT_DEST"
139
140#Install the service
141echo "Installing service..."
142update-rc.d redis_$REDIS_PORT defaults && echo "Success!"
143/etc/init.d/redis_$REDIS_PORT start || die "Failed starting service..."
144
145#tada
146echo "Installation successful!"
147exit 0
148
149
150
151