install_server.sh 9.4 KB

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