emqx 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053
  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. [ "$DEBUG" -eq 1 ] && set -x
  7. RUNNER_ROOT_DIR="$(cd "$(dirname "$(readlink "$0" || echo "$0")")"/..; pwd -P)"
  8. # shellcheck disable=SC1090,SC1091
  9. . "$RUNNER_ROOT_DIR"/releases/emqx_vars
  10. # defined in emqx_vars
  11. export RUNNER_ROOT_DIR
  12. export EMQX_ETC_DIR
  13. export REL_VSN
  14. export SCHEMA_MOD
  15. RUNNER_SCRIPT="$RUNNER_BIN_DIR/$REL_NAME"
  16. CODE_LOADING_MODE="${CODE_LOADING_MODE:-embedded}"
  17. REL_DIR="$RUNNER_ROOT_DIR/releases/$REL_VSN"
  18. WHOAMI=$(whoami)
  19. # Make sure log directory exists
  20. mkdir -p "$RUNNER_LOG_DIR"
  21. # hocon try to read environment variables starting with "EMQX_"
  22. export HOCON_ENV_OVERRIDE_PREFIX='EMQX_'
  23. export ERTS_DIR="$RUNNER_ROOT_DIR/erts-$ERTS_VSN"
  24. export BINDIR="$ERTS_DIR/bin"
  25. export EMU="beam"
  26. export PROGNAME="erl"
  27. export ERTS_LIB_DIR="$RUNNER_ROOT_DIR/lib"
  28. DYNLIBS_DIR="$RUNNER_ROOT_DIR/dynlibs"
  29. # Echo to stderr on errors
  30. echoerr() {
  31. echo -e "$*" 1>&2;
  32. }
  33. die() {
  34. set +x
  35. echoerr "ERROR: $1"
  36. errno=${2:-1}
  37. exit "$errno"
  38. }
  39. assert_node_alive() {
  40. if ! relx_nodetool "ping" > /dev/null; then
  41. die "node_is_not_running!" 1
  42. fi
  43. }
  44. usage() {
  45. local command="$1"
  46. case "$command" in
  47. start)
  48. echo "Start EMQX service in daemon mode"
  49. ;;
  50. stop)
  51. echo "Stop the running EMQX program"
  52. ;;
  53. console)
  54. echo "Boot up EMQX service in an interactive Erlang or Elixir shell"
  55. echo "This command needs a tty"
  56. ;;
  57. console_clean)
  58. echo "This command does NOT boot up the EMQX service"
  59. echo "It only starts an interactive Erlang or Elixir console with all the"
  60. echo "EMQX code available"
  61. ;;
  62. foreground)
  63. echo "Start EMQX in foreground mode without an interactive shell"
  64. ;;
  65. pid)
  66. echo "Print out EMQX process identifier"
  67. ;;
  68. ping)
  69. echo "Check if the EMQX node is up and running"
  70. echo "This command exit with 0 silently if node is running"
  71. ;;
  72. escript)
  73. echo "Execute a escript using the Erlang runtime from EMQX package installation"
  74. echo "For example $REL_NAME escript /path/to/my/escript my_arg1 my_arg2"
  75. ;;
  76. attach)
  77. echo "This command is applicable when EMQX is started in daemon mode."
  78. echo "It attaches the current shell to EMQX's control console"
  79. echo "through a named pipe."
  80. echo "WARNING: try to use the safer alternative, remote_console command."
  81. ;;
  82. remote_console)
  83. echo "Start an interactive shell running an Erlang or Elixir node which "
  84. echo "hidden-connects to the running EMQX node".
  85. echo "This command is mostly used for troubleshooting."
  86. ;;
  87. ertspath)
  88. echo "Print path to Erlang runtime bin dir"
  89. ;;
  90. rpc)
  91. echo "Usge $REL_NAME rpc MODULE FUNCTION [ARGS, ...]"
  92. echo "Connect to the EMQX node and make an Erlang RPC"
  93. echo "This command blocks for at most 60 seconds."
  94. echo "It exits with non-zero code in case of any RPC failure"
  95. echo "including connection error and runtime exception"
  96. ;;
  97. rpcterms)
  98. echo "Usge $REL_NAME rpcterms MODULE FUNCTION [ARGS, ...]"
  99. echo "Connect to the EMQX node and make an Erlang RPC"
  100. echo "The result of the RPC call is pretty-printed as an "
  101. echo "Erlang term"
  102. ;;
  103. root_dir)
  104. echo "Print EMQX installation root dir"
  105. ;;
  106. eval)
  107. echo "Evaluate an Erlang or Elixir expression in the EMQX node"
  108. ;;
  109. eval-erl)
  110. echo "Evaluate an Erlang expression in the EMQX node, even on Elixir node"
  111. ;;
  112. versions)
  113. echo "List installed EMQX versions and their status"
  114. ;;
  115. unpack)
  116. echo "Usage: $REL_NAME unpack [VERSION]"
  117. echo "Unpacks a release package VERSION, it assumes that this"
  118. echo "release package tarball has already been deployed at one"
  119. echo "of the following locations:"
  120. echo " releases/<relname>-<version>.tar.gz"
  121. ;;
  122. install)
  123. echo "Usage: $REL_NAME install [VERSION]"
  124. echo "Installs a release package VERSION, it assumes that this"
  125. echo "release package tarball has already been deployed at one"
  126. echo "of the following locations:"
  127. echo " releases/<relname>-<version>.tar.gz"
  128. echo ""
  129. echo " --no-permanent Install release package VERSION but"
  130. echo " don't make it permanent"
  131. ;;
  132. uninstall)
  133. echo "Usage: $REL_NAME uninstall [VERSION]"
  134. echo "Uninstalls a release VERSION, it will only accept"
  135. echo "versions that are not currently in use"
  136. ;;
  137. upgrade)
  138. echo "Usage: $REL_NAME upgrade [VERSION]"
  139. echo "Upgrades the currently running release to VERSION, it assumes"
  140. echo "that a 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. downgrade)
  148. echo "Usage: $REL_NAME downgrade [VERSION]"
  149. echo "Downgrades the currently running release to VERSION, it assumes"
  150. echo "that a release package tarball has already been deployed at one"
  151. echo "of the following locations:"
  152. echo " releases/<relname>-<version>.tar.gz"
  153. echo ""
  154. echo " --no-permanent Install release package VERSION but"
  155. echo " don't make it permanent"
  156. ;;
  157. check_config)
  158. echo "Checks the EMQX config without generating any files"
  159. ;;
  160. *)
  161. echo "Usage: $REL_NAME COMMAND [help]"
  162. echo ''
  163. echo "Commonly used COMMANDs:"
  164. echo " start: Start EMQX in daemon mode"
  165. echo " console: Start EMQX in an interactive Erlang or Elixir shell"
  166. echo " foreground: Start EMQX in foreground mode without an interactive shell"
  167. echo " stop: Stop the running EMQX node"
  168. echo " ctl: Administration commands, execute '$REL_NAME ctl help' for more details"
  169. echo ''
  170. echo "More:"
  171. echo " Shell attach: remote_console | attach"
  172. echo " Up/Down-grade: upgrade | downgrade | install | uninstall"
  173. echo " Install info: ertspath | root_dir"
  174. echo " Runtime info: pid | ping | versions"
  175. echo " Validate Config: check_config"
  176. echo " Advanced: console_clean | escript | rpc | rpcterms | eval | eval-erl"
  177. echo ''
  178. echo "Execute '$REL_NAME COMMAND help' for more information"
  179. ;;
  180. esac
  181. }
  182. COMMAND="${1:-}"
  183. if [ -z "$COMMAND" ]; then
  184. usage 'help'
  185. exit 1
  186. elif [ "$COMMAND" = 'help' ]; then
  187. usage 'help'
  188. exit 0
  189. fi
  190. if [ "${2:-}" = 'help' ]; then
  191. ## 'ctl' command has its own usage info
  192. if [ "$COMMAND" != 'ctl' ]; then
  193. usage "$COMMAND"
  194. exit 0
  195. fi
  196. fi
  197. ## IS_BOOT_COMMAND is set for later to inspect node name and cookie from hocon config (or env variable)
  198. case "${COMMAND}" in
  199. start|console|console_clean|foreground|check_config)
  200. IS_BOOT_COMMAND='yes'
  201. ;;
  202. ertspath)
  203. echo "$ERTS_DIR"
  204. exit 0
  205. ;;
  206. root_dir)
  207. echo "$RUNNER_ROOT_DIR"
  208. exit 0
  209. ;;
  210. *)
  211. IS_BOOT_COMMAND='no'
  212. ;;
  213. esac
  214. ## backward compatible
  215. if [ -d "$ERTS_DIR/lib" ]; then
  216. export LD_LIBRARY_PATH="$ERTS_DIR/lib:$LD_LIBRARY_PATH"
  217. fi
  218. # Simple way to check the correct user and fail early
  219. check_user() {
  220. # Validate that the user running the script is the owner of the
  221. # RUN_DIR.
  222. if [ "$RUNNER_USER" ] && [ "x$WHOAMI" != "x$RUNNER_USER" ]; then
  223. if [ "x$WHOAMI" != "xroot" ]; then
  224. echo "You need to be root or use sudo to run this command"
  225. exit 1
  226. fi
  227. CMD="DEBUG=$DEBUG \"$RUNNER_SCRIPT\" "
  228. for ARG in "$@"; do
  229. CMD="${CMD} \"$ARG\""
  230. done
  231. # This will drop privileges into the runner user
  232. # It exec's in a new shell and the current shell will exit
  233. exec su - "$RUNNER_USER" -c "$CMD"
  234. fi
  235. }
  236. # Make sure the user running this script is the owner and/or su to that user
  237. check_user "$@"
  238. ES=$?
  239. if [ "$ES" -ne 0 ]; then
  240. exit $ES
  241. fi
  242. COMPATIBILITY_CHECK='
  243. io:format("BEAM_OK~n", []),
  244. try
  245. [_|_] = L = crypto:info_lib(),
  246. io:format("CRYPTO_OK ~0p~n", [L])
  247. catch
  248. _ : _ ->
  249. %% so logger has the chance to log something
  250. timer:sleep(100),
  251. halt(1)
  252. end,
  253. try
  254. mnesia_hook:module_info(),
  255. io:format("MNESIA_OK~n", [])
  256. catch
  257. _ : _ ->
  258. io:format("WARNING: Mnesia app has no post-coommit hook support~n", []),
  259. halt(2)
  260. end,
  261. halt(0).
  262. '
  263. compatiblity_info() {
  264. # RELEASE_LIB is used by Elixir
  265. "$BINDIR/$PROGNAME" \
  266. -noshell \
  267. -boot_var RELEASE_LIB "$ERTS_LIB_DIR/lib" \
  268. -boot "$REL_DIR/start_clean" \
  269. -eval "$COMPATIBILITY_CHECK"
  270. }
  271. # Collect Erlang/OTP runtime sanity and compatibility in one go
  272. if [ "$IS_BOOT_COMMAND" = 'yes' ]; then
  273. # Read BUILD_INFO early as the next commands may mess up the shell
  274. BUILD_INFO="$(cat "${REL_DIR}/BUILD_INFO")"
  275. COMPATIBILITY_INFO="$(compatiblity_info 2>/dev/null || true)"
  276. if ! (echo -e "$COMPATIBILITY_INFO" | grep -q 'CRYPTO_OK'); then
  277. ## failed to start, might be due to missing libs, try to be portable
  278. export LD_LIBRARY_PATH="${LD_LIBRARY_PATH:-$DYNLIBS_DIR}"
  279. if [ "$LD_LIBRARY_PATH" != "$DYNLIBS_DIR" ]; then
  280. export LD_LIBRARY_PATH="$DYNLIBS_DIR:$LD_LIBRARY_PATH"
  281. fi
  282. ## Turn off debug, because COMPATIBILITY_INFO needs to capture stderr
  283. set +x
  284. COMPATIBILITY_INFO="$(compatiblity_info 2>&1 || true)"
  285. if ! (echo -e "$COMPATIBILITY_INFO" | grep -q 'BEAM_OK'); then
  286. ## not able to start beam.smp
  287. set +x
  288. echoerr "$COMPATIBILITY_INFO"
  289. echoerr "Please ensure it is running on the correct platform:"
  290. echoerr "$BUILD_INFO"
  291. echoerr "Version=$REL_VSN"
  292. echoerr "Required dependencies: openssl-1.1.1 (libcrypto), libncurses and libatomic1"
  293. exit 1
  294. elif ! (echo -e "$COMPATIBILITY_INFO" | grep -q 'CRYPTO_OK'); then
  295. ## not able to start crypto app
  296. set +x
  297. echoerr "$COMPATIBILITY_INFO"
  298. exit 2
  299. fi
  300. echoerr "Using libs from '${DYNLIBS_DIR}' due to missing from the OS."
  301. fi
  302. [ "$DEBUG" -eq 1 ] && set -x
  303. fi
  304. # Warn the user if ulimit -n is less than 1024
  305. ULIMIT_F=$(ulimit -n)
  306. if [ "$ULIMIT_F" -lt 1024 ]; then
  307. echo "!!!!"
  308. echo "!!!! WARNING: ulimit -n is ${ULIMIT_F}; 1024 is the recommended minimum."
  309. echo "!!!!"
  310. fi
  311. SED_REPLACE="sed -i "
  312. case $(sed --help 2>&1) in
  313. *GNU*) SED_REPLACE="sed -i ";;
  314. *BusyBox*) SED_REPLACE="sed -i ";;
  315. *) SED_REPLACE="sed -i '' ";;
  316. esac
  317. # Get node pid
  318. relx_get_pid() {
  319. if output="$(relx_nodetool rpcterms os getpid)"
  320. then
  321. # shellcheck disable=SC2001 # Escaped quote taken as closing quote in editor
  322. echo "$output" | sed -e 's/"//g'
  323. return 0
  324. else
  325. echo "$output"
  326. return 1
  327. fi
  328. }
  329. # Connect to a remote node
  330. remsh() {
  331. # Generate a unique id used to allow multiple remsh to the same node
  332. # transparently
  333. id="remsh$(relx_gen_id)-${NAME}"
  334. # Get the node's ticktime so that we use the same thing.
  335. TICKTIME="$(relx_nodetool rpcterms net_kernel get_net_ticktime)"
  336. # shellcheck disable=SC2086
  337. # Setup remote shell command to control node
  338. if [ "$IS_ELIXIR" = no ] || [ "${EMQX_CONSOLE_FLAVOR:-}" = 'erl' ] ; then
  339. set -- "$BINDIR/erl" "$NAME_TYPE" "$id" \
  340. -remsh "$NAME" -boot "$REL_DIR/start_clean" \
  341. -boot_var ERTS_LIB_DIR "$ERTS_LIB_DIR" \
  342. -boot_var RELEASE_LIB "$ERTS_LIB_DIR" \
  343. -setcookie "$COOKIE" \
  344. -hidden \
  345. -kernel net_ticktime "$TICKTIME" \
  346. $EPMD_ARGS
  347. else
  348. set -- "$REL_DIR/iex" \
  349. --remsh "$NAME" \
  350. --boot-var RELEASE_LIB "$ERTS_LIB_DIR" \
  351. --cookie "$COOKIE" \
  352. --hidden \
  353. --erl "-kernel net_ticktime $TICKTIME" \
  354. --erl "$EPMD_ARGS" \
  355. --erl "$NAME_TYPE $id" \
  356. --boot "$REL_DIR/start_clean"
  357. fi
  358. exec "$@"
  359. }
  360. # Generate a random id
  361. relx_gen_id() {
  362. od -t x -N 4 /dev/urandom | head -n1 | awk '{print $2}'
  363. }
  364. call_nodetool() {
  365. "$ERTS_DIR/bin/escript" "$RUNNER_ROOT_DIR/bin/nodetool" "$@"
  366. }
  367. # Control a node
  368. relx_nodetool() {
  369. command="$1"; shift
  370. ERL_FLAGS="${ERL_FLAGS:-} $EPMD_ARGS" \
  371. call_nodetool "$NAME_TYPE" "$NAME" \
  372. -setcookie "$COOKIE" "$command" "$@"
  373. }
  374. call_hocon() {
  375. call_nodetool hocon "$@" \
  376. || die "call_hocon_failed: $*" $?
  377. }
  378. ## Resolve boot configs in a batch
  379. ## This is because starting the Erlang beam with all modules loaded
  380. ## and parsing HOCON config + environment variables is a non-trivial task
  381. CONF_KEYS=( 'node.data_dir' 'node.name' 'node.cookie' 'node.db_backend' 'cluster.proto_dist' )
  382. if [ "$IS_ENTERPRISE" = 'yes' ]; then
  383. CONF_KEYS+=( 'license.file' 'license.key' )
  384. fi
  385. if [ "$IS_BOOT_COMMAND" = 'yes' ]; then
  386. if [ "${EMQX_BOOT_CONFIGS:-}" = '' ]; then
  387. EMQX_BOOT_CONFIGS="$(call_hocon -s "$SCHEMA_MOD" -c "$EMQX_ETC_DIR"/emqx.conf multi_get "${CONF_KEYS[@]}")"
  388. ## export here so the 'console' command recursively called from
  389. ## 'start' command does not have to parse the configs again
  390. export EMQX_BOOT_CONFIGS
  391. fi
  392. else
  393. # For non-boot commands, we try to get data_dir and ssl_dist_optfile from 'ps -ef' output
  394. # shellcheck disable=SC2009
  395. PS_LINE="$(ps -ef | grep "\-[r]oot $RUNNER_ROOT_DIR" || true)"
  396. if [ "$(echo -e "$PS_LINE" | wc -l)" -eq 1 ]; then
  397. ## only one emqx node is running
  398. ## strip 'emqx_data_dir ' and ' --' because the dir in between may contain spaces
  399. DATA_DIR="$(echo -e "$PS_LINE" | grep -oE "\-emqx_data_dir.*" | sed -E 's#.+emqx_data_dir[[:blank:]]##g' | sed -E 's#[[:blank:]]--$##g' || true)"
  400. if [ "$DATA_DIR" = '' ]; then
  401. ## this should not happen unless -emqx_data_dir is not set
  402. die "node_is_not_running!" 1
  403. fi
  404. # get ssl_dist_optfile option
  405. SSL_DIST_OPTFILE="$(echo -e "$PS_LINE" | grep -oE '\-ssl_dist_optfile\s.+\s' | awk '{print $2}' || true)"
  406. if [ -z "$SSL_DIST_OPTFILE" ]; then
  407. EMQX_BOOT_CONFIGS="node.data_dir=${DATA_DIR}\ncluster.proto_dist=inet_tcp"
  408. else
  409. EMQX_BOOT_CONFIGS="node.data_dir=${DATA_DIR}\ncluster.proto_dist=inet_tls"
  410. fi
  411. else
  412. ## None or more than one node is running, resolve from boot config
  413. EMQX_BOOT_CONFIGS="$(call_hocon -s "$SCHEMA_MOD" -c "$EMQX_ETC_DIR"/emqx.conf multi_get "${CONF_KEYS[@]}")"
  414. fi
  415. fi
  416. get_boot_config() {
  417. path_to_value="$1"
  418. echo -e "$EMQX_BOOT_CONFIGS" | grep "$path_to_value=" | sed -e "s/$path_to_value=//g" | tr -d \"
  419. }
  420. EPMD_ARGS="-start_epmd false -epmd_module ekka_epmd -proto_dist ekka"
  421. PROTO_DIST="$(get_boot_config 'cluster.proto_dist' || true)"
  422. # this environment variable is required by ekka_dist module
  423. # because proto_dist is overriden to ekka, and there is a lack of ekka_tls module
  424. export EKKA_PROTO_DIST_MOD="${PROTO_DIST:-inet_tcp}"
  425. if [ "$EKKA_PROTO_DIST_MOD" = 'inet_tls' ]; then
  426. if [ "$IS_BOOT_COMMAND" = 'yes' ]; then
  427. SSL_DIST_OPTFILE=${EMQX_SSL_DIST_OPTFILE:-"$EMQX_ETC_DIR/ssl_dist.conf"}
  428. case "$SSL_DIST_OPTFILE" in
  429. *\ *)
  430. # there is unfortunately no way to support space for this option because we'd need to grep
  431. # from 'ps -ef' result to get this option for non-boot commands (nodetool) to run
  432. set +x
  433. echoerr "Got space in: $SSL_DIST_OPTFILE"
  434. echoerr "No space is allowed for Erlang distribution over SSL option file path."
  435. echoerr "Configure it from environment variable EMQX_SSL_DIST_OPTFILE."
  436. echoerr "Or make sure emqx root path '$RUNNER_ROOT_DIR' has no space"
  437. exit 1
  438. ;;
  439. *)
  440. true
  441. ;;
  442. esac
  443. fi
  444. EPMD_ARGS="${EPMD_ARGS} -ssl_dist_optfile $SSL_DIST_OPTFILE"
  445. fi
  446. DATA_DIR="$(get_boot_config 'node.data_dir')"
  447. # ensure no trailing /
  448. DATA_DIR="${DATA_DIR%/}"
  449. if [[ $DATA_DIR != /* ]]; then
  450. # relative path
  451. DATA_DIR="${RUNNER_ROOT_DIR}/${DATA_DIR}"
  452. fi
  453. CONFIGS_DIR="$DATA_DIR/configs"
  454. mkdir -p "$CONFIGS_DIR"
  455. check_license() {
  456. if [ "$IS_ENTERPRISE" == "no" ]; then
  457. return 0
  458. fi
  459. file_license="${EMQX_LICENSE__FILE:-$(get_boot_config 'license.file')}"
  460. if [[ -n "$file_license" && ("$file_license" != "undefined") ]]; then
  461. call_nodetool check_license_file "$file_license"
  462. else
  463. key_license="${EMQX_LICENSE__KEY:-$(get_boot_config 'license.key')}"
  464. if [[ -n "$key_license" && ("$key_license" != "undefined") ]]; then
  465. call_nodetool check_license_key "$key_license"
  466. else
  467. set +x
  468. echoerr "License not found."
  469. echoerr "Please specify one via EMQX_LICENSE__KEY or EMQX_LICENSE__FILE variables"
  470. echoerr "or via license.key|file in emqx_enterprise.conf."
  471. return 1
  472. fi
  473. fi
  474. }
  475. # Run an escript in the node's environment
  476. relx_escript() {
  477. shift; scriptpath="$1"; shift
  478. "$ERTS_DIR/bin/escript" "$RUNNER_ROOT_DIR/$scriptpath" "$@"
  479. }
  480. # Output a start command for the last argument of run_erl
  481. relx_start_command() {
  482. printf "exec \"%s\" \"%s\"" "$RUNNER_SCRIPT" \
  483. "$START_OPTION"
  484. }
  485. # Function to check configs without generating them
  486. check_config() {
  487. ## this command checks the configs without generating any files
  488. call_hocon -v -s "$SCHEMA_MOD" -c "$EMQX_ETC_DIR"/emqx.conf check_schema
  489. }
  490. # Function to generate app.config and vm.args
  491. # sets two environment variables CONF_FILE and ARGS_FILE
  492. generate_config() {
  493. local name_type="$1"
  494. local node_name="$2"
  495. ## Delete the *.siz files first or it can't start after
  496. ## changing the config 'log.rotation.size'
  497. rm -rf "${RUNNER_LOG_DIR}"/*.siz
  498. ## timestamp for each generation
  499. local NOW_TIME
  500. NOW_TIME="$(date +'%Y.%m.%d.%H.%M.%S')"
  501. ## this command populates two files: app.<time>.config and vm.<time>.args
  502. ## NOTE: the generate command merges environment variables to the base config (emqx.conf),
  503. ## but does not include the cluster-override.conf and local-override.conf
  504. ## meaning, certain overrides will not be mapped to app.<time>.config file
  505. call_hocon -v -t "$NOW_TIME" -s "$SCHEMA_MOD" -c "$EMQX_ETC_DIR"/emqx.conf -d "$DATA_DIR"/configs generate
  506. ## filenames are per-hocon convention
  507. CONF_FILE="$CONFIGS_DIR/app.$NOW_TIME.config"
  508. ARGS_FILE="$CONFIGS_DIR/vm.$NOW_TIME.args"
  509. ## Merge hocon generated *.args into the vm.args
  510. TMP_ARG_FILE="$CONFIGS_DIR/vm.args.tmp"
  511. cp "$EMQX_ETC_DIR/vm.args" "$TMP_ARG_FILE"
  512. echo "" >> "$TMP_ARG_FILE"
  513. echo "-pa \"${REL_DIR}/consolidated\"" >> "$TMP_ARG_FILE"
  514. ## read lines from generated vm.<time>.args file
  515. ## drop comment lines, and empty lines using sed
  516. ## pipe the lines to a while loop
  517. sed '/^#/d' "$ARGS_FILE" | sed '/^$/d' | while IFS='' read -r ARG_LINE || [ -n "$ARG_LINE" ]; do
  518. ## in the loop, split the 'key[:space:]value' pair
  519. ARG_KEY=$(echo "$ARG_LINE" | awk '{$NF="";print}')
  520. ARG_VALUE=$(echo "$ARG_LINE" | awk '{print $NF}')
  521. ## use the key to look up in vm.args file for the value
  522. TMP_ARG_VALUE=$(grep "^$ARG_KEY" "$TMP_ARG_FILE" || true | awk '{print $NF}')
  523. ## compare generated (to override) value to original (to be overridden) value
  524. if [ "$ARG_VALUE" != "$TMP_ARG_VALUE" ] ; then
  525. ## if they are different
  526. if [ -n "$TMP_ARG_VALUE" ]; then
  527. ## if the old value is present, replace it with generated value
  528. sh -c "$SED_REPLACE 's|^$ARG_KEY.*$|$ARG_LINE|' \"$TMP_ARG_FILE\""
  529. else
  530. ## otherwise append generated value to the end
  531. echo "$ARG_LINE" >> "$TMP_ARG_FILE"
  532. fi
  533. fi
  534. done
  535. echo "$name_type $node_name" >> "$TMP_ARG_FILE"
  536. echo "-mnesia dir '\"$DATA_DIR/mnesia/$NAME\"'" >> "$TMP_ARG_FILE"
  537. ## rename the generated vm.<time>.args file
  538. mv -f "$TMP_ARG_FILE" "$ARGS_FILE"
  539. }
  540. # check if a PID is down
  541. is_down() {
  542. PID="$1"
  543. if ps -p "$PID" >/dev/null; then
  544. # still around
  545. # shellcheck disable=SC2009 # this grep pattern is not a part of the progra names
  546. if ps -p "$PID" | grep -q 'defunct'; then
  547. # zombie state, print parent pid
  548. parent="$(ps -o ppid= -p "$PID" | tr -d ' ')"
  549. echo "WARN: $PID is marked <defunct>, parent:"
  550. ps -p "$parent"
  551. return 0
  552. fi
  553. return 1
  554. fi
  555. # it's gone
  556. return 0
  557. }
  558. wait_for() {
  559. local WAIT_TIME
  560. local CMD
  561. WAIT_TIME="$1"
  562. shift
  563. CMD="$*"
  564. while true; do
  565. if $CMD >/dev/null 2>&1; then
  566. return 0
  567. fi
  568. if [ "$WAIT_TIME" -le 0 ]; then
  569. return 1
  570. fi
  571. WAIT_TIME=$((WAIT_TIME - 1))
  572. sleep 1
  573. done
  574. }
  575. wait_until_return_val() {
  576. local RESULT
  577. local WAIT_TIME
  578. local CMD
  579. RESULT="$1"
  580. WAIT_TIME="$2"
  581. shift 2
  582. CMD="$*"
  583. while true; do
  584. if [ "$($CMD 2>/dev/null)" = "$RESULT" ]; then
  585. return 0
  586. fi
  587. if [ "$WAIT_TIME" -le 0 ]; then
  588. return 1
  589. fi
  590. WAIT_TIME=$((WAIT_TIME - 1))
  591. sleep 1
  592. done
  593. }
  594. latest_vm_args() {
  595. local hint_var_name="$1"
  596. local vm_args_file
  597. vm_args_file="$(find "$CONFIGS_DIR" -type f -name "vm.*.args" | sort | tail -1)"
  598. if [ -f "$vm_args_file" ]; then
  599. echo "$vm_args_file"
  600. else
  601. set +x
  602. echoerr "Node not initialized?"
  603. echoerr "Generated config file vm.*.args is not found for command '$COMMAND'"
  604. echoerr "in config dir: $CONFIGS_DIR"
  605. echoerr "In case the file has been deleted while the node is running,"
  606. echoerr "set environment variable '$hint_var_name' to continue"
  607. exit 1
  608. fi
  609. }
  610. # backward compatible with 4.x
  611. tr_log_to_env() {
  612. local log_to=${EMQX_LOG__TO:-undefined}
  613. # unset because it's unknown to 5.0
  614. unset EMQX_LOG__TO
  615. case "${log_to}" in
  616. console)
  617. export EMQX_LOG__CONSOLE_HANDLER__ENABLE='true'
  618. export EMQX_LOG__FILE_HANDLERS__DEFAULT__ENABLE='false'
  619. ;;
  620. file)
  621. export EMQX_LOG__CONSOLE_HANDLER__ENABLE='false'
  622. export EMQX_LOG__FILE_HANDLERS__DEFAULT__ENABLE='true'
  623. ;;
  624. both)
  625. export EMQX_LOG__CONSOLE_HANDLER__ENABLE='true'
  626. export EMQX_LOG__FILE_HANDLERS__DEFAULT__ENABLE='true'
  627. ;;
  628. default)
  629. # want to use config file defaults, do nothing
  630. ;;
  631. undefined)
  632. # value not set, do nothing
  633. ;;
  634. *)
  635. echoerr "Unknown environment value for EMQX_LOG__TO=${log_to} discarded"
  636. ;;
  637. esac
  638. }
  639. maybe_log_to_console() {
  640. if [ "${EMQX_LOG__TO:-}" = 'default' ]; then
  641. # want to use config file defaults, do nothing
  642. unset EMQX_LOG__TO
  643. else
  644. tr_log_to_env
  645. # ensure defaults
  646. export EMQX_LOG__CONSOLE_HANDLER__ENABLE="${EMQX_LOG__CONSOLE_HANDLER__ENABLE:-true}"
  647. export EMQX_LOG__FILE_HANDLERS__DEFAULT__ENABLE="${EMQX_LOG__FILE_HANDLERS__DEFAULT__ENABLE:-false}"
  648. fi
  649. }
  650. if [ -n "${EMQX_NODE_NAME:-}" ]; then
  651. export EMQX_NODE__NAME="${EMQX_NODE_NAME}"
  652. unset EMQX_NODE_NAME
  653. fi
  654. ## Possible ways to configure emqx node name:
  655. ## 1. configure node.name in emqx.conf
  656. ## 2. override with environment variable EMQX_NODE__NAME
  657. ## Node name is either short-name (without '@'), e.g. 'emqx'
  658. ## or long name (with '@') e.g. 'emqx@example.net' or 'emqx@127.0.0.1'
  659. NAME="${EMQX_NODE__NAME:-}"
  660. if [ -z "$NAME" ]; then
  661. if [ "$IS_BOOT_COMMAND" = 'yes' ]; then
  662. # for boot commands, inspect emqx.conf for node name
  663. NAME="$(get_boot_config 'node.name')"
  664. else
  665. vm_args_file="$(latest_vm_args 'EMQX_NODE__NAME')"
  666. NAME="$(grep -E '^-s?name' "${vm_args_file}" | awk '{print $2}')"
  667. fi
  668. fi
  669. # force to use 'emqx' short name
  670. [ -z "$NAME" ] && NAME='emqx'
  671. case "$NAME" in
  672. *@*)
  673. NAME_TYPE='-name'
  674. ;;
  675. *)
  676. NAME_TYPE='-sname'
  677. esac
  678. SHORT_NAME="$(echo "$NAME" | awk -F'@' '{print $1}')"
  679. if ! (echo "$SHORT_NAME" | grep -q '^[0-9A-Za-z_\-]\+$'); then
  680. echo "Invalid node name, should be of format '^[0-9A-Za-z_-]+$'."
  681. exit 1
  682. fi
  683. # This also changes the program name from 'beam.smp' to node name
  684. # e.g. the 'ps' command output
  685. export ESCRIPT_NAME="$SHORT_NAME"
  686. PIPE_DIR="${PIPE_DIR:-/$DATA_DIR/${WHOAMI}_erl_pipes/$NAME/}"
  687. ## make EMQX_NODE_COOKIE right
  688. if [ -n "${EMQX_NODE_COOKIE:-}" ]; then
  689. export EMQX_NODE__COOKIE="${EMQX_NODE_COOKIE}"
  690. unset EMQX_NODE_COOKIE
  691. fi
  692. COOKIE="${EMQX_NODE__COOKIE:-}"
  693. if [ -z "$COOKIE" ]; then
  694. if [ "$IS_BOOT_COMMAND" = 'yes' ]; then
  695. COOKIE="$(get_boot_config 'node.cookie')"
  696. else
  697. vm_args_file="$(latest_vm_args 'EMQX_NODE__COOKIE')"
  698. COOKIE="$(grep -E '^-setcookie' "${vm_args_file}" | awk '{print $2}')"
  699. fi
  700. fi
  701. if [ -z "$COOKIE" ]; then
  702. die "Please set node.cookie in $EMQX_ETC_DIR/emqx.conf or override from environment variable EMQX_NODE__COOKIE"
  703. fi
  704. ## check if OTP version has mnesia_hook feature; if not, fallback to
  705. ## using Mnesia DB backend.
  706. if [[ "$IS_BOOT_COMMAND" == 'yes' && "$(get_boot_config 'node.db_backend')" == "rlog" ]]; then
  707. if ! (echo -e "$COMPATIBILITY_INFO" | grep -q 'MNESIA_OK'); then
  708. echoerr "DB Backend is RLOG, but an incompatible OTP version has been detected. Falling back to using Mnesia DB backend."
  709. export EMQX_NODE__DB_BACKEND=mnesia
  710. export EMQX_NODE__DB_ROLE=core
  711. fi
  712. fi
  713. cd "$RUNNER_ROOT_DIR"
  714. case "${COMMAND}" in
  715. start)
  716. # Make sure a node IS not running
  717. if relx_nodetool "ping" >/dev/null 2>&1; then
  718. die "node_is_already_running!"
  719. fi
  720. # this flag passes down to console mode
  721. # so we know it's intended to be run in daemon mode
  722. export _EMQX_START_DAEMON_MODE=1
  723. case "$COMMAND" in
  724. start)
  725. shift
  726. START_OPTION="console"
  727. HEART_OPTION="start"
  728. ;;
  729. esac
  730. RUN_PARAM="$*"
  731. # Set arguments for the heart command
  732. set -- "$RUNNER_SCRIPT" "$HEART_OPTION"
  733. [ "$RUN_PARAM" ] && set -- "$@" "$RUN_PARAM"
  734. # Export the HEART_COMMAND
  735. HEART_COMMAND="$RUNNER_SCRIPT $COMMAND"
  736. export HEART_COMMAND
  737. ## See: http://erlang.org/doc/man/run_erl.html
  738. # Export the RUN_ERL_LOG_GENERATIONS
  739. export RUN_ERL_LOG_GENERATIONS=${RUN_ERL_LOG_GENERATIONS:-"5"}
  740. # Export the RUN_ERL_LOG_MAXSIZE
  741. export RUN_ERL_LOG_MAXSIZE=${RUN_ERL_LOG_MAXSIZE:-"10485760"}
  742. mkdir -p "$PIPE_DIR"
  743. "$BINDIR/run_erl" -daemon "$PIPE_DIR" "$RUNNER_LOG_DIR" \
  744. "$(relx_start_command)"
  745. WAIT_TIME=${EMQX_WAIT_FOR_START:-120}
  746. if wait_until_return_val "true" "$WAIT_TIME" 'relx_nodetool' \
  747. 'eval' 'emqx:is_running()'; then
  748. echo "$EMQX_DESCRIPTION $REL_VSN is started successfully!"
  749. exit 0
  750. else
  751. echo "$EMQX_DESCRIPTION $REL_VSN failed to start in ${WAIT_TIME} seconds."
  752. echo "Please find more information in erlang.log.N"
  753. echo "Or run 'env DEBUG=1 $0 console' to have logs printed to console."
  754. exit 1
  755. fi
  756. ;;
  757. stop)
  758. # Wait for the node to completely stop...
  759. PID="$(relx_get_pid)"
  760. if ! relx_nodetool "stop"; then
  761. die "Graceful shutdown failed PID=[$PID]"
  762. fi
  763. WAIT_TIME="${EMQX_WAIT_FOR_STOP:-120}"
  764. if ! wait_for "$WAIT_TIME" 'is_down' "$PID"; then
  765. msg="dangling after ${WAIT_TIME} seconds"
  766. # also log to syslog
  767. logger -t "${REL_NAME}[${PID}]" "STOP: $msg"
  768. # log to user console
  769. set +x
  770. echoerr "Stop failed, $msg"
  771. echo "ERROR: $PID is still around"
  772. ps -p "$PID"
  773. exit 1
  774. fi
  775. echo "ok"
  776. logger -t "${REL_NAME}[${PID}]" "STOP: OK"
  777. ;;
  778. pid)
  779. ## Get the VM's pid
  780. if ! relx_get_pid; then
  781. exit 1
  782. fi
  783. ;;
  784. ping)
  785. assert_node_alive
  786. echo pong
  787. ;;
  788. escript)
  789. ## Run an escript under the node's environment
  790. if ! relx_escript "$@"; then
  791. exit 1
  792. fi
  793. ;;
  794. attach)
  795. assert_node_alive
  796. shift
  797. exec "$BINDIR/to_erl" "$PIPE_DIR"
  798. ;;
  799. remote_console)
  800. assert_node_alive
  801. shift
  802. remsh
  803. ;;
  804. upgrade|downgrade|install|unpack|uninstall)
  805. if [ -z "${2:-}" ]; then
  806. echo "Missing version argument"
  807. echo "Usage: $REL_NAME $COMMAND {version}"
  808. exit 1
  809. fi
  810. shift
  811. assert_node_alive
  812. ERL_FLAGS="${ERL_FLAGS:-} $EPMD_ARGS" \
  813. exec "$BINDIR/escript" "$RUNNER_ROOT_DIR/bin/install_upgrade.escript" \
  814. "$COMMAND" "{'$REL_NAME', \"$NAME_TYPE\", '$NAME', '$COOKIE'}" "$@"
  815. ;;
  816. versions)
  817. assert_node_alive
  818. shift
  819. ERL_FLAGS="${ERL_FLAGS:-} $EPMD_ARGS" \
  820. exec "$BINDIR/escript" "$RUNNER_ROOT_DIR/bin/install_upgrade.escript" \
  821. "versions" "{'$REL_NAME', \"$NAME_TYPE\", '$NAME', '$COOKIE'}" "$@"
  822. ;;
  823. console|console_clean|foreground)
  824. # .boot file typically just $REL_NAME (ie, the app name)
  825. # however, for debugging, sometimes start_clean.boot is useful.
  826. # For e.g. 'setup', one may even want to name another boot script.
  827. case "$COMMAND" in
  828. console|foreground)
  829. if [ -f "$REL_DIR/$REL_NAME.boot" ]; then
  830. BOOTFILE="$REL_DIR/$REL_NAME"
  831. else
  832. BOOTFILE="$REL_DIR/start"
  833. fi
  834. ;;
  835. console_clean)
  836. BOOTFILE="$REL_DIR/start_clean"
  837. ;;
  838. esac
  839. case "$COMMAND" in
  840. foreground)
  841. FOREGROUNDOPTIONS="-noshell -noinput +Bd"
  842. ;;
  843. *)
  844. FOREGROUNDOPTIONS=''
  845. ;;
  846. esac
  847. # set before generate_config
  848. if [ "${_EMQX_START_DAEMON_MODE:-}" = 1 ]; then
  849. tr_log_to_env
  850. else
  851. maybe_log_to_console
  852. fi
  853. #generate app.config and vm.args
  854. generate_config "$NAME_TYPE" "$NAME"
  855. check_license
  856. # Setup beam-required vars
  857. EMU="beam"
  858. PROGNAME="${0}"
  859. export EMU
  860. export PROGNAME
  861. # Store passed arguments since they will be erased by `set`
  862. # add emqx_data_dir to boot command so it is visible from 'ps -ef'
  863. ARGS="$*"
  864. # shellcheck disable=SC2086
  865. # Build an array of arguments to pass to exec later on
  866. # Build it here because this command will be used for logging.
  867. if [ "$IS_ELIXIR" = no ] || [ "${EMQX_CONSOLE_FLAVOR:-}" = 'erl' ] ; then
  868. # pass down RELEASE_LIB so we can switch to IS_ELIXIR=no
  869. # to boot an Erlang node from the elixir release
  870. set -- "$BINDIR/erlexec" \
  871. $FOREGROUNDOPTIONS \
  872. -boot "$BOOTFILE" \
  873. -boot_var RELEASE_LIB "$ERTS_LIB_DIR" \
  874. -boot_var ERTS_LIB_DIR "$ERTS_LIB_DIR" \
  875. -mode "$CODE_LOADING_MODE" \
  876. -config "$CONF_FILE" \
  877. -args_file "$ARGS_FILE" \
  878. $EPMD_ARGS
  879. else
  880. set -- "$REL_DIR/iex" \
  881. --boot "$BOOTFILE" \
  882. --boot-var RELEASE_LIB "${ERTS_LIB_DIR}" \
  883. --erl-config "${CONF_FILE}" \
  884. --vm-args "${ARGS_FILE}" \
  885. --erl "$FOREGROUNDOPTIONS" \
  886. --erl "-mode $CODE_LOADING_MODE" \
  887. --erl "$EPMD_ARGS" \
  888. --werl
  889. fi
  890. # Log the startup
  891. logger -t "${REL_NAME}[$$]" "EXEC: $* -- ${1+$ARGS} -emqx_data_dir ${DATA_DIR}"
  892. # Start the VM
  893. exec "$@" -- ${1+$ARGS} -emqx_data_dir "${DATA_DIR}"
  894. ;;
  895. ctl)
  896. assert_node_alive
  897. shift
  898. relx_nodetool rpc_infinity emqx_ctl run_command "$@"
  899. ;;
  900. rpc)
  901. assert_node_alive
  902. shift
  903. relx_nodetool rpc "$@"
  904. ;;
  905. rpcterms)
  906. assert_node_alive
  907. shift
  908. relx_nodetool rpcterms "$@"
  909. ;;
  910. eval)
  911. assert_node_alive
  912. shift
  913. if [ "$IS_ELIXIR" = "yes" ]
  914. then
  915. "$REL_DIR/elixir" \
  916. --hidden \
  917. --name "rand-$(relx_gen_id)-$NAME" \
  918. --cookie "$COOKIE" \
  919. --boot "$REL_DIR/start_clean" \
  920. --boot-var RELEASE_LIB "$ERTS_LIB_DIR" \
  921. --vm-args "$REL_DIR/remote.vm.args" \
  922. --erl "-start_epmd false -epmd_module ekka_epmd" \
  923. --rpc-eval "$NAME" "$@"
  924. else
  925. relx_nodetool "eval" "$@"
  926. fi
  927. ;;
  928. eval-erl)
  929. assert_node_alive
  930. shift
  931. relx_nodetool "eval" "$@"
  932. ;;
  933. check_config)
  934. check_config
  935. ;;
  936. *)
  937. usage "$COMMAND"
  938. exit 1
  939. ;;
  940. esac
  941. exit 0