25개 이상의 토픽을 선택하실 수 없습니다. Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

130 lines
3.3 KiB

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