emqx_ctl 3.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. #!/bin/sh
  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. # Echo to stderr on errors
  9. echoerr() { echo "$@" 1>&2; }
  10. if [ -z "$WITH_EPMD" ]; then
  11. EPMD_ARG="-start_epmd false -epmd_module ekka_epmd -proto_dist ekka"
  12. else
  13. EPMD_ARG="-start_epmd true"
  14. fi
  15. relx_get_nodename() {
  16. id="longname$(relx_gen_id)-${NAME}"
  17. "$BINDIR/erl" -boot start_clean -eval '[Host] = tl(string:tokens(atom_to_list(node()),"@")), io:format("~s~n", [Host]), halt()' -noshell "${NAME_TYPE}" "$id"
  18. }
  19. # Control a node
  20. relx_nodetool() {
  21. command="$1"; shift
  22. ERL_FLAGS="$ERL_FLAGS $EPMD_ARG $PROTO_DIST_ARG" \
  23. ERL_LIBS="${LIB_EKKA_DIR}:${ERL_LIBS:-}" \
  24. "$ERTS_DIR/bin/escript" "$ROOTDIR/bin/nodetool" "$NAME_TYPE" "$NAME" \
  25. -setcookie "$COOKIE" "$command" "$@"
  26. }
  27. if [ -z "$NAME_ARG" ]; then
  28. NODENAME="${EMQX_NODE_NAME:-}"
  29. # check if there is a node running, inspect its name
  30. # shellcheck disable=SC2009 # pgrep does not support Extended Regular Expressions
  31. [ -z "$NODENAME" ] && NODENAME=$(ps -ef | grep -E '\progname\s.*emqx\s' | grep -o -E '\-name (\S*)' | awk '{print $2}')
  32. [ -z "$NODENAME" ] && NODENAME=$(grep -E '^[ \t]*node.name[ \t]*=[ \t]*' "$RUNNER_ETC_DIR/emqx.conf" 2> /dev/null | tail -1 | cut -d = -f 2-)
  33. if [ -z "$NODENAME" ]; then
  34. echoerr "vm.args needs to have a -name parameter."
  35. echoerr " -sname is not supported."
  36. echoerr "perhaps you do not have read permissions on $RUNNER_ETC_DIR/emqx.conf"
  37. exit 1
  38. else
  39. NAME_ARG="-name ${NODENAME# *}"
  40. fi
  41. fi
  42. # Extract the name type and name from the NAME_ARG for REMSH
  43. NAME_TYPE="$(echo "$NAME_ARG" | awk '{print $1}')"
  44. NAME="$(echo "$NAME_ARG" | awk '{print $2}')"
  45. # Extract the target cookie
  46. if [ -z "$COOKIE_ARG" ]; then
  47. COOKIE="${EMQX_NODE_COOKIE:-}"
  48. # check if there is a node running, steal its cookie
  49. # shellcheck disable=SC2009 # pgrep does not support Extended Regular Expressions
  50. [ -z "$COOKIE" ] && COOKIE=$(ps -ef | grep -E '\-progname\s.*emqx\s' | grep -o -E '\-setcookie (\S*)' | awk '{print $2}')
  51. [ -z "$COOKIE" ] && COOKIE=$(grep -E '^[ \t]*node.cookie[ \t]*=[ \t]*' "$RUNNER_ETC_DIR/emqx.conf" 2> /dev/null | tail -1 | cut -d = -f 2-)
  52. if [ -z "$COOKIE" ]; then
  53. echoerr "vm.args needs to have a -setcookie parameter."
  54. echoerr "please check $RUNNER_ETC_DIR/emqx.conf"
  55. exit 1
  56. else
  57. COOKIE_ARG="-setcookie $COOKIE"
  58. fi
  59. fi
  60. # Extract cookie name from COOKIE_ARG
  61. COOKIE="$(echo "$COOKIE_ARG" | awk '{print $2}')"
  62. # Support for IPv6 Dist. See: https://github.com/emqtt/emqttd/issues/1460
  63. PROTO_DIST=$(grep -E '^[ \t]*cluster.proto_dist[ \t]*=[ \t]*' "$RUNNER_ETC_DIR"/emqx.conf 2> /dev/null | tail -1 | cut -d = -f 2-)
  64. if [ -z "$PROTO_DIST" ]; then
  65. PROTO_DIST_ARG=""
  66. else
  67. PROTO_DIST_ARG="-proto_dist $PROTO_DIST"
  68. fi
  69. export ROOTDIR="$RUNNER_ROOT_DIR"
  70. export ERTS_DIR="$ROOTDIR/erts-$ERTS_VSN"
  71. export BINDIR="$ERTS_DIR/bin"
  72. cd "$ROOTDIR"
  73. relx_nodetool rpc emqx_ctl run_command "$@"