Skip to content

Commit 19b82c3

Browse files
authored
Merge pull request #9 from cyberark/conjurinc-ops-423
All libraries are loaded by init script & retry function added
2 parents 6f76400 + 78ac6b4 commit 19b82c3

File tree

18 files changed

+187
-56
lines changed

18 files changed

+187
-56
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,7 @@ files within it's directory.
136136
<ol>
137137
<li><b>die</b>: print message and exit 1</li>
138138
<li><b>spushd/spopd</b>: Safe verisons of pushd & popd that call die if the push/pop fails, they also drop stdout. </li>
139+
<li><b>retry</b>: Retry a command until it succeeds up to a user specified maximum number of attempts. Escalating delay between attempts.</li>
139140
</ol>
140141
</td>
141142
</tr>

filehandling/lib

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,28 @@
11
#!/bin/bash
22

33
: "${BASH_LIB_DIR:?BASH_LIB_DIR must be set. Please source bash-lib/init before other scripts from bash-lib.}"
4-
. "${BASH_LIB_DIR}/helpers/lib"
54

65
#https://stackoverflow.com/a/23002317
76
function abs_path() {
87
# generate absolute path from relative path
9-
# $1 : relative filename
8+
# path : relative filename
109
# return : absolute path
11-
if [ -d "$1" ]; then
10+
if [[ -z "${1:-}" ]]; then
11+
path="."
12+
else
13+
path="${1}"
14+
fi
15+
if [ -d "${path}" ]; then
1216
# dir
13-
(spushd "$1"; pwd)
14-
elif [ -f "$1" ]; then
17+
(spushd "${path}"; pwd)
18+
elif [ -f "${path}" ]; then
1519
# file
16-
if [[ $1 = /* ]]; then
17-
echo "$1"
18-
elif [[ $1 == */* ]]; then
19-
echo "$(spushd "${1%/*}"; pwd)/${1##*/}"
20+
if [[ ${path} = /* ]]; then
21+
echo "${path}"
22+
elif [[ ${path} == */* ]]; then
23+
echo "$(spushd "${path%/*}"; pwd)/${path##*/}"
2024
else
21-
echo "$(pwd)/$1"
25+
echo "$(pwd)/${path}"
2226
fi
2327
fi
24-
}
28+
}

git/lib

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
#!/bin/bash
22

33
: "${BASH_LIB_DIR:?BASH_LIB_DIR must be set. Please source bash-lib/init before other scripts from bash-lib.}"
4-
. "${BASH_LIB_DIR}/helpers/lib"
54

65
# Get the top level of a git repo
76
function repo_root(){
@@ -76,4 +75,4 @@ space seperated with three fields: subtree_path renmote_url remote_name"
7675
function tracked_files_excluding_subtrees(){
7776
subtrees="$(cat_gittrees | awk '{print $1}' | paste -sd '|' -)"
7877
all_files_in_repo | grep -E -v "${subtrees}"
79-
}
78+
}

helpers/lib

