From cf23d9efe95d5c0abd769276f4ac7eaa8d56d623 Mon Sep 17 00:00:00 2001 From: Zaiming Shi Date: Thu, 28 Jan 2021 13:34:38 +0100 Subject: [PATCH] chore(bin/emqx): Do not rely on beam.smp program name --- .ci/build_packages/tests.sh | 2 +- .ci/fvt_tests/docker-compose.yaml | 10 +++++----- bin/emqx | 23 ++++++++--------------- bin/emqx_ctl | 25 +++++++++---------------- deploy/packages/rpm/init.script | 4 ++-- 5 files changed, 25 insertions(+), 39 deletions(-) diff --git a/.ci/build_packages/tests.sh b/.ci/build_packages/tests.sh index 1f07bd8c6..8895ec3b3 100755 --- a/.ci/build_packages/tests.sh +++ b/.ci/build_packages/tests.sh @@ -127,7 +127,7 @@ running_test(){ IDLE_TIME=$((IDLE_TIME+1)) done pytest -v /paho-mqtt-testing/interoperability/test_client/V5/test_connect.py::test_basic - emqx stop || kill $(ps -ef |grep emqx | grep beam.smp |awk '{print $2}') + emqx stop || kill $(ps -ef | grep -E '\-progname\s.+emqx\s' |awk '{print $2}') if [ $(sed -n '/^ID=/p' /etc/os-release | sed -r 's/ID=(.*)/\1/g' | sed 's/"//g') = ubuntu ] \ || [ $(sed -n '/^ID=/p' /etc/os-release | sed -r 's/ID=(.*)/\1/g' | sed 's/"//g') = debian ] \ diff --git a/.ci/fvt_tests/docker-compose.yaml b/.ci/fvt_tests/docker-compose.yaml index 467a0ed25..050fd269b 100644 --- a/.ci/fvt_tests/docker-compose.yaml +++ b/.ci/fvt_tests/docker-compose.yaml @@ -11,8 +11,8 @@ services: - "EMQX_CLUSTER__STATIC__SEEDS=emqx@node1.emqx.io, emqx@node2.emqx.io" - "EMQX_ZONE__EXTERNAL__RETRY_INTERVAL=2s" - "EMQX_MQTT__MAX_TOPIC_ALIAS=10" - command: - - /bin/sh + command: + - /bin/sh - -c - | sed -i "s 127.0.0.1 $$(ip route show |grep "link" |awk '{print $$1}') g" /opt/emqx/etc/acl.conf @@ -27,7 +27,7 @@ services: emqx-bridge: aliases: - node1.emqx.io - + emqx2: container_name: node2.emqx.io image: emqx/emqx:build-alpine-amd64 @@ -38,8 +38,8 @@ services: - "EMQX_CLUSTER__STATIC__SEEDS=emqx@node1.emqx.io, emqx@node2.emqx.io" - "EMQX_ZONE__EXTERNAL__RETRY_INTERVAL=2s" - "EMQX_MQTT__MAX_TOPIC_ALIAS=10" - command: - - /bin/sh + command: + - /bin/sh - -c - | sed -i "s 127.0.0.1 $$(ip route show |grep "link" |awk '{print $$1}') g" /opt/emqx/etc/acl.conf diff --git a/bin/emqx b/bin/emqx index ead67c505..44d9f439b 100755 --- a/bin/emqx +++ b/bin/emqx @@ -246,15 +246,11 @@ if [ -z "$RELX_CONFIG_PATH" ]; then fi fi -# Extract the target node name from node.args if [ -z "$NAME_ARG" ]; then - if [ ! -z "$EMQX_NODE_NAME" ]; then - NODENAME="$EMQX_NODE_NAME" - elif [ ! -z `ps -ef | grep "$ERTS_PATH/beam.smp" | grep -o -E '\-name (\S*)' | awk '{print $2}'` ]; then - NODENAME=`ps -ef | grep "$ERTS_PATH/beam.smp" | grep -o -E '\-name (\S*)' | awk '{print $2}'` - else - NODENAME=`egrep '^[ \t]*node.name[ \t]*=[ \t]*' "$RUNNER_ETC_DIR/emqx.conf" 2> /dev/null | tail -1 | cut -d = -f 2-` - fi + NODENAME="${EMQX_NODE_NAME:-}" + # check if there is a node running, inspect its name + [ -z "$NODENAME" ] && NODENAME=`ps -ef | grep -E '\-progname\s.*emqx\s' | grep -o -E '\-name (\S*)' | awk '{print $2}'` + [ -z "$NODENAME" ] && NODENAME=`egrep '^[ \t]*node.name[ \t]*=[ \t]*' "$RUNNER_ETC_DIR/emqx.conf" 2> /dev/null | tail -1 | cut -d = -f 2-` if [ -z "$NODENAME" ]; then echoerr "vm.args needs to have a -name parameter." echoerr " -sname is not supported." @@ -273,13 +269,10 @@ PIPE_DIR="${PIPE_DIR:-/$RUNNER_DATA_DIR/${WHOAMI}_erl_pipes/$NAME/}" # Extract the target cookie if [ -z "$COOKIE_ARG" ]; then - if [ ! -z "$EMQX_NODE_COOKIE" ]; then - COOKIE="$EMQX_NODE_COOKIE" - elif [ ! -z `ps -ef | grep "$ERTS_PATH/beam.smp" | grep -o -E '\-setcookie (\S*)' | awk '{print $2}'` ]; then - COOKIE=`ps -ef | grep "$ERTS_PATH/beam.smp" | grep -o -E '\-setcookie (\S*)' | awk '{print $2}'` - else - COOKIE=`egrep '^[ \t]*node.cookie[ \t]*=[ \t]*' "$RUNNER_ETC_DIR/emqx.conf" 2> /dev/null | tail -1 | cut -d = -f 2-` - fi + COOKIE="${EMQX_NODE_COOKIE:-}" + # check if there is a node running, steal its cookie + [ -z "$COOKIE" ] && COOKIE=`ps -ef | grep -E '\-progname\s.*emqx\s' | grep -o -E '\-setcookie (\S*)' | awk '{print $2}'` + [ -z "$COOKIE" ] && COOKIE=`egrep '^[ \t]*node.cookie[ \t]*=[ \t]*' "$RUNNER_ETC_DIR/emqx.conf" 2> /dev/null | tail -1 | cut -d = -f 2-` if [ -z "$COOKIE" ]; then echoerr "vm.args needs to have a -setcookie parameter." echoerr "please check $RUNNER_ETC_DIR/emqx.conf" diff --git a/bin/emqx_ctl b/bin/emqx_ctl index 613b3b38b..88e28e716 100755 --- a/bin/emqx_ctl +++ b/bin/emqx_ctl @@ -31,19 +31,15 @@ relx_nodetool() { } -# Extract the target node name from node.args if [ -z "$NAME_ARG" ]; then - if [ ! -z "$EMQX_NODE_NAME" ]; then - NODENAME="$EMQX_NODE_NAME" - elif [ ! -z `ps -ef | grep "$ERTS_PATH/beam.smp" | grep -o -E '\-name (\S*)' | awk '{print $2}'` ]; then - NODENAME=`ps -ef | grep "$ERTS_PATH/beam.smp" | grep -o -E '\-name (\S*)' | awk '{print $2}'` - else - NODENAME=`egrep '^[ \t]*node.name[ \t]*=[ \t]*' $RUNNER_ETC_DIR/emqx.conf 2> /dev/null | tail -1 | cut -d = -f 2-` - fi + NODENAME="${EMQX_NODE_NAME:-}" + # check if there is a node running, inspect its name + [ -z "$NODENAME" ] && NODENAME=`ps -ef | grep -E '\progname\s.*emqx\s' | grep -o -E '\-name (\S*)' | awk '{print $2}'` + [ -z "$NODENAME" ] && NODENAME=`egrep '^[ \t]*node.name[ \t]*=[ \t]*' "$RUNNER_ETC_DIR/emqx.conf" 2> /dev/null | tail -1 | cut -d = -f 2-` if [ -z "$NODENAME" ]; then echoerr "vm.args needs to have a -name parameter." echoerr " -sname is not supported." - echoerr "please check $RUNNER_ETC_DIR/emqx.conf" + echoerr "perhaps you do not have read permissions on $RUNNER_ETC_DIR/emqx.conf" exit 1 else NAME_ARG="-name ${NODENAME# *}" @@ -56,13 +52,10 @@ NAME="$(echo "$NAME_ARG" | awk '{print $2}')" # Extract the target cookie if [ -z "$COOKIE_ARG" ]; then - if [ ! -z "$EMQX_NODE_COOKIE" ]; then - COOKIE="$EMQX_NODE_COOKIE" - elif [ ! -z `ps -ef | grep "$ERTS_PATH/beam.smp" | grep -o -E '\-setcookie (\S*)' | awk '{print $2}'` ]; then - COOKIE=`ps -ef | grep "$ERTS_PATH/beam.smp" | grep -o -E '\-setcookie (\S*)' | awk '{print $2}'` - else - COOKIE=`egrep '^[ \t]*node.cookie[ \t]*=[ \t]*' $RUNNER_ETC_DIR/emqx.conf 2> /dev/null | tail -1 | cut -d = -f 2-` - fi + COOKIE="${EMQX_NODE_COOKIE:-}" + # check if there is a node running, steal its cookie + [ -z "$COOKIE" ] && COOKIE=`ps -ef | grep -E '\-progname\s.*emqx\s' | grep -o -E '\-setcookie (\S*)' | awk '{print $2}'` + [ -z "$COOKIE" ] && COOKIE=`egrep '^[ \t]*node.cookie[ \t]*=[ \t]*' "$RUNNER_ETC_DIR/emqx.conf" 2> /dev/null | tail -1 | cut -d = -f 2-` if [ -z "$COOKIE" ]; then echoerr "vm.args needs to have a -setcookie parameter." echoerr "please check $RUNNER_ETC_DIR/emqx.conf" diff --git a/deploy/packages/rpm/init.script b/deploy/packages/rpm/init.script index 08608f05b..33ff030a5 100755 --- a/deploy/packages/rpm/init.script +++ b/deploy/packages/rpm/init.script @@ -38,7 +38,7 @@ status -p $pidfile -l $(basename $lockfile) $NAME >/dev/null 2>&1 running=$? find_pid() { - ps ax | grep beam.smp | grep -E "\-progname.+$NAME" | awk '{print $1}' + ps ax | grep -E "\-progname.+$NAME" | awk '{print $1}' } check_pid_status() { @@ -92,7 +92,7 @@ stop() { hardstop() { echo -n $"Shutting down $NAME: " - su - emqx -c "ps -ef | grep beam.smp | grep '\-progname $NAME ' | grep -v grep | awk '{print \$2}' | xargs kill -9" + su - emqx -c "ps -ef | grep '\-progname $NAME ' | grep -v grep | awk '{print \$2}' | xargs kill -9" for n in $(seq 1 10); do sleep 1 check_pid_status