]>
Commit | Line | Data |
---|---|---|
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 | ||
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 | ||
45 | #try and create | |
46 | mkdir -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" | |
50 | read -p "Please select the data directory for this instance [$_REDIS_DATA_DIR] " REDIS_DATA_DIR | |
51 | if [ !"$REDIS_DATA_DIR" ] ; then | |
52 | REDIS_DATA_DIR=$_REDIS_DATA_DIR | |
53 | echo "Selected default - $REDIS_DATA_DIR" | |
54 | fi | |
55 | #try the data directory setting | |
56 | mkdir -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` | |
61 | read -p "Please select the redis executable path [$_REDIS_EXECUTABLE] " REDIS_EXECUTABLE | |
62 | if [ ! -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 | ||
70 | fi | |
71 | ||
72 | #render the tmplates | |
73 | TMP_FILE="/tmp/$REDIS_PORT.conf" | |
74 | TPL_FILE="./redis.conf.tpl" | |
75 | INIT_TPL_FILE="./redis_init_script.tpl" | |
76 | INIT_SCRIPT_DEST="/etc/init.d/redis_$REDIS_PORT" | |
77 | ||
78 | #check the default for redis cli | |
79 | CLI_EXEC=`which redis-cli` | |
80 | if [ ! "$CLI_EXEC" ] ; then | |
81 | CLI_EXEC=`dirname $REDIS_EXECUTABLE`"/redis-cli" | |
82 | fi | |
83 | ||
84 | #Generate config file from template | |
85 | echo "## Generated by install_server.sh ##" > $TMP_FILE | |
86 | cat $TPL_FILE | while read line; do eval "echo \"$line\"" >> $TMP_FILE; done | |
87 | cp -f $TMP_FILE $REDIS_CONFIG_FILE || exit 1 | |
88 | ||
89 | #Generate sample script from template file | |
90 | rm -f $TMP_FILE | |
91 | ||
92 | #we hard code the configs here to avoid issues with templates containing env vars | |
93 | #kinda lame but works! | |
94 | REDIS_INIT_HEADER=\ | |
95 | "#/bin/sh\n | |
96 | #Configurations injected by install_server below....\n\n | |
97 | EXEC=$REDIS_EXECUTABLE\n | |
98 | CLIEXEC=$CLI_EXEC\n | |
99 | PIDFILE=/var/run/redis_${REDIS_PORT}.pid\n | |
100 | CONF=\"$REDIS_CONFIG_FILE\"\n\n | |
101 | ###############\n\n" | |
102 | ||
103 | #combine the header and the template (which is actually a static footer) | |
104 | echo $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 | |
107 | cp -f $TMP_FILE $INIT_SCRIPT_DEST || die "Could not copy redis init script to $INIT_SCRIPT_DEST" | |
108 | echo "Copied $TMP_FILE => $INIT_SCRIPT_DEST" | |
109 | ||
110 | #Install the service | |
111 | echo "Installing service..." | |
112 | update-rc.d redis_$REDIS_PORT defaults && echo "Success!" | |
113 | /etc/init.d/redis_$REDIS_PORT start || die "Failed starting service..." | |
114 | ||
115 | #tada | |
116 | echo "Installation successful!" | |
117 | exit 0 | |
118 | ||
119 | ||
120 | ||
121 |