2
0

install_server.sh 8.3 KB

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