25'ten fazla konu seçemezsiniz Konular bir harf veya rakamla başlamalı, kısa çizgiler ('-') içerebilir ve en fazla 35 karakter uzunluğunda olabilir.

374 satır
10 KiB

14 yıl önce
  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. # Add ERTS bin dir to our path
  114. ERTS_PATH=$RUNNER_BASE_DIR/erts-$ERTS_VSN/bin
  115. # Setup command to control the node
  116. NODETOOL="$ERTS_PATH/escript $ERTS_PATH/nodetool $NAME_ARG $COOKIE_ARG"
  117. # Setup remote shell command to control node
  118. REMSH="$ERTS_PATH/erl -hidden $REMSH_NAME_ARG $REMSH_REMSH_ARG $COOKIE_ARG"
  119. # Common functions
  120. # Ping node without allowing nodetool to take stdin
  121. ping_node() {
  122. $NODETOOL ping < /dev/null
  123. }
  124. # Set the PID global variable, return 1 on error
  125. get_pid() {
  126. PID=`$NODETOOL getpid < /dev/null`
  127. ES=$?
  128. if [ "$ES" -ne 0 ]; then
  129. echo "Node is not running!"
  130. return 1
  131. fi
  132. # don't allow empty or init pid's
  133. if [ -z $PID ] || [ "$PID" -le 1 ]; then
  134. return 1
  135. fi
  136. return 0
  137. }
  138. # Check the first argument for instructions
  139. case "$1" in
  140. start|start_boot)
  141. # Make sure there is not already a node running
  142. RES=`ping_node`
  143. if [ "$RES" = "pong" ]; then
  144. echo "Node is already running!"
  145. exit 1
  146. fi
  147. case "$1" in
  148. start)
  149. shift
  150. START_OPTION="console"
  151. HEART_OPTION="start"
  152. ;;
  153. start_boot)
  154. shift
  155. START_OPTION="console_boot"
  156. HEART_OPTION="start_boot"
  157. ;;
  158. esac
  159. RUN_PARAM=$(printf "\'%s\' " "$@")
  160. HEART_COMMAND="$RUNNER_BASE_DIR/bin/$SCRIPT $HEART_OPTION $RUN_PARAM"
  161. export HEART_COMMAND
  162. mkdir -p $PIPE_DIR
  163. $ERTS_PATH/run_erl -daemon $PIPE_DIR $RUNNER_LOG_DIR "exec $RUNNER_BASE_DIR/bin/$SCRIPT $START_OPTION $RUN_PARAM" 2>&1
  164. ;;
  165. stop)
  166. # Wait for the node to completely stop...
  167. case `uname -s` in
  168. Darwin)
  169. # Make sure we explicitly set this because iTerm.app doesn't for
  170. # some reason.
  171. COMMAND_MODE=unix2003
  172. esac
  173. # Get the PID from nodetool
  174. get_pid
  175. GPR=$?
  176. if [ "$GPR" -ne 0 ] || [ -z $PID ]; then
  177. exit $GPR
  178. fi
  179. # Tell nodetool to initiate a stop
  180. $NODETOOL stop
  181. ES=$?
  182. if [ "$ES" -ne 0 ]; then
  183. exit $ES
  184. fi
  185. # Wait for the node to completely stop...
  186. while `kill -s 0 $PID 2>/dev/null`
  187. do
  188. sleep 1
  189. done
  190. ;;
  191. restart)
  192. ## Restart the VM without exiting the process
  193. $NODETOOL restart
  194. ES=$?
  195. if [ "$ES" -ne 0 ]; then
  196. exit $ES
  197. fi
  198. ;;
  199. reboot)
  200. ## Restart the VM completely (uses heart to restart it)
  201. $NODETOOL reboot
  202. ES=$?
  203. if [ "$ES" -ne 0 ]; then
  204. exit $ES
  205. fi
  206. ;;
  207. ping)
  208. ## See if the VM is alive
  209. ping_node
  210. ES=$?
  211. if [ "$ES" -ne 0 ]; then
  212. exit $ES
  213. fi
  214. ;;
  215. attach)
  216. # Make sure a node is running
  217. ping_node
  218. ES=$?
  219. if [ "$ES" -ne 0 ]; then
  220. echo "Node is not running!"
  221. exit $ES
  222. fi
  223. shift
  224. exec $ERTS_PATH/to_erl $PIPE_DIR
  225. ;;
  226. remote_console)
  227. # Make sure a node is running
  228. ping_node
  229. ES=$?
  230. if [ "$ES" -ne 0 ]; then
  231. echo "Node is not running!"
  232. exit $ES
  233. fi
  234. shift
  235. exec $REMSH
  236. ;;
  237. upgrade)
  238. if [ -z "$2" ]; then
  239. echo "Missing upgrade package argument"
  240. echo "Usage: $SCRIPT upgrade {package base name}"
  241. echo "NOTE {package base name} MUST NOT include the .tar.gz suffix"
  242. exit 1
  243. fi
  244. # Make sure a node IS running
  245. ping_node
  246. ES=$?
  247. if [ "$ES" -ne 0 ]; then
  248. echo "Node is not running!"
  249. exit $ES
  250. fi
  251. node_name=`echo $NAME_ARG | awk '{print $2}'`
  252. erlang_cookie=`echo $COOKIE_ARG | awk '{print $2}'`
  253. $ERTS_PATH/escript $RUNNER_BASE_DIR/bin/install_upgrade.escript $node_name $erlang_cookie $2
  254. ;;
  255. console|console_clean|console_boot)
  256. # .boot file typically just $SCRIPT (ie, the app name)
  257. # however, for debugging, sometimes start_clean.boot is useful.
  258. # For e.g. 'setup', one may even want to name another boot script.
  259. case "$1" in
  260. console) BOOTFILE=$SCRIPT ;;
  261. console_clean) BOOTFILE=start_clean ;;
  262. console_boot)
  263. shift
  264. BOOTFILE="$1"
  265. shift
  266. ;;
  267. esac
  268. # Setup beam-required vars
  269. ROOTDIR=$RUNNER_BASE_DIR
  270. BINDIR=$ROOTDIR/erts-$ERTS_VSN/bin
  271. EMU=beam
  272. PROGNAME=`echo $0 | sed 's/.*\\///'`
  273. CMD="$BINDIR/erlexec -boot $RUNNER_BASE_DIR/releases/$APP_VSN/$BOOTFILE -mode embedded -config $CONFIG_PATH -args_file $VMARGS_PATH"
  274. export EMU
  275. export ROOTDIR
  276. export BINDIR
  277. export PROGNAME
  278. # Dump environment info for logging purposes
  279. echo "Exec: $CMD" -- ${1+"$@"}
  280. echo "Root: $ROOTDIR"
  281. # Log the startup
  282. logger -t "$SCRIPT[$$]" "Starting up"
  283. # Start the VM
  284. exec $CMD -- ${1+"$@"}
  285. ;;
  286. foreground)
  287. # start up the release in the foreground for use by runit
  288. # or other supervision services
  289. BOOTFILE=$SCRIPT
  290. FOREGROUNDOPTIONS="-noinput +Bd"
  291. # Setup beam-required vars
  292. ROOTDIR=$RUNNER_BASE_DIR
  293. BINDIR=$ROOTDIR/erts-$ERTS_VSN/bin
  294. EMU=beam
  295. PROGNAME=`echo $0 | sed 's/.*\///'`
  296. CMD="$BINDIR/erlexec $FOREGROUNDOPTIONS -boot $RUNNER_BASE_DIR/releases/$APP_VSN/$BOOTFILE -config $CONFIG_PATH -args_file $VMARGS_PATH"
  297. export EMU
  298. export ROOTDIR
  299. export BINDIR
  300. export PROGNAME
  301. # Dump environment info for logging purposes
  302. echo "Exec: $CMD" -- ${1+"$@"}
  303. echo "Root: $ROOTDIR"
  304. # Start the VM
  305. exec $CMD -- ${1+"$@"}
  306. ;;
  307. getpid)
  308. # Get the PID from nodetool
  309. get_pid
  310. ES=$?
  311. if [ "$ES" -ne 0 ] || [ -z $PID ]; then
  312. exit $ES
  313. fi
  314. echo $PID
  315. ;;
  316. *)
  317. echo "Usage: $SCRIPT {start|start_boot <file>|foreground|stop|restart|reboot|ping|console|getpid|console_clean|console_boot <file>|attach|remote_console|upgrade}"
  318. exit 1
  319. ;;
  320. esac
  321. exit 0