install_server.sh 9.7 KB

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