forked from docker-library/docs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvariant.sh
executable file
·160 lines (141 loc) · 4.19 KB
/
variant.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
#!/bin/bash
set -eo pipefail
repo="$1"
if [ -z "$repo" ]; then
echo >&2 "usage: $0 repo"
echo >&2 " ie: $0 hylang"
exit 1
fi
dir="$(dirname "$(readlink -f "$BASH_SOURCE")")"
repoDir="$dir/../$repo"
# prints "$2$1$3$1...$N"
join() {
local sep="$1"; shift
local out; printf -v out "${sep//%/%%}%s" "$@"
echo "${out#$sep}"
}
commaJoin() {
local items=( $(xargs -n1 <<<"$1" | sort -u) ); shift
local sep=', '
case "${#items[@]}" in
0)
return
;;
1)
echo "$items"
return
;;
2)
sep=' '
;;
esac
items[-1]="or ${items[-1]}"
join "$sep" "${items[@]}"
}
tagFiles() {
local tag="$1"; shift
local tagUltimate="${tag##*-}" # 3.6-stretch -> stretch
local tagPenultimate="${tag%-*}" # 2.7.15-windowsservercore-1803 -> 2.7.15-windowsservercore
tagPenultimate="${tagPenultimate##*-}" # 2.7.15-windowsservercore -> windowsservercore
echo \
"$repoDir/variant-$tag.md" \
"$repoDir/variant-$tagUltimate.md" \
"$repoDir/variant-$tagPenultimate.md" \
"$dir/variant-$tag.md" \
"$dir/variant-$tagUltimate.md" \
"$dir/variant-$tagPenultimate.md"
}
_repo() {
local repo=$1; shift
# if we haven't set BASHBREW_LIBRARY explicitly (like Jenkins does, for example), don't trust the local library
if [ -z "${BASHBREW_LIBRARY:-}" ]; then
repo="https://github.com/docker-library/official-images/raw/master/library/$repo"
fi
echo "$repo"
}
bbRepo="$(_repo "$repo")"
IFS=$'\n'
tags=( $(bashbrew cat -f '
{{- $archSpecific := getenv "ARCH_SPECIFIC_DOCS" -}}
{{- range ($archSpecific | ternary (archFilter arch .Entries) .Entries) -}}
{{- join "\n" .Tags -}}
{{- "\n" -}}
{{- end -}}
' "$bbRepo") )
unset IFS
text=
declare -A includedFiles=()
for tag in "${tags[@]}"; do
for f in $(tagFiles "$tag"); do
if [ -n "${includedFiles[$f]}" ]; then
# make sure we don't duplicate variant sections
break
fi
if [ -f "$f" ]; then
includedFiles[$f]=1
if [ -s "$f" ]; then
# an empty file can be used to disable a specific "variant" section for an image
text+=$'\n' # give a little space
text+="$(< "$f")"
text+=$'\n' # parameter expansion eats the trailing newline
fi
break
fi
done
done
if [ -n "$text" ]; then
default="$([ -f "$repoDir/variant.md" ] && cat "$repoDir/variant.md" || cat "$dir/variant.md")"
default+=$'\n' # parameter expansion eats the trailing newline
if [ "$repo" != 'debian' ] && [ "$repo" != 'ubuntu' ]; then
# what is 'bullseye', 'buster' and 'sid'
# https://github.com/docker-library/python/issues/343
debian=( $(bashbrew list --uniq "$(_repo 'debian')" | grep -vE 'stable|slim|backports|experimental|testing' | cut -d: -f2) )
ubuntu=( $(bashbrew list "$(_repo 'ubuntu')" | grep -vE 'devel|latest|[0-9]' | cut -d: -f2) )
foundDebianTags=
foundUbuntuTags=
for tag in ${tags[@]}; do
for suite in "${debian[@]}"; do
case "$tag" in
*-"$suite" | "$suite"-* | *-"$suite"-* | "$suite" )
foundDebianTags+=" $suite"
;;
esac
done
for suite in "${ubuntu[@]}"; do
case "$tag" in
*-"$suite" | "$suite"-* | *-"$suite"-* | "$suite" )
foundUbuntuTags+=" $suite"
;;
esac
done
done
if [ -n "$foundDebianTags" ]; then
default+=$'\n' # give a little space
default+="$( sed -e 's/%%DEB-SUITES%%/'"$(commaJoin "$foundDebianTags")"'/' "$dir/variant-default-debian.md" )"
default+=$'\n' # parameter expansion eats the trailing newline
fi
if [ -n "$foundUbuntuTags" ]; then
default+=$'\n' # give a little space
default+="$( sed -e 's/%%DEB-SUITES%%/'"$(commaJoin "$foundUbuntuTags")"'/' "$dir/variant-default-ubuntu.md" )"
default+=$'\n' # parameter expansion eats the trailing newline
fi
fi
# buildpack-deps text
potentialTags="$(bashbrew list --uniq "$bbRepo" | cut -d: -f2)"
for tag in $potentialTags; do
baseImage="$(bashbrew cat -f '{{ .ArchLastStageFrom (.TagEntry.Architectures | first) .TagEntry }}' "$bbRepo:$tag" 2>/dev/null)"
case "$baseImage" in
buildpack-deps:*-*) ;; # "scm", "curl" -- not large images
buildpack-deps:*)
default+=$'\n' # give a little space
default+="$(< "$dir/variant-default-buildpack-deps.md")"
default+=$'\n' # parameter expansion eats the trailing newline
break
;;
esac
done
echo
echo
echo -n "$default"
echo "$text"
fi