You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

258 lines
7.3 KiB

14 years ago
  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 name type and name from the NAME_ARG for REMSH
  44. REMSH_TYPE=`echo $NAME_ARG | awk '{print $1}'`
  45. REMSH_NAME=`echo $NAME_ARG | awk '{print $2}'`
  46. # Note the `date +%s`, used to allow multiple remsh to the same node transparently
  47. REMSH_NAME_ARG="$REMSH_TYPE remsh`date +%s`@`echo $REMSH_NAME | awk -F@ '{print $2}'`"
  48. REMSH_REMSH_ARG="-remsh $REMSH_NAME"
  49. # Extract the target cookie
  50. COOKIE_ARG=`grep '^-setcookie' $VMARGS_PATH`
  51. if [ -z "$COOKIE_ARG" ]; then
  52. echo "vm.args needs to have a -setcookie parameter."
  53. exit 1
  54. fi
  55. # Add ERTS bin dir to our path
  56. ERTS_PATH=$RUNNER_BASE_DIR/erts-$ERTS_VSN/bin
  57. # Setup command to control the node
  58. NODETOOL="$ERTS_PATH/escript $ERTS_PATH/nodetool $NAME_ARG $COOKIE_ARG"
  59. # Setup remote shell command to control node
  60. REMSH="$ERTS_PATH/erl $REMSH_NAME_ARG $REMSH_REMSH_ARG $COOKIE_ARG"
  61. # Check the first argument for instructions
  62. case "$1" in
  63. start)
  64. # Make sure there is not already a node running
  65. RES=`$NODETOOL ping`
  66. if [ "$RES" = "pong" ]; then
  67. echo "Node is already running!"
  68. exit 1
  69. fi
  70. shift # remove $1
  71. RUN_PARAM=$(printf "\'%s\' " "$@")
  72. HEART_COMMAND="$RUNNER_BASE_DIR/bin/$SCRIPT start $RUN_PARAM"
  73. export HEART_COMMAND
  74. mkdir -p $PIPE_DIR
  75. $ERTS_PATH/run_erl -daemon $PIPE_DIR $RUNNER_LOG_DIR "exec $RUNNER_BASE_DIR/bin/$SCRIPT console $RUN_PARAM" 2>&1
  76. ;;
  77. stop)
  78. # Wait for the node to completely stop...
  79. case `uname -s` in
  80. Linux|Darwin|FreeBSD|DragonFly|NetBSD|OpenBSD)
  81. # PID COMMAND
  82. PID=`ps ax -o pid= -o command=|\
  83. grep "$RUNNER_BASE_DIR/.*/[b]eam"|awk '{print $1}'`
  84. ;;
  85. SunOS)
  86. # PID COMMAND
  87. PID=`ps -ef -o pid= -o args=|\
  88. grep "$RUNNER_BASE_DIR/.*/[b]eam"|awk '{print $1}'`
  89. ;;
  90. CYGWIN*)
  91. # UID PID PPID TTY STIME COMMAND
  92. PID=`ps -efW|grep "$RUNNER_BASE_DIR/.*/[b]eam"|awk '{print $2}'`
  93. ;;
  94. esac
  95. $NODETOOL stop
  96. ES=$?
  97. if [ "$ES" -ne 0 ]; then
  98. exit $ES
  99. fi
  100. while `kill -0 $PID 2>/dev/null`;
  101. do
  102. sleep 1
  103. done
  104. ;;
  105. restart)
  106. ## Restart the VM without exiting the process
  107. $NODETOOL restart
  108. ES=$?
  109. if [ "$ES" -ne 0 ]; then
  110. exit $ES
  111. fi
  112. ;;
  113. reboot)
  114. ## Restart the VM completely (uses heart to restart it)
  115. $NODETOOL reboot
  116. ES=$?
  117. if [ "$ES" -ne 0 ]; then
  118. exit $ES
  119. fi
  120. ;;
  121. ping)
  122. ## See if the VM is alive
  123. $NODETOOL ping
  124. ES=$?
  125. if [ "$ES" -ne 0 ]; then
  126. exit $ES
  127. fi
  128. ;;
  129. attach)
  130. # Make sure a node IS running
  131. RES=`$NODETOOL ping`
  132. ES=$?
  133. if [ "$ES" -ne 0 ]; then
  134. echo "Node is not running!"
  135. exit $ES
  136. fi
  137. shift
  138. exec $ERTS_PATH/to_erl $PIPE_DIR
  139. ;;
  140. remote_console)
  141. # Make sure a node IS running
  142. RES=`$NODETOOL ping`
  143. ES=$?
  144. if [ "$ES" -ne 0 ]; then
  145. echo "Node is not running!"
  146. exit $ES
  147. fi
  148. shift
  149. exec $REMSH
  150. ;;
  151. upgrade)
  152. if [ -z "$2" ]; then
  153. echo "Missing upgrade package argument"
  154. echo "Usage: $SCRIPT upgrade {package base name}"
  155. echo "NOTE {package base name} MUST NOT include the .tar.gz suffix"
  156. exit 1
  157. fi
  158. # Make sure a node IS running
  159. RES=`$NODETOOL ping`
  160. ES=$?
  161. if [ "$ES" -ne 0 ]; then
  162. echo "Node is not running!"
  163. exit $ES
  164. fi
  165. node_name=`echo $NAME_ARG | awk '{print $2}'`
  166. erlang_cookie=`echo $COOKIE_ARG | awk '{print $2}'`
  167. $ERTS_PATH/escript $RUNNER_BASE_DIR/bin/install_upgrade.escript $node_name $erlang_cookie $2
  168. ;;
  169. console|console_clean)
  170. # .boot file typically just $SCRIPT (ie, the app name)
  171. # however, for debugging, sometimes start_clean.boot is useful:
  172. case "$1" in
  173. console) BOOTFILE=$SCRIPT ;;
  174. console_clean) BOOTFILE=start_clean ;;
  175. esac
  176. # Setup beam-required vars
  177. ROOTDIR=$RUNNER_BASE_DIR
  178. BINDIR=$ROOTDIR/erts-$ERTS_VSN/bin
  179. EMU=beam
  180. PROGNAME=`echo $0 | sed 's/.*\\///'`
  181. CMD="$BINDIR/erlexec -boot $RUNNER_BASE_DIR/releases/$APP_VSN/$BOOTFILE -mode embedded -config $CONFIG_PATH -args_file $VMARGS_PATH"
  182. export EMU
  183. export ROOTDIR
  184. export BINDIR
  185. export PROGNAME
  186. # Dump environment info for logging purposes
  187. echo "Exec: $CMD" -- ${1+"$@"}
  188. echo "Root: $ROOTDIR"
  189. # Log the startup
  190. logger -t "$SCRIPT[$$]" "Starting up"
  191. # Start the VM
  192. exec $CMD -- ${1+"$@"}
  193. ;;
  194. foreground)
  195. # start up the release in the foreground for use by runit
  196. # or other supervision services
  197. BOOTFILE=$SCRIPT
  198. FOREGROUNDOPTIONS="-noinput +Bd"
  199. # Setup beam-required vars
  200. ROOTDIR=$RUNNER_BASE_DIR
  201. BINDIR=$ROOTDIR/erts-$ERTS_VSN/bin
  202. EMU=beam
  203. PROGNAME=`echo $0 | sed 's/.*\///'`
  204. CMD="$BINDIR/erlexec $FOREGROUNDOPTIONS -boot $RUNNER_BASE_DIR/releases/$APP_VSN/$BOOTFILE -config $CONFIG_PATH -args_file $VMARGS_PATH"
  205. export EMU
  206. export ROOTDIR
  207. export BINDIR
  208. export PROGNAME
  209. # Dump environment info for logging purposes
  210. echo "Exec: $CMD" -- ${1+"$@"}
  211. echo "Root: $ROOTDIR"
  212. # Start the VM
  213. exec $CMD -- ${1+"$@"}
  214. ;;
  215. *)
  216. echo "Usage: $SCRIPT {start|foreground|stop|restart|reboot|ping|console|console_clean|attach|remote_console|upgrade}"
  217. exit 1
  218. ;;
  219. esac
  220. exit 0