Skip to content

Commit

Permalink
Handle non-success error codes in healthcheck (#1651)
Browse files Browse the repository at this point in the history
* Handle non-success error codes in healthcheck

Only attempt to parse response as JSON for 200 success responses
Avoids attempting to parse HTML from 502 Bad Gateway responses

* Update healthcheck script to use tmp output folder

Set .gitignore in tmp folder (and remove from main gitignore)
Tidy handling of showing output
  • Loading branch information
stuartleeks authored Apr 6, 2022
1 parent 9482022 commit db5cc7c
Showing 1 changed file with 19 additions and 5 deletions.
24 changes: 19 additions & 5 deletions devops/scripts/api_healthcheck.sh
Original file line number Diff line number Diff line change
@@ -1,19 +1,33 @@
#!/bin/bash
set -e

response=$(curl -k "https://${TRE_ID}.${LOCATION}.cloudapp.azure.com/api/health")
# Get the directory that this script is in
DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" >/dev/null 2>&1 && pwd )"
# Create a .gitignore'd directory for temp output
mkdir -p "$DIR/script_tmp"
echo '*' > "$DIR/script_tmp/.gitignore"
api_response_file="$DIR/script_tmp/api_response.txt"

echo "Calling /health endpoint..."
response_code=$(curl --insecure --silent --output "$api_response_file" --write-out "%{http_code}" "https://${TRE_ID}.${LOCATION}.cloudapp.azure.com/api/health")

echo "Got response from https://${TRE_ID}.${LOCATION}.cloudapp.azure.com/api/health:"
echo "$response"
echo

not_ok_count=$(echo "${response}" | jq -r '[.services | .[] | select(.status!="OK")] | length')
if [[ "$response_code" != "200" ]]; then
echo "*** ⚠️ API _not_ healthy ***"
echo "Non-success code returned: $response_code"
echo "Response:"
cat "$api_response_file"
exit 1
fi

not_ok_count=$(jq -r '[.services | .[] | select(.status!="OK")] | length' < "$api_response_file")

if [[ "$not_ok_count" == "0" ]]; then
echo "*** ✅ API healthy ***"
else
echo "*** ⚠️ API _not_ healthy ***"
echo "Unhealthy services:"
echo "${response}" | jq -r '[.services | .[] | select(.status!="OK")]'
jq -r '[.services | .[] | select(.status!="OK")]' < "$api_response_file"
exit 1
fi

0 comments on commit db5cc7c

Please sign in to comment.