emqx_ctl 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  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. # Extract the target node name from node.args
  26. if [ -z "$NAME_ARG" ]; then
  27. if [ ! -z "$EMQX_NODE_NAME" ]; then
  28. NODENAME="$EMQX_NODE_NAME"
  29. elif [ ! -z `ps -ef | grep "$ERTS_PATH/beam.smp" | grep -o -E '\-name (\S*)' | awk '{print $2}'` ]; then
  30. NODENAME=`ps -ef | grep "$ERTS_PATH/beam.smp" | grep -o -E '\-name (\S*)' | awk '{print $2}'`
  31. else
  32. NODENAME=`egrep '^[ \t]*node.name[ \t]*=[ \t]*' $RUNNER_ETC_DIR/emqx.conf 2> /dev/null | tail -1 | cut -d = -f 2-`
  33. fi
  34. if [ -z "$NODENAME" ]; then
  35. echoerr "vm.args needs to have a -name parameter."
  36. echoerr " -sname is not supported."
  37. echoerr "please check $RUNNER_ETC_DIR/emqx.conf"
  38. exit 1
  39. else
  40. NAME_ARG="-name ${NODENAME# *}"
  41. fi
  42. fi
  43. # Extract the name type and name from the NAME_ARG for REMSH
  44. NAME_TYPE="$(echo "$NAME_ARG" | awk '{print $1}')"
  45. NAME="$(echo "$NAME_ARG" | awk '{print $2}')"
  46. # Extract the target cookie
  47. if [ -z "$COOKIE_ARG" ]; then
  48. if [ ! -z "$EMQX_NODE_COOKIE" ]; then
  49. COOKIE="$EMQX_NODE_COOKIE"
  50. elif [ ! -z `ps -ef | grep "$ERTS_PATH/beam.smp" | grep -o -E '\-setcookie (\S*)' | awk '{print $2}'` ]; then
  51. COOKIE=`ps -ef | grep "$ERTS_PATH/beam.smp" | grep -o -E '\-setcookie (\S*)' | awk '{print $2}'`
  52. else
  53. COOKIE=`egrep '^[ \t]*node.cookie[ \t]*=[ \t]*' $RUNNER_ETC_DIR/emqx.conf 2> /dev/null | tail -1 | cut -d = -f 2-`
  54. fi
  55. if [ -z "$COOKIE" ]; then
  56. echoerr "vm.args needs to have a -setcookie parameter."
  57. echoerr "please check $RUNNER_ETC_DIR/emqx.conf"
  58. exit 1
  59. else
  60. COOKIE_ARG="-setcookie $COOKIE"
  61. fi
  62. fi
  63. # Extract cookie name from COOKIE_ARG
  64. COOKIE="$(echo "$COOKIE_ARG" | awk '{print $2}')"
  65. # Support for IPv6 Dist. See: https://github.com/emqtt/emqttd/issues/1460
  66. PROTO_DIST=`egrep '^[ \t]*cluster.proto_dist[ \t]*=[ \t]*' $RUNNER_ETC_DIR/emqx.conf 2> /dev/null | tail -1 | cut -d = -f 2-`
  67. if [ -z "$PROTO_DIST" ]; then
  68. PROTO_DIST_ARG=""
  69. else
  70. PROTO_DIST_ARG="-proto_dist $PROTO_DIST"
  71. fi
  72. export ROOTDIR="$RUNNER_ROOT_DIR"
  73. export ERTS_DIR="$ROOTDIR/erts-$ERTS_VSN"
  74. export BINDIR="$ERTS_DIR/bin"
  75. cd "$ROOTDIR"
  76. relx_nodetool rpc emqx_ctl run_command "$@"