@@ -45,21 +45,25 @@ exists() {
4545# Log and execute a command in a subshell, capturing and echoing its
4646# output before returning its exit status.
4747#
48- # Output is also captured in the variable EXEC_AND_CAPTURE_RESULT
49- # for other functions to inspect.
48+ # Also captures output and status in the global variables:
49+ #
50+ # LAST_EXEC_AND_CAPTURE_OUTPUT
51+ # LAST_EXEC_AND_CAPTURE_STATUS
52+ #
53+ # so that the caller can inspect them as well.
5054exec_and_capture () {
5155 local _cmd=" $* "
5256
5357 info " Executing: ${_cmd} "
5458
5559 set +e
56- EXEC_AND_CAPTURE_RESULT =$( ${_cmd} 2>&1 )
57- local _status =$?
60+ LAST_EXEC_AND_CAPTURE_OUTPUT =$( ${_cmd} 2>&1 )
61+ LAST_EXEC_AND_CAPTURE_STATUS =$?
5862 set -e
5963
60- echo " ${EXEC_AND_CAPTURE_RESULT } "
61- info " Status: ${_status } "
62- return $_status
64+ echo " ${LAST_EXEC_AND_CAPTURE_OUTPUT } "
65+ info " Status: ${LAST_EXEC_AND_CAPTURE_STATUS } "
66+ return $LAST_EXEC_AND_CAPTURE_STATUS
6367}
6468
6569# If the passed command fails with output matching the given regex,
@@ -82,19 +86,24 @@ with_retries_if() {
8286 shift 3
8387
8488 local _cmd=" $* "
89+ local _result
8590 local _status
8691
87- for (( i = 0 ; i < _retries; i++ )) ; do
88- info " Attempt $(( i + 1 )) of $_retries : ${_cmd} "
89- exec_and_capture " ${_cmd} "
90- _status=$?
91- if [[ " ${_status} " == 0 ]]; then
92+ for (( i = 1 ; i <= _retries; i++ )) ; do
93+ if [[ $i -gt 1 ]]; then
94+ info " Retrying in ${_delay} seconds..."
95+ sleep " ${_delay} "
96+ fi
97+
98+ info " Attempt ${i} of $_retries : ${_cmd} "
99+
100+ if exec_and_capture " ${_cmd} " ; then
101+ _status=$LAST_EXEC_AND_CAPTURE_STATUS
92102 break # command succeeded
93103 else
94- if [[ " ${EXEC_AND_CAPTURE_OUTPUT} " =~ ${_error_regex} ]]; then
95- info " Retrying in ${_delay} seconds..."
96- sleep " ${_delay} "
97- else
104+ _result=" ${LAST_EXEC_AND_CAPTURE_OUTPUT} "
105+ _status=$LAST_EXEC_AND_CAPTURE_STATUS
106+ if ! [[ " ${_result} " =~ ${_error_regex} ]]; then
98107 info " Command failed but output did not match /${_error_regex} /. Aborting retries."
99108 break
100109 fi
@@ -260,7 +269,7 @@ install_package_file() {
260269 info " Installing release package '${_package_file} ' of type '${_package_type} '"
261270 case $_package_type in
262271 rpm)
263- rpm_with_retries -Uvh " $_package_file "
272+ rpm_with_retries -Uvh --replacepkgs " $_package_file "
264273 ;;
265274 deb)
266275 dpkg_with_retries -i " $_package_file "
0 commit comments