30
30
# this scripts should be run as root
31
31
32
32
die () {
33
- echo " ERROR: $1 . Aborting!"
33
+ echo " ERROR: $1 . Aborting!"
34
34
exit 1
35
35
}
36
36
37
+
38
+ # Absolute path to this script
39
+ SCRIPT=$( readlink -f $0 )
40
+ # Absolute path this script is in
41
+ SCRIPTPATH=$( dirname $SCRIPT )
42
+
37
43
# Initial defaults
38
44
_REDIS_PORT=6379
39
45
40
46
echo " Welcome to the redis service installer"
41
- echo " This script will help you easily set up a running redis server
42
-
43
- "
47
+ echo " This script will help you easily set up a running redis server"
48
+ echo
44
49
45
- # check for root user TODO: replace this with a call to "id"
46
- if [ ` whoami ` != " root " ] ; then
50
+ # check for root user
51
+ if [ " $( id -u ) " -ne 0 ] ; then
47
52
echo " You must run this script as root. Sorry!"
48
53
exit 1
49
54
fi
50
55
51
-
52
56
# Read the redis port
53
- read -p " Please select the redis port for this instance: [$_REDIS_PORT ] " REDIS_PORT
54
- if [ ! ` echo $REDIS_PORT | egrep " ^[0-9]+\$ " ` ] ; then
57
+ read -p " Please select the redis port for this instance: [$_REDIS_PORT ] " REDIS_PORT
58
+ if ! echo $REDIS_PORT | egrep -q ' ^[0-9]+$ ' ; then
55
59
echo " Selecting default: $_REDIS_PORT "
56
- REDIS_PORT=$_REDIS_PORT
60
+ REDIS_PORT=$_REDIS_PORT
57
61
fi
58
62
59
63
# read the redis config file
60
64
_REDIS_CONFIG_FILE=" /etc/redis/$REDIS_PORT .conf"
61
65
read -p " Please select the redis config file name [$_REDIS_CONFIG_FILE ] " REDIS_CONFIG_FILE
62
- if [ ! " $REDIS_CONFIG_FILE " ] ; then
66
+ if [ -z " $REDIS_CONFIG_FILE " ] ; then
63
67
REDIS_CONFIG_FILE=$_REDIS_CONFIG_FILE
64
68
echo " Selected default - $REDIS_CONFIG_FILE "
65
69
fi
66
- # try and create it
67
- mkdir -p ` dirname " $REDIS_CONFIG_FILE " ` || die " Could not create redis config directory"
68
70
69
71
# read the redis log file path
70
72
_REDIS_LOG_FILE=" /var/log/redis_$REDIS_PORT .log"
71
73
read -p " Please select the redis log file name [$_REDIS_LOG_FILE ] " REDIS_LOG_FILE
72
- if [ ! " $REDIS_LOG_FILE " ] ; then
74
+ if [ -z " $REDIS_LOG_FILE " ] ; then
73
75
REDIS_LOG_FILE=$_REDIS_LOG_FILE
74
76
echo " Selected default - $REDIS_LOG_FILE "
75
77
fi
78
80
# get the redis data directory
79
81
_REDIS_DATA_DIR=" /var/lib/redis/$REDIS_PORT "
80
82
read -p " Please select the data directory for this instance [$_REDIS_DATA_DIR ] " REDIS_DATA_DIR
81
- if [ ! " $REDIS_DATA_DIR " ] ; then
83
+ if [ -z " $REDIS_DATA_DIR " ] ; then
82
84
REDIS_DATA_DIR=$_REDIS_DATA_DIR
83
85
echo " Selected default - $REDIS_DATA_DIR "
84
86
fi
85
- mkdir -p $REDIS_DATA_DIR || die " Could not create redis data directory"
86
87
87
88
# get the redis executable path
88
- _REDIS_EXECUTABLE=` which redis-server`
89
+ _REDIS_EXECUTABLE=` command -v redis-server`
89
90
read -p " Please select the redis executable path [$_REDIS_EXECUTABLE ] " REDIS_EXECUTABLE
90
- if [ ! -f " $REDIS_EXECUTABLE " ] ; then
91
+ if [ ! -x " $REDIS_EXECUTABLE " ] ; then
91
92
REDIS_EXECUTABLE=$_REDIS_EXECUTABLE
92
-
93
- if [ ! -f " $REDIS_EXECUTABLE " ] ; then
93
+
94
+ if [ ! -x " $REDIS_EXECUTABLE " ] ; then
94
95
echo " Mmmmm... it seems like you don't have a redis executable. Did you run make install yet?"
95
96
exit 1
96
97
fi
97
-
98
98
fi
99
99
100
+ # check the default for redis cli
101
+ CLI_EXEC=` command -v redis-cli`
102
+ if [ -z " $CLI_EXEC " ] ; then
103
+ CLI_EXEC=` dirname $REDIS_EXECUTABLE ` " /redis-cli"
104
+ fi
100
105
101
- # render the tmplates
102
- TMP_FILE=" /tmp/$REDIS_PORT .conf"
103
- DEFAULT_CONFIG=" ../redis.conf"
104
- INIT_TPL_FILE=" ./redis_init_script.tpl"
105
- INIT_SCRIPT_DEST=" /etc/init.d/redis_$REDIS_PORT "
106
- PIDFILE=" /var/run/redis_$REDIS_PORT .pid"
106
+ echo " Selected config:"
107
107
108
+ echo " Port : $REDIS_PORT "
109
+ echo " Config file : $REDIS_CONFIG_FILE "
110
+ echo " Log file : $REDIS_LOG_FILE "
111
+ echo " Data dir : $REDIS_DATA_DIR "
112
+ echo " Executable : $REDIS_EXECUTABLE "
113
+ echo " Cli Executable : $CLI_EXEC "
108
114
115
+ read -p " Is this ok? Then press ENTER to go on or Ctrl-C to abort." _UNUSED_
109
116
110
- # check the default for redis cli
111
- CLI_EXEC=` which redis-cli`
112
- if [ ! " $CLI_EXEC " ] ; then
113
- CLI_EXEC=` dirname $REDIS_EXECUTABLE ` " /redis-cli"
117
+ mkdir -p ` dirname " $REDIS_CONFIG_FILE " ` || die " Could not create redis config directory"
118
+ mkdir -p ` dirname " $REDIS_LOG_FILE " ` || die " Could not create redis log dir"
119
+ mkdir -p " $REDIS_DATA_DIR " || die " Could not create redis data directory"
120
+
121
+ # render the templates
122
+ TMP_FILE=" /tmp/${REDIS_PORT} .conf"
123
+ DEFAULT_CONFIG=" ${SCRIPTPATH} /../redis.conf"
124
+ INIT_TPL_FILE=" ${SCRIPTPATH} /redis_init_script.tpl"
125
+ INIT_SCRIPT_DEST=" /etc/init.d/redis_${REDIS_PORT} "
126
+ PIDFILE=" /var/run/redis_${REDIS_PORT} .pid"
127
+
128
+ if [ ! -f " $DEFAULT_CONFIG " ]; then
129
+ echo " Mmmmm... the default config is missing. Did you switch to the utils directory?"
130
+ exit 1
114
131
fi
115
132
116
133
# Generate config file from the default config file as template
117
134
# changing only the stuff we're controlling from this script
118
135
echo " ## Generated by install_server.sh ##" > $TMP_FILE
119
136
120
- SED_EXPR=" s#^port [0-9]{4}\$ #port ${REDIS_PORT} #;\
121
- s#^logfile .+\$ #logfile ${REDIS_LOG_FILE} #;\
122
- s#^dir .+\$ #dir ${REDIS_DATA_DIR} #;\
123
- s#^pidfile .+\$ #pidfile ${PIDFILE} #;\
124
- s#^daemonize no\$ #daemonize yes#;"
125
- echo $SED_EXPR
137
+ read -r SED_EXPR << -EOF
138
+ s#^port [0-9]{4}\$ #port ${REDIS_PORT} #; \
139
+ s#^logfile .+\$ #logfile ${REDIS_LOG_FILE} #; \
140
+ s#^dir .+\$ #dir ${REDIS_DATA_DIR} #; \
141
+ s#^pidfile .+\$ #pidfile ${PIDFILE} #; \
142
+ s#^daemonize no\$ #daemonize yes#;
143
+ EOF
126
144
sed -r " $SED_EXPR " $DEFAULT_CONFIG >> $TMP_FILE
127
145
128
146
# cat $TPL_FILE | while read line; do eval "echo \"$line\"" >> $TMP_FILE; done
129
- cp -f $TMP_FILE $REDIS_CONFIG_FILE || exit 1
147
+ cp $TMP_FILE $REDIS_CONFIG_FILE || die " Could not write redis config file $REDIS_CONFIG_FILE "
130
148
131
149
# Generate sample script from template file
132
150
rm -f $TMP_FILE
@@ -138,53 +156,90 @@ REDIS_INIT_HEADER=\
138
156
#Configurations injected by install_server below....\n\n
139
157
EXEC=$REDIS_EXECUTABLE \n
140
158
CLIEXEC=$CLI_EXEC \n
141
- PIDFILE=$PIDFILE \n
159
+ PIDFILE=\" $PIDFILE \" \n
142
160
CONF=\" $REDIS_CONFIG_FILE \" \n\n
143
161
REDISPORT=\" $REDIS_PORT \" \n\n
144
162
###############\n\n"
145
163
146
164
REDIS_CHKCONFIG_INFO=\
147
165
" # REDHAT chkconfig header\n\n
148
166
# chkconfig: - 58 74\n
149
- # description: redis_6379 is the redis daemon.\n
167
+ # description: redis_ ${REDIS_PORT} is the redis daemon.\n
150
168
### BEGIN INIT INFO\n
151
169
# Provides: redis_6379\n
152
- # Required-Start: $network $local_fs $remote_fs \n
153
- # Required-Stop: $network $local_fs $remote_fs \n
170
+ # Required-Start: \ $ network \ $ local_fs \ $ remote_fs\n
171
+ # Required-Stop: \ $ network \ $ local_fs \ $ remote_fs\n
154
172
# Default-Start: 2 3 4 5\n
155
173
# Default-Stop: 0 1 6\n
156
- # Should-Start: $syslog $named \n
157
- # Should-Stop: $syslog $named \n
158
- # Short-Description: start and stop redis_6379 \n
174
+ # Should-Start: \ $ syslog \ $ named\n
175
+ # Should-Stop: \ $ syslog \ $ named\n
176
+ # Short-Description: start and stop redis_ ${REDIS_PORT} \n
159
177
# Description: Redis daemon\n
160
178
### END INIT INFO\n\n"
161
179
162
- if [ ! ` which chkconfig` ] ; then
163
- # combine the header and the template (which is actually a static footer)
164
- echo $REDIS_INIT_HEADER > $TMP_FILE && cat $INIT_TPL_FILE >> $TMP_FILE || die " Could not write init script to $TMP_FILE "
165
- else
180
+ if command -v chkconfig > /dev/null; then
166
181
# if we're a box with chkconfig on it we want to include info for chkconfig
167
- echo -e $REDIS_INIT_HEADER $REDIS_CHKCONFIG_INFO > $TMP_FILE && cat $INIT_TPL_FILE >> $TMP_FILE || die " Could not write init script to $TMP_FILE "
182
+ echo " $REDIS_INIT_HEADER " " $REDIS_CHKCONFIG_INFO " > $TMP_FILE && cat $INIT_TPL_FILE >> $TMP_FILE || die " Could not write init script to $TMP_FILE "
183
+ else
184
+ # combine the header and the template (which is actually a static footer)
185
+ echo " $REDIS_INIT_HEADER " > $TMP_FILE && cat $INIT_TPL_FILE >> $TMP_FILE || die " Could not write init script to $TMP_FILE "
168
186
fi
169
187
188
+ # ##
189
+ # Generate sample script from template file
190
+ # - No need to check which system we are on. The init info are comments and
191
+ # do not interfere with update_rc.d systems. Additionally:
192
+ # Ubuntu/debian by default does not come with chkconfig, but does issue a
193
+ # warning if init info is not available.
194
+
195
+ cat > ${TMP_FILE} << EOT
196
+ #/bin/sh
197
+ #Configurations injected by install_server below....
198
+
199
+ EXEC=$REDIS_EXECUTABLE
200
+ CLIEXEC=$CLI_EXEC
201
+ PIDFILE=$PIDFILE
202
+ CONF="$REDIS_CONFIG_FILE "
203
+ REDISPORT="$REDIS_PORT "
204
+ ###############
205
+ # SysV Init Information
206
+ # chkconfig: - 58 74
207
+ # description: redis_${REDIS_PORT} is the redis daemon.
208
+ ### BEGIN INIT INFO
209
+ # Provides: redis_${REDIS_PORT}
210
+ # Required-Start: \$ network \$ local_fs \$ remote_fs
211
+ # Required-Stop: \$ network \$ local_fs \$ remote_fs
212
+ # Default-Start: 2 3 4 5
213
+ # Default-Stop: 0 1 6
214
+ # Should-Start: \$ syslog \$ named
215
+ # Should-Stop: \$ syslog \$ named
216
+ # Short-Description: start and stop redis_${REDIS_PORT}
217
+ # Description: Redis daemon
218
+ ### END INIT INFO
219
+
220
+ EOT
221
+ cat ${INIT_TPL_FILE} >> ${TMP_FILE}
222
+
170
223
# copy to /etc/init.d
171
- cp -f $TMP_FILE $INIT_SCRIPT_DEST && chmod +x $INIT_SCRIPT_DEST || die " Could not copy redis init script to $INIT_SCRIPT_DEST "
224
+ cp $TMP_FILE $INIT_SCRIPT_DEST && \
225
+ chmod +x $INIT_SCRIPT_DEST || die " Could not copy redis init script to $INIT_SCRIPT_DEST "
172
226
echo " Copied $TMP_FILE => $INIT_SCRIPT_DEST "
173
227
174
228
# Install the service
175
229
echo " Installing service..."
176
- if [ ! ` which chkconfig` ] ; then
230
+ if command -v chkconfig > /dev/null 2>&1 ; then
231
+ # we're chkconfig, so lets add to chkconfig and put in runlevel 345
232
+ chkconfig --add redis_${REDIS_PORT} && echo " Successfully added to chkconfig!"
233
+ chkconfig --level 345 redis_${REDIS_PORT} on && echo " Successfully added to runlevels 345!"
234
+ elif command -v update-rc.d > /dev/null 2>&1 ; then
177
235
# if we're not a chkconfig box assume we're able to use update-rc.d
178
- update-rc.d redis_$REDIS_PORT defaults && echo " Success!"
236
+ update-rc.d redis_${ REDIS_PORT} defaults && echo " Success!"
179
237
else
180
- # we're chkconfig, so lets add to chkconfig and put in runlevel 345
181
- chkconfig --add redis_$REDIS_PORT && echo " Successfully added to chkconfig!"
182
- chkconfig --level 345 redis_$REDIS_PORT on && echo " Successfully added to runlevels 345!"
238
+ echo " No supported init tool found."
183
239
fi
184
-
240
+
185
241
/etc/init.d/redis_$REDIS_PORT start || die " Failed starting service..."
186
242
187
243
# tada
188
244
echo " Installation successful!"
189
245
exit 0
190
-
0 commit comments