forked from docker-library/official-images
-
Notifications
You must be signed in to change notification settings - Fork 3
/
diff-pr.sh
executable file
·418 lines (355 loc) · 10.9 KB
/
diff-pr.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
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
#!/usr/bin/env bash
set -Eeuo pipefail
shopt -s dotglob
# make sure we can GTFO
trap 'echo >&2 Ctrl+C captured, exiting; exit 1' SIGINT
# if bashbrew is missing, bail early with a sane error
bashbrew --version > /dev/null
usage() {
cat <<-EOUSAGE
usage: $0 [PR number] [repo[:tag]]
ie: $0 1024
$0 9001 debian php django
EOUSAGE
}
# TODO flags parsing
allFiles=
listTarballContents=1
findCopies='20%'
uninterestingTarballContent=(
# "config_diff_2017_01_07.log"
'var/log/YaST2/'
# "ks-script-mqmz_080.log"
# "ks-script-ycfq606i.log"
'var/log/anaconda/'
# "2016-12-20/"
'var/lib/yum/history/'
'var/lib/dnf/history/'
# "a/f8c032d2be757e1a70f00336b55c434219fee230-acl-2.2.51-12.el7-x86_64/var_uuid"
'var/lib/yum/yumdb/'
'var/lib/dnf/yumdb/'
# "b42ff584.0"
'etc/pki/tls/rootcerts/'
# "09/401f736622f2c9258d14388ebd47900bbab126"
'usr/lib/.build-id/'
)
# prints "$2$1$3$1...$N"
join() {
local sep="$1"; shift
local out; printf -v out "${sep//%/%%}%s" "$@"
echo "${out#$sep}"
}
uninterestingTarballGrep="^([.]?/)?($(join '|' "${uninterestingTarballContent[@]}"))"
if [ "$#" -eq 0 ]; then
usage >&2
exit 1
fi
pull="$1" # PR number
shift
diffDir="$(readlink -f "$BASH_SOURCE")"
diffDir="$(dirname "$diffDir")"
tempDir="$(mktemp -d)"
trap "rm -rf '$tempDir'" EXIT
cd "$tempDir"
git clone --quiet \
https://github.com/docker-library/official-images.git \
oi
if [ "$pull" != '0' ]; then
git -C oi fetch --quiet \
origin "pull/$pull/merge":refs/heads/pull
else
git -C oi fetch --quiet --update-shallow \
"$diffDir" HEAD:refs/heads/pull
fi
externalPins=
if [ "$#" -eq 0 ]; then
externalPins="$(git -C oi/.external-pins diff --no-renames --name-only HEAD...pull -- '*/**')"
images="$(git -C oi/library diff --no-renames --name-only HEAD...pull -- .)"
if [ -z "$images" ] && [ -z "$externalPins" ]; then
exit 0
fi
images="$(xargs -rn1 basename <<<"$images")"
set -- $images
fi
export BASHBREW_LIBRARY="$PWD/oi/library"
: "${BASHBREW_ARCH:=amd64}" # TODO something smarter with arches
export BASHBREW_ARCH
# TODO something less hacky than "git archive" hackery, like a "bashbrew archive" or "bashbrew context" or something
template='
tempDir="$(mktemp -d)"
{{- "\n" -}}
{{- range $.Entries -}}
{{- $arch := .HasArchitecture arch | ternary arch (.Architectures | first) -}}
{{- /* cannot replace ArchDockerFroms with bashbrew fetch or the arch selector logic has to be duplicated 🥹*/ -}}
{{- $froms := $.ArchDockerFroms $arch . -}}
{{- $outDir := join "_" $.RepoName (.Tags | last) -}}
git -C "{{ gitCache }}" archive --format=tar
{{- " " -}}
{{- "--prefix=" -}}
{{- $outDir -}}
{{- "/" -}}
{{- " " -}}
{{- .ArchGitCommit $arch -}}
{{- ":" -}}
{{- $dir := .ArchDirectory $arch -}}
{{- (eq $dir ".") | ternary "" $dir -}}
{{- "\n" -}}
mkdir -p "$tempDir/{{- $outDir -}}" && echo "{{- .ArchBuilder $arch -}}" > "$tempDir/{{- $outDir -}}/.bashbrew-builder" && echo "{{- .ArchFile $arch -}}" > "$tempDir/{{- $outDir -}}/.bashbrew-file"
{{- "\n" -}}
{{- end -}}
tar -cC "$tempDir" . && rm -rf "$tempDir"
'
_tar-t() {
tar -t "$@" \
| grep -vE "$uninterestingTarballGrep" \
| sed -e 's!^[.]/!!' \
-r \
-e 's!([/.-]|^)((lib)?(c?python|py)-?)[0-9]+([.][0-9]+)?([/.-]|$)!\1\2XXX\6!g' \
| sort
}
_jq() {
if [ "$#" -eq 0 ]; then
set -- '.'
fi
jq --tab -S "$@"
}
copy-tar() {
local src="$1"; shift
local dst="$1"; shift
if [ -n "$allFiles" ]; then
mkdir -p "$dst"
cp -al "$src"/*/ "$dst/"
return
fi
local d indexes=() dockerfiles=()
for d in "$src"/*/.bashbrew-file; do
[ -f "$d" ] || continue
local bf; bf="$(< "$d")"
local dDir; dDir="$(dirname "$d")"
local builder; builder="$(< "$dDir/.bashbrew-builder")"
if [ "$builder" = 'oci-import' ]; then
indexes+=( "$dDir/$bf" )
else
dockerfiles+=( "$dDir/$bf" )
if [ "$bf" = 'Dockerfile' ]; then
# if "Dockerfile.builder" exists, let's check that too (busybox, hello-world)
if [ -f "$dDir/$bf.builder" ]; then
dockerfiles+=( "$dDir/$bf.builder" )
fi
fi
fi
rm "$d" "$dDir/.bashbrew-builder" # remove the ".bashbrew-*" files we created
done
# now that we're done with our globbing needs, let's disable globbing so it doesn't give us wrong answers
local -
set -o noglob
for i in "${indexes[@]}"; do
local iName; iName="$(basename "$i")"
local iDir; iDir="$(dirname "$i")"
local iDirName; iDirName="$(basename "$iDir")"
local iDst="$dst/$iDirName"
mkdir -p "$iDst"
_jq . "$i" > "$iDst/$iName"
local digest
digest="$(jq -r --arg name "$iName" '
if $name == "index.json" then
.manifests[0].digest
else
.digest
end
' "$i")"
local blob="blobs/${digest//://}"
local blobDir; blobDir="$(dirname "$blob")"
local manifest="$iDir/$blob"
mkdir -p "$iDst/$blobDir"
_jq . "$manifest" > "$iDst/$blob"
local configDigest; configDigest="$(jq -r '.config.digest' "$manifest")"
local blob="blobs/${configDigest//://}"
local blobDir; blobDir="$(dirname "$blob")"
local config="$iDir/$blob"
mkdir -p "$iDst/$blobDir"
_jq . "$config" > "$iDst/$blob"
local layers
layers="$(jq -r '[ .layers[].digest | @sh ] | join(" ")' "$manifest")"
eval "layers=( $layers )"
local layerDigest
for layerDigest in "${layers[@]}"; do
local blob="blobs/${layerDigest//://}"
local blobDir; blobDir="$(dirname "$blob")"
local layer="$iDir/$blob"
mkdir -p "$iDst/$blobDir"
_tar-t -f "$layer" > "$iDst/$blob 'tar -t'"
done
done
for d in "${dockerfiles[@]}"; do
local dDir; dDir="$(dirname "$d")"
local dDirName; dDirName="$(basename "$dDir")"
# TODO choke on "syntax" parser directive
# TODO handle "escape" parser directive reasonably
local flatDockerfile; flatDockerfile="$(
gawk '
BEGIN { line = "" }
/^[[:space:]]*#/ {
gsub(/^[[:space:]]+/, "")
print
next
}
{
if (match($0, /^(.*)(\\[[:space:]]*)$/, m)) {
line = line m[1]
next
}
print line $0
line = ""
}
' "$d"
)"
local IFS=$'\n'
local copyAddContext; copyAddContext="$(awk '
toupper($1) == "COPY" || toupper($1) == "ADD" {
for (i = 2; i < NF; i++) {
if ($i ~ /^--from=/) {
next
}
if ($i !~ /^--chown=/) {
print $i
}
}
}
' <<<"$flatDockerfile")"
local dBase; dBase="$(basename "$d")"
local files=(
"$dBase"
$copyAddContext
# some extra files which are likely interesting if they exist, but no big loss if they do not
' .dockerignore' # will be used automatically by "docker build"
' *.manifest' # debian/ubuntu "package versions" list
' *.ks' # fedora "kickstart" (rootfs build script)
' build*.txt' # ubuntu "build-info.txt", debian "build-command.txt"
# usefulness yet to be proven:
#' *.log'
#' {MD5,SHA1,SHA256}SUMS'
#' *.{md5,sha1,sha256}'
# (the space prefix is removed below and is used to ignore non-matching globs so that bad "Dockerfile" entries appropriately lead to failure)
)
unset IFS
mkdir -p "$dst/$dDirName"
local f origF failureMatters
for origF in "${files[@]}"; do
f="${origF# }" # trim off leading space (indicates we don't care about failure)
[ "$f" = "$origF" ] && failureMatters=1 || failureMatters=
local globbed
# "find: warning: -path ./xxx/ will not match anything because it ends with /."
local findGlobbedPath="${f%/}"
findGlobbedPath="${findGlobbedPath#./}"
local globbedStr; globbedStr="$(cd "$dDir" && find -path "./$findGlobbedPath")"
local -a globbed=( $globbedStr )
if [ "${#globbed[@]}" -eq 0 ]; then
globbed=( "$f" )
fi
local g
for g in "${globbed[@]}"; do
local srcG="$dDir/$g" dstG="$dst/$dDirName/$g"
if [ -z "$failureMatters" ] && [ ! -e "$srcG" ]; then
continue
fi
local gDir; gDir="$(dirname "$dstG")"
mkdir -p "$gDir"
cp -alT "$srcG" "$dstG"
if [ -n "$listTarballContents" ]; then
case "$g" in
*.tar.* | *.tgz)
if [ -s "$dstG" ]; then
_tar-t -f "$dstG" > "$dstG 'tar -t'"
fi
;;
esac
fi
done
done
done
}
# a "bashbrew cat" template that gives us the last / "least specific" tags for the arguments
# (in other words, this is "bashbrew list --uniq" but last instead of first)
templateLastTags='
{{- range .TagEntries -}}
{{- $.RepoName -}}
{{- ":" -}}
{{- .Tags | last -}}
{{- "\n" -}}
{{- end -}}
'
_metadata-files() {
if [ "$#" -gt 0 ]; then
bashbrew list "$@" 2>>temp/_bashbrew.err | sort -uV > temp/_bashbrew-list || :
bashbrew cat --format '{{ range .Entries }}{{ range .Architectures }}{{ . }}{{ "\n" }}{{ end }}{{ end }}' "$@" 2>>temp/_bashbrew.err | sort -u > temp/_bashbrew-arches || :
"$diffDir/_bashbrew-cat-sorted.sh" "$@" 2>>temp/_bashbrew.err > temp/_bashbrew-cat || :
bashbrew cat --format "$templateLastTags" "$@" \
| sort -V \
| xargs -r bashbrew list --uniq --build-order 2>>temp/_bashbrew.err \
| xargs -r bashbrew cat --format "$templateLastTags" 2>>temp/_bashbrew.err \
> temp/_bashbrew-list-build-order || :
script="$(bashbrew cat --format "$template" "$@")"
mkdir tar
( eval "$script" | tar -xiC tar )
copy-tar tar temp
rm -rf tar
fi
if [ -n "$externalPins" ] && command -v crane &> /dev/null; then
local file
for file in $externalPins; do
[ -e "oi/$file" ] || continue
local pin digest dir
pin="$("$diffDir/.external-pins/tag.sh" "$file")"
digest="$(< "oi/$file")"
dir="temp/$file"
mkdir -p "$dir"
bashbrew remote arches --json "$pin@$digest" | _jq > "$dir/bashbrew.json"
local manifests manifest
manifests="$(jq -r '
[ (
.arches
| if has(env.BASHBREW_ARCH) then
.[env.BASHBREW_ARCH]
else
.[keys_unsorted | first]
end
)[].digest | @sh ]
| join(" ")
' "$dir/bashbrew.json")"
eval "manifests=( $manifests )"
for manifest in "${manifests[@]}"; do
crane manifest "$pin@$manifest" | _jq > "$dir/manifest-${manifest//:/_}.json"
local config
config="$(jq -r '.config.digest' "$dir/manifest-${manifest//:/_}.json")"
crane blob "$pin@$config" | _jq > "$dir/manifest-${manifest//:/_}-config.json"
done
done
fi
}
mkdir temp
git -C temp init --quiet
git -C temp config user.name 'Bogus'
git -C temp config user.email 'bogus@bogus'
# handle "new-image" PRs gracefully
for img; do touch "$BASHBREW_LIBRARY/$img"; [ -s "$BASHBREW_LIBRARY/$img" ] || echo 'Maintainers: New Image! :D (@docker-library-bot)' > "$BASHBREW_LIBRARY/$img"; done
_metadata-files "$@"
git -C temp add . || :
git -C temp commit --quiet --allow-empty -m 'initial' || :
git -C oi clean --quiet --force
git -C oi checkout --quiet pull
# handle "deleted-image" PRs gracefully :(
for img; do touch "$BASHBREW_LIBRARY/$img"; [ -s "$BASHBREW_LIBRARY/$img" ] || echo 'Maintainers: Deleted Image D: (@docker-library-bot)' > "$BASHBREW_LIBRARY/$img"; done
git -C temp rm --quiet -rf . || :
_metadata-files "$@"
git -C temp add .
git -C temp diff \
--find-copies-harder \
--find-copies="$findCopies" \
--find-renames="$findCopies" \
--ignore-blank-lines \
--ignore-space-at-eol \
--ignore-space-change \
--irreversible-delete \
--minimal \
--staged