forked from os-autoinst/scripts
-
Notifications
You must be signed in to change notification settings - Fork 0
/
_common
92 lines (82 loc) · 2.92 KB
/
_common
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
#!/bin/bash -e
# Common shell script snippets to be used when interacting with openQA
# instances, for example over openqa-cli.
client_call=()
warn() { echo "$@" >&2; }
error-handler() {
local line=$1
# shellcheck disable=SC2207
local c=($(caller))
echo "${c[1]}: ERROR: line $line" >&2
}
# From openqa-cli JSON output filter and return the id/ids of jobs,
# for example from a query to the 'jobs get' route or the result string of a
# 'jobs post' or 'isos post'
job_ids() {
jq -r '.ids[]' "$@"
}
# Wrapper around jq that outputs the first lines of JSON in case
# jq has a problem with it, and the calling command and line
runjq() {
local rc output
local jq_output_limit="${jq_output_limit:-15}"
input=$(</dev/stdin)
set +e
output=$(echo "$input" | jq "$@" 2>&1)
rc=$?
set -e
[[ "$rc" == 0 ]] && echo "$output" && return
output=$(echo "$output" | head -"$jq_output_limit")
echo "jq ($(caller)): $output (Input: >>>$input<<<)" >&2
return $rc
}
# Wrapper around curl that reports the HTTP status if it is not 200, and the
# calling command and line
runcurl() {
local rc status_code body response
local verbose="${verbose:-"false"}"
$verbose && echo "[debug] curl: Fetching ($*)" >&2
set +e
response=$(curl -w "\n%{http_code}\n" "$@" 2>&1)
rc=$?
set -e
[[ "$rc" != 0 ]] && echo "curl ($(caller)): Error fetching ($*): $response" >&2 && return 1
status_code=$(echo "$response" | tail -1)
[[ "$status_code" != 200 ]] && echo "curl ($(caller)): Error fetching url ($*): Got Status $status_code" >&2 && return 1
# remove last line
body=$(echo "$response" | tac | tail -n+2 | tac)
echo "$body"
}
comment_on_job() {
local id=$1 comment=$2 force_result=${3:-''}
if [[ -n $force_result ]]; then
comment="label:force_result:$force_result:$comment"
fi
"${client_call[@]}" -X POST jobs/"$id"/comments text="$comment"
}
search_log() {
local id=$1 search_term=$2 out=$3 grep_timeout=${4:-5}
local rc=0 grep_output
local grep_opts="${grep_opts:-"-qPzo"}"
# shellcheck disable=SC2086
grep_output=$(timeout "$grep_timeout" grep $grep_opts "$search_term" "$out" 2>&1) || rc=$?
if [[ "$rc" == 1 ]]; then
return 1
elif [[ "$rc" == 124 ]]; then
warn "grep was killed, possibly timed out: cmd=>grep $grep_opts '$search_term' '$out'< output='$grep_output'"
return $rc
elif [[ "$rc" != 0 ]]; then
# unexpected error, e.g. "exceeded PCRE's backtracking limit"
warn "grep failed: cmd=>grep $grep_opts '$search_term' '$out'< output='$grep_output'"
return $rc
fi
}
label_on_issue() {
local id=$1 search_term=$2 label=$3 restart=${4:-''} force_result=${5:-''}
local out=${out:?}
search_log "$id" "$search_term" "$out" || return
comment_on_job "$id" "$label" "$force_result"
if [ "$restart" = "1" ]; then
"${client_call[@]}" -X POST jobs/"$id"/restart
fi
}