emqx 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631
  1. #!/bin/sh
  2. # -*- tab-width:4;indent-tabs-mode:nil -*-
  3. # ex: ts=4 sw=4 et
  4. set -e
  5. ROOT_DIR="$(cd $(dirname $(readlink $0 || echo $0))/..; pwd -P)"
  6. . $ROOT_DIR/releases/emqx_vars
  7. RUNNER_SCRIPT="$RUNNER_BIN_DIR/$REL_NAME"
  8. CODE_LOADING_MODE="${CODE_LOADING_MODE:-embedded}"
  9. REL_DIR="$RUNNER_ROOT_DIR/releases/$REL_VSN"
  10. WHOAMI=$(whoami)
  11. # Make sure log directory exists
  12. mkdir -p "$RUNNER_LOG_DIR"
  13. # Make sure data directory exists
  14. mkdir -p "$RUNNER_DATA_DIR"
  15. relx_usage() {
  16. command="$1"
  17. case "$command" in
  18. unpack)
  19. echo "Usage: $REL_NAME unpack [VERSION]"
  20. echo "Unpacks a release package VERSION, it assumes that this"
  21. echo "release package tarball has already been deployed at one"
  22. echo "of the following locations:"
  23. echo " releases/<relname>-<version>.tar.gz"
  24. echo " releases/<relname>-<version>.zip"
  25. ;;
  26. install)
  27. echo "Usage: $REL_NAME install [VERSION]"
  28. echo "Installs a release package VERSION, it assumes that this"
  29. echo "release package tarball has already been deployed at one"
  30. echo "of the following locations:"
  31. echo " releases/<relname>-<version>.tar.gz"
  32. echo " releases/<relname>-<version>.zip"
  33. echo ""
  34. echo " --no-permanent Install release package VERSION but"
  35. echo " don't make it permanent"
  36. ;;
  37. uninstall)
  38. echo "Usage: $REL_NAME uninstall [VERSION]"
  39. echo "Uninstalls a release VERSION, it will only accept"
  40. echo "versions that are not currently in use"
  41. ;;
  42. upgrade)
  43. echo "Usage: $REL_NAME upgrade [VERSION]"
  44. echo "Upgrades the currently running release to VERSION, it assumes"
  45. echo "that a release package tarball has already been deployed at one"
  46. echo "of the following locations:"
  47. echo " releases/<relname>-<version>.tar.gz"
  48. echo " releases/<relname>-<version>.zip"
  49. echo ""
  50. echo " --no-permanent Install release package VERSION but"
  51. echo " don't make it permanent"
  52. ;;
  53. downgrade)
  54. echo "Usage: $REL_NAME downgrade [VERSION]"
  55. echo "Downgrades the currently running release to VERSION, it assumes"
  56. echo "that a release package tarball has already been deployed at one"
  57. echo "of the following locations:"
  58. echo " releases/<relname>-<version>.tar.gz"
  59. echo " releases/<relname>-<version>.zip"
  60. echo ""
  61. echo " --no-permanent Install release package VERSION but"
  62. echo " don't make it permanent"
  63. ;;
  64. *)
  65. echo "Usage: $REL_NAME {start|start_boot <file>|ertspath|foreground|stop|restart|reboot|pid|ping|console|console_clean|console_boot <file>|attach|remote_console|upgrade|downgrade|install|uninstall|versions|escript|rpc|rpcterms|eval|root_dir}"
  66. ;;
  67. esac
  68. }
  69. # Simple way to check the correct user and fail early
  70. check_user() {
  71. # Validate that the user running the script is the owner of the
  72. # RUN_DIR.
  73. if ([ "$RUNNER_USER" ] && [ "x$WHOAMI" != "x$RUNNER_USER" ]); then
  74. if [ "x$WHOAMI" != "xroot" ]; then
  75. echo "You need to be root or use sudo to run this command"
  76. exit 1
  77. fi
  78. CMD="\"$RUNNER_SCRIPT\" "
  79. for ARG in "$@"; do
  80. CMD="${CMD} \"$ARG\""
  81. done
  82. # This will drop priviledges into the runner user
  83. # It exec's in a new shell and the current shell will exit
  84. exec su - $RUNNER_USER -c "$CMD"
  85. fi
  86. }
  87. # Make sure the user running this script is the owner and/or su to that user
  88. check_user $@
  89. ES=$?
  90. if [ "$ES" -ne 0 ]; then
  91. exit $ES
  92. fi
  93. if [ -z "$WITH_EPMD" ]; then
  94. EPMD_ARG="-start_epmd false -epmd_module ekka_epmd -proto_dist ekka"
  95. else
  96. EPMD_ARG="-start_epmd true $PROTO_DIST_ARG"
  97. fi
  98. # Warn the user if ulimit -n is less than 1024
  99. ULIMIT_F=`ulimit -n`
  100. if [ "$ULIMIT_F" -lt 1024 ]; then
  101. echo "!!!!"
  102. echo "!!!! WARNING: ulimit -n is ${ULIMIT_F}; 1024 is the recommended minimum."
  103. echo "!!!!"
  104. fi
  105. # Echo to stderr on errors
  106. echoerr() { echo "$@" 1>&2; }
  107. # By default, use cuttlefish to generate app.config and vm.args
  108. CUTTLEFISH="${USE_CUTTLEFISH:-yes}"
  109. SED_REPLACE="sed -i "
  110. case $(sed --help 2>&1) in
  111. *GNU*) SED_REPLACE="sed -i ";;
  112. *BusyBox*) SED_REPLACE="sed -i ";;
  113. *) SED_REPLACE="sed -i '' ";;
  114. esac
  115. # Get node pid
  116. relx_get_pid() {
  117. if output="$(relx_nodetool rpcterms os getpid)"
  118. then
  119. echo "$output" | sed -e 's/"//g'
  120. return 0
  121. else
  122. echo "$output"
  123. return 1
  124. fi
  125. }
  126. relx_get_nodename() {
  127. id="longname$(relx_gen_id)-${NAME}"
  128. "$BINDIR/erl" -boot "$REL_DIR/start_clean" -eval '[Host] = tl(string:tokens(atom_to_list(node()),"@")), io:format("~s~n", [Host]), halt()' -noshell ${NAME_TYPE} $id
  129. }
  130. # Connect to a remote node
  131. relx_rem_sh() {
  132. # Generate a unique id used to allow multiple remsh to the same node
  133. # transparently
  134. id="remsh$(relx_gen_id)-${NAME}"
  135. # Get the node's ticktime so that we use the same thing.
  136. TICKTIME="$(relx_nodetool rpcterms net_kernel get_net_ticktime)"
  137. # Setup remote shell command to control node
  138. exec "$BINDIR/erl" "$NAME_TYPE" "$id" -remsh "$NAME" -boot "$REL_DIR/start_clean" \
  139. -boot_var ERTS_LIB_DIR "$ERTS_LIB_DIR" \
  140. -setcookie "$COOKIE" -hidden -kernel net_ticktime $TICKTIME $EPMD_ARG
  141. }
  142. # Generate a random id
  143. relx_gen_id() {
  144. od -t x -N 4 /dev/urandom | head -n1 | awk '{print $2}'
  145. }
  146. # Control a node
  147. relx_nodetool() {
  148. command="$1"; shift
  149. ERL_FLAGS="$ERL_FLAGS $EPMD_ARG" \
  150. "$ERTS_DIR/bin/escript" "$ROOTDIR/bin/nodetool" "$NAME_TYPE" "$NAME" \
  151. -setcookie "$COOKIE" "$command" $@
  152. }
  153. # Run an escript in the node's environment
  154. relx_escript() {
  155. shift; scriptpath="$1"; shift
  156. export RUNNER_ROOT_DIR
  157. "$ERTS_DIR/bin/escript" "$ROOTDIR/$scriptpath" $@
  158. }
  159. # Output a start command for the last argument of run_erl
  160. relx_start_command() {
  161. printf "exec \"%s\" \"%s\"" "$RUNNER_SCRIPT" \
  162. "$START_OPTION"
  163. }
  164. # Function to generate app.config and vm.args
  165. generate_config() {
  166. ## Delete the *.siz files first or it cann't start after
  167. ## changing the config 'log.rotation.size'
  168. rm -rf "${RUNNER_LOG_DIR}"/*.siz
  169. if [ "$CUTTLEFISH" != "yes" ]; then
  170. # Note: we have added a parameter '-vm_args' to this. It
  171. # appears redundant but it is not! the erlang vm allows us to
  172. # access all arguments to the erl command EXCEPT '-args_file',
  173. # so in order to get access to this file location from within
  174. # the vm, we need to pass it in twice.
  175. CONFIG_ARGS=" -config $RUNNER_ETC_DIR/app.config -args_file $RUNNER_ETC_DIR/vm.args -vm_args $RUNNER_ETC_DIR/vm.args "
  176. else
  177. CONFIG_ARGS=`$ERTS_PATH/escript $RUNNER_ROOT_DIR/bin/cuttlefish -i $REL_DIR/emqx.schema -c $RUNNER_ETC_DIR/emqx.conf -d $RUNNER_DATA_DIR/configs generate`
  178. ## Merge cuttlefish generated *.args into the vm.args
  179. CUTTLE_GEN_ARG_FILE=`echo "$CONFIG_ARGS" | sed -n 's/^.*\(vm_args[[:space:]]\)//p' | awk '{print $1}'`
  180. TMP_ARG_FILE="$RUNNER_DATA_DIR/configs/vm.args.tmp"
  181. cp "$RUNNER_ETC_DIR/vm.args" "$TMP_ARG_FILE"
  182. echo "" >> "$TMP_ARG_FILE"
  183. sed '/^#/d' $CUTTLE_GEN_ARG_FILE | sed '/^$/d' | while IFS='' read -r ARG_LINE || [ -n "$ARG_LINE" ]; do
  184. ARG_KEY=`echo "$ARG_LINE" | awk '{$NF="";print}'`
  185. ARG_VALUE=`echo "$ARG_LINE" | awk '{print $NF}'`
  186. TMP_ARG_VALUE=`grep "^$ARG_KEY" "$TMP_ARG_FILE" | awk '{print $NF}'`
  187. if [ "$ARG_VALUE" != "$TMP_ARG_VALUE" ] ; then
  188. if [ ! -z $TMP_ARG_VALUE ]; then
  189. sh -c "$SED_REPLACE 's/^$ARG_KEY.*$/$ARG_LINE/' $TMP_ARG_FILE"
  190. else
  191. echo "$ARG_LINE" >> "$TMP_ARG_FILE"
  192. fi
  193. fi
  194. done
  195. mv -f "$TMP_ARG_FILE" "$CUTTLE_GEN_ARG_FILE"
  196. fi
  197. if ! relx_nodetool chkconfig $CONFIG_ARGS; then
  198. echoerr "Error reading $CONFIG_ARGS"
  199. exit 1
  200. fi
  201. }
  202. # Call bootstrapd for daemon commands like start/stop/console
  203. bootstrapd() {
  204. if [ -e "$RUNNER_DATA_DIR/.erlang.cookie" ]; then
  205. chown $RUNNER_USER $RUNNER_DATA_DIR/.erlang.cookie
  206. fi
  207. }
  208. # Use $CWD/etc/sys.config if exists
  209. if [ -z "$RELX_CONFIG_PATH" ]; then
  210. if [ -f "$RUNNER_ETC_DIR/sys.config" ]; then
  211. RELX_CONFIG_PATH="-config $RUNNER_ETC_DIR/sys.config"
  212. else
  213. RELX_CONFIG_PATH=""
  214. fi
  215. fi
  216. if [ -z "$NAME_ARG" ]; then
  217. NODENAME="${EMQX_NODE_NAME:-}"
  218. # check if there is a node running, inspect its name
  219. [ -z "$NODENAME" ] && NODENAME=`ps -ef | grep -E '\-progname\s.*emqx\s' | grep -o -E '\-name (\S*)' | awk '{print $2}'`
  220. [ -z "$NODENAME" ] && NODENAME=`egrep '^[ \t]*node.name[ \t]*=[ \t]*' "$RUNNER_ETC_DIR/emqx.conf" 2> /dev/null | tail -1 | cut -d = -f 2-`
  221. if [ -z "$NODENAME" ]; then
  222. echoerr "vm.args needs to have a -name parameter."
  223. echoerr " -sname is not supported."
  224. echoerr "perhaps you do not have read permissions on $RUNNER_ETC_DIR/emqx.conf"
  225. exit 1
  226. else
  227. NAME_ARG="-name ${NODENAME# *}"
  228. fi
  229. fi
  230. # Extract the name type and name from the NAME_ARG for REMSH
  231. NAME_TYPE="$(echo "$NAME_ARG" | awk '{print $1}')"
  232. NAME="$(echo "$NAME_ARG" | awk '{print $2}')"
  233. NODENAME="$(echo "$NAME" | awk -F'@' '{print $1}')"
  234. export ESCRIPT_NAME="$NODENAME"
  235. PIPE_DIR="${PIPE_DIR:-/$RUNNER_DATA_DIR/${WHOAMI}_erl_pipes/$NAME/}"
  236. # Extract the target cookie
  237. if [ -z "$COOKIE_ARG" ]; then
  238. COOKIE="${EMQX_NODE_COOKIE:-}"
  239. # check if there is a node running, steal its cookie
  240. [ -z "$COOKIE" ] && COOKIE=`ps -ef | grep -E '\-progname\s.*emqx\s' | grep -o -E '\-setcookie (\S*)' | awk '{print $2}'`
  241. [ -z "$COOKIE" ] && COOKIE=`egrep '^[ \t]*node.cookie[ \t]*=[ \t]*' "$RUNNER_ETC_DIR/emqx.conf" 2> /dev/null | tail -1 | cut -d = -f 2-`
  242. if [ -z "$COOKIE" ]; then
  243. echoerr "vm.args needs to have a -setcookie parameter."
  244. echoerr "please check $RUNNER_ETC_DIR/emqx.conf"
  245. exit 1
  246. else
  247. COOKIE_ARG="-setcookie $COOKIE"
  248. fi
  249. fi
  250. # Extract cookie name from COOKIE_ARG
  251. COOKIE="$(echo "$COOKIE_ARG" | awk '{print $2}')"
  252. # Support for IPv6 Dist. See: https://github.com/emqtt/emqttd/issues/1460
  253. PROTO_DIST=`egrep '^[ \t]*cluster.proto_dist[ \t]*=[ \t]*' "$RUNNER_ETC_DIR/emqx.conf" 2> /dev/null | tail -1 | cut -d = -f 2-`
  254. if [ -z "$PROTO_DIST" ]; then
  255. PROTO_DIST_ARG=""
  256. else
  257. PROTO_DIST_ARG="-proto_dist $PROTO_DIST"
  258. fi
  259. export ROOTDIR="$RUNNER_ROOT_DIR"
  260. export ERTS_DIR="$ROOTDIR/erts-$ERTS_VSN"
  261. export BINDIR="$ERTS_DIR/bin"
  262. export EMU="beam"
  263. export PROGNAME="erl"
  264. export LD_LIBRARY_PATH="$ERTS_DIR/lib:$LD_LIBRARY_PATH"
  265. ERTS_LIB_DIR="$ERTS_DIR/../lib"
  266. MNESIA_DATA_DIR="$RUNNER_DATA_DIR/mnesia/$NAME"
  267. cd "$ROOTDIR"
  268. # User can specify an sname without @hostname
  269. # This will fail when creating remote shell
  270. # So here we check for @ and add @hostname if missing
  271. case $NAME in
  272. *@*)
  273. # Nothing to do
  274. ;;
  275. *)
  276. NAME=$NAME@$(relx_get_nodename)
  277. ;;
  278. esac
  279. # Check the first argument for instructions
  280. case "$1" in
  281. start|start_boot)
  282. # Make sure a node IS not running
  283. if relx_nodetool "ping" >/dev/null 2>&1; then
  284. echo "Node is already running!"
  285. exit 1
  286. fi
  287. # Bootstrap daemon command (check perms & drop to $RUNNER_USER)
  288. bootstrapd $@
  289. # Save this for later.
  290. CMD=$1
  291. case "$1" in
  292. start)
  293. shift
  294. START_OPTION="console"
  295. HEART_OPTION="start"
  296. ;;
  297. start_boot)
  298. shift
  299. START_OPTION="console_boot"
  300. HEART_OPTION="start_boot"
  301. ;;
  302. esac
  303. RUN_PARAM="$@"
  304. # Set arguments for the heart command
  305. set -- "$RUNNER_SCRIPT" "$HEART_OPTION"
  306. [ "$RUN_PARAM" ] && set -- "$@" "$RUN_PARAM"
  307. # Export the HEART_COMMAND
  308. HEART_COMMAND="$RUNNER_SCRIPT $CMD"
  309. export HEART_COMMAND
  310. ## See: http://erlang.org/doc/man/run_erl.html
  311. # Export the RUN_ERL_LOG_GENERATIONS
  312. export RUN_ERL_LOG_GENERATIONS=${RUN_ERL_LOG_GENERATIONS:-"5"}
  313. # Export the RUN_ERL_LOG_MAXSIZE
  314. export RUN_ERL_LOG_MAXSIZE=${RUN_ERL_LOG_MAXSIZE:-"10485760"}
  315. mkdir -p "$PIPE_DIR"
  316. "$BINDIR/run_erl" -daemon "$PIPE_DIR" "$RUNNER_LOG_DIR" \
  317. "$(relx_start_command)"
  318. WAIT_TIME=${WAIT_FOR_ERLANG:-15}
  319. while [ $WAIT_TIME -gt 0 ]; do
  320. if ! relx_nodetool "ping" >/dev/null 2>&1; then
  321. WAIT_TIME=`expr $WAIT_TIME - 1`
  322. sleep 1
  323. continue
  324. fi
  325. sleep 1
  326. if relx_nodetool "ping" >/dev/null 2>&1; then
  327. echo "$EMQX_DISCR $REL_VSN is started successfully!"
  328. exit 0
  329. fi
  330. done && echo "$EMQX_DISCR $REL_VSN failed to start within ${WAIT_FOR_ERLANG:-15} seconds,"
  331. echo "see the output of '$0 console' for more information."
  332. echo "If you want to wait longer, set the environment variable"
  333. echo "WAIT_FOR_ERLANG to the number of seconds to wait."
  334. exit 1
  335. ;;
  336. stop)
  337. # Wait for the node to completely stop...
  338. PID="$(relx_get_pid)"
  339. if ! relx_nodetool "stop"; then
  340. exit 1
  341. fi
  342. while $(kill -s 0 "$PID" 2>/dev/null); do
  343. sleep 1
  344. done
  345. ;;
  346. restart|reboot)
  347. echo "$EMQX_DISCR $REL_VSN is stopped: $($RUNNER_BIN_DIR/emqx stop)"
  348. $RUNNER_BIN_DIR/emqx start
  349. ;;
  350. pid)
  351. ## Get the VM's pid
  352. if ! relx_get_pid; then
  353. exit 1
  354. fi
  355. ;;
  356. ping)
  357. ## See if the VM is alive
  358. if ! relx_nodetool "ping"; then
  359. exit 1
  360. fi
  361. ;;
  362. escript)
  363. ## Run an escript under the node's environment
  364. if ! relx_escript $@; then
  365. exit 1
  366. fi
  367. ;;
  368. attach)
  369. # Make sure a node IS running
  370. if ! relx_nodetool "ping" > /dev/null; then
  371. echo "Node is not running!"
  372. exit 1
  373. fi
  374. # Bootstrap daemon command (check perms & drop to $RUNNER_USER)
  375. bootstrapd $@
  376. shift
  377. exec "$BINDIR/to_erl" "$PIPE_DIR"
  378. ;;
  379. remote_console)
  380. # Make sure a node IS running
  381. if ! relx_nodetool "ping" > /dev/null; then
  382. echo "Node is not running!"
  383. exit 1
  384. fi
  385. # Bootstrap daemon command (check perms & drop to $RUNNER_USER)
  386. bootstrapd $@
  387. shift
  388. relx_rem_sh
  389. ;;
  390. upgrade|downgrade|install|unpack|uninstall)
  391. if [ -z "$2" ]; then
  392. echo "Missing version argument"
  393. echo "Usage: $REL_NAME $1 {version}"
  394. exit 1
  395. fi
  396. COMMAND="$1"; shift
  397. # Make sure a node IS running
  398. if ! relx_nodetool "ping" > /dev/null; then
  399. echo "Node is not running!"
  400. exit 1
  401. fi
  402. ERL_FLAGS="$ERL_FLAGS $EPMD_ARG" \
  403. exec "$BINDIR/escript" "$ROOTDIR/bin/install_upgrade.escript" \
  404. "$COMMAND" "{'$REL_NAME', \"$NAME_TYPE\", '$NAME', '$COOKIE'}" "$@"
  405. ;;
  406. versions)
  407. # Make sure a node IS running
  408. if ! relx_nodetool "ping" > /dev/null; then
  409. echo "Node is not running!"
  410. exit 1
  411. fi
  412. COMMAND="$1"; shift
  413. ERL_FLAGS="$ERL_FLAGS $EPMD_ARG" \
  414. exec "$BINDIR/escript" "$ROOTDIR/bin/install_upgrade.escript" \
  415. "versions" "{'$REL_NAME', \"$NAME_TYPE\", '$NAME', '$COOKIE'}" "$@"
  416. ;;
  417. console|console_clean|console_boot)
  418. # Bootstrap daemon command (check perms & drop to $RUNNER_USER)
  419. bootstrapd $@
  420. # .boot file typically just $REL_NAME (ie, the app name)
  421. # however, for debugging, sometimes start_clean.boot is useful.
  422. # For e.g. 'setup', one may even want to name another boot script.
  423. case "$1" in
  424. console)
  425. if [ -f "$REL_DIR/$REL_NAME.boot" ]; then
  426. BOOTFILE="$REL_DIR/$REL_NAME"
  427. else
  428. BOOTFILE="$REL_DIR/start"
  429. fi
  430. ;;
  431. console_clean)
  432. BOOTFILE="$REL_DIR/start_clean"
  433. ;;
  434. console_boot)
  435. shift
  436. BOOTFILE="$1"
  437. shift
  438. ;;
  439. esac
  440. #generate app.config and vm.args
  441. generate_config
  442. # Setup beam-required vars
  443. EMU="beam"
  444. PROGNAME="${0#*/}"
  445. export EMU
  446. export PROGNAME
  447. # Store passed arguments since they will be erased by `set`
  448. ARGS="$@"
  449. # Build an array of arguments to pass to exec later on
  450. # Build it here because this command will be used for logging.
  451. set -- "$BINDIR/erlexec" -boot "$BOOTFILE" -mode "$CODE_LOADING_MODE" \
  452. -boot_var ERTS_LIB_DIR "$ERTS_LIB_DIR" \
  453. -mnesia dir "\"${MNESIA_DATA_DIR}\"" \
  454. $RELX_CONFIG_PATH $CONFIG_ARGS $EPMD_ARG
  455. # Dump environment info for logging purposes
  456. echo "Exec: $@" -- ${1+$ARGS}
  457. echo "Root: $ROOTDIR"
  458. # Log the startup
  459. echo "$RUNNER_ROOT_DIR"
  460. logger -t "$REL_NAME[$$]" "Starting up"
  461. # Start the VM
  462. exec "$@" -- ${1+$ARGS}
  463. ;;
  464. foreground)
  465. # Bootstrap daemon command (check perms & drop to $RUNNER_USER)
  466. bootstrapd $@
  467. # start up the release in the foreground for use by runit
  468. # or other supervision services
  469. #generate app.config and vm.args
  470. generate_config
  471. [ -f "$REL_DIR/$REL_NAME.boot" ] && BOOTFILE="$REL_NAME" || BOOTFILE=start
  472. FOREGROUNDOPTIONS="-noshell -noinput +Bd"
  473. # Setup beam-required vars
  474. EMU=beam
  475. PROGNAME="${0#*/}"
  476. export EMU
  477. export PROGNAME
  478. # Store passed arguments since they will be erased by `set`
  479. ARGS="$@"
  480. # Build an array of arguments to pass to exec later on
  481. # Build it here because this command will be used for logging.
  482. set -- "$BINDIR/erlexec" $FOREGROUNDOPTIONS \
  483. -boot "$REL_DIR/$BOOTFILE" -mode "$CODE_LOADING_MODE" \
  484. -boot_var ERTS_LIB_DIR "$ERTS_LIB_DIR" \
  485. -mnesia dir "\"${MNESIA_DATA_DIR}\"" \
  486. $RELX_CONFIG_PATH $CONFIG_ARGS $EPMD_ARG
  487. # Dump environment info for logging purposes
  488. echo "Exec: $@" -- ${1+$ARGS}
  489. echo "Root: $ROOTDIR"
  490. # Start the VM
  491. exec "$@" -- ${1+$ARGS}
  492. ;;
  493. ertspath)
  494. echo $ERTS_PATH
  495. ;;
  496. rpc)
  497. # Make sure a node IS running
  498. if ! relx_nodetool "ping" > /dev/null; then
  499. echo "Node is not running!"
  500. exit 1
  501. fi
  502. shift
  503. relx_nodetool rpc $@
  504. ;;
  505. rpcterms)
  506. # Make sure a node IS running
  507. if ! relx_nodetool "ping" > /dev/null; then
  508. echo "Node is not running!"
  509. exit 1
  510. fi
  511. shift
  512. relx_nodetool rpcterms $@
  513. ;;
  514. root_dir)
  515. # Make sure a node IS running
  516. if ! relx_nodetool "ping" > /dev/null; then
  517. echo "Node is not running!"
  518. exit 1
  519. fi
  520. shift
  521. relx_nodetool "eval" 'code:root_dir()'
  522. ;;
  523. eval)
  524. # Make sure a node IS running
  525. if ! relx_nodetool "ping" > /dev/null; then
  526. echo "Node is not running!"
  527. exit 1
  528. fi
  529. shift
  530. relx_nodetool "eval" $@
  531. ;;
  532. *)
  533. relx_usage $1
  534. exit 1
  535. ;;
  536. esac
  537. exit 0