Skip to content

Commit aa33004

Browse files
committed
Add initial jq-based templating engine
1 parent 15b89bb commit aa33004

12 files changed

+226
-104
lines changed

.gitattributes

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
/*/**/Dockerfile linguist-generated
2+
/Dockerfile*.template linguist-language=Dockerfile
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
name: Verify Templating
2+
3+
on:
4+
pull_request:
5+
push:
6+
7+
defaults:
8+
run:
9+
shell: 'bash -Eeuo pipefail -x {0}'
10+
11+
jobs:
12+
apply-templates:
13+
name: Check For Uncomitted Changes
14+
runs-on: ubuntu-latest
15+
steps:
16+
- uses: actions/checkout@v2
17+
- name: Apply Templates
18+
run: ./apply-templates.sh
19+
- name: Check Git Status
20+
run: |
21+
status="$(git status --short)"
22+
[ -z "$status" ]

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
.jq-template.awk

10/Dockerfile

Lines changed: 6 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

8/Dockerfile

Lines changed: 6 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

9/Dockerfile

Lines changed: 6 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Dockerfile.template

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
FROM buildpack-deps:%%SUITE%%
1+
FROM buildpack-deps:{{ .debian }}
22

33
RUN set -ex; \
44
if ! command -v gpg > /dev/null; then \
@@ -39,9 +39,9 @@ ENV GCC_MIRRORS \
3939
# only attempt the origin FTP as a mirror of last resort
4040
ftp://ftp.gnu.org/gnu/gcc
4141

42-
# Last Modified: placeholder
43-
ENV GCC_VERSION placeholder
44-
# Docker EOL: placeholder
42+
# Last Modified: {{ .lastModified }}
43+
ENV GCC_VERSION {{ .version }}
44+
# Docker EOL: {{ .eol }}
4545

4646
RUN set -ex; \
4747
\
@@ -65,12 +65,12 @@ RUN set -ex; \
6565
return 1; \
6666
}; \
6767
\
68-
_fetch "gcc-$GCC_VERSION/gcc-$GCC_VERSION.tar.%%TARBALL-COMPRESSION%%.sig" 'gcc.tar.%%TARBALL-COMPRESSION%%.sig'; \
69-
_fetch "gcc-$GCC_VERSION/gcc-$GCC_VERSION.tar.%%TARBALL-COMPRESSION%%" 'gcc.tar.%%TARBALL-COMPRESSION%%'; \
70-
gpg --batch --verify gcc.tar.%%TARBALL-COMPRESSION%%.sig gcc.tar.%%TARBALL-COMPRESSION%%; \
68+
_fetch "gcc-$GCC_VERSION/gcc-$GCC_VERSION.tar.{{ .compression }}.sig" 'gcc.tar.{{ .compression }}.sig'; \
69+
_fetch "gcc-$GCC_VERSION/gcc-$GCC_VERSION.tar.{{ .compression }}" 'gcc.tar.{{ .compression }}'; \
70+
gpg --batch --verify gcc.tar.{{ .compression }}.sig gcc.tar.{{ .compression }}; \
7171
mkdir -p /usr/src/gcc; \
72-
tar -xf gcc.tar.%%TARBALL-COMPRESSION%% -C /usr/src/gcc --strip-components=1; \
73-
rm gcc.tar.%%TARBALL-COMPRESSION%%*; \
72+
tar -xf gcc.tar.{{ .compression }} -C /usr/src/gcc --strip-components=1; \
73+
rm gcc.tar.{{ .compression }}*; \
7474
\
7575
cd /usr/src/gcc; \
7676
\

apply-templates.sh

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
#!/usr/bin/env bash
2+
set -Eeuo pipefail
3+
4+
[ -f versions.json ] # run "versions.sh" first
5+
6+
jqt='.jq-template.awk'
7+
if [ -n "${BASHBREW_SCRIPTS:-}" ]; then
8+
jqt="$BASHBREW_SCRIPTS/jq-template.awk"
9+
elif [ "$BASH_SOURCE" -nt "$jqt" ]; then
10+
wget -qO "$jqt" 'https://github.com/docker-library/bashbrew/raw/ac3e8e9541cb362a579b05bec41dd40d1df1c6e6/scripts/jq-template.awk'
11+
fi
12+
13+
if [ "$#" -eq 0 ]; then
14+
versions="$(jq -r 'keys | map(@sh) | join(" ")' versions.json)"
15+
eval "set -- $versions"
16+
fi
17+
18+
generated_warning() {
19+
cat <<-EOH
20+
#
21+
# NOTE: THIS DOCKERFILE IS GENERATED VIA "apply-templates.sh"
22+
#
23+
# PLEASE DO NOT EDIT IT DIRECTLY.
24+
#
25+
26+
EOH
27+
}
28+
29+
for version; do
30+
export version
31+
32+
echo "processing $version ..."
33+
34+
{
35+
generated_warning
36+
gawk -f "$jqt" Dockerfile.template
37+
} > "$version/Dockerfile"
38+
done

