forked from openshift/assisted-service
/
utils.sh
244 lines (201 loc) · 6.45 KB
/
utils.sh
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
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
set -o nounset
set -o pipefail
set -o errexit
function wait_for_crd() {
crd="$1"
namespace="${2:-}"
wait_for_condition "crd/${crd}" "Established" "60s" "${namespace}"
}
function remote_agents() {
namespace="$1"
hostnames="$2"
oc get agent -n ${namespace} --no-headers -l "agent-install.openshift.io/bmh in ( ${hostnames} )"
}
export -f remote_agents
function installed_remote_agents() {
namespace="$1"
hostnames="$2"
remote_agents ${namespace} ${hostnames} | grep Done
}
export -f installed_remote_agents
function wait_for_operator() {
subscription="$1"
namespace="${2:-}"
echo "Waiting for operator ${subscription} to get installed on namespace ${namespace}..."
for _ in $(seq 1 60); do
csv=$(oc -n "${namespace}" get subscription "${subscription}" -o jsonpath='{.status.installedCSV}' || true)
if [[ -n "${csv}" ]]; then
if [[ "$(oc -n "${namespace}" get csv "${csv}" -o jsonpath='{.status.phase}')" == "Succeeded" ]]; then
echo "ClusterServiceVersion (${csv}) is ready"
return 0
fi
fi
sleep 10
done
echo "Timed out waiting for csv to become ready!"
return 1
}
function wait_for_pod() {
pod="$1"
namespace="${2:-}"
selector="${3:-}"
wait_for_condition "pod" "Ready" "30m" "${namespace}" "${selector}"
}
function wait_for_pods(){
while [[ $(oc get pods -n $1 -o 'jsonpath={..status.conditions[?(@.type=="Ready")].status}'| tr ' ' '\n' | sort -u) != "True" ]]; do
echo "Waiting for pods in namespace $1 to be ready"
oc get pods -n $1 -o 'jsonpath={..status.containerStatuses}' | jq "."
sleep 5;
done
echo "Pods in namespace $1 are ready"
}
function wait_for_deployment() {
deployment="$1"
namespace="${2}"
timeout="${3}"
echo "Waiting for (deployment) on namespace (${namespace}) with name (${deployment}) to be created..."
for i in {1..40}; do
oc get deployment "${deployment}" --namespace="${namespace}" |& grep -ivE "(no resources found|not found)" && break || sleep 10
done
echo "Waiting for (deployment) on namespace (${namespace}) with name (${deployment}) to rollout..."
oc rollout status "deploy/${deployment}" -n "${namespace}" --timeout="${timeout}"
}
function hash() {
input=$1
length=$2
echo "${input}" | md5sum - | cut -c -${length}
}
function disk_to_wwn() {
# The WWN token has to begin with a number, so it was prefixed with an
# hardcoded 0 and afterwards 15 bytes of a digested hash from the original
# disk name.
echo "0x0$(hash ${1} 15)"
}
function wait_for_condition() {
object="$1"
condition="$2"
timeout="$3"
namespace="${4:-}"
selector="${5:-}"
echo "Waiting for (${object}) on namespace (${namespace}) with labels (${selector}) to be created..."
for i in {1..40}; do
oc get ${object} --selector="${selector}" --namespace=${namespace} |& grep -ivE "(no resources found|not found)" && break || sleep 10
done
echo "Waiting for (${object}) on namespace (${namespace}) with labels (${selector}) to become (${condition})..."
oc wait -n "${namespace}" --for=condition=${condition} --selector "${selector}" ${object} --timeout=${timeout}
}
function wait_for_object_amount() {
object="$1"
amount="$2"
interval="$3"
namespace="${4:-}"
until [ $(oc get ${object} -n "${namespace}" --no-headers | wc -l) -eq ${amount} ]; do
sleep ${interval}
done
echo "done" $(oc get ${object} -n "${namespace}" --no-headers | wc -l)
}
function wait_for_cmd_amount() {
amount="$1"
interval="$2"
cmd="$3"
until [ $(${cmd} "${@:4}" | wc -l) -eq ${amount} ]; do
sleep ${interval}
done
echo "done" $(${cmd} "${@:4}" | wc -l)
}
function wait_for_boolean_field() {
object="$1"
field="$2"
namespace="${3:-}"
interval="${4:-10}"
for i in {1..50}; do
date --rfc-3339=seconds
value=$(oc get -n ${namespace} ${object} -o custom-columns=field:${field} --no-headers)
if [ "${value}" = "true" ]; then
return 0
fi
sleep ${interval}
done
echo "Value of field ${field} of object ${object} under namespace ${namespace} has never become true"
return 1
}
function wait_for_resource() {
object="$1"
namespace="$2"
interval="${4:-10}"
set +e
for i in {1..50}; do
date --rfc-3339=seconds
value=$(oc get -n ${namespace} ${object} --no-headers | wc -l)
if [ "${value}" -ne 0 ]; then
return 0
fi
sleep ${interval}
done
set -e
echo "The object ${object} under namespace ${namespace} not found!"
return 1
}
function get_image_without_tag() {
# given "<registry>/<repository>/<project>:<tag>" or
# "<registry>/<repository>/<project>@sha256:<sha>" or
# "<registry>:<port>/<repository>/<project>:<tag>"
# return "<registry>:<port>/<repository>/<project>"
echo "${1%:*}" | cut -d@ -f1
}
function get_image_namespace() {
# given "<registry>/<repository>/<project>:<tag>"
# return "<registry>/<repository>"
dirname "${1}"
}
function get_image_without_registry() {
# given "<registry>/<repository>/<project>:<tag>"
# return "<repository>/<project>:<tag>"
echo "${1#*/}"
}
function get_image_namespace_without_registry() {
# given "<registry>/<repository>/<project>:<tag>"
# return "<repository>"
get_image_namespace $(get_image_without_registry "${1}")
}
function get_image_repository_only() {
# given "<registry>/<repository>/<project>:<tag>"
# return "<repository>/<project>"
get_image_without_registry $(get_image_without_tag "${1}")
}
function nth_ip() {
network=$1
idx=$2
python -c "from ansible_collections.ansible.utils.plugins.filter import nthhost; print(nthhost.nthhost('"$network"', $idx))"
}
function retry() {
attempts=5
interval=1
local OPTIND
while getopts "a:i:" opt ; do
case "${opt}" in
a )
attempts="${OPTARG}"
;;
i )
interval="${OPTARG}"
;;
* )
;;
esac
done
shift $((OPTIND-1))
rc=0
for attempt in $(seq "${attempts}")
do
echo "Attempt ${attempt}/${attempts} to execute \"$*\"..."
if "$@"; then
return 0
else
rc=$?
echo "Failed with exit code ${rc}, retrying \"$*\"..."
sleep "${interval}"
fi
done
return ${rc}
}