node 8.2 KB

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