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.

186 lines
5.0 KiB

пре 14 година
пре 14 година
пре 14 година
пре 14 година
пре 14 година
пре 14 година
  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. HEART_COMMAND="$RUNNER_BASE_DIR/bin/$SCRIPT start"
  63. export HEART_COMMAND
  64. mkdir -p $PIPE_DIR
  65. shift # remove $1
  66. $ERTS_PATH/run_erl -daemon $PIPE_DIR $RUNNER_LOG_DIR "exec $RUNNER_BASE_DIR/bin/$SCRIPT console $@" 2>&1
  67. ;;
  68. stop)
  69. # Wait for the node to completely stop...
  70. case `uname -s` in
  71. Linux|Darwin|FreeBSD|DragonFly|NetBSD|OpenBSD)
  72. # PID COMMAND
  73. PID=`ps ax -o pid= -o command=|\
  74. grep "$RUNNER_BASE_DIR/.*/[b]eam"|awk '{print $1}'`
  75. ;;
  76. SunOS)
  77. # PID COMMAND
  78. PID=`ps -ef -o pid= -o args=|\
  79. grep "$RUNNER_BASE_DIR/.*/[b]eam"|awk '{print $1}'`
  80. ;;
  81. CYGWIN*)
  82. # UID PID PPID TTY STIME COMMAND
  83. PID=`ps -efW|grep "$RUNNER_BASE_DIR/.*/[b]eam"|awk '{print $2}'`
  84. ;;
  85. esac
  86. $NODETOOL stop
  87. ES=$?
  88. if [ "$ES" -ne 0 ]; then
  89. exit $ES
  90. fi
  91. while `kill -0 $PID 2>/dev/null`;
  92. do
  93. sleep 1
  94. done
  95. ;;
  96. restart)
  97. ## Restart the VM without exiting the process
  98. $NODETOOL restart
  99. ES=$?
  100. if [ "$ES" -ne 0 ]; then
  101. exit $ES
  102. fi
  103. ;;
  104. reboot)
  105. ## Restart the VM completely (uses heart to restart it)
  106. $NODETOOL reboot
  107. ES=$?
  108. if [ "$ES" -ne 0 ]; then
  109. exit $ES
  110. fi
  111. ;;
  112. ping)
  113. ## See if the VM is alive
  114. $NODETOOL ping
  115. ES=$?
  116. if [ "$ES" -ne 0 ]; then
  117. exit $ES
  118. fi
  119. ;;
  120. attach)
  121. # Make sure a node IS running
  122. RES=`$NODETOOL ping`
  123. ES=$?
  124. if [ "$ES" -ne 0 ]; then
  125. echo "Node is not running!"
  126. exit $ES
  127. fi
  128. shift
  129. exec $ERTS_PATH/to_erl $PIPE_DIR
  130. ;;
  131. console|console_clean)
  132. # .boot file typically just $SCRIPT (ie, the app name)
  133. # however, for debugging, sometimes start_clean.boot is useful:
  134. case "$1" in
  135. console) BOOTFILE=$SCRIPT ;;
  136. console_clean) BOOTFILE=start_clean ;;
  137. esac
  138. # Setup beam-required vars
  139. ROOTDIR=$RUNNER_BASE_DIR
  140. BINDIR=$ROOTDIR/erts-$ERTS_VSN/bin
  141. EMU=beam
  142. PROGNAME=`echo $0 | sed 's/.*\\///'`
  143. CMD="$BINDIR/erlexec -boot $RUNNER_BASE_DIR/releases/$APP_VSN/$BOOTFILE -mode embedded -config $CONFIG_PATH -args_file $VMARGS_PATH -- ${1+"$@"}"
  144. export EMU
  145. export ROOTDIR
  146. export BINDIR
  147. export PROGNAME
  148. # Dump environment info for logging purposes
  149. echo "Exec: $CMD"
  150. echo "Root: $ROOTDIR"
  151. # Log the startup
  152. logger -t "$SCRIPT[$$]" "Starting up"
  153. # Start the VM
  154. exec $CMD
  155. ;;
  156. *)
  157. echo "Usage: $SCRIPT {start|stop|restart|reboot|ping|console|console_clean|attach}"
  158. exit 1
  159. ;;
  160. esac
  161. exit 0