]>
Commit | Line | Data |
---|---|---|
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 | ||
10 | die () { | |
11 | echo "ERROR: $1. Aborting!" | |
12 | exit 1 | |
13 | } | |
14 | ||
15 | ||
16 | #Initial defaults | |
17 | _REDIS_PORT=6379 | |
18 | ||
19 | echo "Welcome to the redis service installer" | |
20 | echo "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" | |
25 | if [ `whoami` != "root" ] ; then | |
26 | echo "You must run this script as root. Sorry!" | |
27 | exit 1 | |
28 | fi | |
29 | ||
30 | #Read the redis port | |
31 | read -p "Please select the redis port for this instance: [$_REDIS_PORT] " REDIS_PORT | |
32 | if [ ! `echo $REDIS_PORT | egrep "^[0-9]+\$"` ] ; then | |
33 | echo "Selecting default: $_REDIS_PORT" | |
34 | REDIS_PORT=$_REDIS_PORT | |
35 | fi | |
36 | ||
37 | #read the redis config file | |
38 | _REDIS_CONFIG_FILE="/etc/redis/$REDIS_PORT.conf" | |
39 | read -p "Please select the redis config file name [$_REDIS_CONFIG_FILE] " REDIS_CONFIG_FILE | |
40 | if [ !"$REDIS_CONFIG_FILE" ] ; then | |
41 | REDIS_CONFIG_FILE=$_REDIS_CONFIG_FILE | |
42 | echo "Selected default - $REDIS_CONFIG_FILE" | |
43 | fi | |
44 | #try and create it | |
45 | mkdir -p `dirname "$REDIS_CONFIG_FILE"` || die "Could not create redis config directory" | |
46 | ||
47 | #read the redis log file path | |
48 | _REDIS_LOG_FILE="/var/log/redis_$REDIS_PORT.log" | |
49 | read -p "Please select the redis log file name [$_REDIS_LOG_FILE] " REDIS_LOG_FILE | |
50 | if [ !"$REDIS_LOG_FILE" ] ; then | |
51 | REDIS_LOG_FILE=$_REDIS_LOG_FILE | |
52 | echo "Selected default - $REDIS_LOG_FILE" | |
53 | fi | |
54 | ||
55 | ||
56 | #get the redis data directory | |
57 | _REDIS_DATA_DIR="/var/lib/redis/$REDIS_PORT" | |
58 | read -p "Please select the data directory for this instance [$_REDIS_DATA_DIR] " REDIS_DATA_DIR | |
59 | if [ !"$REDIS_DATA_DIR" ] ; then | |
60 | REDIS_DATA_DIR=$_REDIS_DATA_DIR | |
61 | echo "Selected default - $REDIS_DATA_DIR" | |
62 | fi | |
63 | mkdir -p $REDIS_DATA_DIR || die "Could not create redis data directory" | |
64 | ||
65 | #get the redis executable path | |
66 | _REDIS_EXECUTABLE=`which redis-server` | |
67 | read -p "Please select the redis executable path [$_REDIS_EXECUTABLE] " REDIS_EXECUTABLE | |
68 | if [ ! -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 | ||
76 | fi | |
77 | ||
78 | #render the tmplates | |
79 | TMP_FILE="/tmp/$REDIS_PORT.conf" | |
80 | TPL_FILE="./redis.conf.tpl" | |
81 | INIT_TPL_FILE="./redis_init_script.tpl" | |
82 | INIT_SCRIPT_DEST="/etc/init.d/redis_$REDIS_PORT" | |
83 | ||
84 | #check the default for redis cli | |
85 | CLI_EXEC=`which redis-cli` | |
86 | if [ ! "$CLI_EXEC" ] ; then | |
87 | CLI_EXEC=`dirname $REDIS_EXECUTABLE`"/redis-cli" | |
88 | fi | |
89 | ||
90 | #Generate config file from template | |
91 | echo "## Generated by install_server.sh ##" > $TMP_FILE | |
92 | cat $TPL_FILE | while read line; do eval "echo \"$line\"" >> $TMP_FILE; done | |
93 | cp -f $TMP_FILE $REDIS_CONFIG_FILE || exit 1 | |
94 | ||
95 | #Generate sample script from template file | |
96 | rm -f $TMP_FILE | |
97 | ||
98 | #we hard code the configs here to avoid issues with templates containing env vars | |
99 | #kinda lame but works! | |
100 | REDIS_INIT_HEADER=\ | |
101 | "#/bin/sh\n | |
102 | #Configurations injected by install_server below....\n\n | |
103 | EXEC=$REDIS_EXECUTABLE\n | |
104 | CLIEXEC=$CLI_EXEC\n | |
105 | PIDFILE=/var/run/redis_${REDIS_PORT}.pid\n | |
106 | CONF=\"$REDIS_CONFIG_FILE\"\n\n | |
107 | ###############\n\n" | |
108 | ||
109 | #combine the header and the template (which is actually a static footer) | |
110 | echo $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 | |
113 | cp -f $TMP_FILE $INIT_SCRIPT_DEST && chmod +x $INIT_SCRIPT_DEST || die "Could not copy redis init script to $INIT_SCRIPT_DEST" | |
114 | echo "Copied $TMP_FILE => $INIT_SCRIPT_DEST" | |
115 | ||
116 | #Install the service | |
117 | echo "Installing service..." | |
118 | update-rc.d redis_$REDIS_PORT defaults && echo "Success!" | |
119 | /etc/init.d/redis_$REDIS_PORT start || die "Failed starting service..." | |
120 | ||
121 | #tada | |
122 | echo "Installation successful!" | |
123 | exit 0 | |
124 | ||
125 | ||
126 | ||
127 |