-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #4 from adrianmusante/HEALTH-CHECK
Health Check utility
- Loading branch information
Showing
4 changed files
with
83 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
#!/bin/bash | ||
set -eo pipefail | ||
|
||
_help() { | ||
local _bin="${0##*/}" | ||
cat 1>&2 <<MSG | ||
Utility to check if SonarQube is healthy. | ||
Usage: $_bin [options] ... | ||
Some of the options include: | ||
-u <HEALTH_CHECK_URL> URL used to check the status of SonarQube. (Optional) | ||
-s <STATUS> Repeat this option to add more valid status. Possible status: | ||
- STARTING: Server initialization is ongoing | ||
- UP: SonarQube instance is up and running (always added as valid) | ||
- DOWN: Instance is up but not running (e.g., due to migration failure) | ||
- RESTARTING: Restart has been requested | ||
- DB_MIGRATION_NEEDED: Database migration required | ||
- DB_MIGRATION_RUNNING: Database migration in progress | ||
-h display this help and exit | ||
Example: | ||
- $_bin | ||
- $_bin -u http://my-host:9000/api/system/status | ||
- $_bin -s STARTING -s RESTARTING | ||
- $_bin -s DB_MIGRATION_NEEDED -s DB_MIGRATION_RUNNING | ||
MSG | ||
exit 2 | ||
} | ||
|
||
regex_in() { | ||
echo "^($(printf '%s\n' "$@" | tr ' ' '\n' | tr ',' '\n' | tr ';' '\n' | grep . | tr -d ' ' | tr '\n' '|' | rev | cut -d '|' -f2- | rev))\$" | ||
} | ||
|
||
declare -a statuses=(UP) | ||
health_check_url="" | ||
while getopts u:s:h opt; do | ||
case $opt in | ||
u) health_check_url="$OPTARG" ;; | ||
s) statuses+=("$OPTARG");; | ||
*) _help ;; | ||
#... | ||
esac | ||
done | ||
|
||
if [ -z "$health_check_url" ]; then | ||
. "$ADDONS_HOME/scripts/sonarqube-env.sh" | ||
health_check_url="$SONARQUBE_API_URL/system/status" | ||
fi | ||
|
||
result="$(curl -sSfL "$health_check_url" || true)" | ||
if [ -z "${result:-}" ]; then | ||
echo "Couldn't connect to $health_check_url" 1>&2 | ||
exit 1 | ||
fi | ||
|
||
echo "$result" | ||
status="$(echo "$result" | json_pp 2>/dev/null | grep '"status"' | cut -d '"' -f4 | tr -d '\\n')" | ||
grep -qE "$(regex_in "${statuses[@]}")" <(echo "$status") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters