emqx 21 KB

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