generate-stackbrew-library.sh

Lines changed: 16 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
#!/bin/bash
2-
set -e
1+
#!/usr/bin/env bash
2+
set -Eeuo pipefail
33

44
declare -A aliases=(
55
[10]='latest'
@@ -8,11 +8,13 @@ declare -A aliases=(
88
self="$(basename "$BASH_SOURCE")"
99
cd "$(dirname "$(readlink -f "$BASH_SOURCE")")"
1010

11-
versions=( */ )
12-
versions=( "${versions[@]%/}" )
11+
if [ "$#" -eq 0 ]; then
12+
versions="$(jq -r 'keys | map(@sh) | join(" ")' versions.json)"
13+
eval "set -- $versions"
14+
fi
1315

1416
# sort version numbers with highest first
15-
IFS=$'\n'; versions=( $(echo "${versions[*]}" | sort -rV) ); unset IFS
17+
IFS=$'\n'; set -- $(sort -rV <<<"$*"); unset IFS
1618

1719
# get the most recent commit which modified any of "$@"
1820
fileCommit() {
@@ -40,7 +42,7 @@ getArches() {
4042
local repo="$1"; shift
4143
local officialImagesUrl='https://github.com/docker-library/official-images/raw/master/library/'
4244

43-
eval "declare -A -g parentRepoToArches=( $(
45+
eval "declare -g -A parentRepoToArches=( $(
4446
find -name 'Dockerfile' -exec awk '
4547
toupper($1) == "FROM" && $2 !~ /^('"$repo"'|scratch|.*\/.*)(:|$)/ {
4648
print "'"$officialImagesUrl"'" $2
@@ -67,11 +69,10 @@ join() {
6769
echo "${out#$sep}"
6870
}
6971

70-
for version in "${versions[@]}"; do
71-
commit="$(dirCommit "$version")"
72+
for version; do
73+
export version
7274

73-
dockerfile="$(git show "$commit":"$version/Dockerfile")"
74-
fullVersion="$(awk '$1 == "ENV" && $2 == "GCC_VERSION" { print $3; exit }' <<<"$dockerfile")"
75+
fullVersion="$(jq -r '.[env.version].version' versions.json)"
7576

7677
versionAliases=()
7778
while [ "$fullVersion" != "$version" -a "${fullVersion%[.-]*}" != "$fullVersion" ]; do
@@ -83,18 +84,20 @@ for version in "${versions[@]}"; do
8384
${aliases[$version]:-}
8485
)
8586

86-
parent="$(awk 'toupper($1) == "FROM" { print $2 }' <<<"$dockerfile")"
87+
commit="$(dirCommit "$version")"
88+
89+
parent="$(awk 'toupper($1) == "FROM" { print $2 }' "$version/Dockerfile")"
8790
# no i386 for now: https://github.com/docker-library/gcc/issues/38
8891
# no mips64le for now: https://github.com/docker-library/gcc/issues/67
8992
arches="$(echo " ${parentRepoToArches[$parent]} " | sed -r -e 's/ i386 / /g' -e 's/ mips64le / /g')"
9093

9194
echo
92-
grep -m1 '^# Last Modified: ' <<<"$dockerfile"
95+
jq -r '"# Last Modified: " + .[env.version].lastModified' versions.json
9396
cat <<-EOE
9497
Tags: $(join ', ' "${versionAliases[@]}")
9598
Architectures: $(join ', ' $arches)
9699
GitCommit: $commit
97100
Directory: $version
98101
EOE
99-
grep -m1 '^# Docker EOL: ' <<<"$dockerfile"
102+
jq -r '"# Docker EOL: " + .[env.version].eol' versions.json
100103
done

update.sh

Lines changed: 3 additions & 82 deletions
Original file line numberDiff line numberDiff line change
@@ -1,86 +1,7 @@
1-
#!/bin/bash
1+
#!/usr/bin/env bash
22
set -Eeuo pipefail
33

4-
defaultDebianSuite='buster'
5-
declare -A debianSuites=(
6-
#[6]='jessie'
7-
)
8-
94
cd "$(dirname "$(readlink -f "$BASH_SOURCE")")"
105

11-
versions=( "$@" )
12-
if [ ${#versions[@]} -eq 0 ]; then
13-
versions=( */ )
14-
fi
15-
versions=( "${versions[@]%/}" )
16-
17-
#packagesUrl='https://ftpmirror.gnu.org/gcc/'
18-
packagesUrl='https://mirrors.kernel.org/gnu/gcc/' # the actual HTML of the page changes based on which mirror we end up hitting, so let's hit a specific one for now... :'(
19-
packages="$(wget -qO- "$packagesUrl")"
20-
21-
# our own "supported" window is 18 months from the most recent release because upstream doesn't have a good guideline, but appears to only release maintenance updates for 2-3 years after the initial release
22-
# in addition, maintenance releases are _usually_ less than a year apart; from 4.7+ there's a handful of outliers, like 4.7.3->4.7.4 at ~14 months, 6.4->6.5 at ~15 months, etc
23-
export TZ=UTC
24-
eolPeriod='18 months'
25-
today="$(date +'%s')"
26-
eolAgo="$(date +'%s' -d "$(date -d "@$today") - $eolPeriod")"
27-
eolAge="$(( $today - $eolAgo ))"
28-
eols=()
29-
30-
dateFormat='%Y-%m-%d'
31-
32-
for version in "${versions[@]}"; do
33-
fullVersion="$(grep -E '<a href="(gcc-)?'"$version." <<<"$packages" | sed -r 's!.*<a href="(gcc-)?([^"/]+)/?".*!\2!' | sort -V | tail -1)"
34-
lastModified="$(grep -Em1 '<a href="(gcc-)?'"$fullVersion"'/"' <<<"$packages" | awk -F ' +' '{ print $2 }')"
35-
lastModified="$(date -d "$lastModified" +"$dateFormat")"
36-
37-
releaseAge="$(( $today - $(date +'%s' -d "$lastModified") ))"
38-
if [ $releaseAge -gt $eolAge ]; then
39-
eols+=( "$version ($fullVersion)" )
40-
fi
41-
eolDate="$(date -d "$lastModified + $eolPeriod" +"$dateFormat")"
42-
43-
debianSuite="${debianSuites[$version]:-$defaultDebianSuite}"
44-
45-
compression=
46-
for tryCompression in xz bz2 gz; do
47-
if \
48-
wget --quiet --spider "$packagesUrl/gcc-$fullVersion/gcc-$fullVersion.tar.$tryCompression" \
49-
|| wget --quiet --spider "$packagesUrl/$fullVersion/gcc-$fullVersion.tar.$tryCompression" \
50-
; then
51-
compression="$tryCompression"
52-
break
53-
fi
54-
done
55-
if [ -z "$compression" ]; then
56-
echo >&2 "error: $fullVersion does not seem to even really exist"
57-
exit 1
58-
fi
59-
60-
echo "$version: $fullVersion ($lastModified vs $eolDate); $debianSuite, $compression"
61-
62-
sed -r \
63-
-e 's!%%SUITE%%!'"$debianSuite"'!g' \
64-
-e 's!^(ENV GCC_VERSION) .*!\1 '"$fullVersion"'!' \
65-
-e 's!^(# Last Modified:) .*!\1 '"$lastModified"'!' \
66-
-e 's!^(# Docker EOL:) .*!\1 '"$eolDate"'!' \
67-
-e 's!%%TARBALL-COMPRESSION%%!'"$compression"'!g' \
68-
Dockerfile.template \
69-
> "$version/Dockerfile"
70-
done
71-
72-
if [ ${#eols[@]} -gt 0 ]; then
73-
{
74-
echo
75-
echo
76-
echo
77-
echo " WARNING: the following releases are older than $eolPeriod:"
78-
echo
79-
for eol in "${eols[@]}"; do
80-
echo " - $eol"
81-
done
82-
echo
83-
echo
84-
echo
85-
} >&2
86-
fi
6+
./versions.sh "$@"
7+
./apply-templates.sh "$@"

versions.json

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
{
2+
"10": {
3+
"compression": "xz",
4+
"debian": "buster",
5+
"eol": "2022-10-08",
6+
"lastModified": "2021-04-08",
7+
"version": "10.3.0"
8+
},
9+
"8": {
10+
"compression": "xz",
11+
"debian": "buster",
12+
"eol": "2021-09-04",
13+
"lastModified": "2020-03-04",
14+
"version": "8.4.0"
15+
},
16+
"9": {
17+
"compression": "xz",
18+
"debian": "buster",
19+
"eol": "2021-09-12",
20+
"lastModified": "2020-03-12",
21+
"version": "9.3.0"
22+
}
23+
}

0 commit comments

Comments
 (0)