Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[#112] Remove python dependency #113

Merged
merged 1 commit into from
Dec 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions image.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,8 @@ packages:
- apr
- procps-ng
- bind-utils
- jq
- xmlstarlet

modules:
repositories:
Expand Down
23 changes: 6 additions & 17 deletions modules/activemq-artemis-launch/added/drain.sh
Original file line number Diff line number Diff line change
Expand Up @@ -48,21 +48,21 @@ 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
fi
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
Expand Down Expand Up @@ -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}"

Expand All @@ -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 <<EOF
import sys, json
addrs = ''
value = json.load(sys.stdin)['value']
for addr in value:
addrs = addrs + ' ' + addr
print(addrs)
EOF
)
all_addresses=$(echo "$RET_VALUE" | python3 -c "$PYCMD")
arr=($all_addresses)
for address in "${arr[@]}"
for address in $(echo ${RET_VALUE} | jq -r '.value[]')
do
echo "[drain.sh] checking on address ${address}"
M_COUNT=$(curl -G -k "http://${AMQ_USER}:${AMQ_PASSWORD}@${BROKER_HOST}:8161/console/jolokia/read/org.apache.activemq.artemis:broker=%22${AMQ_NAME}%22,address=%22${address}%22,component=addresses/MessageCount")
value=$(echo "$M_COUNT" | python3 -c "import sys, json; print(json.load(sys.stdin)['value'])")
value=$(echo "$M_COUNT" | jq -r '.value')
if [[ $value -gt 0 ]]; then
echo "[drain.sh] scaledown not complete. There are $value messages on address $address"
"${instanceDir}/bin/artemis-service" stop
Expand Down
87 changes: 29 additions & 58 deletions modules/activemq-artemis-launch/added/readinessProbe.sh
Original file line number Diff line number Diff line change
Expand Up @@ -16,63 +16,34 @@ COUNT=30
SLEEP=1
DEBUG_SCRIPT=true

EVALUATE_SCRIPT=$(cat <<EOF
import xml.etree.ElementTree
from urllib.parse import urlsplit
import socket
import sys

# calculate the open ports
tcp_lines = []
for tcp_file_path in ["/proc/net/tcp", "/proc/net/tcp6"]:
try:
tcp_file = open(tcp_file_path, "r")
lines = tcp_file.readlines()
if len(lines) > 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("^ +(?<en>[0-9]+): +(?<la>[0-9A-F]+):(?<lp>[0-9A-F]+) +(?<ra>[0-9A-F]+):(?<rp>[0-9A-F]+) +(?<cs>[0-9A-F]+)")' /proc/net/tcp)"
TCP_CON="${TCP_CON}$(jq --raw-input --slurp 'split("\n") | .[1:-1] | .[] | capture("^ +(?<en>[0-9]+): +(?<la>[0-9A-F]+):(?<lp>[0-9A-F]+) +(?<ra>[0-9A-F]+):(?<rp>[0-9A-F]+) +(?<cs>[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("^(?<scheme>[^:]+)://((?<user>[^@]+)@)*(?<host>[^:]+):(?<port>[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
)

Expand All @@ -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
Expand Down
Loading