Parse JSON with JQ (#437)

* feat: Parse JSON with JQ
This commit is contained in:
Kroese 2023-12-07 23:18:47 +01:00 committed by GitHub
parent ff9fd9b377
commit ce6d60c611
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 62 additions and 74 deletions

View File

@ -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

View File

@ -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

View File

@ -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