emqx 22 KB

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