emqx 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718
  1. #!/bin/bash
  2. # -*- tab-width:4;indent-tabs-mode:nil -*-
  3. # ex: ts=4 sw=4 et
  4. set -e
  5. set -o pipefail
  6. DEBUG="${DEBUG:-0}"
  7. if [ "$DEBUG" -eq 1 ]; then
  8. set -x
  9. fi
  10. ROOT_DIR="$(cd "$(dirname "$(readlink "$0" || echo "$0")")"/..; pwd -P)"
  11. # shellcheck disable=SC1090
  12. . "$ROOT_DIR"/releases/emqx_vars
  13. RUNNER_SCRIPT="$RUNNER_BIN_DIR/$REL_NAME"
  14. CODE_LOADING_MODE="${CODE_LOADING_MODE:-embedded}"
  15. REL_DIR="$RUNNER_ROOT_DIR/releases/$REL_VSN"
  16. SCHEMA_MOD=emqx_machine_schema
  17. WHOAMI=$(whoami)
  18. # Make sure log directory exists
  19. mkdir -p "$RUNNER_LOG_DIR"
  20. # Make sure data directory exists
  21. mkdir -p "$RUNNER_DATA_DIR"
  22. # Make sure data/configs exists
  23. CONFIGS_DIR="$RUNNER_DATA_DIR/configs"
  24. mkdir -p "$CONFIGS_DIR"
  25. # hocon try to read environment variables starting with "EMQX_"
  26. export HOCON_ENV_OVERRIDE_PREFIX='EMQX_'
  27. export ROOTDIR="$RUNNER_ROOT_DIR"
  28. export ERTS_DIR="$ROOTDIR/erts-$ERTS_VSN"
  29. export BINDIR="$ERTS_DIR/bin"
  30. export EMU="beam"
  31. export PROGNAME="erl"
  32. export LD_LIBRARY_PATH="$ERTS_DIR/lib:$LD_LIBRARY_PATH"
  33. export ERTS_LIB_DIR="$ERTS_DIR/../lib"
  34. MNESIA_DATA_DIR="$RUNNER_DATA_DIR/mnesia/$NAME"
  35. # Echo to stderr on errors
  36. echoerr() { echo "ERROR: $*" 1>&2; }
  37. die() {
  38. echoerr "ERROR: $1"
  39. errno=${2:-1}
  40. exit "$errno"
  41. }
  42. assert_node_alive() {
  43. if ! relx_nodetool "ping" > /dev/null; then
  44. die "node_is_not_running!" 1
  45. fi
  46. }
  47. relx_usage() {
  48. command="$1"
  49. case "$command" in
  50. unpack)
  51. echo "Usage: $REL_NAME unpack [VERSION]"
  52. echo "Unpacks a release package VERSION, it assumes that this"
  53. echo "release package tarball has already been deployed at one"
  54. echo "of the following locations:"
  55. echo " releases/<relname>-<version>.tar.gz"
  56. echo " releases/<relname>-<version>.zip"
  57. ;;
  58. install)
  59. echo "Usage: $REL_NAME install [VERSION]"
  60. echo "Installs a release package VERSION, it assumes that this"
  61. echo "release package tarball has already been deployed at one"
  62. echo "of the following locations:"
  63. echo " releases/<relname>-<version>.tar.gz"
  64. echo " releases/<relname>-<version>.zip"
  65. echo ""
  66. echo " --no-permanent Install release package VERSION but"
  67. echo " don't make it permanent"
  68. ;;
  69. uninstall)
  70. echo "Usage: $REL_NAME uninstall [VERSION]"
  71. echo "Uninstalls a release VERSION, it will only accept"
  72. echo "versions that are not currently in use"
  73. ;;
  74. upgrade)
  75. echo "Usage: $REL_NAME upgrade [VERSION]"
  76. echo "Upgrades the currently running release to VERSION, it assumes"
  77. echo "that a release package tarball has already been deployed at one"
  78. echo "of the following locations:"
  79. echo " releases/<relname>-<version>.tar.gz"
  80. echo " releases/<relname>-<version>.zip"
  81. echo ""
  82. echo " --no-permanent Install release package VERSION but"
  83. echo " don't make it permanent"
  84. ;;
  85. downgrade)
  86. echo "Usage: $REL_NAME downgrade [VERSION]"
  87. echo "Downgrades the currently running release to VERSION, it assumes"
  88. echo "that a release package tarball has already been deployed at one"
  89. echo "of the following locations:"
  90. echo " releases/<relname>-<version>.tar.gz"
  91. echo " releases/<relname>-<version>.zip"
  92. echo ""
  93. echo " --no-permanent Install release package VERSION but"
  94. echo " don't make it permanent"
  95. ;;
  96. *)
  97. echo "Usage: $REL_NAME {start|start_boot <file>|ertspath|foreground|stop|pid|ping|console|console_clean|console_boot <file>|attach|remote_console|upgrade|downgrade|install|uninstall|versions|escript|ctl|rpc|rpcterms|eval|root_dir}"
  98. ;;
  99. esac
  100. }
  101. # Simple way to check the correct user and fail early
  102. check_user() {
  103. # Validate that the user running the script is the owner of the
  104. # RUN_DIR.
  105. if [ "$RUNNER_USER" ] && [ "x$WHOAMI" != "x$RUNNER_USER" ]; then
  106. if [ "x$WHOAMI" != "xroot" ]; then
  107. echo "You need to be root or use sudo to run this command"
  108. exit 1
  109. fi
  110. CMD="DEBUG=$DEBUG \"$RUNNER_SCRIPT\" "
  111. for ARG in "$@"; do
  112. CMD="${CMD} \"$ARG\""
  113. done
  114. # This will drop priviledges into the runner user
  115. # It exec's in a new shell and the current shell will exit
  116. exec su - "$RUNNER_USER" -c "$CMD"
  117. fi
  118. }
  119. # Make sure the user running this script is the owner and/or su to that user
  120. check_user "$@"
  121. ES=$?
  122. if [ "$ES" -ne 0 ]; then
  123. exit $ES
  124. fi
  125. if [ -z "$WITH_EPMD" ]; then
  126. EPMD_ARG="-start_epmd false -epmd_module ekka_epmd -proto_dist ekka"
  127. else
  128. EPMD_ARG="-start_epmd true $PROTO_DIST_ARG"
  129. fi
  130. # Warn the user if ulimit -n is less than 1024
  131. ULIMIT_F=$(ulimit -n)
  132. if [ "$ULIMIT_F" -lt 1024 ]; then
  133. echo "!!!!"
  134. echo "!!!! WARNING: ulimit -n is ${ULIMIT_F}; 1024 is the recommended minimum."
  135. echo "!!!!"
  136. fi
  137. SED_REPLACE="sed -i "
  138. case $(sed --help 2>&1) in
  139. *GNU*) SED_REPLACE="sed -i ";;
  140. *BusyBox*) SED_REPLACE="sed -i ";;
  141. *) SED_REPLACE="sed -i '' ";;
  142. esac
  143. # Get node pid
  144. relx_get_pid() {
  145. if output="$(relx_nodetool rpcterms os getpid)"
  146. then
  147. # shellcheck disable=SC2001 # Escaped quote taken as closing quote in editor
  148. echo "$output" | sed -e 's/"//g'
  149. return 0
  150. else
  151. echo "$output"
  152. return 1
  153. fi
  154. }
  155. # Connect to a remote node
  156. relx_rem_sh() {
  157. # Generate a unique id used to allow multiple remsh to the same node
  158. # transparently
  159. id="remsh$(relx_gen_id)-${NAME}"
  160. # Get the node's ticktime so that we use the same thing.
  161. TICKTIME="$(relx_nodetool rpcterms net_kernel get_net_ticktime)"
  162. # shellcheck disable=SC2086 # $EPMD_ARG is supposed to be split by whitespace
  163. # Setup remote shell command to control node
  164. exec "$BINDIR/erl" "$NAME_TYPE" "$id" -remsh "$NAME" -boot "$REL_DIR/start_clean" \
  165. -boot_var ERTS_LIB_DIR "$ERTS_LIB_DIR" \
  166. -setcookie "$COOKIE" -hidden -kernel net_ticktime "$TICKTIME" $EPMD_ARG
  167. }
  168. # Generate a random id
  169. relx_gen_id() {
  170. od -t x -N 4 /dev/urandom | head -n1 | awk '{print $2}'
  171. }
  172. # Control a node
  173. relx_nodetool() {
  174. command="$1"; shift
  175. export RUNNER_ROOT_DIR
  176. export REL_VSN
  177. ERL_FLAGS="$ERL_FLAGS $EPMD_ARG" \
  178. "$ERTS_DIR/bin/escript" "$ROOTDIR/bin/nodetool" "$NAME_TYPE" "$NAME" \
  179. -setcookie "$COOKIE" "$command" "$@"
  180. }
  181. call_hocon() {
  182. export RUNNER_ROOT_DIR
  183. export RUNNER_ETC_DIR
  184. export REL_VSN
  185. "$ERTS_DIR/bin/escript" "$ROOTDIR/bin/nodetool" hocon "$@" \
  186. || die "call_hocon_failed: $*" $?
  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. local name_type="$1"
  202. local node_name="$2"
  203. ## Delete the *.siz files first or it cann't start after
  204. ## changing the config 'log.rotation.size'
  205. rm -rf "${RUNNER_LOG_DIR}"/*.siz
  206. EMQX_LICENSE_CONF_OPTION=""
  207. if [ "${EMQX_LICENSE_CONF:-}" != "" ]; then
  208. EMQX_LICENSE_CONF_OPTION="-c ${EMQX_LICENSE_CONF}"
  209. fi
  210. ## timestamp for each generation
  211. local NOW_TIME
  212. NOW_TIME="$(call_hocon now_time)"
  213. ## ths command populates two files: app.<time>.config and vm.<time>.args
  214. ## disable SC2086 to allow EMQX_LICENSE_CONF_OPTION to split
  215. # shellcheck disable=SC2086
  216. call_hocon -v -t "$NOW_TIME" -s $SCHEMA_MOD -c "$RUNNER_ETC_DIR"/emqx.conf $EMQX_LICENSE_CONF_OPTION -d "$RUNNER_DATA_DIR"/configs generate
  217. ## filenames are per-hocon convention
  218. local CONF_FILE="$CONFIGS_DIR/app.$NOW_TIME.config"
  219. local HOCON_GEN_ARG_FILE="$CONFIGS_DIR/vm.$NOW_TIME.args"
  220. CONFIG_ARGS="-config $CONF_FILE -args_file $HOCON_GEN_ARG_FILE"
  221. ## Merge hocon generated *.args into the vm.args
  222. TMP_ARG_FILE="$CONFIGS_DIR/vm.args.tmp"
  223. cp "$RUNNER_ETC_DIR/vm.args" "$TMP_ARG_FILE"
  224. echo "" >> "$TMP_ARG_FILE"
  225. echo "-pa ${REL_DIR}/consolidated" >> "$TMP_ARG_FILE"
  226. ## read lines from generated vm.<time>.args file
  227. ## drop comment lines, and empty lines using sed
  228. ## pipe the lines to a while loop
  229. sed '/^#/d' "$HOCON_GEN_ARG_FILE" | sed '/^$/d' | while IFS='' read -r ARG_LINE || [ -n "$ARG_LINE" ]; do
  230. ## in the loop, split the 'key[:space:]value' pair
  231. ARG_KEY=$(echo "$ARG_LINE" | awk '{$NF="";print}')
  232. ARG_VALUE=$(echo "$ARG_LINE" | awk '{print $NF}')
  233. ## use the key to look up in vm.args file for the value
  234. TMP_ARG_VALUE=$(grep "^$ARG_KEY" "$TMP_ARG_FILE" || true | awk '{print $NF}')
  235. ## compare generated (to override) value to original (to be overriden) value
  236. if [ "$ARG_VALUE" != "$TMP_ARG_VALUE" ] ; then
  237. ## if they are different
  238. if [ -n "$TMP_ARG_VALUE" ]; then
  239. ## if the old value is present, replace it with generated value
  240. sh -c "$SED_REPLACE 's|^$ARG_KEY.*$|$ARG_LINE|' $TMP_ARG_FILE"
  241. else
  242. ## otherwise append generated value to the end
  243. echo "$ARG_LINE" >> "$TMP_ARG_FILE"
  244. fi
  245. fi
  246. done
  247. echo "$name_type $node_name" >> "$TMP_ARG_FILE"
  248. ## rename the generated vm.<time>.args file
  249. mv -f "$TMP_ARG_FILE" "$HOCON_GEN_ARG_FILE"
  250. # shellcheck disable=SC2086
  251. if ! relx_nodetool chkconfig $CONFIG_ARGS; then
  252. die "failed_to_check_config $CONFIG_ARGS"
  253. fi
  254. }
  255. # Call bootstrapd for daemon commands like start/stop/console
  256. bootstrapd() {
  257. if [ -e "$RUNNER_DATA_DIR/.erlang.cookie" ]; then
  258. chown "$RUNNER_USER" "$RUNNER_DATA_DIR"/.erlang.cookie
  259. fi
  260. }
  261. # check if a PID is down
  262. is_down() {
  263. PID="$1"
  264. if ps -p "$PID" >/dev/null; then
  265. # still around
  266. # shellcheck disable=SC2009 # this grep pattern is not a part of the progra names
  267. if ps -p "$PID" | grep -q 'defunct'; then
  268. # zombie state, print parent pid
  269. parent="$(ps -o ppid= -p "$PID" | tr -d ' ')"
  270. echo "WARN: $PID is marked <defunct>, parent:"
  271. ps -p "$parent"
  272. return 0
  273. fi
  274. return 1
  275. fi
  276. # it's gone
  277. return 0
  278. }
  279. wait_for() {
  280. local WAIT_TIME
  281. local CMD
  282. WAIT_TIME="$1"
  283. shift
  284. CMD="$*"
  285. while true; do
  286. if $CMD >/dev/null 2>&1; then
  287. return 0
  288. fi
  289. if [ "$WAIT_TIME" -le 0 ]; then
  290. return 1
  291. fi
  292. WAIT_TIME=$((WAIT_TIME - 1))
  293. sleep 1
  294. done
  295. }
  296. # Use $CWD/etc/sys.config if exists
  297. if [ -z "$RELX_CONFIG_PATH" ]; then
  298. if [ -f "$RUNNER_ETC_DIR/sys.config" ]; then
  299. RELX_CONFIG_PATH="-config $RUNNER_ETC_DIR/sys.config"
  300. else
  301. RELX_CONFIG_PATH=""
  302. fi
  303. fi
  304. IS_BOOT_COMMAND='no'
  305. case "$1" in
  306. start|start_boot)
  307. IS_BOOT_COMMAND='yes'
  308. ;;
  309. console|console_clean|console_boot)
  310. IS_BOOT_COMMAND='yes'
  311. ;;
  312. foreground)
  313. IS_BOOT_COMMAND='yes'
  314. ;;
  315. esac
  316. ## Possible ways to configure emqx node name:
  317. ## 1. configure node.name in emqx.conf
  318. ## 2. override with environment variable EMQX_NODE_NAME
  319. ## Node name is either short-name (without '@'), e.g. 'emqx'
  320. ## or long name (with '@') e.g. 'emqx@example.net' or 'emqx@127.0.0.1'
  321. NAME="${EMQX_NODE_NAME:-}"
  322. if [ -z "$NAME" ]; then
  323. if [ "$IS_BOOT_COMMAND" = 'no' ]; then
  324. # for non-boot commands, inspect vm.<time>.args for node name
  325. # shellcheck disable=SC2012,SC2086
  326. LATEST_VM_ARGS="$(ls -t $CONFIGS_DIR/vm.*.args | head -1)"
  327. if [ -z "$LATEST_VM_ARGS" ]; then
  328. echoerr "no_vm_arg_file_found_for $1 in $CONFIGS_DIR/"
  329. exit 1
  330. fi
  331. NAME="$(grep -E '^-s?name' "$LATEST_VM_ARGS" | awk '{print $2}')"
  332. else
  333. # for boot commands, inspect emqx.conf for node name
  334. NAME="$(call_hocon -s $SCHEMA_MOD -c "$RUNNER_ETC_DIR"/emqx.conf get node.name | tr -d \")"
  335. fi
  336. fi
  337. # force to use 'emqx' short name
  338. [ -z "$NAME" ] && NAME='emqx'
  339. case "$NAME" in
  340. *@*)
  341. NAME_TYPE='-name'
  342. ;;
  343. *)
  344. NAME_TYPE='-sname'
  345. esac
  346. SHORT_NAME="$(echo "$NAME" | awk -F'@' '{print $1}')"
  347. export ESCRIPT_NAME="$SHORT_NAME"
  348. PIPE_DIR="${PIPE_DIR:-/$RUNNER_DATA_DIR/${WHOAMI}_erl_pipes/$NAME/}"
  349. COOKIE="${EMQX_NODE_COOKIE:-}"
  350. if [ -z "$COOKIE" ]; then
  351. if [ "$IS_BOOT_COMMAND" = 'yes' ]; then
  352. COOKIE="$(call_hocon -s $SCHEMA_MOD -c "$RUNNER_ETC_DIR"/emqx.conf get node.cookie | tr -d \")"
  353. else
  354. # shellcheck disable=SC2012,SC2086
  355. LATEST_VM_ARGS="$(ls -t $CONFIGS_DIR/vm.*.args | head -1)"
  356. if [ -z "$LATEST_VM_ARGS" ]; then
  357. echoerr "no_vm_arg_file_found_for $1 in $CONFIGS_DIR/"
  358. exit 1
  359. fi
  360. COOKIE="$(grep -E '^-setcookie' "$LATEST_VM_ARGS" | awk '{print $2}')"
  361. fi
  362. fi
  363. if [ -z "$COOKIE" ]; then
  364. die "Please set node.cookie in $RUNNER_ETC_DIR/emqx.conf or override from environment variable EMQX_NODE_COOKIE"
  365. fi
  366. # Support for IPv6 Dist. See: https://github.com/emqtt/emqttd/issues/1460
  367. PROTO_DIST="$(call_hocon -s $SCHEMA_MOD -c "$RUNNER_ETC_DIR"/emqx.conf get cluster.proto_dist | tr -d \")"
  368. if [ -z "$PROTO_DIST" ]; then
  369. PROTO_DIST_ARG=""
  370. else
  371. PROTO_DIST_ARG="-proto_dist $PROTO_DIST"
  372. fi
  373. cd "$ROOTDIR"
  374. case "$1" in
  375. start|start_boot)
  376. # Make sure a node IS not running
  377. if relx_nodetool "ping" >/dev/null 2>&1; then
  378. die "node_is_already_running!"
  379. fi
  380. # Bootstrap daemon command (check perms & drop to $RUNNER_USER)
  381. bootstrapd
  382. # this flag passes down to console mode
  383. # so we know it's intended to be run in daemon mode
  384. export _EMQX_START_MODE="$1"
  385. # Save this for later.
  386. CMD=$1
  387. case "$1" in
  388. start)
  389. shift
  390. START_OPTION="console"
  391. HEART_OPTION="start"
  392. ;;
  393. start_boot)
  394. shift
  395. START_OPTION="console_boot"
  396. HEART_OPTION="start_boot"
  397. ;;
  398. esac
  399. RUN_PARAM="$*"
  400. # Set arguments for the heart command
  401. set -- "$RUNNER_SCRIPT" "$HEART_OPTION"
  402. [ "$RUN_PARAM" ] && set -- "$@" "$RUN_PARAM"
  403. # Export the HEART_COMMAND
  404. HEART_COMMAND="$RUNNER_SCRIPT $CMD"
  405. export HEART_COMMAND
  406. ## See: http://erlang.org/doc/man/run_erl.html
  407. # Export the RUN_ERL_LOG_GENERATIONS
  408. export RUN_ERL_LOG_GENERATIONS=${RUN_ERL_LOG_GENERATIONS:-"5"}
  409. # Export the RUN_ERL_LOG_MAXSIZE
  410. export RUN_ERL_LOG_MAXSIZE=${RUN_ERL_LOG_MAXSIZE:-"10485760"}
  411. mkdir -p "$PIPE_DIR"
  412. "$BINDIR/run_erl" -daemon "$PIPE_DIR" "$RUNNER_LOG_DIR" \
  413. "$(relx_start_command)"
  414. WAIT_TIME=${WAIT_FOR_ERLANG:-15}
  415. if wait_for "$WAIT_TIME" 'relx_nodetool' 'ping'; then
  416. echo "$EMQX_DESCRIPTION $REL_VSN is started successfully!"
  417. exit 0
  418. else
  419. echo "$EMQX_DESCRIPTION $REL_VSN failed to start within ${WAIT_TIME} 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. fi
  425. ;;
  426. stop)
  427. # Wait for the node to completely stop...
  428. PID="$(relx_get_pid)"
  429. if ! relx_nodetool "stop"; then
  430. echoerr "Graceful shutdown failed PID=[$PID]"
  431. exit 1
  432. fi
  433. WAIT_TIME="${WAIT_FOR_ERLANG_STOP:-60}"
  434. if ! wait_for "$WAIT_TIME" 'is_down' "$PID"; then
  435. msg="dangling after ${WAIT_TIME} seconds"
  436. # also log to syslog
  437. logger -t "${REL_NAME}[${PID}]" "STOP: $msg"
  438. # log to user console
  439. echoerr "stop failed, $msg"
  440. exit 1
  441. fi
  442. logger -t "${REL_NAME}[${PID}]" "STOP: OK"
  443. ;;
  444. restart|reboot)
  445. echo "$EMQX_DESCRIPTION $REL_VSN is stopped: $("$RUNNER_BIN_DIR"/emqx stop)"
  446. "$RUNNER_BIN_DIR"/emqx start
  447. ;;
  448. pid)
  449. ## Get the VM's pid
  450. if ! relx_get_pid; then
  451. exit 1
  452. fi
  453. ;;
  454. ping)
  455. assert_node_alive
  456. ;;
  457. escript)
  458. ## Run an escript under the node's environment
  459. if ! relx_escript "$@"; then
  460. exit 1
  461. fi
  462. ;;
  463. attach)
  464. assert_node_alive
  465. # Bootstrap daemon command (check perms & drop to $RUNNER_USER)
  466. bootstrapd
  467. shift
  468. exec "$BINDIR/to_erl" "$PIPE_DIR"
  469. ;;
  470. remote_console)
  471. assert_node_alive
  472. # Bootstrap daemon command (check perms & drop to $RUNNER_USER)
  473. bootstrapd
  474. shift
  475. relx_rem_sh
  476. ;;
  477. upgrade|downgrade|install|unpack|uninstall)
  478. if [ -z "$2" ]; then
  479. echo "Missing version argument"
  480. echo "Usage: $REL_NAME $1 {version}"
  481. exit 1
  482. fi
  483. COMMAND="$1"; shift
  484. assert_node_alive
  485. ERL_FLAGS="$ERL_FLAGS $EPMD_ARG" \
  486. exec "$BINDIR/escript" "$ROOTDIR/bin/install_upgrade.escript" \
  487. "$COMMAND" "{'$REL_NAME', \"$NAME_TYPE\", '$NAME', '$COOKIE'}" "$@"
  488. ;;
  489. versions)
  490. assert_node_alive
  491. COMMAND="$1"; shift
  492. ERL_FLAGS="$ERL_FLAGS $EPMD_ARG" \
  493. exec "$BINDIR/escript" "$ROOTDIR/bin/install_upgrade.escript" \
  494. "versions" "{'$REL_NAME', \"$NAME_TYPE\", '$NAME', '$COOKIE'}" "$@"
  495. ;;
  496. console|console_clean|console_boot)
  497. # Bootstrap daemon command (check perms & drop to $RUNNER_USER)
  498. bootstrapd
  499. # .boot file typically just $REL_NAME (ie, the app name)
  500. # however, for debugging, sometimes start_clean.boot is useful.
  501. # For e.g. 'setup', one may even want to name another boot script.
  502. case "$1" in
  503. console)
  504. if [ -f "$REL_DIR/$REL_NAME.boot" ]; then
  505. BOOTFILE="$REL_DIR/$REL_NAME"
  506. else
  507. BOOTFILE="$REL_DIR/start"
  508. fi
  509. ;;
  510. console_clean)
  511. BOOTFILE="$REL_DIR/start_clean"
  512. ;;
  513. console_boot)
  514. shift
  515. BOOTFILE="$1"
  516. shift
  517. ;;
  518. esac
  519. # set before generate_config
  520. if [ "${_EMQX_START_MODE:-}" = '' ]; then
  521. export EMQX_LOG__CONSOLE_HANDLER__ENABLE="${EMQX_LOG__CONSOLE_HANDLER__ENABLE:-true}"
  522. fi
  523. #generate app.config and vm.args
  524. generate_config "$NAME_TYPE" "$NAME"
  525. # Setup beam-required vars
  526. EMU="beam"
  527. PROGNAME="${0#*/}"
  528. export EMU
  529. export PROGNAME
  530. # Store passed arguments since they will be erased by `set`
  531. ARGS="$*"
  532. # shellcheck disable=SC2086 # $RELX_CONFIG_PATH $CONFIG_ARGS $EPMD_ARG are supposed to be split by whitespace
  533. # Build an array of arguments to pass to exec later on
  534. # Build it here because this command will be used for logging.
  535. set -- "$BINDIR/erlexec" \
  536. -boot "$BOOTFILE" -mode "$CODE_LOADING_MODE" \
  537. -boot_var ERTS_LIB_DIR "$ERTS_LIB_DIR" \
  538. -mnesia dir "\"${MNESIA_DATA_DIR}\"" \
  539. $RELX_CONFIG_PATH $CONFIG_ARGS $EPMD_ARG
  540. # Log the startup
  541. logger -t "${REL_NAME}[$$]" "EXEC: $* -- ${1+$ARGS}"
  542. # Start the VM
  543. exec "$@" -- ${1+$ARGS}
  544. ;;
  545. foreground)
  546. # Bootstrap daemon command (check perms & drop to $RUNNER_USER)
  547. bootstrapd
  548. # start up the release in the foreground for use by runit
  549. # or other supervision services
  550. # set before generate_config
  551. export EMQX_LOG__CONSOLE_HANDLER__ENABLE="${EMQX_LOG__CONSOLE_HANDLER__ENABLE:-true}"
  552. #generate app.config and vm.args
  553. generate_config "$NAME_TYPE" "$NAME"
  554. [ -f "$REL_DIR/$REL_NAME.boot" ] && BOOTFILE="$REL_NAME" || BOOTFILE=start
  555. FOREGROUNDOPTIONS="-noshell -noinput +Bd"
  556. # Setup beam-required vars
  557. EMU=beam
  558. PROGNAME="${0#*/}"
  559. export EMU
  560. export PROGNAME
  561. # Store passed arguments since they will be erased by `set`
  562. ARGS="$*"
  563. # shellcheck disable=SC2086 # $RELX_CONFIG_PATH $CONFIG_ARGS $EPMD_ARG are supposed to be split by whitespace
  564. # Build an array of arguments to pass to exec later on
  565. # Build it here because this command will be used for logging.
  566. set -- "$BINDIR/erlexec" $FOREGROUNDOPTIONS \
  567. -boot "$REL_DIR/$BOOTFILE" -mode "$CODE_LOADING_MODE" \
  568. -boot_var ERTS_LIB_DIR "$ERTS_LIB_DIR" \
  569. -mnesia dir "\"${MNESIA_DATA_DIR}\"" \
  570. $RELX_CONFIG_PATH $CONFIG_ARGS $EPMD_ARG
  571. # Log the startup
  572. logger -t "${REL_NAME}[$$]" "EXEC: $* -- ${1+$ARGS}"
  573. # Start the VM
  574. exec "$@" -- ${1+$ARGS}
  575. ;;
  576. ertspath)
  577. echo "$ERTS_PATH"
  578. ;;
  579. ctl)
  580. assert_node_alive
  581. shift
  582. relx_nodetool rpc emqx_ctl run_command "$@"
  583. ;;
  584. rpc)
  585. assert_node_alive
  586. shift
  587. relx_nodetool rpc "$@"
  588. ;;
  589. rpcterms)
  590. assert_node_alive
  591. shift
  592. relx_nodetool rpcterms "$@"
  593. ;;
  594. root_dir)
  595. assert_node_alive
  596. shift
  597. relx_nodetool "eval" 'code:root_dir()'
  598. ;;
  599. eval)
  600. assert_node_alive
  601. shift
  602. relx_nodetool "eval" "$@"
  603. ;;
  604. *)
  605. relx_usage "$1"
  606. exit 1
  607. ;;
  608. esac
  609. exit 0