Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

212 rader
5.9 KiB

14 år sedan
  1. #!/bin/sh
  2. # -*- tab-width:4;indent-tabs-mode:nil -*-
  3. # ex: ts=4 sw=4 et
  4. RUNNER_SCRIPT_DIR=$(cd ${0%/*} && pwd)
  5. RUNNER_BASE_DIR=${RUNNER_SCRIPT_DIR%/*}
  6. RUNNER_ETC_DIR=$RUNNER_BASE_DIR/etc
  7. RUNNER_LOG_DIR=$RUNNER_BASE_DIR/log
  8. # Note the trailing slash on $PIPE_DIR/
  9. PIPE_DIR=/tmp/$RUNNER_BASE_DIR/
  10. RUNNER_USER=
  11. # Make sure this script is running as the appropriate user
  12. if [ ! -z "$RUNNER_USER" ] && [ `whoami` != "$RUNNER_USER" ]; then
  13. exec sudo -u $RUNNER_USER -i $0 $@
  14. fi
  15. # Make sure CWD is set to runner base dir
  16. cd $RUNNER_BASE_DIR
  17. # Make sure log directory exists
  18. mkdir -p $RUNNER_LOG_DIR
  19. # Identify the script name
  20. SCRIPT=`basename $0`
  21. # Parse out release and erts info
  22. START_ERL=`cat $RUNNER_BASE_DIR/releases/start_erl.data`
  23. ERTS_VSN=${START_ERL% *}
  24. APP_VSN=${START_ERL#* }
  25. # Use releases/VSN/vm.args if it exists otherwise use etc/vm.args
  26. if [ -e "$RUNNER_BASE_DIR/releases/$APP_VSN/vm.args" ]; then
  27. VMARGS_PATH="$RUNNER_BASE_DIR/releases/$APP_VSN/vm.args"
  28. else
  29. VMARGS_PATH="$RUNNER_ETC_DIR/vm.args"
  30. fi
  31. # Use releases/VSN/sys.config if it exists otherwise use etc/app.config
  32. if [ -e "$RUNNER_BASE_DIR/releases/$APP_VSN/sys.config" ]; then
  33. CONFIG_PATH="$RUNNER_BASE_DIR/releases/$APP_VSN/sys.config"
  34. else
  35. CONFIG_PATH="$RUNNER_ETC_DIR/app.config"
  36. fi
  37. # Extract the target node name from node.args
  38. NAME_ARG=`egrep '^-s?name' $VMARGS_PATH`
  39. if [ -z "$NAME_ARG" ]; then
  40. echo "vm.args needs to have either -name or -sname parameter."
  41. exit 1
  42. fi
  43. # Extract the target cookie
  44. COOKIE_ARG=`grep '^-setcookie' $VMARGS_PATH`
  45. if [ -z "$COOKIE_ARG" ]; then
  46. echo "vm.args needs to have a -setcookie parameter."
  47. exit 1
  48. fi
  49. # Add ERTS bin dir to our path
  50. ERTS_PATH=$RUNNER_BASE_DIR/erts-$ERTS_VSN/bin
  51. # Setup command to control the node
  52. NODETOOL="$ERTS_PATH/escript $ERTS_PATH/nodetool $NAME_ARG $COOKIE_ARG"
  53. # Check the first argument for instructions
  54. case "$1" in
  55. start)
  56. # Make sure there is not already a node running
  57. RES=`$NODETOOL ping`
  58. if [ "$RES" = "pong" ]; then
  59. echo "Node is already running!"
  60. exit 1
  61. fi
  62. shift # remove $1
  63. RUN_PARAM=$(printf "\'%s\' " "$@")
  64. HEART_COMMAND="$RUNNER_BASE_DIR/bin/$SCRIPT start $RUN_PARAM"
  65. export HEART_COMMAND
  66. mkdir -p $PIPE_DIR
  67. $ERTS_PATH/run_erl -daemon $PIPE_DIR $RUNNER_LOG_DIR "exec $RUNNER_BASE_DIR/bin/$SCRIPT console $RUN_PARAM" 2>&1
  68. ;;
  69. stop)
  70. # Wait for the node to completely stop...
  71. case `uname -s` in
  72. Linux|Darwin|FreeBSD|DragonFly|NetBSD|OpenBSD)
  73. # PID COMMAND
  74. PID=`ps ax -o pid= -o command=|\
  75. grep "$RUNNER_BASE_DIR/.*/[b]eam"|awk '{print $1}'`
  76. ;;
  77. SunOS)
  78. # PID COMMAND
  79. PID=`ps -ef -o pid= -o args=|\
  80. grep "$RUNNER_BASE_DIR/.*/[b]eam"|awk '{print $1}'`
  81. ;;
  82. CYGWIN*)
  83. # UID PID PPID TTY STIME COMMAND
  84. PID=`ps -efW|grep "$RUNNER_BASE_DIR/.*/[b]eam"|awk '{print $2}'`
  85. ;;
  86. esac
  87. $NODETOOL stop
  88. ES=$?
  89. if [ "$ES" -ne 0 ]; then
  90. exit $ES
  91. fi
  92. while `kill -0 $PID 2>/dev/null`;
  93. do
  94. sleep 1
  95. done
  96. ;;
  97. restart)
  98. ## Restart the VM without exiting the process
  99. $NODETOOL restart
  100. ES=$?
  101. if [ "$ES" -ne 0 ]; then
  102. exit $ES
  103. fi
  104. ;;
  105. reboot)
  106. ## Restart the VM completely (uses heart to restart it)
  107. $NODETOOL reboot
  108. ES=$?
  109. if [ "$ES" -ne 0 ]; then
  110. exit $ES
  111. fi
  112. ;;
  113. ping)
  114. ## See if the VM is alive
  115. $NODETOOL ping
  116. ES=$?
  117. if [ "$ES" -ne 0 ]; then
  118. exit $ES
  119. fi
  120. ;;
  121. attach)
  122. # Make sure a node IS running
  123. RES=`$NODETOOL ping`
  124. ES=$?
  125. if [ "$ES" -ne 0 ]; then
  126. echo "Node is not running!"
  127. exit $ES
  128. fi
  129. shift
  130. exec $ERTS_PATH/to_erl $PIPE_DIR
  131. ;;
  132. console|console_clean)
  133. # .boot file typically just $SCRIPT (ie, the app name)
  134. # however, for debugging, sometimes start_clean.boot is useful:
  135. case "$1" in
  136. console) BOOTFILE=$SCRIPT ;;
  137. console_clean) BOOTFILE=start_clean ;;
  138. esac
  139. # Setup beam-required vars
  140. ROOTDIR=$RUNNER_BASE_DIR
  141. BINDIR=$ROOTDIR/erts-$ERTS_VSN/bin
  142. EMU=beam
  143. PROGNAME=`echo $0 | sed 's/.*\\///'`
  144. CMD="$BINDIR/erlexec -boot $RUNNER_BASE_DIR/releases/$APP_VSN/$BOOTFILE -mode embedded -config $CONFIG_PATH -args_file $VMARGS_PATH"
  145. export EMU
  146. export ROOTDIR
  147. export BINDIR
  148. export PROGNAME
  149. # Dump environment info for logging purposes
  150. echo "Exec: $CMD" -- ${1+"$@"}
  151. echo "Root: $ROOTDIR"
  152. # Log the startup
  153. logger -t "$SCRIPT[$$]" "Starting up"
  154. # Start the VM
  155. exec $CMD -- ${1+"$@"}
  156. ;;
  157. foreground)
  158. # start up the release in the foreground for use by runit
  159. # or other supervision services
  160. BOOTFILE=$SCRIPT
  161. FOREGROUNDOPTIONS="-noinput +Bd"
  162. # Setup beam-required vars
  163. ROOTDIR=$RUNNER_BASE_DIR
  164. BINDIR=$ROOTDIR/erts-$ERTS_VSN/bin
  165. EMU=beam
  166. PROGNAME=`echo $0 | sed 's/.*\///'`
  167. CMD="$BINDIR/erlexec $FOREGROUNDOPTIONS -boot $RUNNER_BASE_DIR/releases/$APP_VSN/$BOOTFILE -config $CONFIG_PATH -args_file $VMARGS_PATH"
  168. export EMU
  169. export ROOTDIR
  170. export BINDIR
  171. export PROGNAME
  172. # Dump environment info for logging purposes
  173. echo "Exec: $CMD" -- ${1+"$@"}
  174. echo "Root: $ROOTDIR"
  175. # Start the VM
  176. exec $CMD -- ${1+"$@"}
  177. ;;
  178. *)
  179. echo "Usage: $SCRIPT {start|foreground|stop|restart|reboot|ping|console|console_clean|attach}"
  180. exit 1
  181. ;;
  182. esac
  183. exit 0