install_server.sh 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281
  1. #!/bin/sh
  2. # Copyright 2011 Dvir Volk <dvirsk at gmail dot com>. All rights reserved.
  3. #
  4. # Redistribution and use in source and binary forms, with or without
  5. # modification, are permitted provided that the following conditions are met:
  6. #
  7. # 1. Redistributions of source code must retain the above copyright notice,
  8. # this list of conditions and the following disclaimer.
  9. #
  10. # 2. Redistributions in binary form must reproduce the above copyright
  11. # notice, this list of conditions and the following disclaimer in the
  12. # documentation and/or other materials provided with the distribution.
  13. #
  14. # THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED
  15. # WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
  16. # MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
  17. # EVENT SHALL Dvir Volk OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
  18. # INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  19. # LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
  20. # OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
  21. # LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  22. # NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
  23. # EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  24. #
  25. ################################################################################
  26. #
  27. # Service installer for redis server, runs interactively by default.
  28. #
  29. # To run this script non-interactively (for automation/provisioning purposes),
  30. # feed the variables into the script. Any missing variables will be prompted!
  31. # Tip: Environment variables also support command substitution (see REDIS_EXECUTABLE)
  32. #
  33. # Example:
  34. #
  35. # sudo REDIS_PORT=1234 \
  36. # REDIS_CONFIG_FILE=/etc/redis/1234.conf \
  37. # REDIS_LOG_FILE=/var/log/redis_1234.log \
  38. # REDIS_DATA_DIR=/var/lib/redis/1234 \
  39. # REDIS_EXECUTABLE=`command -v redis-server` ./utils/install_server.sh
  40. #
  41. # This generates a redis config file and an /etc/init.d script, and installs them.
  42. #
  43. # /!\ This script should be run as root
  44. #
  45. # NOTE: This script will not work on Mac OSX.
  46. # It supports Debian and Ubuntu Linux.
  47. #
  48. ################################################################################
  49. die () {
  50. echo "ERROR: $1. Aborting!"
  51. exit 1
  52. }
  53. #Absolute path to this script
  54. SCRIPT=$(readlink -f $0)
  55. #Absolute path this script is in
  56. SCRIPTPATH=$(dirname $SCRIPT)
  57. #Initial defaults
  58. _REDIS_PORT=6379
  59. _MANUAL_EXECUTION=false
  60. echo "Welcome to the redis service installer"
  61. echo "This script will help you easily set up a running redis server"
  62. echo
  63. #check for root user
  64. if [ "$(id -u)" -ne 0 ] ; then
  65. echo "You must run this script as root. Sorry!"
  66. exit 1
  67. fi
  68. if ! echo $REDIS_PORT | egrep -q '^[0-9]+$' ; then
  69. _MANUAL_EXECUTION=true
  70. #Read the redis port
  71. read -p "Please select the redis port for this instance: [$_REDIS_PORT] " REDIS_PORT
  72. if ! echo $REDIS_PORT | egrep -q '^[0-9]+$' ; then
  73. echo "Selecting default: $_REDIS_PORT"
  74. REDIS_PORT=$_REDIS_PORT
  75. fi
  76. fi
  77. if [ -z "$REDIS_CONFIG_FILE" ] ; then
  78. _MANUAL_EXECUTION=true
  79. #read the redis config file
  80. _REDIS_CONFIG_FILE="/etc/redis/$REDIS_PORT.conf"
  81. read -p "Please select the redis config file name [$_REDIS_CONFIG_FILE] " REDIS_CONFIG_FILE
  82. if [ -z "$REDIS_CONFIG_FILE" ] ; then
  83. REDIS_CONFIG_FILE=$_REDIS_CONFIG_FILE
  84. echo "Selected default - $REDIS_CONFIG_FILE"
  85. fi
  86. fi
  87. if [ -z "$REDIS_LOG_FILE" ] ; then
  88. _MANUAL_EXECUTION=true
  89. #read the redis log file path
  90. _REDIS_LOG_FILE="/var/log/redis_$REDIS_PORT.log"
  91. read -p "Please select the redis log file name [$_REDIS_LOG_FILE] " REDIS_LOG_FILE
  92. if [ -z "$REDIS_LOG_FILE" ] ; then
  93. REDIS_LOG_FILE=$_REDIS_LOG_FILE
  94. echo "Selected default - $REDIS_LOG_FILE"
  95. fi
  96. fi
  97. if [ -z "$REDIS_DATA_DIR" ] ; then
  98. _MANUAL_EXECUTION=true
  99. #get the redis data directory
  100. _REDIS_DATA_DIR="/var/lib/redis/$REDIS_PORT"
  101. read -p "Please select the data directory for this instance [$_REDIS_DATA_DIR] " REDIS_DATA_DIR
  102. if [ -z "$REDIS_DATA_DIR" ] ; then
  103. REDIS_DATA_DIR=$_REDIS_DATA_DIR
  104. echo "Selected default - $REDIS_DATA_DIR"
  105. fi
  106. fi
  107. if [ ! -x "$REDIS_EXECUTABLE" ] ; then
  108. _MANUAL_EXECUTION=true
  109. #get the redis executable path
  110. _REDIS_EXECUTABLE=`command -v redis-server`
  111. read -p "Please select the redis executable path [$_REDIS_EXECUTABLE] " REDIS_EXECUTABLE
  112. if [ ! -x "$REDIS_EXECUTABLE" ] ; then
  113. REDIS_EXECUTABLE=$_REDIS_EXECUTABLE
  114. if [ ! -x "$REDIS_EXECUTABLE" ] ; then
  115. echo "Mmmmm... it seems like you don't have a redis executable. Did you run make install yet?"
  116. exit 1
  117. fi
  118. fi
  119. fi
  120. #check the default for redis cli
  121. CLI_EXEC=`command -v redis-cli`
  122. if [ -z "$CLI_EXEC" ] ; then
  123. CLI_EXEC=`dirname $REDIS_EXECUTABLE`"/redis-cli"
  124. fi
  125. echo "Selected config:"
  126. echo "Port : $REDIS_PORT"
  127. echo "Config file : $REDIS_CONFIG_FILE"
  128. echo "Log file : $REDIS_LOG_FILE"
  129. echo "Data dir : $REDIS_DATA_DIR"
  130. echo "Executable : $REDIS_EXECUTABLE"
  131. echo "Cli Executable : $CLI_EXEC"
  132. if $_MANUAL_EXECUTION == true ; then
  133. read -p "Is this ok? Then press ENTER to go on or Ctrl-C to abort." _UNUSED_
  134. fi
  135. mkdir -p `dirname "$REDIS_CONFIG_FILE"` || die "Could not create redis config directory"
  136. mkdir -p `dirname "$REDIS_LOG_FILE"` || die "Could not create redis log dir"
  137. mkdir -p "$REDIS_DATA_DIR" || die "Could not create redis data directory"
  138. #render the templates
  139. TMP_FILE="/tmp/${REDIS_PORT}.conf"
  140. DEFAULT_CONFIG="${SCRIPTPATH}/../redis.conf"
  141. INIT_TPL_FILE="${SCRIPTPATH}/redis_init_script.tpl"
  142. INIT_SCRIPT_DEST="/etc/init.d/redis_${REDIS_PORT}"
  143. PIDFILE="/var/run/redis_${REDIS_PORT}.pid"
  144. if [ ! -f "$DEFAULT_CONFIG" ]; then
  145. echo "Mmmmm... the default config is missing. Did you switch to the utils directory?"
  146. exit 1
  147. fi
  148. #Generate config file from the default config file as template
  149. #changing only the stuff we're controlling from this script
  150. echo "## Generated by install_server.sh ##" > $TMP_FILE
  151. read -r SED_EXPR <<-EOF
  152. s#^port .\+#port ${REDIS_PORT}#; \
  153. s#^logfile .\+#logfile ${REDIS_LOG_FILE}#; \
  154. s#^dir .\+#dir ${REDIS_DATA_DIR}#; \
  155. s#^pidfile .\+#pidfile ${PIDFILE}#; \
  156. s#^daemonize no#daemonize yes#;
  157. EOF
  158. sed "$SED_EXPR" $DEFAULT_CONFIG >> $TMP_FILE
  159. #cat $TPL_FILE | while read line; do eval "echo \"$line\"" >> $TMP_FILE; done
  160. cp $TMP_FILE $REDIS_CONFIG_FILE || die "Could not write redis config file $REDIS_CONFIG_FILE"
  161. #Generate sample script from template file
  162. rm -f $TMP_FILE
  163. #we hard code the configs here to avoid issues with templates containing env vars
  164. #kinda lame but works!
  165. REDIS_INIT_HEADER=\
  166. "#!/bin/sh\n
  167. #Configurations injected by install_server below....\n\n
  168. EXEC=$REDIS_EXECUTABLE\n
  169. CLIEXEC=$CLI_EXEC\n
  170. PIDFILE=\"$PIDFILE\"\n
  171. CONF=\"$REDIS_CONFIG_FILE\"\n\n
  172. REDISPORT=\"$REDIS_PORT\"\n\n
  173. ###############\n\n"
  174. REDIS_CHKCONFIG_INFO=\
  175. "# REDHAT chkconfig header\n\n
  176. # chkconfig: - 58 74\n
  177. # description: redis_${REDIS_PORT} is the redis daemon.\n
  178. ### BEGIN INIT INFO\n
  179. # Provides: redis_6379\n
  180. # Required-Start: \$network \$local_fs \$remote_fs\n
  181. # Required-Stop: \$network \$local_fs \$remote_fs\n
  182. # Default-Start: 2 3 4 5\n
  183. # Default-Stop: 0 1 6\n
  184. # Should-Start: \$syslog \$named\n
  185. # Should-Stop: \$syslog \$named\n
  186. # Short-Description: start and stop redis_${REDIS_PORT}\n
  187. # Description: Redis daemon\n
  188. ### END INIT INFO\n\n"
  189. if command -v chkconfig >/dev/null; then
  190. #if we're a box with chkconfig on it we want to include info for chkconfig
  191. echo "$REDIS_INIT_HEADER" "$REDIS_CHKCONFIG_INFO" > $TMP_FILE && cat $INIT_TPL_FILE >> $TMP_FILE || die "Could not write init script to $TMP_FILE"
  192. else
  193. #combine the header and the template (which is actually a static footer)
  194. echo "$REDIS_INIT_HEADER" > $TMP_FILE && cat $INIT_TPL_FILE >> $TMP_FILE || die "Could not write init script to $TMP_FILE"
  195. fi
  196. ###
  197. # Generate sample script from template file
  198. # - No need to check which system we are on. The init info are comments and
  199. # do not interfere with update_rc.d systems. Additionally:
  200. # Ubuntu/debian by default does not come with chkconfig, but does issue a
  201. # warning if init info is not available.
  202. cat > ${TMP_FILE} <<EOT
  203. #!/bin/sh
  204. #Configurations injected by install_server below....
  205. EXEC=$REDIS_EXECUTABLE
  206. CLIEXEC=$CLI_EXEC
  207. PIDFILE=$PIDFILE
  208. CONF="$REDIS_CONFIG_FILE"
  209. REDISPORT="$REDIS_PORT"
  210. ###############
  211. # SysV Init Information
  212. # chkconfig: - 58 74
  213. # description: redis_${REDIS_PORT} is the redis daemon.
  214. ### BEGIN INIT INFO
  215. # Provides: redis_${REDIS_PORT}
  216. # Required-Start: \$network \$local_fs \$remote_fs
  217. # Required-Stop: \$network \$local_fs \$remote_fs
  218. # Default-Start: 2 3 4 5
  219. # Default-Stop: 0 1 6
  220. # Should-Start: \$syslog \$named
  221. # Should-Stop: \$syslog \$named
  222. # Short-Description: start and stop redis_${REDIS_PORT}
  223. # Description: Redis daemon
  224. ### END INIT INFO
  225. EOT
  226. cat ${INIT_TPL_FILE} >> ${TMP_FILE}
  227. #copy to /etc/init.d
  228. cp $TMP_FILE $INIT_SCRIPT_DEST && \
  229. chmod +x $INIT_SCRIPT_DEST || die "Could not copy redis init script to $INIT_SCRIPT_DEST"
  230. echo "Copied $TMP_FILE => $INIT_SCRIPT_DEST"
  231. #Install the service
  232. echo "Installing service..."
  233. if command -v chkconfig >/dev/null 2>&1; then
  234. # we're chkconfig, so lets add to chkconfig and put in runlevel 345
  235. chkconfig --add redis_${REDIS_PORT} && echo "Successfully added to chkconfig!"
  236. chkconfig --level 345 redis_${REDIS_PORT} on && echo "Successfully added to runlevels 345!"
  237. elif command -v update-rc.d >/dev/null 2>&1; then
  238. #if we're not a chkconfig box assume we're able to use update-rc.d
  239. update-rc.d redis_${REDIS_PORT} defaults && echo "Success!"
  240. else
  241. echo "No supported init tool found."
  242. fi
  243. /etc/init.d/redis_$REDIS_PORT start || die "Failed starting service..."
  244. #tada
  245. echo "Installation successful!"
  246. exit 0