-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Factor out a script to generate a new configuration. (#87)
### What This removes some duplication, but more importantly, it allows us to get a little more sophisticated. Now, when a request fails, we print the response status _and_ the body. Previously, we only printed the status. We sometimes see a 500 Internal Server Error and don't know why. Hopefully this will help us get to the bottom of it. ### How Instead of using `curl -f`, which prints a simple error, we capture the status code using `--write-out '%{http_code}\n'`, and the body using `--output $OUTPUT_FILE`. We can then use this to construct a more useful error message. All other logic stays the same.
- Loading branch information
1 parent
dfd2d84
commit 4bfd5d6
Showing
5 changed files
with
60 additions
and
23 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
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,51 @@ | ||
#!/usr/bin/env bash | ||
|
||
set -e | ||
set -u | ||
set -o pipefail | ||
|
||
CURRENT_DIR="$(cd -- "$(dirname -- "${BASH_SOURCE[0]}")" > /dev/null && echo "$PWD")" | ||
|
||
if [[ $# -lt 2 ]]; then | ||
echo >&2 "Usage: $0 CONFIGURATION-SERVER POSTGRESQL-CONNECTION-URI [EXTRA-CONFIG]" | ||
echo >&2 | ||
echo >&2 ' The configuration server should be in the form "HOST:PORT".' | ||
exit 2 | ||
fi | ||
|
||
export CONFIGURATION_SERVER="$1" # exported to use in the `wait-until` call | ||
POSTGRESQL_CONNECTION_URI="$2" | ||
EXTRA_CONFIG="${3:-{\}}" # this defaults to '{}' | ||
|
||
# wait until the server is up and running | ||
"${CURRENT_DIR}/wait-until" --timeout=30 --report -- sh -c 'curl -fsS "http://${CONFIGURATION_SERVER}/health" > /dev/null' | ||
|
||
function get { | ||
# write HTTP responses to this file | ||
OUTPUT_FILE="$(mktemp)" | ||
trap 'rm -f "$OUTPUT_FILE"' RETURN | ||
|
||
# capture the status in the variable, and the body in $OUTPUT_FILE | ||
response_status="$(curl --silent --output "$OUTPUT_FILE" --write-out '%{http_code}\n' "$@")" | ||
if [[ "$response_status" -ge 200 && "$response_status" -lt 300 ]]; then | ||
cat "$OUTPUT_FILE" | ||
else | ||
# on failure, log the response status and body | ||
echo >&2 "Request to ${1} failed with status ${response_status}." | ||
echo >&2 "Response body:" | ||
cat "$OUTPUT_FILE" >&2 | ||
echo >&2 | ||
echo >&2 | ||
return 1 | ||
fi | ||
} | ||
|
||
# 1. Get an empty configuration. | ||
# 2. Splice in the connection URI and extra config. | ||
# 3. Send that configuration back to get the real thing. | ||
# 4. Reformat with `jq`. | ||
# 5. Print the generated configuration to STDOUT. | ||
get "http://${CONFIGURATION_SERVER}/" \ | ||
| jq --arg uri "$POSTGRESQL_CONNECTION_URI" --argjson extra "$EXTRA_CONFIG" '. * $extra + {"connectionUri": {"uri": {"value": $uri}}}' \ | ||
| get "http://${CONFIGURATION_SERVER}/" -H 'Content-Type: application/json' -d @- \ | ||
| jq . |