emqx 44 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361
  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. if [ "$DEBUG" -eq 2 ]; then
  10. set -x
  11. export PS4='+(${BASH_SOURCE}:${LINENO}): ${FUNCNAME[0]:+${FUNCNAME[0]}(): }'
  12. fi
  13. logerr() {
  14. if [ "${TERM:-dumb}" = dumb ]; then
  15. echo -e "ERROR: $*" 1>&2
  16. else
  17. echo -e "$(tput setaf 1)ERROR: $*$(tput sgr0)" 1>&2
  18. fi
  19. }
  20. logwarn() {
  21. if [ "${TERM:-dumb}" = dumb ]; then
  22. echo "WARNING: $*"
  23. else
  24. echo "$(tput setaf 3)WARNING: $*$(tput sgr0)"
  25. fi
  26. }
  27. logdebug() {
  28. if [ "$DEBUG" -eq 1 ]; then
  29. echo "DEBUG: $*"
  30. fi
  31. }
  32. die() {
  33. set +x
  34. logerr "$1"
  35. errno=${2:-1}
  36. exit "$errno"
  37. }
  38. # We need to find real directory with emqx files on all platforms
  39. # even when bin/emqx is symlinked on several levels
  40. # - readlink -f works perfectly, but `-f` flag has completely different meaning in BSD version,
  41. # so we can't use it universally.
  42. # - `stat -f%R` on MacOS does exactly what `readlink -f` does on Linux, but we can't use it
  43. # as a universal solution either because GNU stat has different syntax and this argument is invalid.
  44. # Also, version of stat which supports this syntax is only available since MacOS 12
  45. if [ "$(uname -s)" == 'Darwin' ]; then
  46. product_version="$(sw_vers -productVersion | cut -d '.' -f 1)"
  47. if [ "$product_version" -ge 12 ]; then
  48. # if homebrew coreutils package is installed, GNU version of stat can take precedence,
  49. # so we use absolute path to ensure we are calling MacOS default
  50. RUNNER_ROOT_DIR="$(cd "$(dirname "$(/usr/bin/stat -f%R "$0" || echo "$0")")"/..; pwd -P)"
  51. else
  52. # try our best to resolve link on MacOS <= 11
  53. RUNNER_ROOT_DIR="$(cd "$(dirname "$(readlink "$0" || echo "$0")")"/..; pwd -P)"
  54. fi
  55. else
  56. RUNNER_ROOT_DIR="$(cd "$(dirname "$(realpath "$0" || echo "$0")")"/..; pwd -P)"
  57. fi
  58. BASE_RUNNER_ROOT_DIR="${BASE_RUNNER_ROOT_DIR:-$RUNNER_ROOT_DIR}"
  59. if [ -f "$RUNNER_ROOT_DIR/relup/version" ]; then
  60. TARGET_VSN=$(cat "$RUNNER_ROOT_DIR/relup/version")
  61. export BASE_RUNNER_ROOT_DIR
  62. logwarn "Loading emqx from hot upgrade dir: $RUNNER_ROOT_DIR/relup"
  63. exec "$RUNNER_ROOT_DIR"/relup/"$TARGET_VSN"/bin/emqx "$@"
  64. else
  65. logdebug "Loading emqx from $RUNNER_ROOT_DIR"
  66. fi
  67. # shellcheck disable=SC1090,SC1091
  68. . "$RUNNER_ROOT_DIR"/releases/emqx_vars
  69. # defined in emqx_vars
  70. export RUNNER_ROOT_DIR
  71. export EMQX_ETC_DIR
  72. export REL_VSN
  73. export SCHEMA_MOD
  74. export IS_ENTERPRISE
  75. RUNNER_SCRIPT="$RUNNER_BIN_DIR/$REL_NAME"
  76. CODE_LOADING_MODE="${CODE_LOADING_MODE:-embedded}"
  77. REL_DIR="$RUNNER_ROOT_DIR/releases/$REL_VSN"
  78. WHOAMI=$(whoami 2>/dev/null || id -u)
  79. # hocon try to read environment variables starting with "EMQX_"
  80. export HOCON_ENV_OVERRIDE_PREFIX='EMQX_'
  81. export ERTS_DIR="$RUNNER_ROOT_DIR/erts-$ERTS_VSN"
  82. export BINDIR="$ERTS_DIR/bin"
  83. export EMU="beam"
  84. export PROGNAME="erl"
  85. export ERTS_LIB_DIR="$RUNNER_ROOT_DIR/lib"
  86. DYNLIBS_DIR="$RUNNER_ROOT_DIR/dynlibs"
  87. assert_node_alive() {
  88. if ! relx_nodetool "ping" > /dev/null; then
  89. exit 1
  90. fi
  91. }
  92. usage() {
  93. local command="$1"
  94. case "$command" in
  95. start)
  96. echo "Start EMQX service in daemon mode"
  97. ;;
  98. stop)
  99. echo "Stop the running EMQX program"
  100. ;;
  101. console)
  102. echo "Boot up EMQX service in an interactive Erlang or Elixir shell"
  103. echo "This command needs a tty"
  104. ;;
  105. console_clean)
  106. echo "This command does NOT boot up the EMQX service"
  107. echo "It only starts an interactive Erlang or Elixir console with all the"
  108. echo "EMQX code available"
  109. ;;
  110. foreground)
  111. echo "Start EMQX in foreground mode without an interactive shell"
  112. ;;
  113. pid)
  114. echo "Print out EMQX process identifier"
  115. ;;
  116. ping)
  117. echo "Check if the EMQX node is up and running"
  118. echo "This command exit with 0 silently if node is running"
  119. ;;
  120. escript)
  121. echo "Execute a escript using the Erlang runtime from EMQX package installation"
  122. echo "For example $REL_NAME escript /path/to/my/escript my_arg1 my_arg2"
  123. ;;
  124. attach)
  125. echo "This command is applicable when EMQX is started in daemon mode."
  126. echo "It attaches the current shell to EMQX's control console"
  127. echo "through a named pipe."
  128. logwarn "try to use the safer alternative, remote_console command."
  129. ;;
  130. remote_console)
  131. echo "Start an interactive shell running an Erlang or Elixir node which "
  132. echo "hidden-connects to the running EMQX node".
  133. echo "This command is mostly used for troubleshooting."
  134. ;;
  135. ertspath)
  136. echo "Print path to Erlang runtime bin dir"
  137. ;;
  138. rpc)
  139. echo "Usage: $REL_NAME rpc MODULE FUNCTION [ARGS, ...]"
  140. echo "Connect to the EMQX node and make an Erlang RPC"
  141. echo "This command blocks for at most 60 seconds."
  142. echo "It exits with non-zero code in case of any RPC failure"
  143. echo "including connection error and runtime exception"
  144. ;;
  145. rpcterms)
  146. echo "Usage: $REL_NAME rpcterms MODULE FUNCTION [ARGS, ...]"
  147. echo "Connect to the EMQX node and make an Erlang RPC"
  148. echo "The result of the RPC call is pretty-printed as an "
  149. echo "Erlang term"
  150. ;;
  151. root_dir)
  152. echo "Print EMQX installation root dir"
  153. ;;
  154. eval)
  155. echo "Evaluate an Erlang expression in the EMQX node."
  156. ;;
  157. eval-ex)
  158. echo "Evaluate an Elixir expression in the EMQX node. Only applies to Elixir node"
  159. ;;
  160. versions)
  161. echo "List installed EMQX release versions and their status"
  162. ;;
  163. unpack)
  164. echo "Usage: $REL_NAME unpack [VERSION]"
  165. echo "Unpacks a release package VERSION, it assumes that this"
  166. echo "release package tarball has already been deployed at one"
  167. echo "of the following locations:"
  168. echo " releases/<relname>-<version>.tar.gz"
  169. ;;
  170. install)
  171. echo "Usage: $REL_NAME install [VERSION]"
  172. echo "Installs a release package VERSION, it assumes that this"
  173. echo "release package tarball has already been deployed at one"
  174. echo "of the following locations:"
  175. echo " releases/<relname>-<version>.tar.gz"
  176. echo ""
  177. echo " --no-permanent Install release package VERSION but"
  178. echo " don't make it permanent"
  179. ;;
  180. uninstall)
  181. echo "Usage: $REL_NAME uninstall [VERSION]"
  182. echo "Uninstalls a release VERSION, it will only accept"
  183. echo "versions that are not currently in use"
  184. ;;
  185. upgrade)
  186. echo "Usage: $REL_NAME upgrade [VERSION]"
  187. echo "Upgrades the currently running release to VERSION, it assumes"
  188. echo "that a release package tarball has already been deployed at one"
  189. echo "of the following locations:"
  190. echo " releases/<relname>-<version>.tar.gz"
  191. echo ""
  192. echo " --no-permanent Install release package VERSION but"
  193. echo " don't make it permanent"
  194. ;;
  195. downgrade)
  196. echo "Usage: $REL_NAME downgrade [VERSION]"
  197. echo "Downgrades the currently running release to VERSION, it assumes"
  198. echo "that a release package tarball has already been deployed at one"
  199. echo "of the following locations:"
  200. echo " releases/<relname>-<version>.tar.gz"
  201. echo ""
  202. echo " --no-permanent Install release package VERSION but"
  203. echo " don't make it permanent"
  204. ;;
  205. check_config)
  206. echo "Checks the EMQX config without generating any files"
  207. ;;
  208. *)
  209. echo "Usage: $REL_NAME COMMAND [help]"
  210. echo ''
  211. echo "Commonly used COMMANDs:"
  212. echo " start: Start EMQX in daemon mode"
  213. echo " console: Start EMQX in an interactive Erlang or Elixir shell"
  214. echo " foreground: Start EMQX in foreground mode without an interactive shell"
  215. echo " stop: Stop the running EMQX node"
  216. echo " ctl: Administration commands, execute '$REL_NAME ctl help' for more details"
  217. echo ''
  218. echo "More:"
  219. echo " Shell attach: remote_console | attach"
  220. # echo " Up/Down-grade: upgrade | downgrade | install | uninstall | versions" # TODO enable when supported
  221. echo " Install Info: ertspath | root_dir"
  222. echo " Runtime Status: pid | ping"
  223. echo " Validate Config: check_config"
  224. echo " Advanced: console_clean | escript | rpc | rpcterms | eval | eval-ex"
  225. echo ''
  226. echo "Execute '$REL_NAME COMMAND help' for more information"
  227. ;;
  228. esac
  229. }
  230. COMMAND="${1:-}"
  231. GREP='grep --color=never'
  232. if [ -z "$COMMAND" ]; then
  233. usage 'help'
  234. exit 1
  235. elif [ "$COMMAND" = 'help' ]; then
  236. usage 'help'
  237. exit 0
  238. fi
  239. if [ "${2:-}" = 'help' ]; then
  240. ## 'ctl' command has its own usage info
  241. if [ "$COMMAND" != 'ctl' ]; then
  242. usage "$COMMAND"
  243. exit 0
  244. fi
  245. fi
  246. ## IS_BOOT_COMMAND is set for later to inspect node name and cookie from hocon config (or env variable)
  247. case "${COMMAND}" in
  248. start|console|console_clean|foreground|check_config)
  249. IS_BOOT_COMMAND='yes'
  250. ;;
  251. ertspath)
  252. echo "$ERTS_DIR"
  253. exit 0
  254. ;;
  255. root_dir)
  256. echo "$RUNNER_ROOT_DIR"
  257. exit 0
  258. ;;
  259. *)
  260. IS_BOOT_COMMAND='no'
  261. ;;
  262. esac
  263. ## backward compatible
  264. if [ -d "$ERTS_DIR/lib" ]; then
  265. export LD_LIBRARY_PATH="$ERTS_DIR/lib:$LD_LIBRARY_PATH"
  266. fi
  267. # Simple way to check the correct user and fail early
  268. check_user() {
  269. # Validate that the user running the script is the owner of the
  270. # RUN_DIR.
  271. if [ "$RUNNER_USER" ] && [ "x$WHOAMI" != "x$RUNNER_USER" ]; then
  272. if [ "x$WHOAMI" != "xroot" ]; then
  273. echo "You need to be root or use sudo to run this command"
  274. exit 1
  275. fi
  276. CMD="DEBUG=$DEBUG \"$RUNNER_SCRIPT\" "
  277. for ARG in "$@"; do
  278. CMD="${CMD} \"$ARG\""
  279. done
  280. # This will drop privileges into the runner user
  281. # It exec's in a new shell and the current shell will exit
  282. exec su - "$RUNNER_USER" -c "$CMD"
  283. fi
  284. }
  285. # Make sure the user running this script is the owner and/or su to that user
  286. check_user "$@"
  287. ES=$?
  288. if [ "$ES" -ne 0 ]; then
  289. exit $ES
  290. fi
  291. # Make sure log directory exists
  292. mkdir -p "$EMQX_LOG_DIR"
  293. # turn off debug as this is static
  294. set +x
  295. COMPATIBILITY_CHECK='
  296. io:format("BEAM_OK~n", []),
  297. try
  298. [_|_] = L = crypto:info_lib(),
  299. io:format("CRYPTO_OK ~0p~n", [L])
  300. catch
  301. _ : _ ->
  302. %% so logger has the chance to log something
  303. timer:sleep(100),
  304. halt(1)
  305. end,
  306. try
  307. mnesia_hook:module_info(),
  308. io:format("MNESIA_OK~n", [])
  309. catch
  310. _ : _ ->
  311. io:format("WARNING: Mnesia app has no post-coommit hook support~n", []),
  312. halt(2)
  313. end,
  314. halt(0).
  315. '
  316. [[ "$DEBUG" -gt 0 ]] && set -x
  317. compatiblity_info() {
  318. # RELEASE_LIB is used by Elixir
  319. # set crash-dump bytes to zero to ensure no crash dump is generated when erl crashes
  320. env ERL_CRASH_DUMP_BYTES=0 "$BINDIR/$PROGNAME" \
  321. -noshell \
  322. +S 2 \
  323. +P 65536 \
  324. +Q 65536 \
  325. -boot "$REL_DIR/start_clean" \
  326. -boot_var RELEASE_LIB "$ERTS_LIB_DIR/lib" \
  327. -eval "$COMPATIBILITY_CHECK"
  328. }
  329. # Collect Erlang/OTP runtime sanity and compatibility in one go
  330. maybe_use_portable_dynlibs() {
  331. # Read BUILD_INFO early as the next commands may mess up the shell
  332. BUILD_INFO="$(cat "${REL_DIR}/BUILD_INFO")"
  333. COMPATIBILITY_INFO="$(compatiblity_info 2>/dev/null || true)"
  334. if ! (echo -e "$COMPATIBILITY_INFO" | $GREP -q 'CRYPTO_OK'); then
  335. ## failed to start, might be due to missing libs, try to be portable
  336. export LD_LIBRARY_PATH="${LD_LIBRARY_PATH:-$DYNLIBS_DIR}"
  337. if [ "$LD_LIBRARY_PATH" != "$DYNLIBS_DIR" ]; then
  338. export LD_LIBRARY_PATH="$DYNLIBS_DIR:$LD_LIBRARY_PATH"
  339. fi
  340. ## Turn off debug, because COMPATIBILITY_INFO needs to capture stderr
  341. COMPATIBILITY_INFO="$(compatiblity_info 2>&1 || true)"
  342. if ! (echo -e "$COMPATIBILITY_INFO" | $GREP -q 'BEAM_OK'); then
  343. ## not able to start beam.smp
  344. logerr "$COMPATIBILITY_INFO"
  345. logerr "Please ensure it is running on the correct platform:"
  346. logerr "$BUILD_INFO"
  347. logerr "Version=$REL_VSN"
  348. logerr "Required dependencies: openssl-1.1.1 (libcrypto), libncurses and libatomic1"
  349. exit 1
  350. elif ! (echo -e "$COMPATIBILITY_INFO" | $GREP -q 'CRYPTO_OK'); then
  351. ## not able to start crypto app
  352. logerr "$COMPATIBILITY_INFO"
  353. exit 2
  354. fi
  355. logwarn "Using libs from '${DYNLIBS_DIR}' due to missing from the OS."
  356. fi
  357. }
  358. SED_REPLACE="sed -i "
  359. case $(sed --help 2>&1) in
  360. *GNU*) SED_REPLACE="sed -i ";;
  361. *BusyBox*) SED_REPLACE="sed -i ";;
  362. *) SED_REPLACE="sed -i '' ";;
  363. esac
  364. # Get node pid
  365. relx_get_pid() {
  366. if output="$(relx_nodetool rpcterms os getpid)"
  367. then
  368. # shellcheck disable=SC2001 # Escaped quote taken as closing quote in editor
  369. echo "$output" | sed -e 's/"//g'
  370. return 0
  371. else
  372. echo "$output"
  373. return 1
  374. fi
  375. }
  376. # Connect to a remote node
  377. remsh() {
  378. # Generate a unique id used to allow multiple remsh to the same node
  379. # transparently
  380. id="remsh$(gen_node_id)-${NAME}"
  381. # shellcheck disable=SC2086
  382. # Setup remote shell command to control node
  383. if [ "$IS_ELIXIR" = no ] || [ "${EMQX_CONSOLE_FLAVOR:-}" = 'erl' ] ; then
  384. set -- "$BINDIR/erl" "$NAME_TYPE" "$id" \
  385. -remsh "$NAME" -boot "$REL_DIR/start_clean" \
  386. -boot_var ERTS_LIB_DIR "$ERTS_LIB_DIR" \
  387. -boot_var RELEASE_LIB "$ERTS_LIB_DIR" \
  388. -setcookie "$COOKIE" \
  389. -hidden \
  390. -kernel net_ticktime "$TICKTIME" \
  391. +P 65536 \
  392. +Q 65536 \
  393. +S 2 \
  394. $EPMD_ARGS
  395. else
  396. set -- "$REL_DIR/iex" \
  397. --remsh "$NAME" \
  398. --boot-var RELEASE_LIB "$ERTS_LIB_DIR" \
  399. --cookie "$COOKIE" \
  400. --hidden \
  401. --erl "-kernel net_ticktime $TICKTIME" \
  402. --erl "$EPMD_ARGS" \
  403. --erl "$NAME_TYPE $id" \
  404. --erl "+P 65536" \
  405. --erl "+Q 65536" \
  406. --erl "+S 2" \
  407. --boot "$REL_DIR/start_clean"
  408. fi
  409. exec "$@"
  410. }
  411. # Generate a random id
  412. gen_node_id() {
  413. od -t u -N 4 /dev/urandom | head -n1 | awk '{print $2 % 1000}'
  414. }
  415. call_nodetool() {
  416. "$ERTS_DIR/bin/escript" "$RUNNER_ROOT_DIR/bin/nodetool" "$@"
  417. }
  418. # Control a node
  419. relx_nodetool() {
  420. command="$1"; shift
  421. ERL_FLAGS="${ERL_FLAGS:-} $EPMD_ARGS -setcookie $COOKIE" \
  422. call_nodetool "$NAME_TYPE" "$NAME" "$command" "$@"
  423. }
  424. call_hocon() {
  425. call_nodetool hocon "$@" \
  426. || die "call_hocon_failed: $*" $?
  427. }
  428. find_emqx_process() {
  429. ## Find the running node from 'ps -ef'
  430. ## * The grep args like '[e]mqx' but not 'emqx' is to avoid greping the grep command itself
  431. ## * The running 'remsh' and 'nodetool' processes must be excluded
  432. if [ -n "${EMQX_NODE__NAME:-}" ]; then
  433. # if node name is provided, filter by node name
  434. # shellcheck disable=SC2009
  435. ps -ef | $GREP '[e]mqx' | $GREP -v -E '(remsh|nodetool)' | $GREP -E "\s-s?name\s${EMQX_NODE__NAME}" | $GREP -oE "\-[r]oot ${RUNNER_ROOT_DIR}.*" || true
  436. else
  437. # shellcheck disable=SC2009
  438. ps -ef | $GREP '[e]mqx' | $GREP -v -E '(remsh|nodetool)' | $GREP -oE "\-[r]oot ${RUNNER_ROOT_DIR}.*" || true
  439. fi
  440. }
  441. ## Resolve boot configs in a batch
  442. ## This is because starting the Erlang beam with all modules loaded
  443. ## and parsing HOCON config + environment variables is a non-trivial task
  444. CONF_KEYS=( 'node.data_dir' 'node.name' 'node.cookie' 'node.db_backend' 'cluster.proto_dist' 'node.dist_net_ticktime' )
  445. if [ "$IS_ENTERPRISE" = 'yes' ]; then
  446. CONF_KEYS+=( 'license.key' )
  447. fi
  448. ## To be backward compatible, read and then unset EMQX_NODE_NAME
  449. if [ -n "${EMQX_NODE_NAME:-}" ]; then
  450. export EMQX_NODE__NAME="${EMQX_NODE_NAME}"
  451. unset EMQX_NODE_NAME
  452. fi
  453. # Turn off debug as the ps output can be quite noisy
  454. set +x
  455. PS_LINE="$(find_emqx_process)"
  456. logdebug "PS_LINE=$PS_LINE"
  457. RUNNING_NODES_COUNT="$(echo -e "$PS_LINE" | sed '/^\s*$/d' | wc -l)"
  458. [ "$RUNNING_NODES_COUNT" -gt 1 ] && logdebug "More than one running node found: count=$RUNNING_NODES_COUNT"
  459. if [ "$IS_BOOT_COMMAND" = 'yes' ]; then
  460. if [ "$RUNNING_NODES_COUNT" -gt 0 ] && [ "$COMMAND" != 'check_config' ]; then
  461. running_node_name=$(echo -e "$PS_LINE" | $GREP -oE "\s-s?name.*" | awk '{print $2}' || true)
  462. if [ -n "$running_node_name" ] && [ "$running_node_name" = "${EMQX_NODE__NAME:-}" ]; then
  463. echo "Node ${running_node_name} is already running!"
  464. exit 1
  465. fi
  466. fi
  467. [ -f "$EMQX_ETC_DIR"/emqx.conf ] || die "emqx.conf is not found in $EMQX_ETC_DIR" 1
  468. maybe_use_portable_dynlibs
  469. if [ "${EMQX_BOOT_CONFIGS:-}" = '' ]; then
  470. EMQX_BOOT_CONFIGS="$(call_hocon -s "$SCHEMA_MOD" -c "$EMQX_ETC_DIR"/emqx.conf multi_get "${CONF_KEYS[@]}")"
  471. ## export here so the 'console' command recursively called from
  472. ## 'start' command does not have to parse the configs again
  473. export EMQX_BOOT_CONFIGS
  474. fi
  475. else
  476. # For non-boot commands, we need below runtime facts to connect to the running node:
  477. # 1. The running node name;
  478. # 2. The Erlang cookie in use by the running node name;
  479. # 3. SSL options if the node is using TLS for Erlang distribution;
  480. # 4. Erlang kernel application's net_ticktime config.
  481. #
  482. # There are 3 sources of truth to get those runtime information.
  483. # Listed in the order of preference:
  484. # 1. The boot command (which can be inspected from 'ps -ef' command output)
  485. # 2. The generated vm.<time>.config file located in the dir pointed by 'node.data_dir'
  486. # 3. The bootstrap config 'etc/emqx.conf'
  487. #
  488. # If failed to read from source 1, the information is retrieved from source 3
  489. # i.e. source 2 is never used.
  490. #
  491. # NOTES:
  492. # * We should avoid getting runtime information with the 3rd approach because 'etc/emqx.conf' might
  493. # be updated after the node is started. e.g. If a user starts the node with name 'emqx@127.0.0.1'
  494. # then update the config in the file to 'node.name = "emqx@local.net"', after this change,
  495. # there would be no way stop the running node 'emqx@127.0.0.1', because 'emqx stop' command
  496. # would try to stop the new node instead.
  497. if [ "$RUNNING_NODES_COUNT" -eq 1 ]; then
  498. ## only one emqx node is running, get running args from 'ps -ef' output
  499. tmp_nodename=$(echo -e "$PS_LINE" | $GREP -oE "\s-s?name.*" | awk '{print $2}' || true)
  500. tmp_cookie=$(echo -e "$PS_LINE" | $GREP -oE "\s-setcookie.*" | awk '{print $2}' || true)
  501. tmp_proto_dist=$(echo -e "$PS_LINE" | $GREP -oE '\s-ekka_proto_dist.*' | awk '{print $2}' || echo 'inet_tcp')
  502. SSL_DIST_OPTFILE="$(echo -e "$PS_LINE" | $GREP -oE '\-ssl_dist_optfile\s.+\s' | awk '{print $2}' || true)"
  503. tmp_ticktime="$(echo -e "$PS_LINE" | $GREP -oE '\s-kernel\snet_ticktime\s.+\s' | awk '{print $3}' || true)"
  504. tmp_datadir="$(echo -e "$PS_LINE" | $GREP -oE "\-emqx_data_dir.*" | sed -E 's#.+emqx_data_dir[[:blank:]]##g' | sed -E 's#[[:blank:]]--$##g' || true)"
  505. ## Make the format like what call_hocon multi_get prints out, but only need 4 args
  506. EMQX_BOOT_CONFIGS="node.name=${tmp_nodename}\nnode.cookie=${tmp_cookie}\ncluster.proto_dist=${tmp_proto_dist}\nnode.dist_net_ticktime=$tmp_ticktime\nnode.data_dir=${tmp_datadir}"
  507. else
  508. if [ "$RUNNING_NODES_COUNT" -gt 1 ]; then
  509. if [ -z "${EMQX_NODE__NAME:-}" ]; then
  510. tmp_nodenames=$(echo -e "$PS_LINE" | $GREP -oE "\s-s?name.*" | awk '{print $2}' | tr '\n' ' ')
  511. logerr "More than one EMQX node found running (root dir: ${RUNNER_ROOT_DIR})"
  512. logerr "Running nodes: $tmp_nodenames"
  513. logerr "Make sure environment variable EMQX_NODE__NAME is set to indicate for which node this command is intended."
  514. exit 1
  515. fi
  516. else
  517. if [ -n "${EMQX_NODE__NAME:-}" ]; then
  518. die "Node $EMQX_NODE__NAME is not running?"
  519. fi
  520. fi
  521. ## We have no choice but to read the bootstrap config (with environment overrides available in the current shell)
  522. [ -f "$EMQX_ETC_DIR"/emqx.conf ] || die "emqx.conf is not found in $EMQX_ETC_DIR" 1
  523. maybe_use_portable_dynlibs
  524. EMQX_BOOT_CONFIGS="$(call_hocon -s "$SCHEMA_MOD" -c "$EMQX_ETC_DIR"/emqx.conf multi_get "${CONF_KEYS[@]}")"
  525. fi
  526. fi
  527. logdebug "EMQX_BOOT_CONFIGS: $EMQX_BOOT_CONFIGS"
  528. [[ "$DEBUG" -gt 0 ]] && set -x
  529. get_boot_config() {
  530. path_to_value="$1"
  531. echo -e "$EMQX_BOOT_CONFIGS" | $GREP "$path_to_value=" | sed -e "s/$path_to_value=//g" | tr -d \"
  532. }
  533. EPMD_ARGS="-start_epmd false -epmd_module ekka_epmd -proto_dist ekka"
  534. PROTO_DIST="$(get_boot_config 'cluster.proto_dist' || true)"
  535. TICKTIME="$(get_boot_config 'node.dist_net_ticktime' || echo '120')"
  536. # this environment variable is required by ekka_dist module
  537. # because proto_dist is overriden to ekka, and there is a lack of ekka_tls module
  538. export EKKA_PROTO_DIST_MOD="${PROTO_DIST:-inet_tcp}"
  539. if [ "$EKKA_PROTO_DIST_MOD" = 'inet_tls' ] || [ "$EKKA_PROTO_DIST_MOD" = 'inet6_tls' ]; then
  540. if [ "$IS_BOOT_COMMAND" = 'yes' ]; then
  541. SSL_DIST_OPTFILE=${EMQX_SSL_DIST_OPTFILE:-"$EMQX_ETC_DIR/ssl_dist.conf"}
  542. case "$SSL_DIST_OPTFILE" in
  543. *\ *)
  544. # there is unfortunately no way to support space for this option because we'd need to grep
  545. # from 'ps -ef' result to get this option for non-boot commands (nodetool) to run
  546. set +x
  547. logerr "Got space in: $SSL_DIST_OPTFILE"
  548. logerr "No space is allowed for Erlang distribution over SSL option file path."
  549. logerr "Configure it from environment variable EMQX_SSL_DIST_OPTFILE."
  550. logerr "Or make sure emqx root path '$RUNNER_ROOT_DIR' has no space"
  551. exit 1
  552. ;;
  553. *)
  554. true
  555. ;;
  556. esac
  557. fi
  558. EPMD_ARGS="${EPMD_ARGS} -ssl_dist_optfile $SSL_DIST_OPTFILE"
  559. fi
  560. DATA_DIR="$(get_boot_config 'node.data_dir')"
  561. # ensure no trailing /
  562. DATA_DIR="${DATA_DIR%/}"
  563. if [[ $DATA_DIR != /* ]]; then
  564. # relative path
  565. DATA_DIR="${BASE_RUNNER_ROOT_DIR}/${DATA_DIR}"
  566. fi
  567. CONFIGS_DIR="$DATA_DIR/configs"
  568. mkdir -p "$CONFIGS_DIR"
  569. check_license() {
  570. if [ "$IS_ENTERPRISE" == "no" ]; then
  571. return 0
  572. fi
  573. key_license="${EMQX_LICENSE__KEY:-$(get_boot_config 'license.key' || echo '')}"
  574. if [[ -n "$key_license" && ("$key_license" != "undefined") ]]; then
  575. call_nodetool check_license_key "$key_license"
  576. else
  577. set +x
  578. logerr "License not found."
  579. logerr "Please specify one via the EMQX_LICENSE__KEY variable"
  580. logerr "or via license.key in emqx.conf."
  581. return 1
  582. fi
  583. }
  584. # When deciding which install upgrade script to run, we have to check
  585. # our own version so we may avoid infinite loops and call the correct
  586. # version.
  587. current_script_version() {
  588. curr_script=$(basename "${BASH_SOURCE[0]}")
  589. suffix=${curr_script#*-}
  590. if [[ "${suffix}" == "${curr_script}" ]]; then
  591. # there's no suffix, so we're running the default `emqx` script;
  592. # we'll have to trust the REL_VSN variable
  593. echo "$REL_VSN"
  594. else
  595. echo "${suffix}"
  596. fi
  597. }
  598. parse_semver() {
  599. echo "$1" | tr '.|-' ' '
  600. }
  601. max_version_of() {
  602. local vsn1="$1"
  603. local vsn2="$2"
  604. echo "${vsn1}" "${vsn2}" | tr " " "\n" | sort -rV | head -n1
  605. }
  606. versioned_script_path() {
  607. local script_name="$1"
  608. local vsn="$2"
  609. echo "$RUNNER_ROOT_DIR/bin/$script_name-$vsn"
  610. }
  611. does_script_version_exist() {
  612. local script_name="$1"
  613. local vsn="$2"
  614. if [[ -f "$(versioned_script_path "$script_name" "$vsn")" ]]; then
  615. return 0
  616. else
  617. return 1
  618. fi
  619. }
  620. # extract_from_package packege_path destination file1 file2
  621. extract_from_package() {
  622. local package="$1"
  623. local dest_dir="$2"
  624. shift 2
  625. tar -C "$dest_dir" -xf "$package" "$@"
  626. }
  627. am_i_the_newest_script() {
  628. local curr_vsn other_vsn
  629. curr_vsn="$(current_script_version)"
  630. other_vsn="$1"
  631. max_vsn="$(max_version_of "$other_vsn" "$curr_vsn")"
  632. if [[ "$max_vsn" == "$curr_vsn" ]]; then
  633. return 0
  634. else
  635. return 1
  636. fi
  637. }
  638. locate_package() {
  639. local package_path candidates vsn
  640. vsn="$1"
  641. if [[ "${IS_ENTERPRISE}" == "yes" ]]; then
  642. package_pattern="$RUNNER_ROOT_DIR/releases/emqx-enterprise-$vsn-*.tar.gz"
  643. else
  644. package_pattern="$RUNNER_ROOT_DIR/releases/emqx-$vsn-*.tar.gz"
  645. fi
  646. # shellcheck disable=SC2207,SC2086
  647. candidates=($(ls $package_pattern))
  648. if [[ "${#candidates[@]}" == 0 ]]; then
  649. logerr "No package matching $package_pattern found."
  650. exit 1
  651. elif [[ "${#candidates[@]}" -gt 1 ]]; then
  652. logerr "Multiple packages matching $package_pattern found. Ensure only one exists."
  653. exit 1
  654. else
  655. echo "${candidates[0]}"
  656. fi
  657. }
  658. ensure_newest_script_is_extracted() {
  659. local newest_vsn="$1"
  660. local package_path tmpdir
  661. if does_script_version_exist "emqx" "$newest_vsn" \
  662. && does_script_version_exist "install_upgrade.escript" "$newest_vsn"; then
  663. return
  664. else
  665. package_path="$(locate_package "$newest_vsn")"
  666. tmpdir="$(mktemp -dp /tmp emqx.XXXXXXXXXXX)"
  667. extract_from_package \
  668. "$package_path" \
  669. "$tmpdir" \
  670. "bin/emqx-$newest_vsn" \
  671. "bin/install_upgrade.escript-$newest_vsn"
  672. cp "$tmpdir/bin/emqx-$newest_vsn" \
  673. "$tmpdir/bin/install_upgrade.escript-$newest_vsn" \
  674. "$RUNNER_ROOT_DIR/bin/"
  675. rm -rf "$tmpdir"
  676. fi
  677. }
  678. # Run an escript in the node's environment
  679. relx_escript() {
  680. shift; scriptpath="$1"; shift
  681. "$ERTS_DIR/bin/escript" "$RUNNER_ROOT_DIR/$scriptpath" "$@"
  682. }
  683. # Output a start command for the last argument of run_erl
  684. relx_start_command() {
  685. printf "exec \"%s\" \"%s\"" "$RUNNER_SCRIPT" \
  686. "$START_OPTION"
  687. }
  688. # Function to check configs without generating them
  689. check_config() {
  690. ## this command checks the configs without generating any files
  691. call_hocon -v \
  692. -s "$SCHEMA_MOD" \
  693. -c "$DATA_DIR"/configs/cluster.hocon \
  694. -c "$EMQX_ETC_DIR"/emqx.conf \
  695. check_schema
  696. }
  697. # Function to generate app.config and vm.args
  698. # sets two environment variables CONF_FILE and ARGS_FILE
  699. generate_config() {
  700. local name_type="$1"
  701. local node_name="$2"
  702. ## Delete the *.siz files first or it can't start after
  703. ## changing the config 'log.rotation.size'
  704. rm -f "${EMQX_LOG_DIR}"/*.siz
  705. ## timestamp for each generation
  706. local NOW_TIME
  707. NOW_TIME="$(date +'%Y.%m.%d.%H.%M.%S')"
  708. ## This command populates two files: app.<time>.config and vm.<time>.args
  709. ## It takes input sources and overlays values in below order:
  710. ## - $DATA_DIR/cluster.hocon (if exists)
  711. ## - etc/emqx.conf
  712. ## - environment variables starts with EMQX_ e.g. EMQX_NODE__ROLE
  713. ##
  714. ## NOTE: it's a known issue that cluster.hocon may change right after the node boots up
  715. ## because it has to sync cluster.hocon from other nodes.
  716. call_hocon -v -t "$NOW_TIME" \
  717. -s "$SCHEMA_MOD" \
  718. -c "$DATA_DIR"/configs/cluster.hocon \
  719. -c "$EMQX_ETC_DIR"/emqx.conf \
  720. -d "$DATA_DIR"/configs generate
  721. ## filenames are per-hocon convention
  722. CONF_FILE="$CONFIGS_DIR/app.$NOW_TIME.config"
  723. ARGS_FILE="$CONFIGS_DIR/vm.$NOW_TIME.args"
  724. ## Merge hocon generated *.args into the vm.args
  725. TMP_ARG_FILE="$CONFIGS_DIR/vm.args.tmp"
  726. cp "$EMQX_ETC_DIR/vm.args" "$TMP_ARG_FILE"
  727. echo "" >> "$TMP_ARG_FILE"
  728. echo "-pa \"${REL_DIR}/consolidated\"" >> "$TMP_ARG_FILE"
  729. ## read lines from generated vm.<time>.args file
  730. ## drop comment lines, and empty lines using sed
  731. ## pipe the lines to a while loop
  732. sed '/^#/d' "$ARGS_FILE" | sed '/^$/d' | while IFS='' read -r ARG_LINE || [ -n "$ARG_LINE" ]; do
  733. ## in the loop, split the 'key[:space:]value' pair
  734. ARG_KEY=$(echo "$ARG_LINE" | awk '{$NF="";print}')
  735. ARG_VALUE=$(echo "$ARG_LINE" | awk '{print $NF}')
  736. ## use the key to look up in vm.args file for the value
  737. TMP_ARG_VALUE=$($GREP "^$ARG_KEY" "$TMP_ARG_FILE" || true | awk '{print $NF}')
  738. ## compare generated (to override) value to original (to be overridden) value
  739. if [ "$ARG_VALUE" != "$TMP_ARG_VALUE" ] ; then
  740. ## if they are different
  741. if [ -n "$TMP_ARG_VALUE" ]; then
  742. ## if the old value is present, replace it with generated value
  743. sh -c "$SED_REPLACE 's|^$ARG_KEY.*$|$ARG_LINE|' \"$TMP_ARG_FILE\""
  744. else
  745. ## otherwise append generated value to the end
  746. echo "$ARG_LINE" >> "$TMP_ARG_FILE"
  747. fi
  748. fi
  749. done
  750. echo "$name_type $node_name" >> "$TMP_ARG_FILE"
  751. echo "-mnesia dir '\"$DATA_DIR/mnesia/$NAME\"'" >> "$TMP_ARG_FILE"
  752. ## rename the generated vm.<time>.args file
  753. mv -f "$TMP_ARG_FILE" "$ARGS_FILE"
  754. }
  755. # check if a PID is defunct
  756. is_defunct() {
  757. local PID="$1"
  758. ps -fp "$PID" | $GREP -q 'defunct'
  759. }
  760. # check if a PID is down
  761. # shellcheck disable=SC2317 # call in func `nodetool_shutdown()`
  762. is_down() {
  763. PID="$1"
  764. if ps -p "$PID" >/dev/null; then
  765. # still around
  766. # shellcheck disable=SC2009 # this grep pattern is not a part of the program names
  767. if is_defunct "$PID"; then
  768. # zombie state, print parent pid
  769. parent="$(ps -o ppid= -p "$PID" | tr -d ' ')"
  770. if [ -z "$parent" ] && ! is_defunct "$PID"; then
  771. # process terminated in the meanwhile
  772. return 0;
  773. fi
  774. logwarn "$PID is marked <defunct>, parent: $(ps -p "$parent")"
  775. return 0
  776. fi
  777. return 1
  778. fi
  779. # it's gone
  780. return 0
  781. }
  782. wait_for() {
  783. local WAIT_TIME
  784. local CMD
  785. WAIT_TIME="$1"
  786. shift
  787. CMD="$*"
  788. while true; do
  789. if $CMD; then
  790. return 0
  791. fi
  792. if [ "$WAIT_TIME" -le 0 ]; then
  793. return 1
  794. fi
  795. WAIT_TIME=$((WAIT_TIME - 1))
  796. sleep 1
  797. done
  798. }
  799. wait_until_return_val() {
  800. local RESULT
  801. local WAIT_TIME
  802. local CMD
  803. RESULT="$1"
  804. WAIT_TIME="$2"
  805. shift 2
  806. CMD="$*"
  807. while true; do
  808. if [ "$($CMD 2>/dev/null)" = "$RESULT" ]; then
  809. return 0
  810. fi
  811. if [ "$WAIT_TIME" -le 0 ]; then
  812. return 1
  813. fi
  814. WAIT_TIME=$((WAIT_TIME - 1))
  815. sleep 1
  816. done
  817. }
  818. # First, there is EMQX_DEFAULT_LOG_HANDLER which can control the default values
  819. # to be used when generating configs.
  820. # It's set in docker entrypoint and in systemd service file.
  821. #
  822. # To be backward compatible with 4.x and v5.0.0 ~ v5.0.24/e5.0.2:
  823. # if EMQX_LOG__TO is set, we try to enable handlers from environment variables.
  824. # i.e. it overrides the default value set in EMQX_DEFAULT_LOG_HANDLER
  825. tr_log_to_env() {
  826. local log_to=${EMQX_LOG__TO:-undefined}
  827. # unset because it's unknown to 5.0
  828. unset EMQX_LOG__TO
  829. case "${log_to}" in
  830. console)
  831. export EMQX_LOG__CONSOLE__ENABLE='true'
  832. export EMQX_LOG__FILE__ENABLE='false'
  833. ;;
  834. file)
  835. export EMQX_LOG__CONSOLE__ENABLE='false'
  836. export EMQX_LOG__FILE__ENABLE='true'
  837. ;;
  838. both)
  839. export EMQX_LOG__CONSOLE__ENABLE='true'
  840. export EMQX_LOG__FILE__ENABLE='true'
  841. ;;
  842. default)
  843. # want to use config file defaults, do nothing
  844. ;;
  845. undefined)
  846. # value not set, do nothing
  847. ;;
  848. *)
  849. logerr "Unknown environment value for EMQX_LOG__TO=${log_to} discarded"
  850. ;;
  851. esac
  852. }
  853. maybe_log_to_console() {
  854. if [ "${EMQX_LOG__TO:-}" = 'default' ]; then
  855. # want to use defaults, do nothing
  856. unset EMQX_LOG__TO
  857. else
  858. tr_log_to_env
  859. export EMQX_DEFAULT_LOG_HANDLER=${EMQX_DEFAULT_LOG_HANDLER:-console}
  860. fi
  861. }
  862. # Warn the user if ulimit -n is less than 1024
  863. maybe_warn_ulimit() {
  864. ULIMIT_F=$(ulimit -n)
  865. if [ "$ULIMIT_F" -lt 1024 ]; then
  866. logwarn "ulimit -n is ${ULIMIT_F}; 1024 is the recommended minimum."
  867. fi
  868. }
  869. ## Possible ways to configure emqx node name:
  870. ## 1. configure node.name in emqx.conf
  871. ## 2. override with environment variable EMQX_NODE__NAME
  872. ## Node name is either short-name (without '@'), e.g. 'emqx'
  873. ## or long name (with '@') e.g. 'emqx@example.net' or 'emqx@127.0.0.1'
  874. NAME="${EMQX_NODE__NAME:-}"
  875. if [ -z "$NAME" ]; then
  876. NAME="$(get_boot_config 'node.name')"
  877. fi
  878. # force to use 'emqx' short name
  879. [ -z "$NAME" ] && NAME='emqx'
  880. case "$NAME" in
  881. *@*)
  882. NAME_TYPE='-name'
  883. ;;
  884. *)
  885. NAME_TYPE='-sname'
  886. esac
  887. SHORT_NAME="$(echo "$NAME" | awk -F'@' '{print $1}')"
  888. HOST_NAME="$(echo "$NAME" | awk -F'@' '{print $2}')"
  889. if ! (echo "$SHORT_NAME" | $GREP -q '^[0-9A-Za-z_\-]\+$'); then
  890. logerr "Invalid node name, should be of format '^[0-9A-Za-z_-]+$'."
  891. exit 1
  892. fi
  893. # This also changes the program name from 'beam.smp' to node name
  894. # e.g. the 'ps' command output
  895. export ESCRIPT_NAME="$SHORT_NAME"
  896. PIPE_DIR="${PIPE_DIR:-/$DATA_DIR/${WHOAMI}_erl_pipes/$NAME/}"
  897. ## Resolve Erlang cookie.
  898. if [ -n "${EMQX_NODE_COOKIE:-}" ]; then
  899. ## To be backward compatible, read and unset EMQX_NODE_COOKIE
  900. export EMQX_NODE__COOKIE="${EMQX_NODE_COOKIE}"
  901. unset EMQX_NODE_COOKIE
  902. fi
  903. COOKIE="${EMQX_NODE__COOKIE:-}"
  904. COOKIE_IN_USE="$(get_boot_config 'node.cookie')"
  905. if [ "$IS_BOOT_COMMAND" != 'yes' ] && [ -n "$COOKIE_IN_USE" ] && [ -n "$COOKIE" ] && [ "$COOKIE" != "$COOKIE_IN_USE" ]; then
  906. die "EMQX_NODE__COOKIE is different from the cookie used by $NAME"
  907. fi
  908. [ -z "$COOKIE" ] && COOKIE="$COOKIE_IN_USE"
  909. [ -z "$COOKIE" ] && COOKIE="$EMQX_DEFAULT_ERLANG_COOKIE"
  910. maybe_warn_default_cookie() {
  911. if [ $IS_BOOT_COMMAND = 'yes' ] && [ "$COOKIE" = "$EMQX_DEFAULT_ERLANG_COOKIE" ]; then
  912. logwarn "Default (insecure) Erlang cookie is in use."
  913. logwarn "Configure node.cookie in $EMQX_ETC_DIR/emqx.conf or override from environment variable EMQX_NODE__COOKIE"
  914. logwarn "NOTE: Use the same cookie for all nodes in the cluster."
  915. fi
  916. }
  917. ## check if OTP version has mnesia_hook feature; if not, fallback to
  918. ## using Mnesia DB backend.
  919. if [[ "$IS_BOOT_COMMAND" == 'yes' && "$(get_boot_config 'node.db_backend')" == "rlog" ]]; then
  920. if ! (echo -e "$COMPATIBILITY_INFO" | $GREP -q 'MNESIA_OK'); then
  921. logwarn "DB Backend is RLOG, but an incompatible OTP version has been detected. Falling back to using Mnesia DB backend."
  922. export EMQX_NODE__DB_BACKEND=mnesia
  923. export EMQX_NODE__ROLE=core
  924. fi
  925. fi
  926. diagnose_boot_failure_and_die() {
  927. local ps_line
  928. local app_status
  929. ps_line="$(find_emqx_process)"
  930. if [ -z "$ps_line" ]; then
  931. echo "Find more information in the latest log file: ${EMQX_LOG_DIR}/erlang.log.*"
  932. exit 1
  933. fi
  934. if ! relx_nodetool "ping" > /dev/null; then
  935. logerr "$NAME seems to be running, but not responding to pings."
  936. echo "Make sure '$HOST_NAME' is a resolvable and reachable hostname."
  937. pipe_shutdown
  938. exit 2
  939. fi
  940. app_status="$(relx_nodetool 'eval' 'emqx:is_running()')"
  941. if [ "$app_status" != 'true' ]; then
  942. logerr "$NAME node is started, but failed to complete the boot sequence in time."
  943. pipe_shutdown
  944. exit 3
  945. fi
  946. }
  947. ## Only works when started in daemon mode
  948. pipe_shutdown() {
  949. if [ -d "$PIPE_DIR" ]; then
  950. echo "Shutting down $NAME from to_erl pipe."
  951. ## can not evaluate init:stop() or erlang:halt() because the shell is restricted
  952. echo 'emqx_machine:brutal_shutdown().' | "$BINDIR/to_erl" "$PIPE_DIR"
  953. fi
  954. }
  955. ## Call nodetool to stop EMQX
  956. nodetool_shutdown() {
  957. # Wait for the node to completely stop...
  958. PID="$(relx_get_pid)"
  959. if ! relx_nodetool "stop"; then
  960. die "Graceful shutdown failed PID=[$PID]"
  961. fi
  962. WAIT_TIME="${EMQX_WAIT_FOR_STOP:-120}"
  963. if ! wait_for "$WAIT_TIME" 'is_down' "$PID"; then
  964. msg="dangling after ${WAIT_TIME} seconds"
  965. # also log to syslog
  966. logger -t "${REL_NAME}[${PID}]" "STOP: $msg"
  967. # log to user console
  968. set +x
  969. logerr "Stop failed, $msg"
  970. echo "ERROR: $PID is still around"
  971. ps -p "$PID"
  972. exit 1
  973. fi
  974. echo "ok"
  975. logger -t "${REL_NAME}[${PID}]" "STOP: OK"
  976. }
  977. cd "$BASE_RUNNER_ROOT_DIR"
  978. case "${COMMAND}" in
  979. start)
  980. maybe_warn_ulimit
  981. maybe_warn_default_cookie
  982. # this flag passes down to console mode
  983. # so we know it's intended to be run in daemon mode
  984. export _EMQX_START_DAEMON_MODE=1
  985. case "$COMMAND" in
  986. start)
  987. shift
  988. START_OPTION="console"
  989. HEART_OPTION="start"
  990. ;;
  991. esac
  992. RUN_PARAM="$*"
  993. # Set arguments for the heart command
  994. set -- "$RUNNER_SCRIPT" "$HEART_OPTION"
  995. [ "$RUN_PARAM" ] && set -- "$@" "$RUN_PARAM"
  996. # Export the HEART_COMMAND
  997. HEART_COMMAND="$RUNNER_SCRIPT $COMMAND"
  998. export HEART_COMMAND
  999. ## See: http://erlang.org/doc/man/run_erl.html
  1000. # Export the RUN_ERL_LOG_GENERATIONS
  1001. export RUN_ERL_LOG_GENERATIONS=${RUN_ERL_LOG_GENERATIONS:-"5"}
  1002. # Export the RUN_ERL_LOG_MAXSIZE
  1003. export RUN_ERL_LOG_MAXSIZE=${RUN_ERL_LOG_MAXSIZE:-"10485760"}
  1004. mkdir -p "$PIPE_DIR"
  1005. "$BINDIR/run_erl" -daemon "$PIPE_DIR" "$EMQX_LOG_DIR" \
  1006. "$(relx_start_command)"
  1007. WAIT_TIME=${EMQX_WAIT_FOR_START:-120}
  1008. if wait_until_return_val "true" "$WAIT_TIME" 'relx_nodetool' \
  1009. 'eval' 'emqx:is_running()'; then
  1010. echo "$EMQX_DESCRIPTION $REL_VSN is started successfully!"
  1011. exit 0
  1012. else
  1013. logerr "${EMQX_DESCRIPTION} ${REL_VSN} using node name '${NAME}' failed ${WAIT_TIME} probes."
  1014. diagnose_boot_failure_and_die
  1015. fi
  1016. ;;
  1017. stop)
  1018. if ! nodetool_shutdown; then
  1019. pipe_shutdown
  1020. fi
  1021. ;;
  1022. pid)
  1023. ## Get the VM's pid
  1024. if ! relx_get_pid; then
  1025. exit 1
  1026. fi
  1027. ;;
  1028. ping)
  1029. assert_node_alive
  1030. echo pong
  1031. ;;
  1032. escript)
  1033. ## Run an escript under the node's environment
  1034. if ! relx_escript "$@"; then
  1035. exit 1
  1036. fi
  1037. ;;
  1038. attach)
  1039. exec "$BINDIR/to_erl" "$PIPE_DIR"
  1040. ;;
  1041. remote_console)
  1042. assert_node_alive
  1043. shift
  1044. remsh
  1045. ;;
  1046. upgrade|downgrade|install|unpack|uninstall)
  1047. if [ -z "${2:-}" ]; then
  1048. echo "Missing version argument"
  1049. echo "Usage: $REL_NAME $COMMAND {version}"
  1050. exit 1
  1051. fi
  1052. shift
  1053. assert_node_alive
  1054. curr_vsn="$(current_script_version)"
  1055. target_vsn="$1"
  1056. newest_vsn="$(max_version_of "$target_vsn" "$curr_vsn")"
  1057. ensure_newest_script_is_extracted "$newest_vsn"
  1058. # if we are not the newest script, run the same command from it
  1059. if ! am_i_the_newest_script "$newest_vsn"; then
  1060. script_path="$(versioned_script_path emqx "$newest_vsn")"
  1061. exec "$script_path" "$COMMAND" "$@"
  1062. fi
  1063. upgrade_script_path="$(versioned_script_path install_upgrade.escript "$newest_vsn")"
  1064. echo "using ${upgrade_script_path} to run ${COMMAND} $*"
  1065. ERL_FLAGS="${ERL_FLAGS:-} $EPMD_ARGS" \
  1066. exec "$BINDIR/escript" "$upgrade_script_path" \
  1067. "$COMMAND" "{'$REL_NAME', \"$NAME_TYPE\", '$NAME', '$COOKIE'}" "$@"
  1068. ;;
  1069. versions)
  1070. assert_node_alive
  1071. shift
  1072. ERL_FLAGS="${ERL_FLAGS:-} $EPMD_ARGS" \
  1073. exec "$BINDIR/escript" "$RUNNER_ROOT_DIR/bin/install_upgrade.escript" \
  1074. "versions" "{'$REL_NAME', \"$NAME_TYPE\", '$NAME', '$COOKIE'}" "$@"
  1075. ;;
  1076. console|console_clean|foreground)
  1077. # .boot file typically just $REL_NAME (ie, the app name)
  1078. # however, for debugging, sometimes start_clean.boot is useful.
  1079. # For e.g. 'setup', one may even want to name another boot script.
  1080. case "$COMMAND" in
  1081. console|foreground)
  1082. if [ -f "$REL_DIR/$REL_NAME.boot" ]; then
  1083. BOOTFILE="$REL_DIR/$REL_NAME"
  1084. else
  1085. BOOTFILE="$REL_DIR/start"
  1086. fi
  1087. ;;
  1088. console_clean)
  1089. BOOTFILE="$REL_DIR/start_clean"
  1090. ;;
  1091. esac
  1092. case "$COMMAND" in
  1093. foreground)
  1094. FOREGROUNDOPTIONS="-enable-feature maybe_expr -noinput -noshell +Bd"
  1095. ;;
  1096. *)
  1097. FOREGROUNDOPTIONS='-enable-feature maybe_expr'
  1098. ;;
  1099. esac
  1100. # set before generate_config
  1101. if [ "${_EMQX_START_DAEMON_MODE:-}" = 1 ]; then
  1102. tr_log_to_env
  1103. else
  1104. maybe_log_to_console
  1105. maybe_warn_ulimit
  1106. maybe_warn_default_cookie
  1107. fi
  1108. #generate app.config and vm.args
  1109. generate_config "$NAME_TYPE" "$NAME"
  1110. check_license
  1111. # Setup beam-required vars
  1112. EMU="beam"
  1113. PROGNAME="${0}"
  1114. export EMU
  1115. export PROGNAME
  1116. # Store passed arguments since they will be erased by `set`
  1117. ARGS="$*"
  1118. # shellcheck disable=SC2086
  1119. # Build an array of arguments to pass to exec later on
  1120. # Build it here because this command will be used for logging.
  1121. if [ "$IS_ELIXIR" = no ] || [ "${EMQX_CONSOLE_FLAVOR:-}" = 'erl' ] ; then
  1122. if [[ "$DEBUG" == 2 ]]; then
  1123. INIT_DEBUG_ARG="-init_debug"
  1124. else
  1125. INIT_DEBUG_ARG=""
  1126. fi
  1127. # pass down RELEASE_LIB so we can switch to IS_ELIXIR=no
  1128. # to boot an Erlang node from the elixir release
  1129. set -- "$BINDIR/erlexec" \
  1130. $FOREGROUNDOPTIONS \
  1131. -boot "$BOOTFILE" \
  1132. -boot_var RELEASE_LIB "$ERTS_LIB_DIR" \
  1133. -boot_var ERTS_LIB_DIR "$ERTS_LIB_DIR" \
  1134. -mode "$CODE_LOADING_MODE" \
  1135. -config "$CONF_FILE" \
  1136. -args_file "$ARGS_FILE" \
  1137. $INIT_DEBUG_ARG \
  1138. $EPMD_ARGS
  1139. else
  1140. if [[ "$DEBUG" == 2 ]]; then
  1141. INIT_DEBUG_ARG="--erl -init_debug"
  1142. else
  1143. INIT_DEBUG_ARG=""
  1144. fi
  1145. set -- "$REL_DIR/iex" \
  1146. --boot "$BOOTFILE" \
  1147. --boot-var RELEASE_LIB "${ERTS_LIB_DIR}" \
  1148. --erl-config "${CONF_FILE}" \
  1149. --vm-args "${ARGS_FILE}" \
  1150. --erl "$FOREGROUNDOPTIONS" \
  1151. --erl "-mode $CODE_LOADING_MODE" \
  1152. --erl "$EPMD_ARGS" \
  1153. $INIT_DEBUG_ARG \
  1154. --werl
  1155. fi
  1156. # Log the startup
  1157. logger -t "${REL_NAME}[$$]" "EXEC: $* -- ${1+$ARGS} -ekka_proto_dist ${EKKA_PROTO_DIST_MOD} -emqx_data_dir ${DATA_DIR}"
  1158. # Start the VM
  1159. # add ekka_proto_dist emqx_data_dir to boot command so it is visible from 'ps -ef'
  1160. # NTOE: order matters! emqx_data_dir has to be positioned at the end of the line to simplify the
  1161. # line parsing when file path contains spaces
  1162. exec "$@" -- ${1+$ARGS} -ekka_proto_dist "${EKKA_PROTO_DIST_MOD}" -emqx_data_dir "${DATA_DIR}"
  1163. ;;
  1164. ctl)
  1165. assert_node_alive
  1166. shift
  1167. relx_nodetool rpc_infinity emqx_ctl run_command "$@"
  1168. ;;
  1169. rpc)
  1170. assert_node_alive
  1171. shift
  1172. relx_nodetool rpc "$@"
  1173. ;;
  1174. rpcterms)
  1175. assert_node_alive
  1176. shift
  1177. relx_nodetool rpcterms "$@"
  1178. ;;
  1179. eval)
  1180. assert_node_alive
  1181. shift
  1182. relx_nodetool "eval" "$@"
  1183. ;;
  1184. eval-ex)
  1185. assert_node_alive
  1186. shift
  1187. if [ "$IS_ELIXIR" = "yes" ]
  1188. then
  1189. "$REL_DIR/elixir" \
  1190. --hidden \
  1191. --name "rand-$(gen_node_id)-$NAME" \
  1192. --cookie "$COOKIE" \
  1193. --boot "$REL_DIR/start_clean" \
  1194. --boot-var RELEASE_LIB "$ERTS_LIB_DIR" \
  1195. --vm-args "$REL_DIR/remote.vm.args" \
  1196. --erl "-start_epmd false" \
  1197. --erl "-epmd_module ekka_epmd" \
  1198. --erl "+P 65536" \
  1199. --erl "+Q 65536" \
  1200. --erl "+S 2" \
  1201. --rpc-eval "$NAME" "$@"
  1202. else
  1203. echo "EMQX node is not an Elixir node"
  1204. usage "$COMMAND"
  1205. exit 1
  1206. fi
  1207. ;;
  1208. check_config)
  1209. check_config
  1210. ;;
  1211. *)
  1212. usage "$COMMAND"
  1213. exit 1
  1214. ;;
  1215. esac
  1216. exit 0