-
Notifications
You must be signed in to change notification settings - Fork 4
/
report-progress.sh
executable file
·285 lines (250 loc) · 10 KB
/
report-progress.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
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
#!/usr/bin/env bash
#
# openshift-tests-partner-cert plugin - progress reporter
# Send progress from openshift-tests execution to sonobuoy worker.
#
set -o pipefail
set -o nounset
set -o errexit
declare -gx PIDS_LOCAL
declare -gx PROGRESS
# os_log_info logger function, printing the current bash script
# and line as prefix.
os_log_info() {
local cnt="progress"
echo "#$(date +%Y-%m-%d_%H:%M:%S) | [${cnt}] | $(caller | awk '{print$2":"$1}')> " "$@"
}
export -f os_log_info
# shellcheck disable=SC1091
source "$(dirname "$0")"/global_env.sh
# shellcheck disable=SC1091
source "$(dirname "$0")"/global_fn.sh
# os_log_info() {
# echo "$(date +%Y%m%d-%H%M%S)> [report] $*"
# }
# wait_progress_api waits for sonobuoy worker is listening.
wait_progress_api() {
local addr_ip
local addr_port
addr_ip=$(echo "${PROGRESS_URL}" |grep -Po '(\d+.\d+.\d+.\d+)')
addr_port=$(echo "${PROGRESS_URL}" |grep -Po '\d{4}')
os_log_info "waiting for sonobuoy-worker service is ready..."
while true
do
test "$(echo '' | curl telnet://"${addr_ip}":"${addr_port}" >/dev/null 2>&1; echo $?)" -eq 0 && break
sleep 1
done
os_log_info "sonobuoy-worker progress api[${PROGRESS_URL}] is ready."
}
# update_progress sends updates to the progress report API (worker).
update_progress() {
# Reporting progress to sonobuoy progress API
component_caller="${1:-"update_progress"}"; shift
msg="${1:-"N/D"}"; shift || true
body="{
\"completed\":${PROGRESS["completed"]},
\"total\":${PROGRESS["total"]},
\"failures\":[${PROGRESS["failures"]}],
\"msg\":\"${msg}\"
}"
os_log_info "Sending report payload [${component_caller}]: ${body}"
curl -s "${PROGRESS_URL}" -d "${body}" || true
}
# wait_pipe_exists wait until the pipe file is created by plugin.
wait_pipe_exists() {
os_log_info "waiting for pipe creation..."
while true
do
test -p "${RESULTS_PIPE}" && break
sleep 1
done
os_log_info "[report] pipe[${RESULTS_PIPE}] created. Starting progress report"
}
# watch_plugin_done watches to block the plugin to be finished prematurely.
watch_plugin_done() {
os_log_info "waiting for plugin done file..."
while true; do
if [[ -f "${PLUGIN_DONE_NOTIFY}" ]]
then
echo "Sonobuoy done detected [done wacther]" |tee -a "${RESULTS_PIPE}"
return
fi
sleep 1
done
os_log_info "plugin done file detected!"
}
# update_pogress_upgrade report the progress when the plugin instance is upgrade.
# The message will be the progress message from ClusterVersion object.
update_pogress_upgrade() {
local progress_st
local progress_message
while true; do
if [[ -f "${PLUGIN_DONE_NOTIFY}" ]]
then
echo "[report_progress] Done file detected"
break
fi
progress_st=$(oc get -o jsonpath='{.status.conditions[?(@.type == "Progressing")].status}' clusterversion version)
progress_message="upgrade-progressing-${progress_st}"
if [[ "$progress_st" == "True" ]]; then
progress_message=$(oc get -o jsonpath='{.status.conditions[?(@.type == "Progressing")].message}' clusterversion version)
else
desired_version=$(oc get -o jsonpath='{.status.desired.version}' clusterversion version)
progress_message="${desired_version}=${progress_message}"
fi
update_progress "updater" "status=${progress_message}";
sleep 10
done
}
# watch_dependency_done watches aggregator API (status) to
# unblock the execution when dependencies was finished the execution.
watch_dependency_done() {
os_log_info "[watch_dependency] Starting dependency check..."
for plugin_name in "${PLUGIN_BLOCKED_BY[@]}"; do
os_log_info "waiting for plugin [${plugin_name}]"
timeout_checks=0
timeout_count=${PLUGIN_WAIT_TIMEOUT_COUNT}
last_count=0
while true;
do
if [[ -f "${PLUGIN_DONE_NOTIFY}" ]]
then
echo "[watch_dependency] Done file detected" |tee -a "${RESULTS_PIPE}"
return
fi
plugin_status=$(jq -r ".plugins[] | select (.plugin == \"${plugin_name}\" ) |.status // \"\"" "${STATUS_FILE}")
if [[ "${plugin_status}" == "${SONOBUOY_PLUGIN_STATUS_COMPLETE}" ]] || [[ "${plugin_status}" == "${SONOBUOY_PLUGIN_STATUS_FAILED}" ]]; then
os_log_info "Plugin[${plugin_name}] with status[${plugin_status}] is finished!"
break
fi
count=$(jq -r ".plugins[] | select (.plugin == \"${plugin_name}\" ) |.progress.completed // 0" "${STATUS_FILE}")
total=$(jq -r ".plugins[] | select (.plugin == \"${plugin_name}\" ) |.progress.total // \"0\"" "${STATUS_FILE}")
remaining=$(( total - count ))
test $remaining -ge 0 && remaining=$(( (total - count) * (-1) ))
blocker_msg=$(jq -r ".plugins[] | select (.plugin == \"${plugin_name}\" ) |.progress.msg // \"\"" "${STATUS_FILE}")
# Dependency plugin is also waiting
if [[ "${blocker_msg}" =~ "status=waiting-for" ]]; then
state_blocking="blocked-by"
# Dependency plugin is also blocked
elif [[ "${blocker_msg}" =~ "status=blocked-by" ]]; then
state_blocking="blocked-by"
else
state_blocking="waiting-for"
fi
# Reporting progress to sonobuoy progress API
msg="status=${state_blocking}=${plugin_name}=(0/${remaining}/0)=[${timeout_checks}/${timeout_count}]"
update_progress "dep-checker" "${msg}";
# Timeout checker
## 1. Dont block by long running plugins (only non-updated)
## Timeout checks will increase only when previous jobs got stuck,
## otherwise it will be reset. It can avoid the plugin run infinitely,
## also avoid to set low timeouts for 'unknown' exec time on dependencies.
if [[ ${count} -gt ${last_count} ]]; then
last_count=${count}
timeout_checks=0
sleep "${PLUGIN_WAIT_TIMEOUT_INTERVAL}"
continue
fi
# dont run timeouts for blockers plugins
if [[ "${blocker_msg}" =~ "status=blocked-by" ]]; then
timeout_checks=0
sleep "${PLUGIN_WAIT_TIMEOUT_INTERVAL}"
continue
fi
# dont run timeouts for if blocker is waiting
if [[ "${blocker_msg}" =~ "status=waiting-for" ]] && [[ "${state_blocking}" =~ "blocked-by" ]]; then
timeout_checks=0
sleep "${PLUGIN_WAIT_TIMEOUT_INTERVAL}"
continue
fi
last_count=${count}
timeout_checks=$(( timeout_checks + 1 ))
if [[ ${timeout_checks} -eq ${timeout_count} ]]; then
echo "Timeout waiting condition 'complete' for plugin[${plugin_name}]."
exit 1
fi
sleep "${PLUGIN_WAIT_TIMEOUT_INTERVAL}"
done
os_log_info "plugin [${plugin_name}] finished!"
done
os_log_info "[plugin dependencies] Finished!"
}
# report_progress reads the pipe file, parses the progress counters and reports it
# to worker progress endpoint until the piple file is closed and Done file is created
# by plugin container.
report_progress() {
local has_update
local err_count
has_update=0
err_count=0
while true
do
# Watch sonobuoy done file
if [[ -f "${PLUGIN_DONE_NOTIFY}" ]]
then
echo "[report_progress] Done file detected"
break
fi
while read -r line;
do
local job_progress
job_progress=$(echo "$line" | grep -Po "\([0-9]{1,}\/[0-9]{1,}\/[0-9]{1,}\)" || true);
if [[ -n "${job_progress}" ]]; then
has_update=1;
PROGRESS["completed"]=$(echo "${job_progress:1:-1}" | cut -d'/' -f 2)
PROGRESS["total"]=$(echo "${job_progress:1:-1}" | cut -d'/' -f 3)
elif [[ $line == failed:* ]]; then
err_count=$(( err_count + 1 ))
if [ -z "${PROGRESS["failures"]}" ]; then
PROGRESS["failures"]=\"$(echo "$line" | cut -d"\"" -f2)\"
else
# Trucanting the 'failures' from payload:
# https://issues.redhat.com/browse/OPCT-24
# Stop flooding the aggregator API with long error messages, when
# there are many errors on the cluster. Note: this will not affect
# the final results, it's just the progress API.
if [[ ${err_count} -gt 30 ]]; then
PROGRESS["failures"]+=,\"$(echo "$line" | cut -d"\"" -f2)\"
else
sig_err=$(echo "$line" | cut -d"\"" -f2 | grep -Po '^(\[sig-[a-zA-Z-]*\])')
err_msg="${sig_err} ERR#${err_count}"
PROGRESS["failures"]+=,\"${err_msg}\"
fi
fi
has_update=1;
fi
if [[ $has_update -eq 1 ]] && [[ "${PLUGIN_ID}" != "${PLUGIN_ID_OPENSHIFT_UPGRADE}" ]]; then
update_progress "updater" "status=running";
has_update=0;
fi
job_progress="";
done <"${RESULTS_PIPE}"
done
}
#
# Main
#
openshift_login
init_config
wait_utils_extractor
update_config
PIDS_LOCAL=()
PROGRESS=( ["completed"]=0 ["total"]=${CERT_TEST_COUNT} ["failures"]="" ["msg"]="" )
wait_status_file
wait_progress_api
update_progress "init" "status=initializing";
wait_pipe_exists
watch_plugin_done &
PIDS_LOCAL+=($!)
watch_dependency_done &
PIDS_LOCAL+=($!)
# upgrade plugin
if [[ "${PLUGIN_ID}" == "${PLUGIN_ID_OPENSHIFT_UPGRADE}" ]]; then
update_pogress_upgrade &
PIDS_LOCAL+=($!)
fi
report_progress
os_log_info "Waiting for PIDs [finalizer]: ${PIDS_LOCAL[*]}"
wait "${PIDS_LOCAL[@]}"
update_progress "finalizer" "status=report-progress-finished";
os_log_info "all done"