emqx_ctl 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  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. 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. ERL_LIBS="${LIB_EKKA_DIR}:${ERL_LIBS:-}" \
  32. "$ERTS_DIR/bin/escript" "$ROOTDIR/bin/nodetool" "$NAME_TYPE" "$NAME" \
  33. -setcookie "$COOKIE" "$command" "$@"
  34. }
  35. if [ -z "$NAME_ARG" ]; then
  36. NODENAME="${EMQX_NODE_NAME:-}"
  37. [ -z "$NODENAME" ] && [ -n "$EMQX_NAME" ] && [ -n "$EMQX_HOST" ] && NODENAME="${EMQX_NAME}@${EMQX_HOST}"
  38. [ -z "$NODENAME" ] && NODENAME="$(grep -E '^-name' "$LATEST_VM_ARGS" | awk '{print $2}')"
  39. if [ -z "$NODENAME" ]; then
  40. echoerr "vm.args needs to have a -name parameter."
  41. echoerr " -sname is not supported."
  42. echoerr "perhaps you do not have read permissions on $RUNNER_ETC_DIR/emqx.conf"
  43. exit 1
  44. else
  45. NAME_ARG="-name ${NODENAME# *}"
  46. fi
  47. fi
  48. # Extract the name type and name from the NAME_ARG for REMSH
  49. NAME_TYPE="$(echo "$NAME_ARG" | awk '{print $1}')"
  50. NAME="$(echo "$NAME_ARG" | awk '{print $2}')"
  51. COOKIE="${EMQX_NODE_COOKIE:-}"
  52. [ -z "$COOKIE" ] && COOKIE="$(grep -E '^-setcookie' "$LATEST_VM_ARGS" | awk '{print $2}')"
  53. if [ -z "$COOKIE" ]; then
  54. echoerr "Please set node.cookie in $RUNNER_ETC_DIR/emqx.conf or override from environment variable EMQX_NODE_COOKIE"
  55. exit 1
  56. fi
  57. # Support for IPv6 Dist. See: https://github.com/emqtt/emqttd/issues/1460
  58. PROTO_DIST=$(grep -E '^[ \t]*cluster.proto_dist[ \t]*=[ \t]*' "$RUNNER_ETC_DIR"/emqx.conf 2> /dev/null | tail -1 | awk -F"= " '{print $NF}')
  59. if [ -z "$PROTO_DIST" ]; then
  60. PROTO_DIST_ARG=""
  61. else
  62. PROTO_DIST_ARG="-proto_dist $PROTO_DIST"
  63. fi
  64. export ROOTDIR="$RUNNER_ROOT_DIR"
  65. export ERTS_DIR="$ROOTDIR/erts-$ERTS_VSN"
  66. export BINDIR="$ERTS_DIR/bin"
  67. cd "$ROOTDIR"
  68. relx_nodetool rpc emqx_ctl run_command "$@"