From f93dfe007e7ceaee8c11eea9a88388c2ffdc8b2c Mon Sep 17 00:00:00 2001 From: Antoine Cotten Date: Tue, 21 Jun 2022 15:45:20 +0200 Subject: [PATCH] feat: Log setup error in case of failed curl command --- setup/entrypoint.sh | 28 +++++++++++++++++++++++++--- setup/helpers.sh | 15 +++++++++++++-- 2 files changed, 38 insertions(+), 5 deletions(-) diff --git a/setup/entrypoint.sh b/setup/entrypoint.sh index 269bb4f..53a8c6d 100755 --- a/setup/entrypoint.sh +++ b/setup/entrypoint.sh @@ -33,14 +33,36 @@ roles_files=( echo "-------- $(date) --------" -state_file="$(dirname ${BASH_SOURCE[0]})/state/.done" +state_file="$(dirname "${BASH_SOURCE[0]}")/state/.done" if [[ -e "$state_file" ]]; then log "State file exists at '${state_file}', skipping setup" exit 0 fi -log 'Waiting for availability of Elasticsearch' -wait_for_elasticsearch +log 'Waiting for availability of Elasticsearch. This can take several minutes.' + +declare -i exit_code=0 +wait_for_elasticsearch || exit_code=$? + +if ((exit_code)); then + case $exit_code in + 6) + suberr 'Could not resolve host. Is Elasticsearch running?' + ;; + 7) + suberr 'Failed to connect to host. Is Elasticsearch healthy?' + ;; + 28) + suberr 'Timeout connecting to host. Is Elasticsearch healthy?' + ;; + *) + suberr "Connection to Elasticsearch failed. Exit code: ${exit_code}" + ;; + esac + + exit $exit_code +fi + sublog 'Elasticsearch is running' for role in "${!roles_files[@]}"; do diff --git a/setup/helpers.sh b/setup/helpers.sh index 2457372..20e8339 100644 --- a/setup/helpers.sh +++ b/setup/helpers.sh @@ -15,6 +15,11 @@ function err { echo "[x] $1" >&2 } +# Log an error at a sub-level. +function suberr { + echo " ⠍ $1" >&2 +} + # Poll the 'elasticsearch' service until it responds with HTTP code 200. function wait_for_elasticsearch { local elasticsearch_host="${ELASTICSEARCH_HOST:-elasticsearch}" @@ -30,7 +35,13 @@ function wait_for_elasticsearch { # retry for max 300s (60*5s) for _ in $(seq 1 60); do - output="$(curl "${args[@]}" || true)" + local -i exit_code=0 + output="$(curl "${args[@]}")" || exit_code=$? + + if ((exit_code)); then + result=$exit_code + fi + if [[ "${output: -3}" -eq 200 ]]; then result=0 break @@ -39,7 +50,7 @@ function wait_for_elasticsearch { sleep 5 done - if ((result)); then + if ((result)) && [[ "${output: -3}" -ne 000 ]]; then echo -e "\n${output::-3}" fi