emqx 22 KB

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