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. "$ERTS_DIR/bin/escript" "$ROOTDIR/bin/nodetool" "$NAME_TYPE" "$NAME" \
  24. -setcookie "$COOKIE" "$command" "$@"
  25. }
  26. if [ -z "$NAME_ARG" ]; then
  27. NODENAME="${EMQX_NODE_NAME:-}"
  28. # check if there is a node running, inspect its name
  29. # shellcheck disable=SC2009 # pgrep does not support Extended Regular Expressions
  30. [ -z "$NODENAME" ] && NODENAME=$(ps -ef | grep -E '\progname\s.*emqx\s' | grep -o -E '\-name (\S*)' | awk '{print $2}')
  31. [ -z "$NODENAME" ] && NODENAME=$(grep -E '^[ \t]*node.name[ \t]*=[ \t]*' "$RUNNER_ETC_DIR/emqx.conf" 2> /dev/null | tail -1 | cut -d = -f 2-)
  32. if [ -z "$NODENAME" ]; then
  33. echoerr "vm.args needs to have a -name parameter."
  34. echoerr " -sname is not supported."
  35. echoerr "perhaps you do not have read permissions on $RUNNER_ETC_DIR/emqx.conf"
  36. exit 1
  37. else
  38. NAME_ARG="-name ${NODENAME# *}"
  39. fi
  40. fi
  41. # Extract the name type and name from the NAME_ARG for REMSH
  42. NAME_TYPE="$(echo "$NAME_ARG" | awk '{print $1}')"
  43. NAME="$(echo "$NAME_ARG" | awk '{print $2}')"
  44. # Extract the target cookie
  45. if [ -z "$COOKIE_ARG" ]; then
  46. COOKIE="${EMQX_NODE_COOKIE:-}"
  47. # check if there is a node running, steal its cookie
  48. # shellcheck disable=SC2009 # pgrep does not support Extended Regular Expressions
  49. [ -z "$COOKIE" ] && COOKIE=$(ps -ef | grep -E '\-progname\s.*emqx\s' | grep -o -E '\-setcookie (\S*)' | awk '{print $2}')
  50. [ -z "$COOKIE" ] && COOKIE=$(grep -E '^[ \t]*node.cookie[ \t]*=[ \t]*' "$RUNNER_ETC_DIR/emqx.conf" 2> /dev/null | tail -1 | cut -d = -f 2-)
  51. if [ -z "$COOKIE" ]; then
  52. echoerr "vm.args needs to have a -setcookie parameter."
  53. echoerr "please check $RUNNER_ETC_DIR/emqx.conf"
  54. exit 1
  55. else
  56. COOKIE_ARG="-setcookie $COOKIE"
  57. fi
  58. fi
  59. # Extract cookie name from COOKIE_ARG
  60. COOKIE="$(echo "$COOKIE_ARG" | awk '{print $2}')"
  61. # Support for IPv6 Dist. See: https://github.com/emqtt/emqttd/issues/1460
  62. PROTO_DIST=$(grep -E '^[ \t]*cluster.proto_dist[ \t]*=[ \t]*' "$RUNNER_ETC_DIR"/emqx.conf 2> /dev/null | tail -1 | cut -d = -f 2-)
  63. if [ -z "$PROTO_DIST" ]; then
  64. PROTO_DIST_ARG=""
  65. else
  66. PROTO_DIST_ARG="-proto_dist $PROTO_DIST"
  67. fi
  68. export ROOTDIR="$RUNNER_ROOT_DIR"
  69. export ERTS_DIR="$ROOTDIR/erts-$ERTS_VSN"
  70. export BINDIR="$ERTS_DIR/bin"
  71. cd "$ROOTDIR"
  72. relx_nodetool rpc emqx_ctl run_command "$@"