-
Notifications
You must be signed in to change notification settings - Fork 98
/
Copy pathcollectBuildInfo.sh
executable file
·179 lines (160 loc) · 6.18 KB
/
collectBuildInfo.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
#!/bin/bash
#
# Copyright (c) 2022 Red Hat, Inc.
# This program and the accompanying materials are made
# available under the terms of the Eclipse Public License 2.0
# which is available at https://www.eclipse.org/legal/epl-2.0/
#
# SPDX-License-Identifier: EPL-2.0
#
# script to collect create/start/finish data on OSBS tasks.
TASK_ID="" # required, task id from brew/osbs
OUTPUT_YML="" # required, path and filename to update
OUTPUT_CSV="" # optional, also write to CSV file
APPEND=0 # by default, create a new yml / csv file
SORT=1 # by default, sort by taskId descending
VERBOSE=0 # by default, be quiet
usage () {
echo "
Usage:
$0 [-t TASK_ID | -b BUILD_ID] [OPTIONS]
Options:
-t Collect data for a given Task ID
-b Collect data for a given Build ID
-f Optionally, write to /path/to/output.yaml
--csv Optionally, also write to /path/to/output.csv
--append When writing to .yaml file (and .csv file), append instead of overwriting
--unsorted Don't sort chronologically by taskId descending
-v, --verbose Verbose output: include additional information
-h, --help Show this help
Example - collect metadata for the current builds in
https://github.com/redhat-developer/devspaces/blob/devspaces-3-rhel-9/dependencies/LATEST_IMAGES_COMMITS:
for d in \$(cat /path/to/LATEST_IMAGES_COMMITS | grep Build | sed -r -e "s@.+buildID=@@"); do \\
$0 -b \$d --append -f /tmp/collectBuildInfo.yml --csv /tmp/collectBuildInfo.csv ; \\
done
"
}
while [[ "$#" -gt 0 ]]; do
case $1 in
'-t') TASK_ID="$2"; shift 1;;
'-b') BUILD_ID="$2"; shift 1;;
'-f') OUTPUT_YML="$2"; shift 1;;
'--csv') OUTPUT_CSV="$2"; shift 1;;
'--append') APPEND=1;;
'--unsorted') SORT=0;;
'-v'|'--verbose') VERBOSE=1;;
'-h'|'--help') usage;;
*) echo "Unknown parameter used: $1."; usage; exit 1;;
esac
shift 1
done
if [[ -z ${TASK_ID} ]] && [[ -z ${BUILD_ID} ]]; then usage; exit 1; fi
# date diffs based on https://unix.stackexchange.com/questions/24626/quickly-calculate-date-differences
datediff() {
d1=$(date -d "$1" +%s)
d2=$(date -d "$2" +%s)
echo -n "$(( (d2 - d1) / 60 )) mins"
}
getTaskIDFromBuildID () {
TASK_ID=$(brew buildinfo $BUILD_ID | grep container_koji_task_id | sed -r -e "s@.+'container_koji_task_id': ([0-9]+),.+@\1@")
echo -n $TASK_ID
}
getContainerFromTaskID () {
if [[ $BUILD_ID ]]; then
getContainerFromBuildID $BUILD_ID
else
# curl -sSLo- https://download.eng.bos.redhat.com/brewroot/work/tasks/4137/48564137/x86_64.log | tail -2 | sed -r -e "s@.+containers/#/(.+)\"@\1@" | head -1
taskid=$1
log="https://download.eng.bos.redhat.com/brewroot/work/tasks/${taskid:(-4)}/${taskid}/x86_64.log"
container=$(curl -sSLko- $log | tail -2 | sed -r -e "s@.+containers/#/(.+)\"@\1@" -e "s@/images/@:@" -e "[email protected]/@@" | head -1)
echo -n $container
fi
}
getContainerFromBuildID () {
# brew buildinfo 2203173 | grep Extra | sed -r -e "s@Extra: @@" | yq -r '.image.index.pull[]' | grep -v sha256
# registry-proxy.engineering.redhat.com/rh-osbs/devspaces-code-rhel9:3.3-6
buildid=$1
container=$(brew buildinfo $buildid | grep Extra | sed -r -e "s@Extra: @@" | yq -r '.image.index.pull[]' | grep -v sha256 \
| sed -r -e "[email protected]/rh-osbs/@@")
echo -n $container
}
getADate() {
name=$1
result=$(echo "$dates" | grep "$name" | sed -r -e "s@$name: @@")
echo $result
}
if [[ $BUILD_ID ]]; then
if [[ $VERBOSE -eq 1 ]]; then
echo -n "For Build ID = $BUILD_ID: "; # getContainerFromBuildID $BUILD_ID
fi
TASK_ID=$(getTaskIDFromBuildID $BUILD_ID)
if [[ $TASK_ID ]]; then
if [[ $VERBOSE -eq 1 ]]; then
echo "Task ID = $TASK_ID"
fi
else
echo "[ERROR] could not compute Task ID for this Build ID"; exit 2
fi
fi
echo -n "For Task ID = $TASK_ID: "; getContainerFromTaskID $TASK_ID
dates="$(brew taskinfo $TASK_ID | grep -E "Created|Started|Finished")"
timeCreate=$(getADate Created)
timeStart=$(getADate Started)
timeFinish=$(getADate Finished)
timeWait=$(datediff "$timeCreate" "$timeStart")
timeBuild=$(datediff "$timeStart" "$timeFinish")
timeTotal=$(datediff "$timeCreate" "$timeFinish")
yaml="- name: ${container%:*}
tag: ${container#*:}
taskId: $TASK_ID
timeCreate: ${timeCreate}
timeStart: ${timeStart}
timeFinish: ${timeFinish}
timeWait: ${timeWait}
timeBuild: ${timeBuild}
timeTotal: ${timeTotal}
"
if [[ $VERBOSE -eq 1 ]]; then
echo;echo "$yaml"
fi
if [[ -f $OUTPUT_YML ]]; then touch $OUTPUT_YML; fi
if [[ $(grep " taskId: $TASK_ID" $OUTPUT_YML) ]]; then
if [[ $VERBOSE -eq 1 ]]; then
echo "[INFO] Skip: taskId $TASK_ID already in $OUTPUT_YML"; echo
else
echo " - skipped"
fi
else
if [[ $OUTPUT_YML ]]; then
if [[ $VERBOSE -eq 0 ]]; then echo; fi
if [[ $APPEND -eq 1 ]]; then
echo "$yaml" >> $OUTPUT_YML
else
echo "$yaml" > $OUTPUT_YML
fi
# sorting removes spaces between yaml entries
if [[ $SORT -eq 1 ]]; then
# sort yaml by taskId (most recent tasks at the end of the file)
yq -Y -i '.|=sort_by(.taskId)' $OUTPUT_YML
fi
if [[ $OUTPUT_CSV ]]; then
if [[ $SORT -eq 1 ]]; then
# always replace csv file with fresh content from the yaml file
cat $OUTPUT_YML | yq -r '.[]|(keys_unsorted)|@csv' | uniq > $OUTPUT_CSV
cat $OUTPUT_YML | yq -r '.[]|flatten|@csv' >> $OUTPUT_CSV
else
# add header if needed (file doesn't exist or is empty)
if [[ ! -f $OUTPUT_CSV ]] || [[ ! -s $OUTPUT_CSV ]]; then
cat $OUTPUT_YML | yq -r '.[]|(keys_unsorted)|@csv' | uniq > $OUTPUT_CSV
fi
# add the new yaml as a line of csv
echo "$yaml" | yq -r '.[]|flatten|@csv' >> $OUTPUT_CSV
fi
fi
if [[ $VERBOSE -eq 1 ]]; then
echo "[INFO] Wrote info to $OUTPUT_YML"
if [[ $OUTPUT_CSV ]]; then echo "[INFO] Wrote info to $OUTPUT_CSV"; fi
echo
fi
fi
fi