Skip to content

Commit bb863ac

Browse files
authored
Merge pull request #9 from jpartlow/fix-retry-function
Fix retry function
2 parents d224f66 + 30bd505 commit bb863ac

File tree

3 files changed

+49
-17
lines changed

3 files changed

+49
-17
lines changed

.github/workflows/pr_testing.yaml

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,8 @@ jobs:
4141
- name: Verify openvox-agent is installed
4242
run: |-
4343
[[ "$(/opt/puppetlabs/bin/puppet --version)" =~ [0-9]+\.[0-9]+\.[0-9]+ ]]
44+
- name: Verify idempotency
45+
run: bolt task run openvox_bootstrap::install --targets localhost --run-as root
4446

4547
test-install-task-on-other-os-via-containers:
4648
strategy:
@@ -75,6 +77,13 @@ jobs:
7577
run: ./openvox_bootstrap/tasks/install_linux.sh
7678
- name: Verify openvox-agent is installed
7779
run: /opt/puppetlabs/bin/puppet --version | grep -E '8\.[0-9]+'
80+
- name: Verify idempotency
81+
env:
82+
PT__installdir: ${{ github.workspace }}
83+
PT_collection: openvox8
84+
PT_apt_source: "https://apt.overlookinfratech.com"
85+
PT_yum_source: "https://yum.overlookinfratech.com"
86+
run: ./openvox_bootstrap/tasks/install_linux.sh
7887

7988
test-install-version:
8089
strategy:
@@ -104,3 +113,10 @@ jobs:
104113
run: ./openvox_bootstrap/tasks/install_linux.sh
105114
- name: Verify openvox-agent is installed
106115
run: /opt/puppetlabs/bin/puppet --version | grep '8.14.0'
116+
- name: Verify idempotency
117+
env:
118+
PT__installdir: ${{ github.workspace }}
119+
PT_version: "8.14.0"
120+
PT_apt_source: "https://apt.overlookinfratech.com"
121+
PT_yum_source: "https://yum.overlookinfratech.com"
122+
run: ./openvox_bootstrap/tasks/install_linux.sh

.github/workflows/pr_testing_install_build_artifact.yaml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,8 @@ jobs:
3131
- name: Verify openvox-agent is installed
3232
run: |-
3333
[[ "$(/opt/puppetlabs/bin/puppet --version)" = "8.15.0" ]]
34+
- name: Verify idempotency
35+
run: bolt task run openvox_bootstrap::install_build_artifact version=8.15.0 --targets localhost --run-as root
3436

3537
test-install-build-artifact-task-on-other-os-via-containers:
3638
strategy:
@@ -64,3 +66,8 @@ jobs:
6466
shell: bash
6567
run: |-
6668
[[ "$(/opt/puppetlabs/bin/puppet --version)" = "8.15.0" ]]
69+
- name: Verify idempotency
70+
env:
71+
PT__installdir: ${{ github.workspace }}
72+
PT_version: "8.15.0"
73+
run: ./openvox_bootstrap/tasks/install_build_artifact_linux.sh

files/common.sh

Lines changed: 26 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -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.
5054
exec_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

Comments
 (0)