fix(scripts): Finds node name and cookie from vm.args

For node name and cookie overriden from environment variable
the only way to find it from another shell is to inspect
the vm.args file.

For node boot commands, the vm.args file may not have been created
yet, so we need to inspect node name in emqx.conf
This commit is contained in:
Zaiming Shi 2021-03-24 11:55:58 +01:00 committed by Zaiming (Stone) Shi
parent a4b30ea77c
commit 4ee0dbdea4
2 changed files with 57 additions and 32 deletions

View File

@ -260,15 +260,43 @@ if [ -z "$RELX_CONFIG_PATH" ]; then
fi fi
fi fi
IS_BOOT_COMMAND='no'
case "$1" in
start|start_boot)
IS_BOOT_COMMAND='yes'
;;
console|console_clean|console_boot)
IS_BOOT_COMMAND='yes'
;;
foreground)
IS_BOOT_COMMAND='yes'
;;
esac
if [ -z "$NAME_ARG" ]; then if [ -z "$NAME_ARG" ]; then
NODENAME="${EMQX_NODE_NAME:-}" NODENAME="${EMQX_NODE_NAME:-}"
# compatible with docker entrypoint # compatible with docker entrypoint
[ -z "$NODENAME" ] && [ -n "$EMQX_NAME" ] && [ -n "$EMQX_HOST" ] && NODENAME="${EMQX_NAME}@${EMQX_HOST}" [ -z "$NODENAME" ] && [ -n "$EMQX_NAME" ] && [ -n "$EMQX_HOST" ] && NODENAME="${EMQX_NAME}@${EMQX_HOST}"
[ -z "$NODENAME" ] && NODENAME=$(grep -E '^[ \t]*node.name[ \t]*=[ \t]*' "$RUNNER_ETC_DIR/emqx.conf" 2> /dev/null | tail -1 | cut -d = -f 2-) if [ -z "$NODENAME" ] && [ "$IS_BOOT_COMMAND" = 'no' ]; then
# for non-boot commands, inspect vm.<time>.args for node name
LATEST_VM_ARGS="$(ls -t $RUNNER_DATA_DIR/configs/vm.*.args | head -1)"
if [ -z "$LATEST_VM_ARGS" ]; then
echo "For command $1, there is no vm.*.args config file found in $RUNNER_DATA_DIR/configs/"
exit 1
fi
NODENAME="$(grep -E '^-name' $LATEST_VM_ARGS | awk '{print $2}')"
else
# for boot commands, inspect emqx.conf for node name
NODENAME=$(grep -E '^[ \t]*node.name[ \t]*=[ \t]*' "$RUNNER_ETC_DIR/emqx.conf" 2> /dev/null | tail -1 | cut -d = -f 2-)
fi
if [ -z "$NODENAME" ]; then if [ -z "$NODENAME" ]; then
echoerr "vm.args needs to have a -name parameter." echoerr "Failed to resolve emqx node name"
echoerr " -sname is not supported." if [ "$IS_BOOT_COMMAND" = 'yes' ]; then
echoerr "perhaps you do not have read permissions on $RUNNER_ETC_DIR/emqx.conf" echoerr "Make user emqx has read permissions on $RUNNER_ETC_DIR/emqx.conf"
fi
echoerr "Maybe set environment variable ENQX_NODE_NAME='name@host.name'"
echoerr "or set EMQX_NAME='name' EMQX_HOST='host.name'"
exit 1 exit 1
else else
NAME_ARG="-name ${NODENAME# *}" NAME_ARG="-name ${NODENAME# *}"
@ -283,22 +311,23 @@ export ESCRIPT_NAME="$NODENAME"
PIPE_DIR="${PIPE_DIR:-/$RUNNER_DATA_DIR/${WHOAMI}_erl_pipes/$NAME/}" PIPE_DIR="${PIPE_DIR:-/$RUNNER_DATA_DIR/${WHOAMI}_erl_pipes/$NAME/}"
# Extract the target cookie # COOKIE is only needed for non-boot commands
if [ -z "$COOKIE_ARG" ]; then # so, either read environment variable override
COOKIE="${EMQX_NODE_COOKIE:-}" # or inspect vm.<time>.args
[ -z "$COOKIE" ] && COOKIE=$(grep -E '^[ \t]*node.cookie[ \t]*=[ \t]*' "$RUNNER_ETC_DIR/emqx.conf" 2> /dev/null | tail -1 | cut -d = -f 2-) COOKIE="${EMQX_NODE_COOKIE:-}"
if [ -z "$COOKIE" ]; then if [ -z "$COOKIE" ] && [ "$IS_BOOT_COMMAND" = 'no' ]; then
echoerr "vm.args needs to have a -setcookie parameter." LATEST_VM_ARGS="$(ls -t $RUNNER_DATA_DIR/configs/vm.*.args | head -1)"
echoerr "please check $RUNNER_ETC_DIR/emqx.conf" if [ -z "$LATEST_VM_ARGS" ]; then
echo "For command $1, there is no vm.*.args config file found in $RUNNER_DATA_DIR/configs/"
exit 1
fi
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 exit 1
else
COOKIE_ARG="-setcookie $COOKIE"
fi fi
fi fi
# Extract cookie name from COOKIE_ARG
COOKIE="$(echo "$COOKIE_ARG" | awk '{print $2}')"
# Support for IPv6 Dist. See: https://github.com/emqtt/emqttd/issues/1460 # 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 | cut -d = -f 2-) PROTO_DIST=$(grep -E '^[ \t]*cluster.proto_dist[ \t]*=[ \t]*' "$RUNNER_ETC_DIR/emqx.conf" 2> /dev/null | tail -1 | cut -d = -f 2-)
if [ -z "$PROTO_DIST" ]; then if [ -z "$PROTO_DIST" ]; then