Lines changed: 55 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,4 +17,58 @@ function spushd(){
1717
#safe popd
1818
function spopd(){
1919
popd >/dev/null || die "popd failed :("
20-
}
20+
}
21+
22+
# Retry a command multiple times until it succeeds, with escalating
23+
# delay between attempts.
24+
# Delay is 2 * n + random up to 30s, then 30s + random after that.
25+
# For large numbers of retries the max delay is effectively the retry
26+
# in minutes.
27+
# Based on:
28+
# https://gist.github.com/sj26/88e1c6584397bb7c13bd11108a579746
29+
# but now quite heavily modified.
30+
function retry {
31+
# Maxiumum amount of fixed delay between attempts
32+
# a random value will still be added.
33+
local -r MAX_BACKOFF=30
34+
35+
if [[ ${#} -lt 2 ]]; then
36+
echo "retry usage: retry <retries> <command>"
37+
exit 1
38+
fi
39+
40+
local retries=$1
41+
shift
42+
43+
if ! [[ ${retries} =~ ^[0-9\.]*$ ]]; then
44+
echo "Invalid number of retries: ${retries} for command '${*}'".
45+
exit 1
46+
fi
47+
48+
local count=0
49+
until "$@"; do
50+
# Command failed, otherwise until would have skipped the loop
51+
52+
# Store return code so it can be reported to the user
53+
exit=$?
54+
count=$((count + 1))
55+
if [ "${count}" -lt "${retries}" ]; then
56+
# There are still retries left, calculate delay and notify user.
57+
backoff=$((2 * count))
58+
if [[ "${backoff}" -gt "${MAX_BACKOFF}" ]]; then
59+
backoff=${MAX_BACKOFF}
60+
fi;
61+
62+
# Add a random amount to the delay to prevent competing processes
63+
# from re-colliding.
64+
wait=$(( backoff + (RANDOM % count) ))
65+
echo "'${*}' Retry $count/$retries exited $exit, retrying in $wait seconds..."
66+
sleep $wait
67+
else
68+
# Out of retries :(
69+
echo "Retry $count/$retries exited $exit, no more retries left."
70+
return $exit
71+
fi
72+
done
73+
return 0
74+
}

init

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,18 +20,18 @@ BASH_LIB_DIR="${BASH_LIB_DIR_RELATIVE}"
2020

2121
# Load the filehandling module for the abspath
2222
# function
23-
. "${BASH_LIB_DIR_RELATIVE}/filehandling/lib"
23+
for lib in helpers logging filehandling git k8s test-utils; do
24+
. "${BASH_LIB_DIR_RELATIVE}/${lib}/lib"
25+
done
2426

2527
# Export the absolute path
2628
# shellcheck disable=SC2086
2729
BASH_LIB_DIR="$(abs_path ${BASH_LIB_DIR_RELATIVE})"
2830
export BASH_LIB_DIR
2931

30-
. "${BASH_LIB_DIR}/helpers/lib"
31-
3232
# Update Submodules
3333
spushd "${BASH_LIB_DIR}"
3434
git submodule update --init --recursive
3535
spopd
3636

37-
export BATS_CMD="${BASH_LIB_DIR}/test-utils/bats/bin/bats"
37+
export BATS_CMD="${BASH_LIB_DIR}/test-utils/bats/bin/bats"

k8s/lib

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
#!/bin/bash
22

33
: "${BASH_LIB_DIR:?BASH_LIB_DIR must be set. Please source bash-lib/init before other scripts from bash-lib.}"
4-
. "${BASH_LIB_DIR}/helpers/lib"
54

65
# Sets additional required environment variables that aren't available in the
76
# secrets.yml file, and performs other preparatory steps
@@ -52,4 +51,4 @@ function run_docker_gke_command() {
5251
/scripts/platform_login
5352
${1}
5453
"
55-
}
54+
}

run-tests

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
# is not assumed to have been run
55
# shellcheck disable=SC2086
66
. "$(dirname ${BASH_SOURCE[0]})/init"
7-
. "${BASH_LIB_DIR}/helpers/lib"
87

98
# Run BATS Tests
109
"${BASH_LIB_DIR}/tests-for-this-repo/run-bats-tests"

test-utils/lib

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,9 @@
11
#!/bin/bash
22

33
: "${BASH_LIB_DIR:?BASH_LIB_DIR must be set. Please source bash-lib/init before other scripts from bash-lib.}"
4-
. "${BASH_LIB_DIR}/git/lib"
5-
. "${BASH_LIB_DIR}/helpers/lib"
64

7-
readonly SHELLCHECK_IMAGE="${SHELLCHECK_IMAGE:-koalaman/shellcheck}"
8-
readonly SHELLCHECK_TAG="${SHELLCHECK_TAG:-v0.6.0}"
5+
SHELLCHECK_IMAGE="${SHELLCHECK_IMAGE:-koalaman/shellcheck}"
6+
SHELLCHECK_TAG="${SHELLCHECK_TAG:-v0.6.0}"
97

108
# Check a single shell script for syntax
119
# and common errors.
Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,22 @@
11
. "${BASH_LIB_DIR}/test-utils/bats-support/load.bash"
22
. "${BASH_LIB_DIR}/test-utils/bats-assert-1/load.bash"
33

4-
. "${BASH_LIB_DIR}/filehandling/lib"
4+
. "${BASH_LIB_DIR}/init"
55

66
@test "abs_path returns absolute path for PWD" {
77
run abs_path .
88
assert_output $PWD
99
assert_success
1010
}
1111

12+
@test "abs_path returns PWD when no arg specified" {
13+
run abs_path
14+
assert_output $PWD
15+
assert_success
16+
}
17+
1218
@test "abs_path returns same path when already absolute" {
1319
run abs_path /tmp
1420
assert_output /tmp
1521
assert_success
16-
}
22+
}

tests-for-this-repo/git.bats

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
. "${BASH_LIB_DIR}/test-utils/bats-support/load.bash"
22
. "${BASH_LIB_DIR}/test-utils/bats-assert-1/load.bash"
33

4-
. "${BASH_LIB_DIR}/git/lib"
4+
. "${BASH_LIB_DIR}/init"
55

66
# run before every test
77
setup(){
@@ -137,4 +137,4 @@ EOF
137137
assert_success
138138
assert_output --partial a_file
139139
assert_success
140-
}
140+
}

0 commit comments

Comments
 (0)