emqx_ctl 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  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. export RUNNER_ROOT_DIR
  9. export REL_VSN
  10. # shellcheck disable=SC2012,SC2086
  11. LATEST_VM_ARGS="$(ls -t $RUNNER_DATA_DIR/configs/vm.*.args | head -1)"
  12. if [ -z "$LATEST_VM_ARGS" ]; then
  13. echo "No vm.*.args config file found in $RUNNER_DATA_DIR/configs/"
  14. exit 1
  15. fi
  16. # Echo to stderr on errors
  17. echoerr() { echo "$@" 1>&2; }
  18. if [ -z "$WITH_EPMD" ]; then
  19. EPMD_ARG="-start_epmd false -epmd_module ekka_epmd -proto_dist ekka"
  20. else
  21. EPMD_ARG="-start_epmd true"
  22. fi
  23. relx_get_nodename() {
  24. id="longname$(relx_gen_id)-${NAME}"
  25. "$BINDIR/erl" -boot start_clean -eval '[Host] = tl(string:tokens(atom_to_list(node()),"@")), io:format("~s~n", [Host]), halt()' -noshell "${NAME_TYPE}" "$id"
  26. }
  27. # Control a node
  28. relx_nodetool() {
  29. command="$1"; shift
  30. ERL_FLAGS="$ERL_FLAGS $EPMD_ARG $PROTO_DIST_ARG" \
  31. "$ERTS_DIR/bin/escript" "$ROOTDIR/bin/nodetool" "$NAME_TYPE" "$NAME" \
  32. -setcookie "$COOKIE" "$command" "$@"
  33. }
  34. if [ -z "$NAME_ARG" ]; then
  35. NODENAME="${EMQX_NODE_NAME:-}"
  36. [ -z "$NODENAME" ] && [ -n "$EMQX_NAME" ] && [ -n "$EMQX_HOST" ] && NODENAME="${EMQX_NAME}@${EMQX_HOST}"
  37. [ -z "$NODENAME" ] && NODENAME="$(grep -E '^-name' "$LATEST_VM_ARGS" | awk '{print $2}')"
  38. if [ -z "$NODENAME" ]; then
  39. echoerr "vm.args needs to have a -name parameter."
  40. echoerr " -sname is not supported."
  41. echoerr "perhaps you do not have read permissions on $RUNNER_ETC_DIR/emqx.conf"
  42. exit 1
  43. else
  44. NAME_ARG="-name ${NODENAME# *}"
  45. fi
  46. fi
  47. # Extract the name type and name from the NAME_ARG for REMSH
  48. NAME_TYPE="$(echo "$NAME_ARG" | awk '{print $1}')"
  49. NAME="$(echo "$NAME_ARG" | awk '{print $2}')"
  50. COOKIE="${EMQX_NODE_COOKIE:-}"
  51. [ -z "$COOKIE" ] && COOKIE="$(grep -E '^-setcookie' "$LATEST_VM_ARGS" | awk '{print $2}')"
  52. if [ -z "$COOKIE" ]; then
  53. echoerr "Please set node.cookie in $RUNNER_ETC_DIR/emqx.conf or override from environment variable EMQX_NODE_COOKIE"
  54. exit 1
  55. fi
  56. # Support for IPv6 Dist. See: https://github.com/emqtt/emqttd/issues/1460
  57. PROTO_DIST=$(grep -E '^[ \t]*cluster.proto_dist[ \t]*=[ \t]*' "$RUNNER_ETC_DIR"/emqx.conf 2> /dev/null | tail -1 | awk -F"= " '{print $NF}')
  58. if [ -z "$PROTO_DIST" ]; then
  59. PROTO_DIST_ARG=""
  60. else
  61. PROTO_DIST_ARG="-proto_dist $PROTO_DIST"
  62. fi
  63. export ROOTDIR="$RUNNER_ROOT_DIR"
  64. export ERTS_DIR="$ROOTDIR/erts-$ERTS_VSN"
  65. export BINDIR="$ERTS_DIR/bin"
  66. cd "$ROOTDIR"
  67. relx_nodetool rpc emqx_ctl run_command "$@"