View File

@ -8,6 +8,12 @@ ROOT_DIR="$(cd "$(dirname "$(readlink "$0" || echo "$0")")"/..; pwd -P)"
# shellcheck disable=SC1090 # shellcheck disable=SC1090
. "$ROOT_DIR"/releases/emqx_vars . "$ROOT_DIR"/releases/emqx_vars
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 # Echo to stderr on errors
echoerr() { echo "$@" 1>&2; } echoerr() { echo "$@" 1>&2; }
@ -34,9 +40,8 @@ relx_nodetool() {
if [ -z "$NAME_ARG" ]; then if [ -z "$NAME_ARG" ]; then
NODENAME="${EMQX_NODE_NAME:-}" NODENAME="${EMQX_NODE_NAME:-}"
# compatible with docker entrypoint
[ -z "$NODENAME" ] && [ -n "$EMQX_NAME" ] && [ -n "$EMQX_HOST" ] && NODENAME="${EMQX_NAME}@${EMQX_HOST}" [ -z "$NODENAME" ] && [ -n "$EMQX_NAME" ] && [ -n "$EMQX_HOST" ] && NODENAME="${EMQX_NAME}@${EMQX_HOST}"
[ -z "$NODENAME" ] && NODENAME=$(grep -E '^[ \t]*node.name[ \t]*=[ \t]*' "$RUNNER_ETC_DIR/emqx.conf" 2> /dev/null | tail -1 | cut -d = -f 2-) [ -z "$NODENAME" ] && NODENAME="$(grep -E '^-name' $LATEST_VM_ARGS | awk '{print $2}')"
if [ -z "$NODENAME" ]; then if [ -z "$NODENAME" ]; then
echoerr "vm.args needs to have a -name parameter." echoerr "vm.args needs to have a -name parameter."
echoerr " -sname is not supported." echoerr " -sname is not supported."
@ -51,22 +56,13 @@ fi
NAME_TYPE="$(echo "$NAME_ARG" | awk '{print $1}')" NAME_TYPE="$(echo "$NAME_ARG" | awk '{print $1}')"
NAME="$(echo "$NAME_ARG" | awk '{print $2}')" NAME="$(echo "$NAME_ARG" | awk '{print $2}')"
# Extract the target cookie COOKIE="${EMQX_NODE_COOKIE:-}"
if [ -z "$COOKIE_ARG" ]; then [ -z "$COOKIE" ] && COOKIE="$(grep -E '^-setcookie' $LATEST_VM_ARGS | awk '{print $2}')"
COOKIE="${EMQX_NODE_COOKIE:-}" if [ -z "$COOKIE" ]; then
[ -z "$COOKIE" ] && COOKIE=$(grep -E '^[ \t]*node.cookie[ \t]*=[ \t]*' "$RUNNER_ETC_DIR/emqx.conf" 2> /dev/null | tail -1 | cut -d = -f 2-) echoerr "Please set node.cookie in $RUNNER_ETC_DIR/emqx.conf or override from environment variable EMQX_NODE_COOKIE"
if [ -z "$COOKIE" ]; then exit 1
echoerr "vm.args needs to have a -setcookie parameter."
echoerr "please check $RUNNER_ETC_DIR/emqx.conf"
exit 1
else
COOKIE_ARG="-setcookie $COOKIE"
fi
fi fi
# Extract cookie name from COOKIE_ARG
COOKIE="$(echo "$COOKIE_ARG" | awk '{print $2}')"
# Support for IPv6 Dist. See: https://github.com/emqtt/emqttd/issues/1460 # 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 | cut -d = -f 2-) PROTO_DIST=$(grep -E '^[ \t]*cluster.proto_dist[ \t]*=[ \t]*' "$RUNNER_ETC_DIR"/emqx.conf 2> /dev/null | tail -1 | cut -d = -f 2-)
if [ -z "$PROTO_DIST" ]; then if [ -z "$PROTO_DIST" ]; then