emqx_ctl 2.8 KB

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