emqx 21 KB

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