-
Notifications
You must be signed in to change notification settings - Fork 2
/
in
executable file
·545 lines (511 loc) · 18.4 KB
/
in
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
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
#!/bin/bash
set -euo pipefail
IFS=$'\n\t'
clearup ()
{
set +x
rm -f "$apijson"
rm -f "$tmp"
rm -f ~/.netrc
}
trap clearup EXIT INT TERM
apijson=$(mktemp -u /tmp/json-XXXXXX)
tmp=$(mktemp -u /tmp/json-XXXXXX)
exec 3>&1
exec 1>&2
payload="$(cat <&0)"
if [ "$(jq -r '.source | has("host")' <<< "$payload")" = 'true' ]; then
host="$(jq -r .source.host <<< "$payload")"
else
echo 'You need to provide the host.'
exit 1
fi
if [ "$(jq -r '.source | has("user")' <<< "$payload")" = 'true' ]; then
user="$(jq -r .source.user <<< "$payload")"
else
echo 'You need to provide the user.'
exit 1
fi
if [ "$(jq -r '.source | has("token")' <<< "$payload")" = 'true' ]; then
token="$(jq -r .source.token <<< "$payload")"
else
echo 'You need to provide the token.'
exit 1
fi
cat > ~/.netrc <<ENDLINE
machine $host
login $user
password $token
ENDLINE
if [ "$(jq -r '.source | has("debug")' <<< "$payload")" = 'true' ]; then
debug="$(jq -r .source.debug <<< "$payload")"
else
debug=
fi
if [ -n "$debug" ] && [ "$debug" = "verbose" ]; then
set -x
fi
if [ "$(jq -r '.source | has("job")' <<< "$payload")" = 'true' ]; then
job="$(jq -r .source.job <<< "$payload")"
else
echo 'You need to provide the job.'
exit 1
fi
if [ "$(jq -r '.source | has("crumb")' <<< "$payload")" = 'true' ] && [ "$(jq -r .source.crumb <<< "$payload")" = 'skip' ]; then
crumb_skip=1
else
crumb_skip=
fi
if [ "$(jq -r '.source | has("port")' <<< "$payload")" = 'true' ]; then
port=":$(jq -r .source.port <<< "$payload")"
else
port=""
fi
if [ "$(jq -r '.source | has("timezone")' <<< "$payload")" = 'true' ]; then
timezone="$(jq -r .source.timezone <<< "$payload")"
else
timezone=UTC
fi
if [ "$(jq -r '.source | has("protocol")' <<< "$payload")" = 'true' ] && [ "$(jq -r .source.protocol <<< "$payload")" = "http" ]; then
protocol="http"
else
protocol="https"
fi
if [ "$(jq -r '.source | has("builds")' <<< "$payload")" = 'true' ]; then
builds=$(jq -r .source.builds <<< "$payload")
else
builds=
fi
if [ "$(jq -r '.params | has("skip")' <<< "$payload")" = 'true' ]; then
skip=$(jq -r .params.skip <<< "$payload")
elif [ "$(jq -r '.source | has("skip")' <<< "$payload")" = 'true' ]; then
skip=$(jq -r .source.skip <<< "$payload")
else
skip=
fi
if [ "$(jq -r '.params | has("timeout")' <<< "$payload")" = 'true' ]; then
timeout=$(jq -r .params.timeout <<< "$payload")
elif [ "$(jq -r '.source | has("timeout")' <<< "$payload")" = 'true' ]; then
timeout=$(jq -r .source.timeout <<< "$payload")
else
timeout=""
fi
if [ "$(jq -r '.params | has("expectedResult")' <<< "$payload")" = 'true' ]; then
case "$(jq -r '.params.expectedResult | type' <<< "$payload")" in
('string')
expectedResult=("$(jq -r '.params.expectedResult' <<< "$payload")")
;;
('array')
mapfile -t expectedResult < <(jq -r '.params.expectedResult|.[]' <<< "$payload" | sort)
;;
(*)
echo "Type '$(jq -r '.params.expectedResult | type' <<< "$payload")' input is invalid for .params.expectedResult"
exit 1
;;
esac
elif [ "$(jq -r '.source | has("expectedResult")' <<< "$payload")" = 'true' ]; then
case "$(jq -r '.source.expectedResult | type' <<< "$payload")" in
('string')
expectedResult=("$(jq -r '.source.expectedResult' <<< "$payload")")
;;
('array')
mapfile -t expectedResult < <(jq -r '.source.expectedResult|.[]' <<< "$payload" | sort)
;;
(*)
echo "Type '$(jq -r '.source.expectedResult | type' <<< "$payload")' input is invalid for .source.expectedResult"
exit 1
;;
esac
else
expectedResult=()
fi
if [ -n "$debug" ]; then
printenv
[ "$(jq -r 'has("version")' <<< "$payload")" = 'true' ] && jq -r -S -C '.version' <<< "$payload"
fi
readable_timestamp()
{
if [ "$1" = 'null' ]; then
echo 'null'
return
fi
TZ="$timezone" date --date="@$(($1/1000))"
}
readable_duration()
{
if [ "$1" = 'null' ]; then
echo 'null'
return
fi
if [ "$1" -lt 1000 ]; then
echo "$1 ms"
return
fi
local T=$(($1/1000))
local D=$((T/60/60/24))
local H=$((T/60/60%24))
local M=$((T/60%60))
local S=$((T%60))
if [ $D -eq 1 ]; then
echo -n "$D day "
elif [ $D -gt 1 ]; then
echo -n "$D days "
fi
if [ $H -eq 1 ]; then
echo -n "$H hour "
elif [ $H -gt 1 ]; then
echo -n "$H hours "
fi
if [ $M -eq 1 ]; then
echo -n "$M minute "
elif [ $M -gt 1 ]; then
echo -n "$M minutes "
fi
if [ $S -eq 1 ]; then
echo "$S second"
elif [ $S -gt 1 ]; then
echo "$S seconds"
fi
}
declare -a IDs=()
declare -A URLs=()
declare -A names=()
declare -A timestamps=()
declare -A durations=()
declare -A results=()
declare -A digests=()
CURL=(curl --retry 3 --max-time 10 -nsS)
CURL_ONCE=(curl --max-time 30 -nsS)
case "$0" in
('/opt/resource/check')
URL="$protocol://$host$port/$job/api/json"
[ -z "$debug" ] && echo "curl -nsS ${URL}"
"${CURL[@]}" "$URL" > "$apijson"
while read -r url; do
[ -z "$debug" ] && echo "curl -nsS ${url}api/json"
"${CURL[@]}" "${url}api/json" | jq -r > "$tmp"
digest="sha256:$(jq -S -M 'del(.estimatedDuration,.nextBuild)' < "$tmp" | sha256sum | awk '{print $1}')"
[ -n "$debug" ] && jq -r -S -C < "$tmp"
id=$(jq -r .id < "$tmp")
name=$(jq -r .fullDisplayName < "$tmp")
timestamp=$(jq -r .timestamp < "$tmp")
duration=$(jq -r .duration < "$tmp")
result=$(jq -r .result < "$tmp")
if [ -z "$skip" ]; then
if [ "$duration" = '0' ] || [ "$result" = 'null' ]; then
continue
fi
fi
if [ -n "${expectedResult[*]}" ] && ! [[ "${expectedResult[*]}" =~ $result ]]; then
continue
fi
IDs+=("$id")
URLs[$id]="$url"
names[$id]=$(echo "$name" | sed -e 's/\\/\\\\/g' -e 's/\"/\\\"/g')
timestamps[$id]=$(readable_timestamp "$timestamp")
durations[$id]=$(readable_duration "$duration")
results[$id]="$result"
digests[$id]="$digest"
if [ "$(jq -r '.version.digest' <<< "$payload")" = "$digest" ]; then
break
fi
if [ -n "$builds" ] && [[ "$builds" -eq "${#IDs[*]}" ]]; then
break
fi
done < <(jq -r '.builds | .[].url' < "$apijson")
;;
('/opt/resource/in')
cd "$1"
URL="$protocol://$host$port/$job/api/json"
expectedMatch=
url=
if [ -n "${expectedResult[*]}" ]; then
duration=0
result=
until [ -n "$timeout" ] && [[ $duration -ge $timeout ]]; do
if [ "$(jq -r '.version | has("url")' <<< "$payload")" = 'true' ]; then
url=$(jq -r '.version.url' <<< "$payload")
[ -z "$debug" ] && echo "curl -nsS ${url}api/json # $duration/$timeout"
"${CURL[@]}" "${url}api/json" | jq -r > "$tmp"
result=$(jq -r .result < "$tmp")
if [[ "${expectedResult[*]}" =~ $result ]]; then
if [ -z "$skip" ] && [ "$(jq -r .duration < "$tmp")" = "0" ]; then
sleep 5
duration=$((duration+5))
else
expectedMatch=1
break
fi
elif [ "$result" = 'null' ]; then
sleep 5
duration=$((duration+5))
else
break
fi
else
[ -z "$debug" ] && echo "curl -nsS ${URL} # $duration/$timeout"
"${CURL[@]}" "$URL" > "$apijson"
while read -r url; do
[ -z "$debug" ] && echo "curl -nsS ${url}api/json # $duration/$timeout"
"${CURL[@]}" "${url}api/json" | jq -r > "$tmp"
result=$(jq -r .result < "$tmp")
if [[ "${expectedResult[*]}" =~ $result ]]; then
if [ -z "$skip" ] && [ "$(jq -r .duration < "$tmp")" = "0" ]; then
sleep 5
duration=$((duration+5))
break
else
expectedMatch=1
break 2
fi
elif [ "$result" = 'null' ]; then
sleep 5
duration=$((duration+5))
break
else
break 2
fi
done < <(jq -r '.builds | .[].url' < "$apijson")
fi
done
if [ -n "$url" ] && [ -z "$expectedMatch" ]; then
IFS=' '
echo "$url doesn't have the expected results in \"${expectedResult[*]}\" ($result) or timed out ($duration/$timeout)."
IFS=$'\n\t'
exit 1
fi
fi
if [ -z "$url" ]; then
if [ "$(jq -r '.version | has("url")' <<< "$payload")" = 'true' ]; then
url=$(jq -r '.version.url' <<< "$payload")
duration=0
until [ -n "$timeout" ] && [[ $duration -ge $timeout ]]; do
[ -z "$debug" ] && echo "curl -nsS ${url}api/json # $duration/$timeout"
"${CURL[@]}" "${url}api/json" | jq -r > "$tmp"
result=$(jq -r .result < "$tmp")
if [ -n "$skip" ] || [ "$result" != 'null' ]; then
break
fi
sleep 5
duration=$((duration+5))
done
else
[ -z "$debug" ] && echo "curl -nsS $URL"
"${CURL[@]}" "$URL" > "$apijson"
while read -r url; do
duration=0
until [ -n "$timeout" ] && [[ $duration -ge $timeout ]]; do
[ -z "$debug" ] && echo "curl -nsS ${url}api/json # $duration/$timeout"
"${CURL[@]}" "${url}api/json" | jq -r > "$tmp"
result=$(jq -r .result < "$tmp")
if [ -n "$skip" ] || [ "$result" != 'null' ]; then
break 2
fi
sleep 5
duration=$((duration+5))
done
break
done < <(jq -r '.builds | .[].url' < "$apijson")
fi
fi
cp "$tmp" api.json
digest="sha256:$(jq -S -M 'del(.estimatedDuration,.nextBuild)' < "$tmp" | sha256sum | awk '{print $1}')"
[ -n "$debug" ] && jq -r -S -C < "$tmp"
id=$(jq -r .id < "$tmp")
name=$(jq -r .fullDisplayName < "$tmp")
timestamp=$(jq -r .timestamp < "$tmp")
timestamp=$(readable_timestamp "$timestamp")
duration=$(jq -r .duration < "$tmp")
duration=$(readable_duration "$duration")
result=$(jq -r .result < "$tmp")
;;
('/opt/resource/out')
cd "$1"
PARAMS=()
if [ "$(jq -r '.params | has("stringParameters")' <<< "$payload")" = 'true' ] || [ "$(jq -r '.params | has("fileParameters")' <<< "$payload")" = 'true' ]; then
URL="$protocol://$host$port/$job/buildWithParameters"
if [ "$(jq -r '.params | has("stringParameters")' <<< "$payload")" = 'true' ]; then
[ -n "$debug" ] && jq -r -S -C '.params.stringParameters' <<< "$payload"
if [ "$(jq -S -M '.params.stringParameters | length' <<< "$payload")" != '0' ]; then
for key in $(jq -r '.params.stringParameters | to_entries | .[].key' <<< "$payload"); do
PARAMS+=("--form" "$key=$(jq -r ".params.stringParameters.$key" <<< "$payload")")
done
fi
fi
if [ "$(jq -r '.params | has("fileParameters")' <<< "$payload")" = 'true' ]; then
[ -n "$debug" ] && jq -r -S -C '.params.fileParameters' <<< "$payload"
if [ "$(jq -S -M '.params.fileParameters | length' <<< "$payload")" != '0' ]; then
for key in $(jq -r '.params.fileParameters | to_entries | .[].key' <<< "$payload"); do
PARAMS+=("--form" "$key=@$(jq -r ".params.fileParameters.$key" <<< "$payload")")
done
fi
fi
else
URL="$protocol://$host$port/$job/build"
fi
if [ -z "$crumb_skip" ]; then
crumbIssuer="$protocol://$host$port/crumbIssuer/api/json"
[ -z "$debug" ] && echo "curl -nsS $crumbIssuer"
"${CURL[@]}" "$crumbIssuer" > crumb.json
crumb=$(jq -r .crumb < crumb.json)
crumbRequestField=$(jq -r .crumbRequestField < crumb.json)
fi
if [ -z "${PARAMS[*]}" ]; then
[ -z "$debug" ] && echo "curl -nsS -X POST $URL"
if [ -z "$crumb_skip" ]; then
"${CURL_ONCE[@]}" -X POST -D headers "$URL" -H "$crumbRequestField: $crumb"
else
"${CURL_ONCE[@]}" -X POST -D headers "$URL"
fi
else
IFS=' '
[ -z "$debug" ] && echo "curl -nsS -X POST $URL ${PARAMS[*]}"
IFS=$'\n\t'
if [ -z "$crumb_skip" ]; then
"${CURL_ONCE[@]}" -X POST -D headers "$URL" -H "$crumbRequestField: $crumb" "${PARAMS[@]}"
else
"${CURL_ONCE[@]}" -X POST -D headers "$URL" "${PARAMS[@]}"
fi
fi
[ -n "$debug" ] && cat headers
queue=$(grep '^Location: ' headers | awk '{print $2}' | tr -d '\r')
duration=0
url=
until [ -n "$timeout" ] && [[ $duration -ge $timeout ]]; do
if [ -n "$url" ] && [ "$url" != 'null' ] && [ -n "${expectedResult[*]}" ]; then
[ -z "$debug" ] && echo "curl -nsS ${url}api/json # $duration/$timeout"
"${CURL[@]}" "${url}api/json" | jq -r > "$tmp"
result=$(jq -r .result < "$tmp")
if [ "$(jq -r .duration < "$tmp")" = '0' ]; then
sleep 5
duration=$((duration+5))
continue
fi
if [[ "${expectedResult[*]}" =~ $result ]]; then
break
elif [ "$result" != 'null' ]; then
IFS=' '
echo "$url doesn't have the expected results in \"${expectedResult[*]}\" ($result)."
IFS=$'\n\t'
exit 1
fi
else
[ -z "$debug" ] && echo "curl -nsS ${queue}api/json # $duration/$timeout"
"${CURL[@]}" "${queue}api/json" > queue.json
[ -n "$debug" ] && jq -r -S -C < queue.json
url=$(jq -r '.executable | .url' < queue.json)
if [ "$url" != 'null' ]; then
if [ -n "${expectedResult[*]}" ]; then
continue
else
break
fi
fi
fi
sleep 5
duration=$((duration+5))
done
if [ -n "$timeout" ] && [[ $duration -ge $timeout ]]; then
if [ "$url" = 'null' ]; then
echo "No build and timed out ($duration/$timeout)."
else
echo "$url timed out ($duration/$timeout)."
fi
exit 1
else
[ -z "$debug" ] && echo "curl -nsS ${url}api/json"
"${CURL[@]}" "${url}api/json" | jq -r > "$tmp"
digest="sha256:$(jq -S -M 'del(.estimatedDuration,.nextBuild)' < "$tmp" | sha256sum | awk '{print $1}')"
[ -n "$debug" ] && jq -r -S -C < "$tmp"
id=$(jq -r .id < "$tmp")
name=$(jq -r .fullDisplayName < "$tmp")
timestamp=$(jq -r .timestamp < "$tmp")
timestamp=$(readable_timestamp "$timestamp")
duration=$(jq -r .duration < "$tmp")
duration=$(readable_duration "$duration")
result=$(jq -r .result < "$tmp")
fi
;;
esac
case "$0" in
('/opt/resource/check')
json=
if [ -n "${IDs[*]}" ]; then
mapfile -t reverseIDs < <(sort -u -n <<<"${IDs[*]}")
else
reverseIDs=()
fi
for id in "${reverseIDs[@]}"; do
if [ -z "$json" ]; then
json=$(cat <<ENDLINE
[
{
"digest": "${digests[$id]}",
"url": "${URLs[$id]}",
"name": "${names[$id]}",
"timestamp": "${timestamps[$id]}",
"duration": "${durations[$id]}",
"result": "${results[$id]}"
}
ENDLINE
)
else
json=$(cat <<ENDLINE
$json,
{
"digest": "${digests[$id]}",
"url": "${URLs[$id]}",
"name": "${names[$id]}",
"timestamp": "${timestamps[$id]}",
"duration": "${durations[$id]}",
"result": "${results[$id]}"
}
ENDLINE
)
fi
done
if [ -n "$json" ]; then
json="$json"']'
else
json='[]'
fi
;;
('/opt/resource/in'|'/opt/resource/out')
if [ "$0" = '/opt/resource/out' ] && [ -n "${expectedResult[*]}" ] && ! [[ "${expectedResult[*]}" =~ $result ]]; then
if [ "$(jq -r 'has("version")' <<< "$payload")" = 'true' ]; then
json=$(cat <<ENDLINE
{
"version": $(jq -S -M '.version' <<< "$payload")
}
ENDLINE
)
else
json="{}"
fi
else
json=$(cat <<ENDLINE
{
"version": {
"digest": "${digest}",
"url": "${url}",
"name": "${name}",
"timestamp": "${timestamp}",
"duration": "${duration}",
"result": "${result}"
},
"metadata": [
{"name": "url", "value": "${url}"},
{"name": "name", "value": "${name}"},
{"name": "timestamp", "value": "${timestamp}"},
{"name": "duration", "value": "${duration}"},
{"name": "result", "value": "${result}"}
]
}
ENDLINE
)
fi
;;
esac
echo "$json" > "$tmp"
sed -i 's/\t/ /g' "$tmp"
jq -n --slurpfile all "$tmp" '$all[0]' >&3