-
Notifications
You must be signed in to change notification settings - Fork 174
Expand file tree
/
Copy pathlib.sh
More file actions
executable file
·197 lines (165 loc) · 4.4 KB
/
lib.sh
File metadata and controls
executable file
·197 lines (165 loc) · 4.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
#!/usr/bin/env bash
set -euo pipefail
# A library of reusable bash functions
usage() {
echo "lib.sh provides a library of reusable bash functions.
Invoke with:
$ scripts/lib.sh method [args...]
Reuse with:
. scripts/lib.sh
method [args...]"
}
info() {
echo "INFO: $(date): $*"
}
die() {
echo >&2 "ERROR:" "$@"
exit 1
}
# Caution when editing: make sure groups would correspond to BASH_REMATCH use.
RELEASE_RC_TAG_BASH_REGEX='^([[:digit:]]+(\.[[:digit:]]+)*)(-rc\.[[:digit:]]+)?$'
curl_cfg() { # Use built-in echo to not expose $2 in the process list.
echo -n "$1 = \"${2//[\"\\]/\\&}\""
}
roxcurl() {
local url="$1"
shift
curl -sSfk --config <(curl_cfg user "admin:${ROX_ADMIN_PASSWORD}") "https://${API_ENDPOINT}${url}" "$@"
}
is_release_version() {
if [[ "$#" -ne 1 ]]; then
die "missing arg. usage: is_release_version <version>"
fi
[[ "$1" =~ $RELEASE_RC_TAG_BASH_REGEX && -z "${BASH_REMATCH[3]}" ]]
}
is_RC_version() {
if [[ "$#" -ne 1 ]]; then
die "missing arg. usage: is_RC_version <version>"
fi
[[ "$1" =~ $RELEASE_RC_TAG_BASH_REGEX && -n "${BASH_REMATCH[3]}" ]]
}
# get_release_stream() - Gets the release major.minor from the output of `make tag`.
get_release_stream() {
if [[ "$#" -ne 1 ]]; then
die "missing arg. usage: get_release_stream <tag>"
fi
[[ "$1" =~ ^([[:digit:]]+\.[[:digit:]]+) ]]
echo "${BASH_REMATCH[1]}"
}
# is_release_test_stream() - A major number of 0 is used to test release handling.
is_release_test_stream() {
if [[ "$#" -ne 1 ]]; then
die "missing arg. usage: is_release_test_stream <tag>"
fi
[[ "$1" =~ ^0\. ]]
}
is_CI() {
[[ "${CI:-}" == "true" ]]
}
is_OPENSHIFT_CI() {
[[ "${OPENSHIFT_CI:-}" == "true" ]]
}
is_GITHUB_ACTIONS() {
[[ -n "${GITHUB_ACTION:-}" ]]
}
is_darwin() {
uname -a | grep -i darwin >/dev/null 2>&1
}
is_linux() {
uname -a | grep -i linux >/dev/null 2>&1
}
test_equals_non_silent() {
if [[ "$#" -lt 2 ]]; then
die "usage: test_equals_non_silent <arg1> <arg2>"
fi
if [[ "$1" != "$2" ]]; then
die "Comparison failed: \"$1\" != \"$2\""
fi
}
test_gt_non_silent() {
if [[ "$#" -lt 2 ]]; then
die "usage: test_gt_non_silent <arg1> <arg2>"
fi
if [[ "$1" -le "$2" ]]; then
die "Comparison failed: \"$1\" <= \"$2\""
fi
}
test_empty_non_silent() {
if [[ "$#" -lt 1 ]]; then
die "usage: test_empty_non_silent <arg1>"
fi
if [[ -n "$1" ]]; then
die "Comparison failed: \"$1\" is not empty"
fi
}
require_environment() {
if [[ "$#" -lt 1 ]]; then
die "usage: require_environment NAME [reason]"
fi
(
set +u
if [[ -z "$(eval echo "\$$1")" ]]; then
varname="$1"
shift
message="missing \"$varname\" environment variable"
if [[ "$#" -gt 0 ]]; then
message="$message: $*"
fi
die "$message"
fi
) || exit 1
}
require_executable() {
if [[ "$#" -lt 1 ]]; then
die "usage: require_executable NAME [reason]"
fi
if ! command -v "$1" >/dev/null 2>&1; then
varname="$1"
shift
message="missing \"$varname\" executable"
if [[ "$#" -gt 0 ]]; then
message="$message: $*"
fi
die "$message"
fi
}
# retry() - retry a command up to a specific numer of times until it exits
# successfully, with exponential back off.
# (original source: https://gist.github.com/sj26/88e1c6584397bb7c13bd11108a579746)
retry() {
if [[ "$#" -lt 3 ]]; then
die "usage: retry <try count> <delay true|false> <command> <args...>"
fi
local tries=$1
local delay=$2
shift; shift;
local count=0
until "$@"; do
exit=$?
wait=$((2 ** count))
count=$((count + 1))
if [[ $count -lt $tries ]]; then
info "Retry $count/$tries exited $exit"
if $delay; then
info "Retrying in $wait seconds..."
sleep $wait
fi
if [[ -n "${RETRY_HOOK:-}" ]]; then
$RETRY_HOOK
fi
else
echo "Retry $count/$tries exited $exit, no more retries left."
return $exit
fi
done
return 0
}
if [[ "${BASH_SOURCE[0]}" == "$0" ]]; then
if [[ "$#" -lt 1 ]]; then
usage
die "When invoked at the command line a method is required."
fi
fn="$1"
shift
"$fn" "$@"
fi