From ce6d60c611381865d23a1dd670a2ef5d386c6ff7 Mon Sep 17 00:00:00 2001 From: Kroese Date: Thu, 7 Dec 2023 23:18:47 +0100 Subject: [PATCH] Parse JSON with JQ (#437) * feat: Parse JSON with JQ --- src/check.sh | 59 +++++++++++++++++------------------------- src/print.sh | 72 ++++++++++++++++++++++++++-------------------------- src/reset.sh | 5 ++-- 3 files changed, 62 insertions(+), 74 deletions(-) diff --git a/src/check.sh b/src/check.sh index 788d675..a12aac3 100644 --- a/src/check.sh +++ b/src/check.sh @@ -9,53 +9,40 @@ file="/run/dsm.url" if [ ! -f "$file" ]; then # Retrieve IP from guest VM for Docker healthcheck - RESPONSE=$(curl -s -m 30 -S http://127.0.0.1:2210/read?command=10 2>&1) - if [[ ! "${RESPONSE}" =~ "\"success\"" ]] ; then - echo "Failed to connect to guest: $RESPONSE" && exit 1 + { json=$(curl -m 30 -sk http://127.0.0.1:2210/read?command=10); rc=$?; } || : + (( rc != 0 )) && echo "Failed to connect to guest: curl error $rc" && exit 1 + + { result=$(echo "$json" | jq -r '.status'); rc=$?; } || : + (( rc != 0 )) && echo "Failed to parse response from guest: jq error $rc ( $json )" && exit 1 + [[ "$result" == "null" ]] && echo "Guest returned invalid response: $json" && exit 1 + + if [[ "$result" != "success" ]] ; then + { msg=$(echo "$json" | jq -r '.message'); rc=$?; } || : + echo "Guest replied ${result}: $msg" && exit 1 fi - # Retrieve the HTTP port number - if [[ ! "${RESPONSE}" =~ "\"http_port\"" ]] ; then - echo "Failed to parse response from guest: $RESPONSE" && exit 1 - fi + { port=$(echo "$json" | jq -r '.data.data.dsm_setting.data.http_port'); rc=$?; } || : + (( rc != 0 )) && echo "Failed to parse response from guest: jq error $rc ( $json )" && exit 1 + [[ "$port" == "null" ]] && echo "Guest has not set a portnumber yet.." && exit 1 + [ -z "${port}" ] && echo "Guest has not set a portnumber yet.." && exit 1 - rest=${RESPONSE#*http_port} - rest=${rest#*:} - rest=${rest%%,*} - PORT=${rest%%\"*} + { ip=$(echo "$json" | jq -r '.data.data.ip.data[] | select((.name=="eth0") and has("ip")).ip'); rc=$?; } || : + (( rc != 0 )) && echo "Failed to parse response from guest: jq error $rc ( $json )" && exit 1 + [[ "$ip" == "null" ]] && echo "Guest returned invalid response: $json" && exit 1 + [ -z "${ip}" ] && echo "Guest has not received an IP yet.." && exit 1 - [ -z "${PORT}" ] && echo "Guest has not set a portnumber yet.." && exit 1 - - # Retrieve the IP address - if [[ ! "${RESPONSE}" =~ "eth0" ]] ; then - echo "Failed to parse response from guest: $RESPONSE" && exit 1 - fi - - rest=${RESPONSE#*eth0} - rest=${rest#*ip} - rest=${rest#*:} - rest=${rest#*\"} - IP=${rest%%\"*} - - [ -z "${IP}" ] && echo "Guest has not received an IP yet.." && exit 1 - - echo "${IP}:${PORT}" > $file + echo "${ip}:${port}" > $file fi -LOCATION=$(cat "$file") +location=$(cat "$file") -if ! curl -m 20 -ILfSs "http://${LOCATION}/" > /dev/null; then +if ! curl -m 20 -ILfSs "http://${location}/" > /dev/null; then rm -f $file - echo "Failed to reach http://${LOCATION}" + echo "Failed to reach http://${location}" exit 1 fi -if [[ "$LOCATION" == "20.20"* ]]; then - echo "Healthcheck OK" -else - echo "Healthcheck OK ( ${LOCATION%:*} )" -fi - +echo "Healthcheck OK" exit 0 diff --git a/src/print.sh b/src/print.sh index 2ff28f7..1a6f998 100644 --- a/src/print.sh +++ b/src/print.sh @@ -1,8 +1,8 @@ #!/usr/bin/env bash set -Eeuo pipefail -info () { echo -e >&2 "\E[1;34m❯\E[1;36m $1\E[0m" ; } -error () { echo -e >&2 "\E[1;31m❯ ERROR: $1\E[0m" ; } +info () { printf "%b%s%b" "\E[1;34m❯ \E[1;36m" "$1" "\E[0m\n" >&2; } +error () { printf "%b%s%b" "\E[1;31m❯ " "ERROR: $1" "\E[0m\n" >&2; } file="/run/dsm.url" @@ -14,53 +14,53 @@ do # Retrieve IP from guest VM - set +e - RESPONSE=$(curl -s -m 30 -S http://127.0.0.1:2210/read?command=10 2>&1) - set -e + { json=$(curl -m 30 -sk http://127.0.0.1:2210/read?command=10); rc=$?; } || : + (( rc != 0 )) && error "Failed to connect to guest: curl error $rc" && continue - if [[ ! "${RESPONSE}" =~ "\"success\"" ]] ; then - error "Failed to connect to guest: $RESPONSE" && continue + { result=$(echo "$json" | jq -r '.status'); rc=$?; } || : + (( rc != 0 )) && error "Failed to parse response from guest: jq error $rc ( $json )" && continue + [[ "$result" == "null" ]] && error "Guest returned invalid response: $json" && continue + + if [[ "$result" != "success" ]] ; then + { msg=$(echo "$json" | jq -r '.message'); rc=$?; } || : + error "Guest replied ${result}: $msg" && continue fi - # Retrieve the HTTP port number - if [[ ! "${RESPONSE}" =~ "\"http_port\"" ]] ; then - error "Failed to parse response from guest: $RESPONSE" && continue - fi + { port=$(echo "$json" | jq -r '.data.data.dsm_setting.data.http_port'); rc=$?; } || : + (( rc != 0 )) && error "Failed to parse response from guest: jq error $rc ( $json )" && continue + [[ "$port" == "null" ]] && error "Guest returned invalid response: $json" && continue + [ -z "${port}" ] && continue - rest=${RESPONSE#*http_port} - rest=${rest#*:} - rest=${rest%%,*} - PORT=${rest%%\"*} + { ip=$(echo "$json" | jq -r '.data.data.ip.data[] | select((.name=="eth0") and has("ip")).ip'); rc=$?; } || : + (( rc != 0 )) && error "Failed to parse response from guest: jq error $rc ( $json )" && continue + [[ "$ip" == "null" ]] && error "Guest returned invalid response: $json" && continue + [ -z "${ip}" ] && continue - [ -z "${PORT}" ] && continue - - # Retrieve the IP address - if [[ ! "${RESPONSE}" =~ "eth0" ]] ; then - error "Failed to parse response from guest: $RESPONSE" && continue - fi - - rest=${RESPONSE#*eth0} - rest=${rest#*ip} - rest=${rest#*:} - rest=${rest#*\"} - IP=${rest%%\"*} - - [ -z "${IP}" ] && continue - - echo "${IP}:${PORT}" > $file + echo "${ip}:${port}" > $file done -LOCATION=$(cat "$file") +location=$(cat "$file") + +if [[ "$location" != "20.20"* ]]; then + + msg="http://${location}" -if [[ "$LOCATION" == "20.20"* ]]; then - MSG="port ${LOCATION##*:}" else - MSG="http://${LOCATION}" + + ip=$(ip address show dev eth0 | grep inet | awk '/inet / { print $2 }' | cut -f1 -d/) + port="${location##*:}" + + if [[ "$ip" == "172."* ]]; then + msg="port ${port}" + else + msg="http://${ip}:${port}" + fi + fi echo "" >&2 info "--------------------------------------------------------" -info " You can now login to DSM at ${MSG}" +info " You can now login to DSM at ${msg}" info "--------------------------------------------------------" echo "" >&2 diff --git a/src/reset.sh b/src/reset.sh index 2a970cf..b5e7035 100644 --- a/src/reset.sh +++ b/src/reset.sh @@ -1,8 +1,9 @@ #!/usr/bin/env bash set -Eeuo pipefail -info () { echo -e "\E[1;34m❯ \E[1;36m$1\E[0m" ; } -error () { echo -e >&2 "\E[1;31m❯ ERROR: $1\E[0m" ; } +info () { printf "%b%s%b" "\E[1;34m❯ \E[1;36m" "$1" "\E[0m\n"; } +error () { printf "%b%s%b" "\E[1;31m❯ " "ERROR: $1" "\E[0m\n" >&2; } + trap 'error "Status $? while: ${BASH_COMMAND} (line $LINENO/$BASH_LINENO)"' ERR [ ! -f "/run/entry.sh" ] && error "Script must run inside Docker container!" && exit 11