install_server.sh 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  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. #Initial defaults
  35. _REDIS_PORT=6379
  36. echo "Welcome to the redis service installer"
  37. echo "This script will help you easily set up a running redis server
  38. "
  39. #check for root user TODO: replace this with a call to "id"
  40. if [ `whoami` != "root" ] ; then
  41. echo "You must run this script as root. Sorry!"
  42. exit 1
  43. fi
  44. #Read the redis port
  45. read -p "Please select the redis port for this instance: [$_REDIS_PORT] " REDIS_PORT
  46. if [ ! `echo $REDIS_PORT | egrep "^[0-9]+\$"` ] ; then
  47. echo "Selecting default: $_REDIS_PORT"
  48. REDIS_PORT=$_REDIS_PORT
  49. fi
  50. #read the redis config file
  51. _REDIS_CONFIG_FILE="/etc/redis/$REDIS_PORT.conf"
  52. read -p "Please select the redis config file name [$_REDIS_CONFIG_FILE] " REDIS_CONFIG_FILE
  53. if [ !"$REDIS_CONFIG_FILE" ] ; then
  54. REDIS_CONFIG_FILE=$_REDIS_CONFIG_FILE
  55. echo "Selected default - $REDIS_CONFIG_FILE"
  56. fi
  57. #try and create it
  58. mkdir -p `dirname "$REDIS_CONFIG_FILE"` || die "Could not create redis config directory"
  59. #read the redis log file path
  60. _REDIS_LOG_FILE="/var/log/redis_$REDIS_PORT.log"
  61. read -p "Please select the redis log file name [$_REDIS_LOG_FILE] " REDIS_LOG_FILE
  62. if [ !"$REDIS_LOG_FILE" ] ; then
  63. REDIS_LOG_FILE=$_REDIS_LOG_FILE
  64. echo "Selected default - $REDIS_LOG_FILE"
  65. fi
  66. #get the redis data directory
  67. _REDIS_DATA_DIR="/var/lib/redis/$REDIS_PORT"
  68. read -p "Please select the data directory for this instance [$_REDIS_DATA_DIR] " REDIS_DATA_DIR
  69. if [ !"$REDIS_DATA_DIR" ] ; then
  70. REDIS_DATA_DIR=$_REDIS_DATA_DIR
  71. echo "Selected default - $REDIS_DATA_DIR"
  72. fi
  73. mkdir -p $REDIS_DATA_DIR || die "Could not create redis data directory"
  74. #get the redis executable path
  75. _REDIS_EXECUTABLE=`which redis-server`
  76. read -p "Please select the redis executable path [$_REDIS_EXECUTABLE] " REDIS_EXECUTABLE
  77. if [ ! -f "$REDIS_EXECUTABLE" ] ; then
  78. REDIS_EXECUTABLE=$_REDIS_EXECUTABLE
  79. if [ ! -f "$REDIS_EXECUTABLE" ] ; then
  80. echo "Mmmmm... it seems like you don't have a redis executable. Did you run make install yet?"
  81. exit 1
  82. fi
  83. fi
  84. #render the tmplates
  85. TMP_FILE="/tmp/$REDIS_PORT.conf"
  86. TPL_FILE="./redis.conf.tpl"
  87. INIT_TPL_FILE="./redis_init_script.tpl"
  88. INIT_SCRIPT_DEST="/etc/init.d/redis_$REDIS_PORT"
  89. PIDFILE="/var/run/redis_$REDIS_PORT.pid"
  90. #check the default for redis cli
  91. CLI_EXEC=`which redis-cli`
  92. if [ ! "$CLI_EXEC" ] ; then
  93. CLI_EXEC=`dirname $REDIS_EXECUTABLE`"/redis-cli"
  94. fi
  95. #Generate config file from template
  96. echo "## Generated by install_server.sh ##" > $TMP_FILE
  97. cat $TPL_FILE | while read line; do eval "echo \"$line\"" >> $TMP_FILE; done
  98. cp -f $TMP_FILE $REDIS_CONFIG_FILE || exit 1
  99. #Generate sample script from template file
  100. rm -f $TMP_FILE
  101. #we hard code the configs here to avoid issues with templates containing env vars
  102. #kinda lame but works!
  103. REDIS_INIT_HEADER=\
  104. "#/bin/sh\n
  105. #Configurations injected by install_server below....\n\n
  106. EXEC=$REDIS_EXECUTABLE\n
  107. CLIEXEC=$CLI_EXEC\n
  108. PIDFILE=$PIDFILE\n
  109. CONF=\"$REDIS_CONFIG_FILE\"\n\n
  110. REDISPORT=\"$REDIS_PORT\"\n\n
  111. ###############\n\n"
  112. REDIS_CHKCONFIG_INFO=\
  113. "# REDHAT chkconfig header\n\n
  114. # chkconfig: - 58 74\n
  115. # description: redis_6379 is the redis daemon.\n
  116. ### BEGIN INIT INFO\n
  117. # Provides: redis_6379\n
  118. # Required-Start: $network $local_fs $remote_fs\n
  119. # Required-Stop: $network $local_fs $remote_fs\n
  120. # Should-Start: $syslog $named\n
  121. # Should-Stop: $syslog $named\n
  122. # Short-Description: start and stop redis_6379\n
  123. # Description: Redis daemon\n
  124. ### END INIT INFO\n\n"
  125. if [[ ! `which chkconfig` ]] ; then
  126. #combine the header and the template (which is actually a static footer)
  127. echo -e $REDIS_INIT_HEADER > $TMP_FILE && cat $INIT_TPL_FILE >> $TMP_FILE || die "Could not write init script to $TMP_FILE"
  128. else
  129. #if we're a box with chkconfig on it we want to include info for chkconfig
  130. 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"
  131. fi
  132. #copy to /etc/init.d
  133. cp -f $TMP_FILE $INIT_SCRIPT_DEST && chmod +x $INIT_SCRIPT_DEST || die "Could not copy redis init script to $INIT_SCRIPT_DEST"
  134. echo "Copied $TMP_FILE => $INIT_SCRIPT_DEST"
  135. #Install the service
  136. echo "Installing service..."
  137. if [[ ! `which chkconfig` ]] ; then
  138. #if we're not a chkconfig box assume we're able to use update-rc.d
  139. update-rc.d redis_$REDIS_PORT defaults && echo "Success!"
  140. else
  141. # we're chkconfig, so lets add to chkconfig and put in runlevel 345
  142. chkconfig --add redis_$REDIS_PORT && echo "Successfully added to chkconfig!"
  143. chkconfig --level 345 redis_$REDIS_PORT on && echo "Successfully added to runlevels 345!"
  144. fi
  145. /etc/init.d/redis_$REDIS_PORT start || die "Failed starting service..."
  146. #tada
  147. echo "Installation successful!"
  148. exit 0