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.

149 lines
4.0 KiB

14 years ago
  1. #!/bin/bash
  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. PIPE_DIR=/tmp/$RUNNER_BASE_DIR/
  9. RUNNER_USER=
  10. # Make sure this script is running as the appropriate user
  11. if [ ! -z "$RUNNER_USER" ] && [ `whoami` != "$RUNNER_USER" ]; then
  12. exec sudo -u $RUNNER_USER -i $0 $@
  13. fi
  14. # Make sure CWD is set to runner base dir
  15. cd $RUNNER_BASE_DIR
  16. # Make sure log directory exists
  17. mkdir -p $RUNNER_LOG_DIR
  18. # Extract the target node name from node.args
  19. NAME_ARG=`grep -e '-[s]*name' $RUNNER_ETC_DIR/vm.args`
  20. if [ -z "$NAME_ARG" ]; then
  21. echo "vm.args needs to have either -name or -sname parameter."
  22. exit 1
  23. fi
  24. # Extract the target cookie
  25. COOKIE_ARG=`grep -e '-setcookie' $RUNNER_ETC_DIR/vm.args`
  26. if [ -z "$COOKIE_ARG" ]; then
  27. echo "vm.args needs to have a -setcookie parameter."
  28. exit 1
  29. fi
  30. # Identify the script name
  31. SCRIPT=`basename $0`
  32. # Parse out release and erts info
  33. START_ERL=`cat $RUNNER_BASE_DIR/releases/start_erl.data`
  34. ERTS_VSN=${START_ERL% *}
  35. APP_VSN=${START_ERL#* }
  36. # Add ERTS bin dir to our path
  37. ERTS_PATH=$RUNNER_BASE_DIR/erts-$ERTS_VSN/bin
  38. # Setup command to control the node
  39. NODETOOL="$ERTS_PATH/escript $ERTS_PATH/nodetool $NAME_ARG $COOKIE_ARG"
  40. # Check the first argument for instructions
  41. case "$1" in
  42. start)
  43. # Make sure there is not already a node running
  44. RES=`$NODETOOL ping`
  45. if [ "$RES" = "pong" ]; then
  46. echo "Node is already running!"
  47. exit 1
  48. fi
  49. HEART_COMMAND="$RUNNER_BASE_DIR/bin/$SCRIPT start"
  50. export HEART_COMMAND
  51. mkdir -p $PIPE_DIR
  52. # Note the trailing slash on $PIPE_DIR/
  53. $ERTS_PATH/run_erl -daemon $PIPE_DIR/ $RUNNER_LOG_DIR "exec $RUNNER_BASE_DIR/bin/$SCRIPT console" 2>&1
  54. ;;
  55. stop)
  56. # Wait for the node to completely stop...
  57. case `uname -s` in
  58. Linux|Darwin|FreeBSD|DragonFly|NetBSD|OpenBSD)
  59. # PID COMMAND
  60. PID=`ps ax -o pid= -o command=|\
  61. grep "$RUNNER_BASE_DIR/.*/[b]eam"|awk '{print $1}'`
  62. ;;
  63. SunOS)
  64. # PID COMMAND
  65. PID=`ps -ef -o pid= -o args=|\
  66. grep "$RUNNER_BASE_DIR/.*/[b]eam"|awk '{print $1}'`
  67. ;;
  68. CYGWIN*)
  69. # UID PID PPID TTY STIME COMMAND
  70. PID=`ps -efW|grep "$RUNNER_BASE_DIR/.*/[b]eam"|awk '{print $2}'`
  71. ;;
  72. esac
  73. $NODETOOL stop
  74. while `kill -0 $PID 2>/dev/null`;
  75. do
  76. sleep 1
  77. done
  78. ;;
  79. restart)
  80. ## Restart the VM without exiting the process
  81. $NODETOOL restart
  82. ;;
  83. reboot)
  84. ## Restart the VM completely (uses heart to restart it)
  85. $NODETOOL reboot
  86. ;;
  87. ping)
  88. ## See if the VM is alive
  89. $NODETOOL ping
  90. ;;
  91. attach)
  92. # Make sure a node IS running
  93. RES=`$NODETOOL ping`
  94. if [ "$RES" != "pong" ]; then
  95. echo "Node is not running!"
  96. exit 1
  97. fi
  98. shift
  99. $ERTS_PATH/to_erl $PIPE_DIR
  100. ;;
  101. console)
  102. # Setup beam-required vars
  103. ROOTDIR=$RUNNER_BASE_DIR
  104. BINDIR=$ROOTDIR/erts-$ERTS_VSN/bin
  105. EMU=beam
  106. PROGNAME=`echo $0 | sed 's/.*\\///'`
  107. CMD="$BINDIR/erlexec -boot $RUNNER_BASE_DIR/releases/$APP_VSN/$SCRIPT -embedded -config $RUNNER_ETC_DIR/app.config -args_file $RUNNER_ETC_DIR/vm.args -- ${1+"$@"}"
  108. export EMU
  109. export ROOTDIR
  110. export BINDIR
  111. export PROGNAME
  112. # Dump environment info for logging purposes
  113. echo "Exec: $CMD"
  114. echo "Root: $ROOTDIR"
  115. # Log the startup
  116. logger -t "$SCRIPT[$$]" "Starting up"
  117. # Start the VM
  118. exec $CMD
  119. ;;
  120. *)
  121. echo "Usage: $SCRIPT {start|stop|restart|reboot|ping|console|attach}"
  122. exit 1
  123. ;;
  124. esac
  125. exit 0