From 2fd0742d2ba5b03abafacdb486c3d20901b268da Mon Sep 17 00:00:00 2001 From: Zaiming Shi Date: Mon, 2 Aug 2021 20:17:53 +0200 Subject: [PATCH] refactor(emqx_ctl): simplify emqx_ctl --- bin/emqx | 13 +++++++- bin/emqx_ctl | 83 ++-------------------------------------------------- 2 files changed, 14 insertions(+), 82 deletions(-) diff --git a/bin/emqx b/bin/emqx index bc1d00e35..00c50362e 100755 --- a/bin/emqx +++ b/bin/emqx @@ -98,7 +98,7 @@ relx_usage() { echo " don't make it permanent" ;; *) - echo "Usage: $REL_NAME {start|start_boot |ertspath|foreground|stop|restart|reboot|pid|ping|console|console_clean|console_boot |attach|remote_console|upgrade|downgrade|install|uninstall|versions|escript|rpc|rpcterms|eval|root_dir}" + echo "Usage: $REL_NAME {start|start_boot |ertspath|foreground|stop|restart|reboot|pid|ping|console|console_clean|console_boot |attach|remote_console|upgrade|downgrade|install|uninstall|versions|escript|ctl|rpc|rpcterms|eval|root_dir}" ;; esac } @@ -652,6 +652,17 @@ case "$1" in ertspath) echo "$ERTS_PATH" ;; + ctl) + # Make sure a node IS running + if ! relx_nodetool "ping" > /dev/null; then + echo "Node is not running!" + exit 1 + fi + + shift + + relx_nodetool rpc emqx_ctl run_command "$@" + ;; rpc) # Make sure a node IS running if ! relx_nodetool "ping" > /dev/null; then diff --git a/bin/emqx_ctl b/bin/emqx_ctl index d4cdef389..f94946840 100755 --- a/bin/emqx_ctl +++ b/bin/emqx_ctl @@ -2,84 +2,5 @@ # -*- tab-width:4;indent-tabs-mode:nil -*- # ex: ts=4 sw=4 et -set -e - -ROOT_DIR="$(cd "$(dirname "$(readlink "$0" || echo "$0")")"/..; pwd -P)" -# shellcheck disable=SC1090 -. "$ROOT_DIR"/releases/emqx_vars - -export ROOTDIR="$RUNNER_ROOT_DIR" -export ERTS_DIR="$ROOTDIR/erts-$ERTS_VSN" -export BINDIR="$ERTS_DIR/bin" - -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 - -call_hocon() { - export RUNNER_ROOT_DIR - export RUNNER_ETC_DIR - "$ERTS_DIR/bin/escript" "$ROOTDIR/bin/nodetool" hocon "$@" \ - || die "ERROR: call_hocon failed: $*" $? -} - -# Support for IPv6 Dist. See: https://github.com/emqtt/emqttd/issues/1460 -PROTO_DIST="$(call_hocon -s emqx_schema -c "$RUNNER_ETC_DIR"/emqx.conf get cluster.proto_dist | tr -d \")" -if [ -z "$PROTO_DIST" ]; then - PROTO_DIST_ARG="" -else - PROTO_DIST_ARG="-proto_dist $PROTO_DIST" -fi - - -# Control a node -relx_nodetool() { - command="$1"; shift - - ERL_FLAGS="$ERL_FLAGS $EPMD_ARG $PROTO_DIST_ARG" \ - "$ERTS_DIR/bin/escript" "$ROOTDIR/bin/nodetool" "$NAME_TYPE" "$NAME" \ - -setcookie "$COOKIE" "$command" "$@" -} - -NAME="${EMQX_NODE_NAME:-}" -[ -n "$EMQX_NAME" ] && [ -n "$EMQX_HOST" ] && NAME="${EMQX_NAME}@${EMQX_HOST}" -[ -z "$NAME" ] && NAME="$(grep -E '^-s?name' "$LATEST_VM_ARGS" | awk '{print $2}')" -if [ -z "$NAME" ]; then - echoerr "Failed to read node name from $LATEST_VM_ARGS" - echoerr "Ensure the node is running and you have permission to read $LATEST_VM_ARGS" - exit 1 -fi - -case "$NAME" in - *@*) - NAME_TYPE='-name' - ;; - *) - NAME_TYPE='-sname' -esac - -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 - -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 "$@"