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.

403 lines
11 KiB

14 years ago
  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. # Test if REMSH_NAME contains a @ and set REMSH_HOSTNAME_PART
  76. # and REMSH_NAME_PART according REMSH_TYPE
  77. MAYBE_FQDN_HOSTNAME=`hostname`
  78. HOSTNAME=`echo $MAYBE_FQDN_HOSTNAME | awk -F. '{print $1}'`
  79. REMSH_HOSTNAME_PART="$MAYBE_FQDN_HOSTNAME"
  80. case "$REMSH_NAME" in
  81. *@*)
  82. REMSH_HOSTNAME_PART=`echo $REMSH_NAME | awk -F@ '{print $2}'`
  83. REMSH_NAME_PART=`echo $REMSH_NAME | awk -F@ '{print $1}'`
  84. ;;
  85. *)
  86. REMSH_NAME_PART="$REMSH_NAME"
  87. if [ "$REMSH_TYPE" = "-sname" ]; then
  88. REMSH_HOSTNAME_PART= "$HOSTNAME"
  89. else
  90. # -name type, check if `hostname` is fqdn
  91. if [ "$MAYBE_FQDN_HOSTNAME" = "$HOSTNAME" ]; then
  92. echo "Hostname must be a fqdn domain name when node is configured with long names"
  93. echo "and the full node name isn't configured in vm.args"
  94. exit 1
  95. fi
  96. fi
  97. ;;
  98. esac
  99. # Note the `date +%s`, used to allow multiple remsh to the same node
  100. # transparently
  101. REMSH_NAME_ARG="$REMSH_TYPE remsh`date +%s`@$REMSH_HOSTNAME_PART"
  102. REMSH_REMSH_ARG="-remsh $REMSH_NAME_PART@$REMSH_HOSTNAME_PART"
  103. # Extract the target cookie
  104. COOKIE_ARG=`grep '^\-setcookie' $VMARGS_PATH`
  105. if [ -z "$COOKIE_ARG" ]; then
  106. echo "vm.args needs to have a -setcookie parameter."
  107. exit 1
  108. fi
  109. # Make sure CWD is set to the right dir
  110. cd $USE_DIR
  111. # Make sure log directory exists
  112. mkdir -p $USE_DIR/log
  113. RUNNER_SCRIPT_DATA=
  114. if [ -e "$RUNNER_BASE_DIR/releases/$APP_VSN/runner_script.data" ]; then
  115. RUNNER_SCRIPT_DATA=`cat $RUNNER_BASE_DIR/releases/$APP_VSN/runner_script.data`
  116. fi
  117. if [ -z "$RUNNER_SCRIPT_DATA" ]; then
  118. echo "normal release"
  119. ROOTDIR=$RUNNER_BASE_DIR
  120. ERTS_PATH=$RUNNER_BASE_DIR/erts-$ERTS_VSN/bin
  121. NODETOOL="$ERTS_PATH/escript $ERTS_PATH/nodetool $NAME_ARG $COOKIE_ARG"
  122. SLIM_ARGS=
  123. elif [ "$RUNNER_SCRIPT_DATA" = "slim" ]; then
  124. echo "slim release"
  125. # Setup system paths
  126. SYSTEM_ERL_PATH=`which erl`
  127. if [ ! -x "$SYSTEM_ERL_PATH" ]; then
  128. echo "Failed to find erl. Is Erlang/OTP available in PATH?"
  129. exit 1
  130. fi
  131. SYSTEM_HOME_BIN=${SYSTEM_ERL_PATH%/*}
  132. ROOTDIR=$SYSTEM_HOME_BIN/../lib/erlang
  133. ERTS_PATH=$ROOTDIR/erts-$ERTS_VSN/bin
  134. unset SYSTEM_ERL_PATH
  135. unset SYSTEM_HOME_BIN
  136. # Setup nodetool path. Is it nice to place nodetool script under releases?
  137. LOCAL_ERTS_PATH=$RUNNER_BASE_DIR/erts-$ERTS_VSN/bin
  138. NODETOOL="$ERTS_PATH/escript $LOCAL_ERTS_PATH/nodetool $NAME_ARG $COOKIE_ARG"
  139. unset LOCAL_ERL_PATH
  140. # Setup additional arguments for slim release
  141. SLIM_ARGS="-boot_var RELTOOL_EXT_LIB $RUNNER_BASE_DIR/lib -sasl releases_dir \"$RUNNER_BASE_DIR/releases\""
  142. else
  143. echo "unknown runner_script.data"
  144. exit 1
  145. fi
  146. # Setup remote shell command to control node
  147. REMSH="$ERTS_PATH/erl -hidden $REMSH_NAME_ARG $REMSH_REMSH_ARG $COOKIE_ARG"
  148. # Common functions
  149. # Ping node without allowing nodetool to take stdin
  150. ping_node() {
  151. $NODETOOL ping < /dev/null
  152. }
  153. # Set the PID global variable, return 1 on error
  154. get_pid() {
  155. PID=`$NODETOOL getpid < /dev/null`
  156. ES=$?
  157. if [ "$ES" -ne 0 ]; then
  158. echo "Node is not running!"
  159. return 1
  160. fi
  161. # don't allow empty or init pid's
  162. if [ -z $PID ] || [ "$PID" -le 1 ]; then
  163. return 1
  164. fi
  165. return 0
  166. }
  167. # Check the first argument for instructions
  168. case "$1" in
  169. start|start_boot)
  170. # Make sure there is not already a node running
  171. RES=`ping_node`
  172. if [ "$RES" = "pong" ]; then
  173. echo "Node is already running!"
  174. exit 1
  175. fi
  176. case "$1" in
  177. start)
  178. shift
  179. START_OPTION="console"
  180. HEART_OPTION="start"
  181. ;;
  182. start_boot)
  183. shift
  184. START_OPTION="console_boot"
  185. HEART_OPTION="start_boot"
  186. ;;
  187. esac
  188. RUN_PARAM=$(printf "\'%s\' " "$@")
  189. HEART_COMMAND="$RUNNER_BASE_DIR/bin/$SCRIPT $HEART_OPTION $RUN_PARAM"
  190. export HEART_COMMAND
  191. mkdir -p $PIPE_DIR
  192. $ERTS_PATH/run_erl -daemon $PIPE_DIR $RUNNER_LOG_DIR "exec $RUNNER_BASE_DIR/bin/$SCRIPT $START_OPTION $RUN_PARAM" 2>&1
  193. ;;
  194. stop)
  195. # Wait for the node to completely stop...
  196. case `uname -s` in
  197. Darwin)
  198. # Make sure we explicitly set this because iTerm.app doesn't for
  199. # some reason.
  200. COMMAND_MODE=unix2003
  201. esac
  202. # Get the PID from nodetool
  203. get_pid
  204. GPR=$?
  205. if [ "$GPR" -ne 0 ] || [ -z $PID ]; then
  206. exit $GPR
  207. fi
  208. # Tell nodetool to initiate a stop
  209. $NODETOOL stop
  210. ES=$?
  211. if [ "$ES" -ne 0 ]; then
  212. exit $ES
  213. fi
  214. # Wait for the node to completely stop...
  215. while `kill -s 0 $PID 2>/dev/null`
  216. do
  217. sleep 1
  218. done
  219. ;;
  220. restart)
  221. ## Restart the VM without exiting the process
  222. $NODETOOL restart
  223. ES=$?
  224. if [ "$ES" -ne 0 ]; then
  225. exit $ES
  226. fi
  227. ;;
  228. reboot)
  229. ## Restart the VM completely (uses heart to restart it)
  230. $NODETOOL reboot
  231. ES=$?
  232. if [ "$ES" -ne 0 ]; then
  233. exit $ES
  234. fi
  235. ;;
  236. ping)
  237. ## See if the VM is alive
  238. ping_node
  239. ES=$?
  240. if [ "$ES" -ne 0 ]; then
  241. exit $ES
  242. fi
  243. ;;
  244. attach)
  245. # Make sure a node is running
  246. ping_node
  247. ES=$?
  248. if [ "$ES" -ne 0 ]; then
  249. echo "Node is not running!"
  250. exit $ES
  251. fi
  252. shift
  253. exec $ERTS_PATH/to_erl $PIPE_DIR
  254. ;;
  255. remote_console)
  256. # Make sure a node is running
  257. ping_node
  258. ES=$?
  259. if [ "$ES" -ne 0 ]; then
  260. echo "Node is not running!"
  261. exit $ES
  262. fi
  263. shift
  264. exec $REMSH
  265. ;;
  266. upgrade)
  267. if [ -z "$2" ]; then
  268. echo "Missing upgrade package argument"
  269. echo "Usage: $SCRIPT upgrade {package base name}"
  270. echo "NOTE {package base name} MUST NOT include the .tar.gz suffix"
  271. exit 1
  272. fi
  273. # Make sure a node IS running
  274. ping_node
  275. ES=$?
  276. if [ "$ES" -ne 0 ]; then
  277. echo "Node is not running!"
  278. exit $ES
  279. fi
  280. node_name=`echo $NAME_ARG | awk '{print $2}'`
  281. erlang_cookie=`echo $COOKIE_ARG | awk '{print $2}'`
  282. $ERTS_PATH/escript $RUNNER_BASE_DIR/bin/install_upgrade.escript $node_name $erlang_cookie $2
  283. ;;
  284. console|console_clean|console_boot)
  285. # .boot file typically just $SCRIPT (ie, the app name)
  286. # however, for debugging, sometimes start_clean.boot is useful.
  287. # For e.g. 'setup', one may even want to name another boot script.
  288. case "$1" in
  289. console) BOOTFILE=$SCRIPT ;;
  290. console_clean) BOOTFILE=start_clean ;;
  291. console_boot)
  292. shift
  293. BOOTFILE="$1"
  294. shift
  295. ;;
  296. esac
  297. # Setup beam-required vars
  298. BINDIR=$ROOTDIR/erts-$ERTS_VSN/bin
  299. EMU=beam
  300. PROGNAME=`echo $0 | sed 's/.*\\///'`
  301. CMD="$BINDIR/erlexec $SLIM_ARGS -boot $RUNNER_BASE_DIR/releases/$APP_VSN/$BOOTFILE -mode embedded -config $CONFIG_PATH -args_file $VMARGS_PATH"
  302. export EMU
  303. export ROOTDIR
  304. export BINDIR
  305. export PROGNAME
  306. # Dump environment info for logging purposes
  307. echo "Exec: $CMD" -- ${1+"$@"}
  308. echo "Root: $ROOTDIR"
  309. # Log the startup
  310. logger -t "$SCRIPT[$$]" "Starting up"
  311. # Start the VM
  312. exec $CMD -- ${1+"$@"}
  313. ;;
  314. foreground)
  315. # start up the release in the foreground for use by runit
  316. # or other supervision services
  317. BOOTFILE=$SCRIPT
  318. FOREGROUNDOPTIONS="-noinput +Bd"
  319. # Setup beam-required vars
  320. BINDIR=$ROOTDIR/erts-$ERTS_VSN/bin
  321. EMU=beam
  322. PROGNAME=`echo $0 | sed 's/.*\///'`
  323. CMD="$BINDIR/erlexec $SLIM_ARGS $FOREGROUNDOPTIONS -boot $RUNNER_BASE_DIR/releases/$APP_VSN/$BOOTFILE -config $CONFIG_PATH -args_file $VMARGS_PATH"
  324. export EMU
  325. export ROOTDIR
  326. export BINDIR
  327. export PROGNAME
  328. # Dump environment info for logging purposes
  329. echo "Exec: $CMD" -- ${1+"$@"}
  330. echo "Root: $ROOTDIR"
  331. # Start the VM
  332. exec $CMD -- ${1+"$@"}
  333. ;;
  334. getpid)
  335. # Get the PID from nodetool
  336. get_pid
  337. ES=$?
  338. if [ "$ES" -ne 0 ] || [ -z $PID ]; then
  339. exit $ES
  340. fi
  341. echo $PID
  342. ;;
  343. *)
  344. echo "Usage: $SCRIPT {start|start_boot <file>|foreground|stop|restart|reboot|ping|console|getpid|console_clean|console_boot <file>|attach|remote_console|upgrade}"
  345. exit 1
  346. ;;
  347. esac
  348. exit 0