From d8cad2cc6cede8c59ab35b91b369312cbf7db79f Mon Sep 17 00:00:00 2001 From: "Zaiming (Stone) Shi" Date: Mon, 11 Apr 2022 23:48:33 +0200 Subject: [PATCH] refactor(bin/emqx_ctl): simplify emqx_ctl There is no need to duplicate the node name/cookie resolution in another script, everything is ready in bin/emqx --- bin/emqx_ctl | 82 ++-------------------------------------------------- 1 file changed, 2 insertions(+), 80 deletions(-) diff --git a/bin/emqx_ctl b/bin/emqx_ctl index bed351b7b..ef3946ee9 100755 --- a/bin/emqx_ctl +++ b/bin/emqx_ctl @@ -2,83 +2,5 @@ # -*- tab-width:4;indent-tabs-mode:nil -*- # ex: ts=4 sw=4 et -set -e - -RUNNER_ROOT_DIR="$(cd "$(dirname "$(readlink "$0" || echo "$0")")"/..; pwd -P)" -# shellcheck disable=SC1090 -. "$RUNNER_ROOT_DIR"/releases/emqx_vars - -export RUNNER_ROOT_DIR -export REL_VSN - -# shellcheck disable=SC2012,SC2086 -LATEST_VM_ARGS="$(ls -t $RUNNER_DATA_DIR/configs/vm.*.args | head -1)" -if [ -z "$LATEST_VM_ARGS" ]; then - echo "No vm.*.args config file found in $RUNNER_DATA_DIR/configs/" - exit 1 -fi - -# Echo to stderr on errors -echoerr() { echo "$@" 1>&2; } - -if [ -z "$WITH_EPMD" ]; then - EPMD_ARG="-start_epmd false -epmd_module ekka_epmd -proto_dist ekka" -else - EPMD_ARG="-start_epmd true" -fi - -relx_get_nodename() { - id="longname$(relx_gen_id)-${NAME}" - "$BINDIR/erl" -boot start_clean -eval '[Host] = tl(string:tokens(atom_to_list(node()),"@")), io:format("~s~n", [Host]), halt()' -noshell "${NAME_TYPE}" "$id" -} - -# Control a node -relx_nodetool() { - command="$1"; shift - - ERL_FLAGS="$ERL_FLAGS $EPMD_ARG $PROTO_DIST_ARG" \ - ERL_LIBS="${LIB_EKKA_DIR}:${ERL_LIBS:-}" \ - "$ERTS_DIR/bin/escript" "$ROOTDIR/bin/nodetool" "$NAME_TYPE" "$NAME" \ - -setcookie "$COOKIE" "$command" "$@" -} - -if [ -z "$NAME_ARG" ]; then - NODENAME="${EMQX_NODE_NAME:-}" - [ -z "$NODENAME" ] && [ -n "$EMQX_NAME" ] && [ -n "$EMQX_HOST" ] && NODENAME="${EMQX_NAME}@${EMQX_HOST}" - [ -z "$NODENAME" ] && NODENAME="$(grep -E '^-name' "$LATEST_VM_ARGS" | awk '{print $2}')" - if [ -z "$NODENAME" ]; then - echoerr "vm.args needs to have a -name parameter." - echoerr " -sname is not supported." - echoerr "perhaps you do not have read permissions on $RUNNER_ETC_DIR/emqx.conf" - exit 1 - else - NAME_ARG="-name ${NODENAME# *}" - fi -fi - -# Extract the name type and name from the NAME_ARG for REMSH -NAME_TYPE="$(echo "$NAME_ARG" | awk '{print $1}')" -NAME="$(echo "$NAME_ARG" | awk '{print $2}')" - -COOKIE="${EMQX_NODE_COOKIE:-}" -[ -z "$COOKIE" ] && COOKIE="$(grep -E '^-setcookie' "$LATEST_VM_ARGS" | awk '{print $2}')" -if [ -z "$COOKIE" ]; then - echoerr "Please set node.cookie in $RUNNER_ETC_DIR/emqx.conf or override from environment variable EMQX_NODE_COOKIE" - exit 1 -fi - -# Support for IPv6 Dist. See: https://github.com/emqtt/emqttd/issues/1460 -PROTO_DIST=$(grep -E '^[ \t]*cluster.proto_dist[ \t]*=[ \t]*' "$RUNNER_ETC_DIR"/emqx.conf 2> /dev/null | tail -1 | awk -F"= " '{print $NF}') -if [ -z "$PROTO_DIST" ]; then - PROTO_DIST_ARG="" -else - PROTO_DIST_ARG="-proto_dist $PROTO_DIST" -fi - -export ROOTDIR="$RUNNER_ROOT_DIR" -export ERTS_DIR="$ROOTDIR/erts-$ERTS_VSN" -export BINDIR="$ERTS_DIR/bin" -cd "$ROOTDIR" - -relx_nodetool rpc emqx_ctl run_command "$@" - +THIS_DIR="$(cd "$(dirname "$(readlink "$0" || echo "$0")")" || true; pwd -P)" +exec "$THIS_DIR/emqx" ctl "$@"