From e391029b34de793b15f3adc1c43b39635d015223 Mon Sep 17 00:00:00 2001 From: Domenico Francesco Bruscino Date: Tue, 3 Dec 2024 10:30:56 +0100 Subject: [PATCH] [#112] Remove python dependency --- image.yaml | 2 + .../activemq-artemis-launch/added/drain.sh | 23 ++--- .../added/readinessProbe.sh | 87 +++++++------------ 3 files changed, 37 insertions(+), 75 deletions(-) diff --git a/image.yaml b/image.yaml index b4439de2..622fce74 100644 --- a/image.yaml +++ b/image.yaml @@ -128,6 +128,8 @@ packages: - apr - procps-ng - bind-utils + - jq + - xmlstarlet modules: repositories: diff --git a/modules/activemq-artemis-launch/added/drain.sh b/modules/activemq-artemis-launch/added/drain.sh index 98ebf366..50f7334d 100755 --- a/modules/activemq-artemis-launch/added/drain.sh +++ b/modules/activemq-artemis-launch/added/drain.sh @@ -48,13 +48,13 @@ echo "[drain.sh] $ENDPOINTS" count=0 foundTarget="false" while true; do - ip=$(echo "$ENDPOINTS" | python3 -c "import sys, json; print(json.load(sys.stdin)['subsets'][0]['addresses'][${count}]['ip'])") + ip=$(echo "$ENDPOINTS" | jq --argjson count ${count} -r '.subsets[0].addresses[$count].ip') if [ $? -ne 0 ]; then echo "[drain.sh] Can't find ip to scale down to tried ${count} ips" exit 1 fi echo "[drain.sh] got ip ${ip} broker ip is ${BROKER_HOST}" - podName=$(echo "$ENDPOINTS" | python3 -c "import sys, json; print(json.load(sys.stdin)['subsets'][0]['addresses'][${count}]['targetRef']['name'])") + podName=$(echo "$ENDPOINTS" | jq --argjson count ${count} -r '.subsets[0].addresses[$count].targetRef.name') if [ $? -ne 0 ]; then echo "[drain.sh] Can't find pod name to scale down to tried ${count}" exit 1 @@ -62,7 +62,7 @@ while true; do echo "[drain.sh] got podName ${podName} broker ip is ${BROKER_HOST}" if [ "$podName" != "$BROKER_HOST" ]; then # found an endpoint pod as a candidate for scaledown target - podNamespace=$(echo "$ENDPOINTS" | python3 -c "import sys, json; print(json.load(sys.stdin)['subsets'][0]['addresses'][${count}]['targetRef']['namespace'])") + podNamespace=$(echo "$ENDPOINTS" | jq --argjson count ${count} -r '.subsets[0].addresses[$count].targetRef.namespace') if [ $? -ne 0 ]; then echo "[drain.sh] Can't find pod namespace to scale down to tried ${count}" exit 1 @@ -135,7 +135,7 @@ waitForJolokia RET_CODE=$(curl -G -k "http://${AMQ_USER}:${AMQ_PASSWORD}@${BROKER_HOST}:8161/console/jolokia/exec/org.apache.activemq.artemis:broker=%22${AMQ_NAME}%22/scaleDown/scaledownconnector") -HTTP_CODE=$(echo "$RET_CODE" | python3 -c "import sys, json; print(json.load(sys.stdin)['status'])") +HTTP_CODE=$(echo "$RET_CODE" | jq -r '.status') echo "[drain.sh] curl return code ${HTTP_CODE}" @@ -159,22 +159,11 @@ waitForJolokia echo "[drain.sh] checking messages are all drained" RET_VALUE=$(curl -G -k "http://${AMQ_USER}:${AMQ_PASSWORD}@${BROKER_HOST}:8161/console/jolokia/read/org.apache.activemq.artemis:broker=%22${AMQ_NAME}%22/AddressNames") -PYCMD=$(cat < 0: - header = lines.pop(0) - tcp_lines.extend(lines) - tcp_file.close() - except IOError: - print("Could not open {}".format(tcp_file_path)) - -listening_ports = [] -for tcp_line in tcp_lines: - stripped = tcp_line.strip() - contents = stripped.split() - # Is the status listening? - if contents[3] == '0A': - netaddr = contents[1].split(":") - port = int(netaddr[1], 16) - listening_ports.append(port) - -#parse the config file to retrieve the transport connectors -xmldoc = xml.etree.ElementTree.parse("${CONFIG_FILE}") - -ns = {"config" : "urn:activemq:core"} -acceptors = xmldoc.findall("config:core/config:acceptors/config:acceptor", ns) - -result=0 - -for acceptor in acceptors: - name = acceptor.get("name") - value = acceptor.text.strip() - print("{} value {}".format(name, value)) - spliturl = urlsplit(value) - port = spliturl.port - - print("{} port {}".format(name, port)) - - if port == None: - print(" {} does not define a port, cannot check acceptor".format(name)) - result=1 - - try: - listening_ports.index(port) - print(" Transport is listening on port {}".format(port)) - except ValueError as e: - print(" Nothing listening on port {}, transport not yet running".format(port)) - result=1 -sys.exit(result) +EVALUATE_SCRIPT=$(cat <<'EOF' + RESULT=0 + TCP_CON="$(jq --raw-input --slurp 'split("\n") | .[1:-1] | .[] | capture("^ +(?[0-9]+): +(?[0-9A-F]+):(?[0-9A-F]+) +(?[0-9A-F]+):(?[0-9A-F]+) +(?[0-9A-F]+)")' /proc/net/tcp)" + TCP_CON="${TCP_CON}$(jq --raw-input --slurp 'split("\n") | .[1:-1] | .[] | capture("^ +(?[0-9]+): +(?[0-9A-F]+):(?[0-9A-F]+) +(?[0-9A-F]+):(?[0-9A-F]+) +(?[0-9A-F]+)")' /proc/net/tcp6)" + + while IFS= read -r ACCEPTOR_XML; do + ACCEPTOR_NAME="$(echo "${ACCEPTOR_XML}" | xmlstarlet sel -t -m //_:acceptor/@name -v .)" + ACCEPTOR_URL="$(echo "${ACCEPTOR_XML}" | xmlstarlet sel -t -m //_:acceptor -v .)" + echo "${ACCEPTOR_NAME} value ${ACCEPTOR_URL}" + + ACCEPTOR_PORT="$(jq --arg url "${ACCEPTOR_URL}" -n -r '$url | capture("^(?[^:]+)://((?[^@]+)@)*(?[^:]+):(?[0-9]+)[?]*").port')" + + if [ -n "${ACCEPTOR_PORT}" ]; then + LISTENING_TCP_CON="$(echo ${TCP_CON} | jq --arg port "$(printf "%04X" ${ACCEPTOR_PORT})" 'select(.cs == "0A" and .lp == $port)')" + + if [ -n "${LISTENING_TCP_CON}" ]; then + echo " Transport is listening on port ${ACCEPTOR_PORT}" + else + echo " Nothing listening on port ${ACCEPTOR_PORT}, transport not yet running" + RESULT=1 + fi + else + echo " ${ACCEPTOR_NAME} does not define a port, cannot check acceptor" + RESULT=1 + fi + done < <(xmlstarlet sel -N c="urn:activemq:core" -t -m "//c:acceptor" -c . -n broker/etc/broker.xml) + + exit ${RESULT} EOF ) @@ -97,7 +68,7 @@ while : ; do PROBE_MESSAGE="No configuration file located: ${CONFIG_FILE}" if [ -f "${CONFIG_FILE}" ] ; then - python3 -c "$EVALUATE_SCRIPT" >"${OUTPUT}" 2>"${ERROR}" + bash -c "${EVALUATE_SCRIPT}" >"${OUTPUT}" 2>"${ERROR}" CONNECT_RESULT=$? if [ true = "${DEBUG_SCRIPT}" ] ; then