emqx 28 KB

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