From 57377e0a0eed4cabbb19476519a4bf02c3aebe71 Mon Sep 17 00:00:00 2001 From: William Wernert Date: Tue, 16 Nov 2021 10:46:48 -0500 Subject: [PATCH 1/4] Add retry support + more precise logging to so-preflight --- setup/so-preflight | 36 ++++++++++++++++++++++++++++-------- 1 file changed, 28 insertions(+), 8 deletions(-) diff --git a/setup/so-preflight b/setup/so-preflight index 756c721dc..5613c1f31 100755 --- a/setup/so-preflight +++ b/setup/so-preflight @@ -15,11 +15,19 @@ # You should have received a copy of the GNU General Public License # along with this program. If not, see . +cd "$(dirname "$0")" || exit 255 + source ../salt/common/tools/sbin/so-common source ./so-functions script_run="$1" +retry_count=10 +retry_sleep=5 +warning_prefix="[WARNING]" +info_prefix="[INFO ]" +error_prefix="[ERROR ]" + if [[ $script_run == true ]]; then preflight_log="${2:-'/root/preflight.log'}" else @@ -129,19 +137,30 @@ __check_url_arr() { local ret_code=0 echo "" >> "$preflight_log" for url in "$@"; do - local status - status=$(curl -s -o /dev/null -w "%{http_code}" -L "$url" 2> /dev/null) - local ret=$? + # Reset vars + local status=999 # Set status to something outside the range of normal HTTP codes but above the 200 range + local ret=1 + local count=0 + + while [[ $ret != 0 && $count -lt $retry_count ]]; do + ((count++)) + [[ $count != 1 ]] && sleep $retry_sleep + status=$(curl -s -o /dev/null -w "%{http_code}" -L "$url" 2> /dev/null) + ret=$? + + [[ $ret != 0 ]] && echo "$warning_prefix ($count/$retry_count) Could not reach $url. curl error code: $ret" >> "$preflight_log" + done + if [[ $ret == 0 ]]; then - printf '%s' " - Successfully reached $url" >> "$preflight_log" + success_str="Successfully reached $url" if [[ $status -ge 400 ]]; then - printf '%s\n' " but server responded with error code $status" >> "$preflight_log" + echo "$warning_prefix $success_str but server responded with HTTP code $status." >> "$preflight_log" else - printf '\n' >> "$preflight_log" + echo "$info_prefix $success_str." >> "$preflight_log" fi else ret_code=1 - echo " - [ERROR]: Could not reach $url" >> "$preflight_log" + echo "$error_prefix Could not reach $url after $retry_count attempts." >> "$preflight_log" fi done echo "" >> "$preflight_log" @@ -153,9 +172,10 @@ main() { local success_str="Pre-flight checks completed successfully!" local fail_str="Pre-flight checks could not complete." + [[ -f $preflight_log ]] || touch "$preflight_log" + detect_os "$preflight_log" - [[ -f $preflight_log ]] || touch "$preflight_log" if [[ $script_run == true ]]; then echo "$intro_str" else From 4e3f43bee418eecc6bf4a404a4bc015603b254c2 Mon Sep 17 00:00:00 2001 From: William Wernert Date: Tue, 16 Nov 2021 10:53:22 -0500 Subject: [PATCH 2/4] Fix variable name --- setup/so-preflight | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/setup/so-preflight b/setup/so-preflight index 5613c1f31..5a03afee0 100755 --- a/setup/so-preflight +++ b/setup/so-preflight @@ -152,11 +152,11 @@ __check_url_arr() { done if [[ $ret == 0 ]]; then - success_str="Successfully reached $url" + url_success_str="Successfully reached $url" if [[ $status -ge 400 ]]; then - echo "$warning_prefix $success_str but server responded with HTTP code $status." >> "$preflight_log" + echo "$warning_prefix $url_success_str but server responded with HTTP code $status." >> "$preflight_log" else - echo "$info_prefix $success_str." >> "$preflight_log" + printf '%s\n' "$info_prefix $url_success_str." >> "$preflight_log" fi else ret_code=1 From d4d9032bfcb19359a7c49105119a4f95f7334e0d Mon Sep 17 00:00:00 2001 From: William Wernert Date: Tue, 16 Nov 2021 10:56:31 -0500 Subject: [PATCH 3/4] Remove confusing punctuation --- setup/so-preflight | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/setup/so-preflight b/setup/so-preflight index 5a03afee0..9515f922c 100755 --- a/setup/so-preflight +++ b/setup/so-preflight @@ -148,7 +148,7 @@ __check_url_arr() { status=$(curl -s -o /dev/null -w "%{http_code}" -L "$url" 2> /dev/null) ret=$? - [[ $ret != 0 ]] && echo "$warning_prefix ($count/$retry_count) Could not reach $url. curl error code: $ret" >> "$preflight_log" + [[ $ret != 0 ]] && echo "$warning_prefix ($count/$retry_count) Could not reach $url, curl error code: $ret" >> "$preflight_log" done if [[ $ret == 0 ]]; then @@ -156,7 +156,7 @@ __check_url_arr() { if [[ $status -ge 400 ]]; then echo "$warning_prefix $url_success_str but server responded with HTTP code $status." >> "$preflight_log" else - printf '%s\n' "$info_prefix $url_success_str." >> "$preflight_log" + printf '%s\n' "$info_prefix $url_success_str" >> "$preflight_log" fi else ret_code=1 From a2152446eafd714948b8c52824457e8382075918 Mon Sep 17 00:00:00 2001 From: William Wernert Date: Tue, 16 Nov 2021 11:08:13 -0500 Subject: [PATCH 4/4] Pad count string to align text --- setup/so-preflight | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/setup/so-preflight b/setup/so-preflight index 9515f922c..3050c94cb 100755 --- a/setup/so-preflight +++ b/setup/so-preflight @@ -147,8 +147,9 @@ __check_url_arr() { [[ $count != 1 ]] && sleep $retry_sleep status=$(curl -s -o /dev/null -w "%{http_code}" -L "$url" 2> /dev/null) ret=$? - - [[ $ret != 0 ]] && echo "$warning_prefix ($count/$retry_count) Could not reach $url, curl error code: $ret" >> "$preflight_log" + local count_str + printf -v count_str '%02d' "$count" + [[ $ret != 0 ]] && echo "$warning_prefix ($count_str/$retry_count) Could not reach $url, curl error code: $ret" >> "$preflight_log" done if [[ $ret == 0 ]]; then