Unverified Commit ce6d60c6 authored by Kroese's avatar Kroese Committed by GitHub
Browse files

Parse JSON with JQ (#437)

* feat: Parse JSON with JQ
parent ff9fd9b3
Loading
Loading
Loading
Loading
+21 −34
Original line number Diff line number Diff line
@@ -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
  fi

  # Retrieve the HTTP port number
  if [[ ! "${RESPONSE}" =~ "\"http_port\"" ]] ; then
    echo "Failed to parse response from guest: $RESPONSE" && exit 1
  fi
  { 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

  rest=${RESPONSE#*http_port}
  rest=${rest#*:}
  rest=${rest%%,*}
  PORT=${rest%%\"*}
  { 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

  [ -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
  if [[ "$result" != "success" ]] ; then
    { msg=$(echo "$json" | jq -r '.message'); rc=$?; } || :
    echo "Guest replied ${result}: $msg" && exit 1
  fi

  rest=${RESPONSE#*eth0}
  rest=${rest#*ip}
  rest=${rest#*:}
  rest=${rest#*\"}
  IP=${rest%%\"*}
  { 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

  [ -z "${IP}" ] && echo "Guest has not received an IP yet.." && exit 1
  { 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

  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

exit 0
+34 −34
Original line number Diff line number Diff line
#!/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
  fi
  { 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

  # Retrieve the HTTP port number
  if [[ ! "${RESPONSE}" =~ "\"http_port\"" ]] ; then
    error "Failed to parse response from guest: $RESPONSE" && continue
  if [[ "$result" != "success" ]] ; then
    { msg=$(echo "$json" | jq -r '.message'); rc=$?; } || :
    error "Guest replied ${result}: $msg" && continue
  fi

  rest=${RESPONSE#*http_port}
  rest=${rest#*:}
  rest=${rest%%,*}
  PORT=${rest%%\"*}
  { 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

  [ -z "${PORT}" ] && continue
  { 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

  # Retrieve the IP address
  if [[ ! "${RESPONSE}" =~ "eth0" ]] ; then
    error "Failed to parse response from guest: $RESPONSE" && continue
  fi
  echo "${ip}:${port}" > $file

  rest=${RESPONSE#*eth0}
  rest=${rest#*ip}
  rest=${rest#*:}
  rest=${rest#*\"}
  IP=${rest%%\"*}
done

  [ -z "${IP}" ] && continue
location=$(cat "$file")

  echo "${IP}:${PORT}" > $file
if [[ "$location" != "20.20"* ]]; then

done
  msg="http://${location}"

LOCATION=$(cat "$file")
else

  ip=$(ip address show dev eth0 | grep inet | awk '/inet / { print $2 }' | cut -f1 -d/)
  port="${location##*:}"

if [[ "$LOCATION" == "20.20"* ]]; then
  MSG="port ${LOCATION##*:}"
  if [[ "$ip" == "172."* ]]; then
    msg="port ${port}"
  else
  MSG="http://${LOCATION}"
    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
+3 −2
Original line number Diff line number Diff line
#!/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