emqx 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882
  1. #!/usr/bin/env bash
  2. # -*- tab-width:4;indent-tabs-mode:nil -*-
  3. # ex: ts=4 sw=4 et
  4. set -euo pipefail
  5. DEBUG="${DEBUG:-0}"
  6. if [ "$DEBUG" -eq 1 ]; then
  7. set -x
  8. fi
  9. ROOT_DIR="$(cd "$(dirname "$(readlink "$0" || echo "$0")")"/..; pwd -P)"
  10. # shellcheck disable=SC1090,SC1091
  11. . "$ROOT_DIR"/releases/emqx_vars
  12. # defined in emqx_vars
  13. export RUNNER_ROOT_DIR
  14. export RUNNER_ETC_DIR
  15. export REL_VSN
  16. RUNNER_SCRIPT="$RUNNER_BIN_DIR/$REL_NAME"
  17. CODE_LOADING_MODE="${CODE_LOADING_MODE:-embedded}"
  18. REL_DIR="$RUNNER_ROOT_DIR/releases/$REL_VSN"
  19. SCHEMA_MOD=emqx_conf_schema
  20. WHOAMI=$(whoami)
  21. # Make sure log directory exists
  22. mkdir -p "$RUNNER_LOG_DIR"
  23. # Make sure data directory exists
  24. mkdir -p "$RUNNER_DATA_DIR"
  25. # Make sure data/configs exists
  26. CONFIGS_DIR="$RUNNER_DATA_DIR/configs"
  27. mkdir -p "$CONFIGS_DIR"
  28. # hocon try to read environment variables starting with "EMQX_"
  29. export HOCON_ENV_OVERRIDE_PREFIX='EMQX_'
  30. export ROOTDIR="$RUNNER_ROOT_DIR"
  31. export ERTS_DIR="$ROOTDIR/erts-$ERTS_VSN"
  32. export BINDIR="$ERTS_DIR/bin"
  33. export EMU="beam"
  34. export PROGNAME="erl"
  35. export ERTS_LIB_DIR="$ERTS_DIR/../lib"
  36. DYNLIBS_DIR="$RUNNER_ROOT_DIR/dynlibs"
  37. # Echo to stderr on errors
  38. echoerr() { echo "ERROR: $*" 1>&2; }
  39. die() {
  40. echoerr "ERROR: $1"
  41. errno=${2:-1}
  42. exit "$errno"
  43. }
  44. assert_node_alive() {
  45. if ! relx_nodetool "ping" > /dev/null; then
  46. die "node_is_not_running!" 1
  47. fi
  48. }
  49. # Echo to stderr on errors
  50. echoerr() { echo "$*" 1>&2; }
  51. check_erlang_start() {
  52. # RELEASE_LIB is used by Elixir
  53. "$BINDIR/$PROGNAME" \
  54. -noshell \
  55. -boot_var RELEASE_LIB "$ERTS_LIB_DIR/lib" \
  56. -boot "$REL_DIR/start_clean" \
  57. -s crypto start \
  58. -s erlang halt
  59. }
  60. usage() {
  61. local command="$1"
  62. case "$command" in
  63. start)
  64. echo "Start EMQ X service in daemon mode"
  65. ;;
  66. stop)
  67. echo "Stop the running EMQ X program"
  68. ;;
  69. console)
  70. echo "Boot up EMQ X service in an interactive Erlang or Elixir shell"
  71. echo "This command needs a tty"
  72. ;;
  73. console_clean)
  74. echo "This command does NOT boot up the EMQ X service"
  75. echo "It only starts an interactive Erlang or Elixir console with all the"
  76. echo "EMQ X code available"
  77. ;;
  78. foreground)
  79. echo "Start EMQ X in foreground mode without an interactive shell"
  80. ;;
  81. pid)
  82. echo "Print out EMQ X process identifier"
  83. ;;
  84. ping)
  85. echo "Check if the EMQ X node is up and running"
  86. echo "This command exit with 0 silently if node is running"
  87. ;;
  88. escript)
  89. echo "Execute a escript using the Erlang runtime from EMQ X package installation"
  90. echo "For example $REL_NAME escript /path/to/my/escript my_arg1 my_arg2"
  91. ;;
  92. attach)
  93. echo "This command is applicable when EMQ X is started in daemon"
  94. echo "mode. it attaches the current shell to EMQ X's control console"
  95. echo "through a named pipe"
  96. echo "WARNING: try to use the safer alternative, remote_console command."
  97. ;;
  98. remote_console)
  99. echo "Start a dummy Erlang or Elixir node and hidden-connect EMQ X to"
  100. echo "with an interactive Erlang or Elixir shell"
  101. ;;
  102. ertspath)
  103. echo "Print path to Erlang runtime dir"
  104. ;;
  105. rpc)
  106. echo "Usge $REL_NAME rpc MODULE FUNCTION [ARGS, ...]"
  107. echo "Connect to the EMQ X node and make an Erlang RPC"
  108. echo "This command blocks for at most 60 seconds."
  109. echo "It exits with non-zero code in case of any RPC failure"
  110. echo "including connection error and runtime exception"
  111. ;;
  112. rpcterms)
  113. echo "Usge $REL_NAME rpcterms MODULE FUNCTION [ARGS, ...]"
  114. echo "Connect to the EMQ X node and make an Erlang RPC"
  115. echo "The result of the RPC call is pretty-printed as an "
  116. echo "Erlang term"
  117. ;;
  118. root_dir)
  119. echo "Print EMQ X installation root dir"
  120. ;;
  121. eval)
  122. echo "Evaluate an Erlang or Elxir expression in the EMQ X node"
  123. ;;
  124. versions)
  125. echo "List installed EMQ X versions and their status"
  126. ;;
  127. unpack)
  128. echo "Usage: $REL_NAME unpack [VERSION]"
  129. echo "Unpacks a release package VERSION, it assumes that this"
  130. echo "release package tarball has already been deployed at one"
  131. echo "of the following locations:"
  132. echo " releases/<relname>-<version>.tar.gz"
  133. ;;
  134. install)
  135. echo "Usage: $REL_NAME install [VERSION]"
  136. echo "Installs a release package VERSION, it assumes that this"
  137. echo "release package tarball has already been deployed at one"
  138. echo "of the following locations:"
  139. echo " releases/<relname>-<version>.tar.gz"
  140. echo ""
  141. echo " --no-permanent Install release package VERSION but"
  142. echo " don't make it permanent"
  143. ;;
  144. uninstall)
  145. echo "Usage: $REL_NAME uninstall [VERSION]"
  146. echo "Uninstalls a release VERSION, it will only accept"
  147. echo "versions that are not currently in use"
  148. ;;
  149. upgrade)
  150. echo "Usage: $REL_NAME upgrade [VERSION]"
  151. echo "Upgrades the currently running release to VERSION, it assumes"
  152. echo "that a release package tarball has already been deployed at one"
  153. echo "of the following locations:"
  154. echo " releases/<relname>-<version>.tar.gz"
  155. echo ""
  156. echo " --no-permanent Install release package VERSION but"
  157. echo " don't make it permanent"
  158. ;;
  159. downgrade)
  160. echo "Usage: $REL_NAME downgrade [VERSION]"
  161. echo "Downgrades the currently running release to VERSION, it assumes"
  162. echo "that a release package tarball has already been deployed at one"
  163. echo "of the following locations:"
  164. echo " releases/<relname>-<version>.tar.gz"
  165. echo ""
  166. echo " --no-permanent Install release package VERSION but"
  167. echo " don't make it permanent"
  168. ;;
  169. *)
  170. echo "Usage: $REL_NAME COMMAND [help]"
  171. echo ''
  172. echo "Commonly used COMMANDs:"
  173. echo " start: Start EMQ X in daemon mode"
  174. echo " console: Start EMQ X in an interactive Erlang or Elixir shell"
  175. echo " foreground: Start EMQ X in foreground mode without an interactive shell"
  176. echo " stop: Stop the running EMQ X node"
  177. echo " ctl: Administration commands, execute '$REL_NAME ctl help' for more details"
  178. echo ''
  179. echo "More:"
  180. echo " Shell attach: remote_console | attach"
  181. echo " Up/Down-grade: upgrade | downgrade | install | uninstall"
  182. echo " Install info: ertspath | root_dir | versions | root_dir"
  183. echo " Runtime info: pid | ping | versions"
  184. echo " Advanced: console_clean | escript | rpc | rpcterms | eval"
  185. echo ''
  186. echo "Execute '$REL_NAME COMMAND help' for more information"
  187. ;;
  188. esac
  189. }
  190. COMMAND="${1:-}"
  191. if [ -z "$COMMAND" ]; then
  192. usage 'nil'
  193. exit 1
  194. fi
  195. if [ "${2:-}" = 'help' ]; then
  196. ## 'ctl' command has its own usage info
  197. if [ "$COMMAND" != 'ctl' ]; then
  198. usage "$COMMAND"
  199. exit 0
  200. fi
  201. fi
  202. if ! check_erlang_start >/dev/null 2>&1; then
  203. BUILT_ON="$(head -1 "${REL_DIR}/BUILT_ON")"
  204. ## failed to start, might be due to missing libs, try to be portable
  205. export LD_LIBRARY_PATH="${LD_LIBRARY_PATH:-$DYNLIBS_DIR}"
  206. if [ "$LD_LIBRARY_PATH" != "$DYNLIBS_DIR" ]; then
  207. export LD_LIBRARY_PATH="$DYNLIBS_DIR:$LD_LIBRARY_PATH"
  208. fi
  209. if ! check_erlang_start; then
  210. ## it's hopeless
  211. echoerr "FATAL: Unable to start Erlang."
  212. echoerr "Please make sure openssl-1.1.1 (libcrypto) and libncurses are installed."
  213. echoerr "Also ensure it's running on the correct platform,"
  214. echoerr "this EMQ X release is built for $BUILT_ON"
  215. exit 1
  216. fi
  217. echoerr "WARNING: There seem to be missing dynamic libs from the OS. Using libs from ${DYNLIBS_DIR}"
  218. fi
  219. ## backward compatible
  220. if [ -d "$ERTS_DIR/lib" ]; then
  221. export LD_LIBRARY_PATH="$ERTS_DIR/lib:$LD_LIBRARY_PATH"
  222. fi
  223. # Simple way to check the correct user and fail early
  224. check_user() {
  225. # Validate that the user running the script is the owner of the
  226. # RUN_DIR.
  227. if [ "$RUNNER_USER" ] && [ "x$WHOAMI" != "x$RUNNER_USER" ]; then
  228. if [ "x$WHOAMI" != "xroot" ]; then
  229. echo "You need to be root or use sudo to run this command"
  230. exit 1
  231. fi
  232. CMD="DEBUG=$DEBUG \"$RUNNER_SCRIPT\" "
  233. for ARG in "$@"; do
  234. CMD="${CMD} \"$ARG\""
  235. done
  236. # This will drop priviledges into the runner user
  237. # It exec's in a new shell and the current shell will exit
  238. exec su - "$RUNNER_USER" -c "$CMD"
  239. fi
  240. }
  241. # Make sure the user running this script is the owner and/or su to that user
  242. check_user "$@"
  243. ES=$?
  244. if [ "$ES" -ne 0 ]; then
  245. exit $ES
  246. fi
  247. # EPMD_ARG="-start_epmd true $PROTO_DIST_ARG"
  248. NO_EPMD="-start_epmd false -epmd_module ekka_epmd -proto_dist ekka"
  249. EPMD_ARG="${EPMD_ARG:-${NO_EPMD}}"
  250. # Warn the user if ulimit -n is less than 1024
  251. ULIMIT_F=$(ulimit -n)
  252. if [ "$ULIMIT_F" -lt 1024 ]; then
  253. echo "!!!!"
  254. echo "!!!! WARNING: ulimit -n is ${ULIMIT_F}; 1024 is the recommended minimum."
  255. echo "!!!!"
  256. fi
  257. SED_REPLACE="sed -i "
  258. case $(sed --help 2>&1) in
  259. *GNU*) SED_REPLACE="sed -i ";;
  260. *BusyBox*) SED_REPLACE="sed -i ";;
  261. *) SED_REPLACE="sed -i '' ";;
  262. esac
  263. # Get node pid
  264. relx_get_pid() {
  265. if output="$(relx_nodetool rpcterms os getpid)"
  266. then
  267. # shellcheck disable=SC2001 # Escaped quote taken as closing quote in editor
  268. echo "$output" | sed -e 's/"//g'
  269. return 0
  270. else
  271. echo "$output"
  272. return 1
  273. fi
  274. }
  275. # Connect to a remote node
  276. relx_rem_sh() {
  277. # Generate a unique id used to allow multiple remsh to the same node
  278. # transparently
  279. id="remsh$(relx_gen_id)-${NAME}"
  280. # Get the node's ticktime so that we use the same thing.
  281. TICKTIME="$(relx_nodetool rpcterms net_kernel get_net_ticktime)"
  282. # shellcheck disable=SC2086 # $EPMD_ARG is supposed to be split by whitespace
  283. # Setup remote shell command to control node
  284. if [ "$IS_ELIXIR" = "yes" ]
  285. then
  286. exec "$REL_DIR/iex" \
  287. --remsh "$NAME" \
  288. --boot-var RELEASE_LIB "$ERTS_LIB_DIR" \
  289. --cookie "$COOKIE" \
  290. --hidden \
  291. --erl "-kernel net_ticktime $TICKTIME" \
  292. --erl "$EPMD_ARG" \
  293. --erl "$NAME_TYPE $id" \
  294. --boot "$REL_DIR/start_clean"
  295. else
  296. exec "$BINDIR/erl" "$NAME_TYPE" "$id" \
  297. -remsh "$NAME" -boot "$REL_DIR/start_clean" \
  298. -boot_var ERTS_LIB_DIR "$ERTS_LIB_DIR" \
  299. -setcookie "$COOKIE" -hidden -kernel net_ticktime "$TICKTIME" \
  300. $EPMD_ARG
  301. fi
  302. }
  303. # Generate a random id
  304. relx_gen_id() {
  305. od -t x -N 4 /dev/urandom | head -n1 | awk '{print $2}'
  306. }
  307. # Control a node
  308. relx_nodetool() {
  309. command="$1"; shift
  310. ERL_FLAGS="${ERL_FLAGS:-} $EPMD_ARG" \
  311. "$ERTS_DIR/bin/escript" "$ROOTDIR/bin/nodetool" "$NAME_TYPE" "$NAME" \
  312. -setcookie "$COOKIE" "$command" "$@"
  313. }
  314. call_hocon() {
  315. "$ERTS_DIR/bin/escript" "$ROOTDIR/bin/nodetool" hocon "$@" \
  316. || die "call_hocon_failed: $*" $?
  317. }
  318. # Run an escript in the node's environment
  319. relx_escript() {
  320. shift; scriptpath="$1"; shift
  321. "$ERTS_DIR/bin/escript" "$ROOTDIR/$scriptpath" "$@"
  322. }
  323. # Output a start command for the last argument of run_erl
  324. relx_start_command() {
  325. printf "exec \"%s\" \"%s\"" "$RUNNER_SCRIPT" \
  326. "$START_OPTION"
  327. }
  328. # Function to generate app.config and vm.args
  329. generate_config() {
  330. local name_type="$1"
  331. local node_name="$2"
  332. ## Delete the *.siz files first or it cann't start after
  333. ## changing the config 'log.rotation.size'
  334. rm -rf "${RUNNER_LOG_DIR}"/*.siz
  335. EMQX_LICENSE_CONF_OPTION=""
  336. if [ "${EMQX_LICENSE_CONF:-}" != "" ]; then
  337. EMQX_LICENSE_CONF_OPTION="-c ${EMQX_LICENSE_CONF}"
  338. fi
  339. ## timestamp for each generation
  340. local NOW_TIME
  341. NOW_TIME="$(call_hocon now_time)"
  342. ## ths command populates two files: app.<time>.config and vm.<time>.args
  343. ## NOTE: the generate command merges environment variables to the base config (emqx.conf),
  344. ## but does not include the cluster-override.conf and local-override.conf
  345. ## meaning, certain overrides will not be mapped to app.<time>.config file
  346. ## disable SC2086 to allow EMQX_LICENSE_CONF_OPTION to split
  347. # shellcheck disable=SC2086
  348. 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
  349. ## filenames are per-hocon convention
  350. local CONF_FILE="$CONFIGS_DIR/app.$NOW_TIME.config"
  351. local HOCON_GEN_ARG_FILE="$CONFIGS_DIR/vm.$NOW_TIME.args"
  352. # This is needed by the Elixir scripts.
  353. # Do NOT append `.config`.
  354. RELEASE_SYS_CONFIG="$CONFIGS_DIR/app.$NOW_TIME"
  355. export RELEASE_SYS_CONFIG
  356. CONFIG_ARGS="-config $CONF_FILE -args_file $HOCON_GEN_ARG_FILE"
  357. ## Merge hocon generated *.args into the vm.args
  358. TMP_ARG_FILE="$CONFIGS_DIR/vm.args.tmp"
  359. cp "$RUNNER_ETC_DIR/vm.args" "$TMP_ARG_FILE"
  360. echo "" >> "$TMP_ARG_FILE"
  361. echo "-pa ${REL_DIR}/consolidated" >> "$TMP_ARG_FILE"
  362. ## read lines from generated vm.<time>.args file
  363. ## drop comment lines, and empty lines using sed
  364. ## pipe the lines to a while loop
  365. sed '/^#/d' "$HOCON_GEN_ARG_FILE" | sed '/^$/d' | while IFS='' read -r ARG_LINE || [ -n "$ARG_LINE" ]; do
  366. ## in the loop, split the 'key[:space:]value' pair
  367. ARG_KEY=$(echo "$ARG_LINE" | awk '{$NF="";print}')
  368. ARG_VALUE=$(echo "$ARG_LINE" | awk '{print $NF}')
  369. ## use the key to look up in vm.args file for the value
  370. TMP_ARG_VALUE=$(grep "^$ARG_KEY" "$TMP_ARG_FILE" || true | awk '{print $NF}')
  371. ## compare generated (to override) value to original (to be overriden) value
  372. if [ "$ARG_VALUE" != "$TMP_ARG_VALUE" ] ; then
  373. ## if they are different
  374. if [ -n "$TMP_ARG_VALUE" ]; then
  375. ## if the old value is present, replace it with generated value
  376. sh -c "$SED_REPLACE 's|^$ARG_KEY.*$|$ARG_LINE|' $TMP_ARG_FILE"
  377. else
  378. ## otherwise append generated value to the end
  379. echo "$ARG_LINE" >> "$TMP_ARG_FILE"
  380. fi
  381. fi
  382. done
  383. echo "$name_type $node_name" >> "$TMP_ARG_FILE"
  384. ## rename the generated vm.<time>.args file
  385. mv -f "$TMP_ARG_FILE" "$HOCON_GEN_ARG_FILE"
  386. # shellcheck disable=SC2086
  387. if ! relx_nodetool chkconfig $CONFIG_ARGS; then
  388. die "failed_to_check_config $CONFIG_ARGS"
  389. fi
  390. }
  391. # check if a PID is down
  392. is_down() {
  393. PID="$1"
  394. if ps -p "$PID" >/dev/null; then
  395. # still around
  396. # shellcheck disable=SC2009 # this grep pattern is not a part of the progra names
  397. if ps -p "$PID" | grep -q 'defunct'; then
  398. # zombie state, print parent pid
  399. parent="$(ps -o ppid= -p "$PID" | tr -d ' ')"
  400. echo "WARN: $PID is marked <defunct>, parent:"
  401. ps -p "$parent"
  402. return 0
  403. fi
  404. return 1
  405. fi
  406. # it's gone
  407. return 0
  408. }
  409. wait_for() {
  410. local WAIT_TIME
  411. local CMD
  412. WAIT_TIME="$1"
  413. shift
  414. CMD="$*"
  415. while true; do
  416. if $CMD >/dev/null 2>&1; then
  417. return 0
  418. fi
  419. if [ "$WAIT_TIME" -le 0 ]; then
  420. return 1
  421. fi
  422. WAIT_TIME=$((WAIT_TIME - 1))
  423. sleep 1
  424. done
  425. }
  426. wait_until_return_val() {
  427. local RESULT
  428. local WAIT_TIME
  429. local CMD
  430. RESULT="$1"
  431. WAIT_TIME="$2"
  432. shift 2
  433. CMD="$*"
  434. while true; do
  435. if [ "$($CMD 2>/dev/null)" = "$RESULT" ]; then
  436. return 0
  437. fi
  438. if [ "$WAIT_TIME" -le 0 ]; then
  439. return 1
  440. fi
  441. WAIT_TIME=$((WAIT_TIME - 1))
  442. sleep 1
  443. done
  444. }
  445. latest_vm_args() {
  446. local hint_var_name="$1"
  447. local vm_args_file
  448. vm_args_file="$(find "$CONFIGS_DIR" -type f -name "vm.*.args" | sort | tail -1)"
  449. if [ -f "$vm_args_file" ]; then
  450. echo "$vm_args_file"
  451. else
  452. echoerr "ERRRO: node not initialized?"
  453. echoerr "Generated config file vm.*.args is not found for command '$COMMAND'"
  454. echoerr "in config dir: $CONFIGS_DIR"
  455. echoerr "In case the file has been deleted while the node is running,"
  456. echoerr "set environment variable '$hint_var_name' to continue"
  457. exit 1
  458. fi
  459. }
  460. ## IS_BOOT_COMMAND is set for later to inspect node name and cookie from hocon config (or env variable)
  461. case "${COMMAND}" in
  462. start|console|console_clean|foreground)
  463. IS_BOOT_COMMAND='yes'
  464. ;;
  465. ertspath)
  466. echo "$ERTS_PATH"
  467. exit 0
  468. ;;
  469. *)
  470. IS_BOOT_COMMAND='no'
  471. ;;
  472. esac
  473. ## make EMQX_NODE_COOKIE right
  474. if [ -n "${EMQX_NODE_NAME:-}" ]; then
  475. export EMQX_NODE__NAME="${EMQX_NODE_NAME}"
  476. unset EMQX_NODE_NAME
  477. fi
  478. ## Possible ways to configure emqx node name:
  479. ## 1. configure node.name in emqx.conf
  480. ## 2. override with environment variable EMQX_NODE__NAME
  481. ## Node name is either short-name (without '@'), e.g. 'emqx'
  482. ## or long name (with '@') e.g. 'emqx@example.net' or 'emqx@127.0.0.1'
  483. NAME="${EMQX_NODE__NAME:-}"
  484. if [ -z "$NAME" ]; then
  485. if [ "$IS_BOOT_COMMAND" = 'yes' ]; then
  486. # for boot commands, inspect emqx.conf for node name
  487. NAME="$(call_hocon -s $SCHEMA_MOD -I "$CONFIGS_DIR/" -c "$RUNNER_ETC_DIR"/emqx.conf get node.name | tr -d \")"
  488. else
  489. vm_args_file="$(latest_vm_args 'EMQX_NODE__NAME')"
  490. NAME="$(grep -E '^-s?name' "${vm_args_file}" | awk '{print $2}')"
  491. fi
  492. fi
  493. # force to use 'emqx' short name
  494. [ -z "$NAME" ] && NAME='emqx'
  495. MNESIA_DATA_DIR="$RUNNER_DATA_DIR/mnesia/$NAME"
  496. case "$NAME" in
  497. *@*)
  498. NAME_TYPE='-name'
  499. ;;
  500. *)
  501. NAME_TYPE='-sname'
  502. esac
  503. SHORT_NAME="$(echo "$NAME" | awk -F'@' '{print $1}')"
  504. export ESCRIPT_NAME="$SHORT_NAME"
  505. PIPE_DIR="${PIPE_DIR:-/$RUNNER_DATA_DIR/${WHOAMI}_erl_pipes/$NAME/}"
  506. ## make EMQX_NODE_COOKIE right
  507. if [ -n "${EMQX_NODE_COOKIE:-}" ]; then
  508. export EMQX_NODE__COOKIE="${EMQX_NODE_COOKIE}"
  509. unset EMQX_NODE_COOKIE
  510. fi
  511. COOKIE="${EMQX_NODE__COOKIE:-}"
  512. if [ -z "$COOKIE" ]; then
  513. if [ "$IS_BOOT_COMMAND" = 'yes' ]; then
  514. COOKIE="$(call_hocon -s $SCHEMA_MOD -I "$CONFIGS_DIR/" -c "$RUNNER_ETC_DIR"/emqx.conf get node.cookie | tr -d \")"
  515. else
  516. vm_args_file="$(latest_vm_args 'EMQX_NODE__COOKIE')"
  517. COOKIE="$(grep -E '^-setcookie' "${vm_args_file}" | awk '{print $2}')"
  518. fi
  519. fi
  520. if [ -z "$COOKIE" ]; then
  521. die "Please set node.cookie in $RUNNER_ETC_DIR/emqx.conf or override from environment variable EMQX_NODE__COOKIE"
  522. fi
  523. cd "$ROOTDIR"
  524. case "${COMMAND}" in
  525. start)
  526. # Make sure a node IS not running
  527. if relx_nodetool "ping" >/dev/null 2>&1; then
  528. die "node_is_already_running!"
  529. fi
  530. # this flag passes down to console mode
  531. # so we know it's intended to be run in daemon mode
  532. export _EMQX_START_MODE="$COMMAND"
  533. case "$COMMAND" in
  534. start)
  535. shift
  536. START_OPTION="console"
  537. HEART_OPTION="start"
  538. ;;
  539. esac
  540. RUN_PARAM="$*"
  541. # Set arguments for the heart command
  542. set -- "$RUNNER_SCRIPT" "$HEART_OPTION"
  543. [ "$RUN_PARAM" ] && set -- "$@" "$RUN_PARAM"
  544. # Export the HEART_COMMAND
  545. HEART_COMMAND="$RUNNER_SCRIPT $COMMAND"
  546. export HEART_COMMAND
  547. ## See: http://erlang.org/doc/man/run_erl.html
  548. # Export the RUN_ERL_LOG_GENERATIONS
  549. export RUN_ERL_LOG_GENERATIONS=${RUN_ERL_LOG_GENERATIONS:-"5"}
  550. # Export the RUN_ERL_LOG_MAXSIZE
  551. export RUN_ERL_LOG_MAXSIZE=${RUN_ERL_LOG_MAXSIZE:-"10485760"}
  552. mkdir -p "$PIPE_DIR"
  553. "$BINDIR/run_erl" -daemon "$PIPE_DIR" "$RUNNER_LOG_DIR" \
  554. "$(relx_start_command)"
  555. WAIT_TIME=${EMQX_WAIT_FOR_START:-120}
  556. if wait_until_return_val "true" "$WAIT_TIME" 'relx_nodetool' \
  557. 'eval' 'emqx:is_running()'; then
  558. echo "$EMQX_DESCRIPTION $REL_VSN is started successfully!"
  559. exit 0
  560. else
  561. echo "$EMQX_DESCRIPTION $REL_VSN failed to start in ${WAIT_TIME} seconds."
  562. echo "Please find more information in erlang.log.N"
  563. echo "Or run 'DEBUG=1 $0 console' to have logs printed to console."
  564. exit 1
  565. fi
  566. ;;
  567. stop)
  568. # Wait for the node to completely stop...
  569. PID="$(relx_get_pid)"
  570. if ! relx_nodetool "stop"; then
  571. echoerr "Graceful shutdown failed PID=[$PID]"
  572. exit 1
  573. fi
  574. WAIT_TIME="${EMQX_WAIT_FOR_STOP:-120}"
  575. if ! wait_for "$WAIT_TIME" 'is_down' "$PID"; then
  576. msg="dangling after ${WAIT_TIME} seconds"
  577. # also log to syslog
  578. logger -t "${REL_NAME}[${PID}]" "STOP: $msg"
  579. # log to user console
  580. echoerr "Stop failed, $msg"
  581. echo "ERROR: $PID is still around"
  582. ps -p "$PID"
  583. exit 1
  584. fi
  585. echo "ok"
  586. logger -t "${REL_NAME}[${PID}]" "STOP: OK"
  587. ;;
  588. pid)
  589. ## Get the VM's pid
  590. if ! relx_get_pid; then
  591. exit 1
  592. fi
  593. ;;
  594. ping)
  595. assert_node_alive
  596. echo pong
  597. ;;
  598. escript)
  599. ## Run an escript under the node's environment
  600. if ! relx_escript "$@"; then
  601. exit 1
  602. fi
  603. ;;
  604. attach)
  605. assert_node_alive
  606. shift
  607. exec "$BINDIR/to_erl" "$PIPE_DIR"
  608. ;;
  609. remote_console)
  610. assert_node_alive
  611. shift
  612. relx_rem_sh
  613. ;;
  614. upgrade|downgrade|install|unpack|uninstall)
  615. if [ -z "${2:-}" ]; then
  616. echo "Missing version argument"
  617. echo "Usage: $REL_NAME $COMMAND {version}"
  618. exit 1
  619. fi
  620. shift
  621. assert_node_alive
  622. ERL_FLAGS="${ERL_FLAGS:-} $EPMD_ARG" \
  623. exec "$BINDIR/escript" "$ROOTDIR/bin/install_upgrade.escript" \
  624. "$COMMAND" "{'$REL_NAME', \"$NAME_TYPE\", '$NAME', '$COOKIE'}" "$@"
  625. ;;
  626. versions)
  627. assert_node_alive
  628. shift
  629. ERL_FLAGS="${ERL_FLAGS:-} $EPMD_ARG" \
  630. exec "$BINDIR/escript" "$ROOTDIR/bin/install_upgrade.escript" \
  631. "versions" "{'$REL_NAME', \"$NAME_TYPE\", '$NAME', '$COOKIE'}" "$@"
  632. ;;
  633. console|console_clean)
  634. # .boot file typically just $REL_NAME (ie, the app name)
  635. # however, for debugging, sometimes start_clean.boot is useful.
  636. # For e.g. 'setup', one may even want to name another boot script.
  637. case "$COMMAND" in
  638. console)
  639. if [ -f "$REL_DIR/$REL_NAME.boot" ]; then
  640. BOOTFILE="$REL_DIR/$REL_NAME"
  641. else
  642. BOOTFILE="$REL_DIR/start"
  643. fi
  644. ;;
  645. console_clean)
  646. BOOTFILE="$REL_DIR/start_clean"
  647. ;;
  648. esac
  649. # set before generate_config
  650. if [ "${_EMQX_START_MODE:-}" = '' ]; then
  651. export EMQX_LOG__CONSOLE_HANDLER__ENABLE="${EMQX_LOG__CONSOLE_HANDLER__ENABLE:-true}"
  652. fi
  653. #generate app.config and vm.args
  654. generate_config "$NAME_TYPE" "$NAME"
  655. # Setup beam-required vars
  656. EMU="beam"
  657. PROGNAME="${0#*/}"
  658. export EMU
  659. export PROGNAME
  660. # Store passed arguments since they will be erased by `set`
  661. ARGS="$*"
  662. # shellcheck disable=SC2086 # $CONFIG_ARGS $EPMD_ARG are supposed to be split by whitespace
  663. # Build an array of arguments to pass to exec later on
  664. # Build it here because this command will be used for logging.
  665. if [ "$IS_ELIXIR" = yes ]
  666. then
  667. set -- "$REL_DIR/iex" \
  668. --boot "$BOOTFILE" \
  669. --erl "-mode $CODE_LOADING_MODE" \
  670. --boot-var RELEASE_LIB "$ERTS_LIB_DIR" \
  671. --erl "-mnesia dir \"${MNESIA_DATA_DIR}\"" \
  672. --erl "$CONFIG_ARGS" \
  673. --erl "$EPMD_ARG" \
  674. --werl
  675. else
  676. set -- "$BINDIR/erlexec" \
  677. -boot "$BOOTFILE" -mode "$CODE_LOADING_MODE" \
  678. -boot_var ERTS_LIB_DIR "$ERTS_LIB_DIR" \
  679. -mnesia dir "\"${MNESIA_DATA_DIR}\"" \
  680. $CONFIG_ARGS $EPMD_ARG
  681. fi
  682. # Log the startup
  683. logger -t "${REL_NAME}[$$]" "EXEC: $* -- ${1+$ARGS}"
  684. # Start the VM
  685. exec "$@" -- ${1+$ARGS}
  686. ;;
  687. foreground)
  688. # start up the release in the foreground for use by runit
  689. # or other supervision services
  690. # set before generate_config
  691. export EMQX_LOG__CONSOLE_HANDLER__ENABLE="${EMQX_LOG__CONSOLE_HANDLER__ENABLE:-true}"
  692. #generate app.config and vm.args
  693. generate_config "$NAME_TYPE" "$NAME"
  694. [ -f "$REL_DIR/$REL_NAME.boot" ] && BOOTFILE="$REL_NAME" || BOOTFILE=start
  695. FOREGROUNDOPTIONS="-noshell -noinput +Bd"
  696. # Setup beam-required vars
  697. EMU=beam
  698. PROGNAME="${0#*/}"
  699. export EMU
  700. export PROGNAME
  701. # Store passed arguments since they will be erased by `set`
  702. ARGS="$*"
  703. # shellcheck disable=SC2086 # $CONFIG_ARGS $EPMD_ARG are supposed to be split by whitespace
  704. # Build an array of arguments to pass to exec later on
  705. # Build it here because this command will be used for logging.
  706. if [ "$IS_ELIXIR" = yes ]
  707. then
  708. set -- "$REL_DIR/elixir" \
  709. --boot "$REL_DIR/start" \
  710. --erl "$FOREGROUNDOPTIONS" \
  711. --erl "-mode $CODE_LOADING_MODE" \
  712. --boot-var RELEASE_LIB "$ERTS_LIB_DIR" \
  713. --boot-var ERTS_LIB_DIR "$ERTS_LIB_DIR" \
  714. --erl "-mnesia dir \"${MNESIA_DATA_DIR}\"" \
  715. --erl "$CONFIG_ARGS" \
  716. --erl "$EPMD_ARG" \
  717. --no-halt
  718. else
  719. set -- "$BINDIR/erlexec" $FOREGROUNDOPTIONS \
  720. -boot "$REL_DIR/$BOOTFILE" -mode "$CODE_LOADING_MODE" \
  721. -boot_var ERTS_LIB_DIR "$ERTS_LIB_DIR" \
  722. -mnesia dir "\"${MNESIA_DATA_DIR}\"" \
  723. $CONFIG_ARGS $EPMD_ARG
  724. fi
  725. # Log the startup
  726. logger -t "${REL_NAME}[$$]" "EXEC: $* -- ${1+$ARGS}"
  727. # Start the VM
  728. exec "$@" -- ${1+$ARGS}
  729. ;;
  730. ctl)
  731. assert_node_alive
  732. shift
  733. relx_nodetool rpc_infinity emqx_ctl run_command "$@"
  734. ;;
  735. rpc)
  736. assert_node_alive
  737. shift
  738. relx_nodetool rpc "$@"
  739. ;;
  740. rpcterms)
  741. assert_node_alive
  742. shift
  743. relx_nodetool rpcterms "$@"
  744. ;;
  745. root_dir)
  746. assert_node_alive
  747. shift
  748. relx_nodetool "eval" 'code:root_dir()'
  749. ;;
  750. eval)
  751. assert_node_alive
  752. shift
  753. if [ "$IS_ELIXIR" = "yes" ]
  754. then
  755. "$REL_DIR/elixir" \
  756. --hidden \
  757. --cookie "$COOKIE" \
  758. --boot "$REL_DIR/start_clean" \
  759. --boot-var RELEASE_LIB "$ERTS_LIB_DIR" \
  760. --vm-args "$(latest_vm_args 'EMQX_NODE__NAME')"\
  761. --rpc-eval "$NAME" "$@"
  762. else
  763. relx_nodetool "eval" "$@"
  764. fi
  765. ;;
  766. *)
  767. usage "$COMMAND"
  768. exit 1
  769. ;;
  770. esac
  771. exit 0