emqx 25 KB

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