Du kannst nicht mehr als 25 Themen auswählen Themen müssen entweder mit einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.

348 Zeilen
9.3 KiB

vor 14 Jahren
  1. #!/bin/sh
  2. # -*- tab-width:4;indent-tabs-mode:nil -*-
  3. # ex: ts=4 sw=4 et
  4. # /bin/sh on Solaris is not a POSIX compatible shell, but /usr/bin/ksh is.
  5. if [ `uname -s` = 'SunOS' -a "${POSIX_SHELL}" != "true" ]; then
  6. POSIX_SHELL="true"
  7. export POSIX_SHELL
  8. # To support 'whoami' add /usr/ucb to path
  9. PATH=/usr/ucb:$PATH
  10. export PATH
  11. exec /usr/bin/ksh $0 "$@"
  12. fi
  13. # clear it so if we invoke other scripts, they run as ksh
  14. unset POSIX_SHELL
  15. RUNNER_SCRIPT_DIR=$(cd ${0%/*} && pwd -P)
  16. RUNNER_SCRIPT=${0##*/}
  17. CALLER_DIR=$PWD
  18. RUNNER_BASE_DIR=${RUNNER_SCRIPT_DIR%/*}
  19. RUNNER_ETC_DIR=$RUNNER_BASE_DIR/etc
  20. # Note the trailing slash on $PIPE_DIR/
  21. PIPE_DIR=/tmp/$RUNNER_BASE_DIR/
  22. RUNNER_USER=
  23. WHOAMI=$(whoami)
  24. # Make sure this script is running as the appropriate user
  25. if ([ "$RUNNER_USER" ] && [ "x$WHOAMI" != "x$RUNNER_USER" ]); then
  26. type sudo > /dev/null 2>&1
  27. if [ $? -ne 0 ]; then
  28. echo "sudo doesn't appear to be installed and your EUID isn't $RUNNER_USER" 1>&2
  29. exit 1
  30. fi
  31. echo "Attempting to restart script through sudo -H -u $RUNNER_USER" >&2
  32. exec sudo -H -u $RUNNER_USER -i $RUNNER_SCRIPT_DIR/$RUNNER_SCRIPT $@
  33. fi
  34. # Identify the script name
  35. SCRIPT=`basename $0`
  36. # Parse out release and erts info
  37. START_ERL=`cat $RUNNER_BASE_DIR/releases/start_erl.data`
  38. ERTS_VSN=${START_ERL% *}
  39. APP_VSN=${START_ERL#* }
  40. # Use $CWD/vm.args if exists, otherwise releases/APP_VSN/vm.args, or
  41. # else etc/vm.args
  42. if [ -e "$CALLER_DIR/vm.args" ]; then
  43. VMARGS_PATH=$CALLER_DIR/vm.args
  44. USE_DIR=$CALLER_DIR
  45. else
  46. USE_DIR=$RUNNER_BASE_DIR
  47. if [ -e "$RUNNER_BASE_DIR/releases/$APP_VSN/vm.args" ]; then
  48. VMARGS_PATH="$RUNNER_BASE_DIR/releases/$APP_VSN/vm.args"
  49. else
  50. VMARGS_PATH="$RUNNER_ETC_DIR/vm.args"
  51. fi
  52. fi
  53. RUNNER_LOG_DIR=$USE_DIR/log
  54. # Make sure log directory exists
  55. mkdir -p $RUNNER_LOG_DIR
  56. # Use releases/VSN/sys.config if it exists otherwise use etc/app.config
  57. if [ -e "$USE_DIR/sys.config" ]; then
  58. CONFIG_PATH="$USE_DIR/sys.config"
  59. else
  60. if [ -e "$RUNNER_BASE_DIR/releases/$APP_VSN/sys.config" ]; then
  61. CONFIG_PATH="$RUNNER_BASE_DIR/releases/$APP_VSN/sys.config"
  62. else
  63. CONFIG_PATH="$RUNNER_ETC_DIR/app.config"
  64. fi
  65. fi
  66. # Extract the target node name from node.args
  67. NAME_ARG=`egrep '^\-s?name' $VMARGS_PATH`
  68. if [ -z "$NAME_ARG" ]; then
  69. echo "vm.args needs to have either -name or -sname parameter."
  70. exit 1
  71. fi
  72. # Extract the name type and name from the NAME_ARG for REMSH
  73. REMSH_TYPE=`echo $NAME_ARG | awk '{print $1}'`
  74. REMSH_NAME=`echo $NAME_ARG | awk '{print $2}'`
  75. # Note the `date +%s`, used to allow multiple remsh to the same node
  76. # transparently
  77. REMSH_NAME_ARG="$REMSH_TYPE remsh`date +%s`@`echo $REMSH_NAME | awk -F@ '{print $2}'`"
  78. REMSH_REMSH_ARG="-remsh $REMSH_NAME"
  79. # Extract the target cookie
  80. COOKIE_ARG=`grep '^\-setcookie' $VMARGS_PATH`
  81. if [ -z "$COOKIE_ARG" ]; then
  82. echo "vm.args needs to have a -setcookie parameter."
  83. exit 1
  84. fi
  85. # Make sure CWD is set to the right dir
  86. cd $USE_DIR
  87. # Make sure log directory exists
  88. mkdir -p $USE_DIR/log
  89. # Add ERTS bin dir to our path
  90. ERTS_PATH=$RUNNER_BASE_DIR/erts-$ERTS_VSN/bin
  91. # Setup command to control the node
  92. NODETOOL="$ERTS_PATH/escript $ERTS_PATH/nodetool $NAME_ARG $COOKIE_ARG"
  93. # Setup remote shell command to control node
  94. REMSH="$ERTS_PATH/erl $REMSH_NAME_ARG $REMSH_REMSH_ARG $COOKIE_ARG"
  95. # Common functions
  96. # Ping node without allowing nodetool to take stdin
  97. ping_node() {
  98. $NODETOOL ping < /dev/null
  99. }
  100. # Set the PID global variable, return 1 on error
  101. get_pid() {
  102. PID=`$NODETOOL getpid < /dev/null`
  103. ES=$?
  104. if [ "$ES" -ne 0 ]; then
  105. echo "Node is not running!"
  106. return 1
  107. fi
  108. # don't allow empty or init pid's
  109. if [ -z $PID ] || [ "$PID" -le 1 ]; then
  110. return 1
  111. fi
  112. return 0
  113. }
  114. # Check the first argument for instructions
  115. case "$1" in
  116. start|start_boot)
  117. # Make sure there is not already a node running
  118. RES=`ping_node`
  119. if [ "$RES" = "pong" ]; then
  120. echo "Node is already running!"
  121. exit 1
  122. fi
  123. case "$1" in
  124. start)
  125. shift
  126. START_OPTION="console"
  127. HEART_OPTION="start"
  128. ;;
  129. start_boot)
  130. shift
  131. START_OPTION="console_boot"
  132. HEART_OPTION="start_boot"
  133. ;;
  134. esac
  135. RUN_PARAM=$(printf "\'%s\' " "$@")
  136. HEART_COMMAND="$RUNNER_BASE_DIR/bin/$SCRIPT $HEART_OPTION $RUN_PARAM"
  137. export HEART_COMMAND
  138. mkdir -p $PIPE_DIR
  139. $ERTS_PATH/run_erl -daemon $PIPE_DIR $RUNNER_LOG_DIR "exec $RUNNER_BASE_DIR/bin/$SCRIPT $START_OPTION $RUN_PARAM" 2>&1
  140. ;;
  141. stop)
  142. # Wait for the node to completely stop...
  143. case `uname -s` in
  144. Darwin)
  145. # Make sure we explicitly set this because iTerm.app doesn't for
  146. # some reason.
  147. COMMAND_MODE=unix2003
  148. esac
  149. # Get the PID from nodetool
  150. get_pid
  151. GPR=$?
  152. if [ "$GPR" -ne 0 ] || [ -z $PID ]; then
  153. exit $GPR
  154. fi
  155. # Tell nodetool to initiate a stop
  156. $NODETOOL stop
  157. ES=$?
  158. if [ "$ES" -ne 0 ]; then
  159. exit $ES
  160. fi
  161. # Wait for the node to completely stop...
  162. while `kill -s 0 $PID 2>/dev/null`
  163. do
  164. sleep 1
  165. done
  166. ;;
  167. restart)
  168. ## Restart the VM without exiting the process
  169. $NODETOOL restart
  170. ES=$?
  171. if [ "$ES" -ne 0 ]; then
  172. exit $ES
  173. fi
  174. ;;
  175. reboot)
  176. ## Restart the VM completely (uses heart to restart it)
  177. $NODETOOL reboot
  178. ES=$?
  179. if [ "$ES" -ne 0 ]; then
  180. exit $ES
  181. fi
  182. ;;
  183. ping)
  184. ## See if the VM is alive
  185. ping_node
  186. ES=$?
  187. if [ "$ES" -ne 0 ]; then
  188. exit $ES
  189. fi
  190. ;;
  191. attach)
  192. # Make sure a node is running
  193. ping_node
  194. ES=$?
  195. if [ "$ES" -ne 0 ]; then
  196. echo "Node is not running!"
  197. exit $ES
  198. fi
  199. shift
  200. exec $ERTS_PATH/to_erl $PIPE_DIR
  201. ;;
  202. remote_console)
  203. # Make sure a node is running
  204. ping_node
  205. ES=$?
  206. if [ "$ES" -ne 0 ]; then
  207. echo "Node is not running!"
  208. exit $ES
  209. fi
  210. shift
  211. exec $REMSH
  212. ;;
  213. upgrade)
  214. if [ -z "$2" ]; then
  215. echo "Missing upgrade package argument"
  216. echo "Usage: $SCRIPT upgrade {package base name}"
  217. echo "NOTE {package base name} MUST NOT include the .tar.gz suffix"
  218. exit 1
  219. fi
  220. # Make sure a node IS running
  221. ping_node
  222. ES=$?
  223. if [ "$ES" -ne 0 ]; then
  224. echo "Node is not running!"
  225. exit $ES
  226. fi
  227. node_name=`echo $NAME_ARG | awk '{print $2}'`
  228. erlang_cookie=`echo $COOKIE_ARG | awk '{print $2}'`
  229. $ERTS_PATH/escript $RUNNER_BASE_DIR/bin/install_upgrade.escript $node_name $erlang_cookie $2
  230. ;;
  231. console|console_clean|console_boot)
  232. # .boot file typically just $SCRIPT (ie, the app name)
  233. # however, for debugging, sometimes start_clean.boot is useful.
  234. # For e.g. 'setup', one may even want to name another boot script.
  235. case "$1" in
  236. console) BOOTFILE=$SCRIPT ;;
  237. console_clean) BOOTFILE=start_clean ;;
  238. console_boot)
  239. shift
  240. BOOTFILE="$1"
  241. shift
  242. ;;
  243. esac
  244. # Setup beam-required vars
  245. ROOTDIR=$RUNNER_BASE_DIR
  246. BINDIR=$ROOTDIR/erts-$ERTS_VSN/bin
  247. EMU=beam
  248. PROGNAME=`echo $0 | sed 's/.*\\///'`
  249. CMD="$BINDIR/erlexec -boot $RUNNER_BASE_DIR/releases/$APP_VSN/$BOOTFILE -mode embedded -config $CONFIG_PATH -args_file $VMARGS_PATH"
  250. export EMU
  251. export ROOTDIR
  252. export BINDIR
  253. export PROGNAME
  254. # Dump environment info for logging purposes
  255. echo "Exec: $CMD" -- ${1+"$@"}
  256. echo "Root: $ROOTDIR"
  257. # Log the startup
  258. logger -t "$SCRIPT[$$]" "Starting up"
  259. # Start the VM
  260. exec $CMD -- ${1+"$@"}
  261. ;;
  262. foreground)
  263. # start up the release in the foreground for use by runit
  264. # or other supervision services
  265. BOOTFILE=$SCRIPT
  266. FOREGROUNDOPTIONS="-noinput +Bd"
  267. # Setup beam-required vars
  268. ROOTDIR=$RUNNER_BASE_DIR
  269. BINDIR=$ROOTDIR/erts-$ERTS_VSN/bin
  270. EMU=beam
  271. PROGNAME=`echo $0 | sed 's/.*\///'`
  272. CMD="$BINDIR/erlexec $FOREGROUNDOPTIONS -boot $RUNNER_BASE_DIR/releases/$APP_VSN/$BOOTFILE -config $CONFIG_PATH -args_file $VMARGS_PATH"
  273. export EMU
  274. export ROOTDIR
  275. export BINDIR
  276. export PROGNAME
  277. # Dump environment info for logging purposes
  278. echo "Exec: $CMD" -- ${1+"$@"}
  279. echo "Root: $ROOTDIR"
  280. # Start the VM
  281. exec $CMD -- ${1+"$@"}
  282. ;;
  283. getpid)
  284. # Get the PID from nodetool
  285. get_pid
  286. ES=$?
  287. if [ "$ES" -ne 0 ] || [ -z $PID ]; then
  288. exit $ES
  289. fi
  290. echo $PID
  291. ;;
  292. *)
  293. echo "Usage: $SCRIPT {start|start_boot <file>|foreground|stop|restart|reboot|ping|console|getpid|console_clean|console_boot <file>|attach|remote_console|upgrade}"
  294. exit 1
  295. ;;
  296. esac
  297. exit 0