emqx 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733
  1. #!/bin/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 EMQ X 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. # Use $CWD/etc/sys.config if exists
  272. if [ -z "$RELX_CONFIG_PATH" ]; then
  273. if [ -f "$RUNNER_ETC_DIR/sys.config" ]; then
  274. RELX_CONFIG_PATH="-config $RUNNER_ETC_DIR/sys.config"
  275. else
  276. RELX_CONFIG_PATH=""
  277. fi
  278. fi
  279. IS_BOOT_COMMAND='no'
  280. case "$1" in
  281. start|start_boot)
  282. IS_BOOT_COMMAND='yes'
  283. ;;
  284. console|console_clean|console_boot)
  285. IS_BOOT_COMMAND='yes'
  286. ;;
  287. foreground)
  288. IS_BOOT_COMMAND='yes'
  289. ;;
  290. esac
  291. if [ -z "$NAME_ARG" ]; then
  292. NODENAME="${EMQX_NODE_NAME:-}"
  293. # compatible with docker entrypoint
  294. [ -z "$NODENAME" ] && [ -n "$EMQX_NAME" ] && [ -n "$EMQX_HOST" ] && NODENAME="${EMQX_NAME}@${EMQX_HOST}"
  295. if [ -z "$NODENAME" ]; then
  296. if [ "$IS_BOOT_COMMAND" = 'no' ]; then
  297. # for non-boot commands, inspect vm.<time>.args for node name
  298. # shellcheck disable=SC2012,SC2086
  299. LATEST_VM_ARGS="$(ls -t $RUNNER_DATA_DIR/configs/vm.*.args | head -1)"
  300. if [ -z "$LATEST_VM_ARGS" ]; then
  301. echo "For command $1, there is no vm.*.args config file found in $RUNNER_DATA_DIR/configs/"
  302. exit 1
  303. fi
  304. NODENAME="$(grep -E '^-name' "$LATEST_VM_ARGS" | awk '{print $2}')"
  305. else
  306. # for boot commands, inspect emqx.conf for node name
  307. NODENAME=$("$ERTS_PATH"/escript "$RUNNER_ROOT_DIR"/bin/cuttlefish -i "$REL_DIR"/emqx.schema -c "$RUNNER_ETC_DIR"/emqx.conf get node.name)
  308. fi
  309. fi
  310. if [ -z "$NODENAME" ]; then
  311. echoerr "Failed to resolve emqx node name"
  312. if [ "$IS_BOOT_COMMAND" = 'yes' ]; then
  313. echoerr "Make sure runner has read permission on '$RUNNER_ETC_DIR/emqx.conf'"
  314. fi
  315. echoerr "Maybe override node name with environment variable ENQX_NODE_NAME='name@host.name'"
  316. echoerr "or, EMQX_NAME='name' and EMQX_HOST='host.name'"
  317. exit 1
  318. fi
  319. NAME_ARG="-name ${NODENAME# *}"
  320. fi
  321. # Extract the name type and name from the NAME_ARG for REMSH
  322. NAME_TYPE="$(echo "$NAME_ARG" | awk '{print $1}')"
  323. NAME="$(echo "$NAME_ARG" | awk '{print $2}')"
  324. NODENAME="$(echo "$NAME" | awk -F'@' '{print $1}')"
  325. export ESCRIPT_NAME="$NODENAME"
  326. PIPE_DIR="${PIPE_DIR:-/$RUNNER_DATA_DIR/${WHOAMI}_erl_pipes/$NAME/}"
  327. COOKIE="${EMQX_NODE_COOKIE:-}"
  328. if [ -z "$COOKIE" ]; then
  329. if [ "$IS_BOOT_COMMAND" = 'yes' ]; then
  330. COOKIE=$("$ERTS_PATH"/escript "$RUNNER_ROOT_DIR"/bin/cuttlefish -i "$REL_DIR"/emqx.schema -c "$RUNNER_ETC_DIR"/emqx.conf get node.cookie)
  331. else
  332. # shellcheck disable=SC2012,SC2086
  333. LATEST_VM_ARGS="$(ls -t $RUNNER_DATA_DIR/configs/vm.*.args | head -1)"
  334. if [ -z "$LATEST_VM_ARGS" ]; then
  335. echo "For command $1, there is no vm.*.args config file found in $RUNNER_DATA_DIR/configs/"
  336. exit 1
  337. fi
  338. COOKIE="$(grep -E '^-setcookie' "$LATEST_VM_ARGS" | awk '{print $2}')"
  339. fi
  340. fi
  341. if [ -z "$COOKIE" ]; then
  342. echoerr "Please set node.cookie in $RUNNER_ETC_DIR/emqx.conf or override from environment variable EMQX_NODE_COOKIE"
  343. exit 1
  344. fi
  345. # Support for IPv6 Dist. See: https://github.com/emqtt/emqttd/issues/1460
  346. PROTO_DIST=$(grep -E '^[ \t]*cluster.proto_dist[ \t]*=[ \t]*' "$RUNNER_ETC_DIR/emqx.conf" 2> /dev/null | tail -1 | awk -F"= " '{print $NF}')
  347. if [ -z "$PROTO_DIST" ]; then
  348. PROTO_DIST_ARG=""
  349. else
  350. PROTO_DIST_ARG="-proto_dist $PROTO_DIST"
  351. fi
  352. cd "$ROOTDIR"
  353. # User can specify an sname without @hostname
  354. # This will fail when creating remote shell
  355. # So here we check for @ and add @hostname if missing
  356. case $NAME in
  357. *@*)
  358. # Nothing to do
  359. ;;
  360. *)
  361. NAME=$NAME@$(relx_get_nodename)
  362. ;;
  363. esac
  364. MNESIA_DATA_DIR="$RUNNER_DATA_DIR/mnesia/$NAME"
  365. # Check the first argument for instructions
  366. case "$1" in
  367. start|start_boot)
  368. # Make sure a node IS not running
  369. if relx_nodetool "ping" >/dev/null 2>&1; then
  370. echo "Node is already running!"
  371. exit 1
  372. fi
  373. # Bootstrap daemon command (check perms & drop to $RUNNER_USER)
  374. bootstrapd
  375. # this flag passes down to console mode
  376. # so we know it's intended to be run in daemon mode
  377. export _EMQX_START_MODE="$1"
  378. # Save this for later.
  379. CMD=$1
  380. case "$1" in
  381. start)
  382. shift
  383. START_OPTION="console"
  384. HEART_OPTION="start"
  385. ;;
  386. start_boot)
  387. shift
  388. START_OPTION="console_boot"
  389. HEART_OPTION="start_boot"
  390. ;;
  391. esac
  392. RUN_PARAM="$*"
  393. # Set arguments for the heart command
  394. set -- "$RUNNER_SCRIPT" "$HEART_OPTION"
  395. [ "$RUN_PARAM" ] && set -- "$@" "$RUN_PARAM"
  396. # Export the HEART_COMMAND
  397. HEART_COMMAND="$RUNNER_SCRIPT $CMD"
  398. export HEART_COMMAND
  399. ## See: http://erlang.org/doc/man/run_erl.html
  400. # Export the RUN_ERL_LOG_GENERATIONS
  401. export RUN_ERL_LOG_GENERATIONS=${RUN_ERL_LOG_GENERATIONS:-"5"}
  402. # Export the RUN_ERL_LOG_MAXSIZE
  403. export RUN_ERL_LOG_MAXSIZE=${RUN_ERL_LOG_MAXSIZE:-"10485760"}
  404. mkdir -p "$PIPE_DIR"
  405. "$BINDIR/run_erl" -daemon "$PIPE_DIR" "$RUNNER_LOG_DIR" \
  406. "$(relx_start_command)"
  407. WAIT_TIME=${WAIT_FOR_ERLANG:-15}
  408. while [ "$WAIT_TIME" -gt 0 ]; do
  409. if ! relx_nodetool "ping" >/dev/null 2>&1; then
  410. WAIT_TIME=$((WAIT_TIME - 1))
  411. sleep 1
  412. continue
  413. fi
  414. sleep 1
  415. if relx_nodetool "ping" >/dev/null 2>&1; then
  416. echo "$EMQX_DESCRIPTION $REL_VSN is started successfully!"
  417. exit 0
  418. fi
  419. done && echo "$EMQX_DESCRIPTION $REL_VSN failed to start within ${WAIT_FOR_ERLANG:-15} seconds,"
  420. echo "see the output of '$0 console' for more information."
  421. echo "If you want to wait longer, set the environment variable"
  422. echo "WAIT_FOR_ERLANG to the number of seconds to wait."
  423. exit 1
  424. ;;
  425. stop)
  426. # Wait for the node to completely stop...
  427. PID="$(relx_get_pid)"
  428. if ! relx_nodetool "stop"; then
  429. exit 1
  430. fi
  431. while kill -s 0 "$PID" 2>/dev/null; do
  432. sleep 1
  433. done
  434. ;;
  435. restart|reboot)
  436. echo "$EMQX_DESCRIPTION $REL_VSN is stopped: $("$RUNNER_BIN_DIR"/emqx stop)"
  437. "$RUNNER_BIN_DIR"/emqx start
  438. ;;
  439. pid)
  440. ## Get the VM's pid
  441. if ! relx_get_pid; then
  442. exit 1
  443. fi
  444. ;;
  445. ping)
  446. ## See if the VM is alive
  447. if ! relx_nodetool "ping"; then
  448. exit 1
  449. fi
  450. ;;
  451. escript)
  452. ## Run an escript under the node's environment
  453. if ! relx_escript "$@"; then
  454. exit 1
  455. fi
  456. ;;
  457. attach)
  458. # Make sure a node IS running
  459. if ! relx_nodetool "ping" > /dev/null; then
  460. echo "Node is not running!"
  461. exit 1
  462. fi
  463. # Bootstrap daemon command (check perms & drop to $RUNNER_USER)
  464. bootstrapd
  465. shift
  466. exec "$BINDIR/to_erl" "$PIPE_DIR"
  467. ;;
  468. remote_console)
  469. # Make sure a node IS running
  470. if ! relx_nodetool "ping" > /dev/null; then
  471. echo "Node is not running!"
  472. exit 1
  473. fi
  474. # Bootstrap daemon command (check perms & drop to $RUNNER_USER)
  475. bootstrapd
  476. shift
  477. relx_rem_sh
  478. ;;
  479. upgrade|downgrade|install|unpack|uninstall)
  480. if [ -z "$2" ]; then
  481. echo "Missing version argument"
  482. echo "Usage: $REL_NAME $1 {version}"
  483. exit 1
  484. fi
  485. COMMAND="$1"; shift
  486. # Make sure a node IS running
  487. if ! relx_nodetool "ping" > /dev/null; then
  488. echo "Node is not running!"
  489. exit 1
  490. fi
  491. ERL_FLAGS="$ERL_FLAGS $EPMD_ARG" \
  492. exec "$BINDIR/escript" "$ROOTDIR/bin/install_upgrade.escript" \
  493. "$COMMAND" "{'$REL_NAME', \"$NAME_TYPE\", '$NAME', '$COOKIE'}" "$@"
  494. ;;
  495. versions)
  496. # Make sure a node IS running
  497. if ! relx_nodetool "ping" > /dev/null; then
  498. echo "Node is not running!"
  499. exit 1
  500. fi
  501. COMMAND="$1"; shift
  502. ERL_FLAGS="$ERL_FLAGS $EPMD_ARG" \
  503. exec "$BINDIR/escript" "$ROOTDIR/bin/install_upgrade.escript" \
  504. "versions" "{'$REL_NAME', \"$NAME_TYPE\", '$NAME', '$COOKIE'}" "$@"
  505. ;;
  506. console|console_clean|console_boot)
  507. # Bootstrap daemon command (check perms & drop to $RUNNER_USER)
  508. bootstrapd
  509. # .boot file typically just $REL_NAME (ie, the app name)
  510. # however, for debugging, sometimes start_clean.boot is useful.
  511. # For e.g. 'setup', one may even want to name another boot script.
  512. case "$1" in
  513. console)
  514. if [ -f "$REL_DIR/$REL_NAME.boot" ]; then
  515. BOOTFILE="$REL_DIR/$REL_NAME"
  516. else
  517. BOOTFILE="$REL_DIR/start"
  518. fi
  519. ;;
  520. console_clean)
  521. BOOTFILE="$REL_DIR/start_clean"
  522. ;;
  523. console_boot)
  524. shift
  525. BOOTFILE="$1"
  526. shift
  527. ;;
  528. esac
  529. # set before generate_config
  530. if [ "${_EMQX_START_MODE:-}" = '' ]; then
  531. export EMQX_LOG__TO="${EMQX_LOG__TO:-console}"
  532. fi
  533. #generate app.config and vm.args
  534. generate_config
  535. # Setup beam-required vars
  536. EMU="beam"
  537. PROGNAME="${0#*/}"
  538. export EMU
  539. export PROGNAME
  540. # Store passed arguments since they will be erased by `set`
  541. ARGS="$*"
  542. # shellcheck disable=SC2086 # $RELX_CONFIG_PATH $CONFIG_ARGS $EPMD_ARG are supposed to be split by whitespace
  543. # Build an array of arguments to pass to exec later on
  544. # Build it here because this command will be used for logging.
  545. set -- "$BINDIR/erlexec" \
  546. -boot "$BOOTFILE" -mode "$CODE_LOADING_MODE" \
  547. -boot_var ERTS_LIB_DIR "$ERTS_LIB_DIR" \
  548. -mnesia dir "\"${MNESIA_DATA_DIR}\"" \
  549. $RELX_CONFIG_PATH $CONFIG_ARGS $EPMD_ARG
  550. # Log the startup
  551. logger -t "${REL_NAME}[$$]" "$* -- ${1+$ARGS}"
  552. # Start the VM
  553. exec "$@" -- ${1+$ARGS}
  554. ;;
  555. foreground)
  556. # Bootstrap daemon command (check perms & drop to $RUNNER_USER)
  557. bootstrapd
  558. # start up the release in the foreground for use by runit
  559. # or other supervision services
  560. # set before generate_config
  561. export EMQX_LOG__TO="${EMQX_LOG__TO:-console}"
  562. #generate app.config and vm.args
  563. generate_config
  564. [ -f "$REL_DIR/$REL_NAME.boot" ] && BOOTFILE="$REL_NAME" || BOOTFILE=start
  565. FOREGROUNDOPTIONS="-noshell -noinput +Bd"
  566. # Setup beam-required vars
  567. EMU=beam
  568. PROGNAME="${0#*/}"
  569. export EMU
  570. export PROGNAME
  571. # Store passed arguments since they will be erased by `set`
  572. ARGS="$*"
  573. # shellcheck disable=SC2086 # $RELX_CONFIG_PATH $CONFIG_ARGS $EPMD_ARG are supposed to be split by whitespace
  574. # Build an array of arguments to pass to exec later on
  575. # Build it here because this command will be used for logging.
  576. set -- "$BINDIR/erlexec" $FOREGROUNDOPTIONS \
  577. -boot "$REL_DIR/$BOOTFILE" -mode "$CODE_LOADING_MODE" \
  578. -boot_var ERTS_LIB_DIR "$ERTS_LIB_DIR" \
  579. -mnesia dir "\"${MNESIA_DATA_DIR}\"" \
  580. $RELX_CONFIG_PATH $CONFIG_ARGS $EPMD_ARG
  581. # Log the startup
  582. logger -t "${REL_NAME}[$$]" "$* -- ${1+$ARGS}"
  583. # Start the VM
  584. exec "$@" -- ${1+$ARGS}
  585. ;;
  586. ertspath)
  587. echo "$ERTS_PATH"
  588. ;;
  589. rpc)
  590. # Make sure a node IS running
  591. if ! relx_nodetool "ping" > /dev/null; then
  592. echo "Node is not running!"
  593. exit 1
  594. fi
  595. shift
  596. relx_nodetool rpc "$@"
  597. ;;
  598. rpcterms)
  599. # Make sure a node IS running
  600. if ! relx_nodetool "ping" > /dev/null; then
  601. echo "Node is not running!"
  602. exit 1
  603. fi
  604. shift
  605. relx_nodetool rpcterms "$@"
  606. ;;
  607. root_dir)
  608. # Make sure a node IS running
  609. if ! relx_nodetool "ping" > /dev/null; then
  610. echo "Node is not running!"
  611. exit 1
  612. fi
  613. shift
  614. relx_nodetool "eval" 'code:root_dir()'
  615. ;;
  616. eval)
  617. # Make sure a node IS running
  618. if ! relx_nodetool "ping" > /dev/null; then
  619. echo "Node is not running!"
  620. exit 1
  621. fi
  622. shift
  623. relx_nodetool "eval" "$@"
  624. ;;
  625. *)
  626. relx_usage "$1"
  627. exit 1
  628. ;;
  629. esac
  630. exit 0