Skip to content

Commit

Permalink
Add new script to also verify "Constraints:" values are correct
Browse files Browse the repository at this point in the history
  • Loading branch information
tianon committed Jun 12, 2019
1 parent 22c68d5 commit bcc52e4
Show file tree
Hide file tree
Showing 2 changed files with 151 additions and 0 deletions.
9 changes: 9 additions & 0 deletions bashbrew/travis.sh
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,15 @@ if [ -n "$extraCommands" ] && naughtyFrom="$(../naughty-from.sh "${repos[@]}")"
exit 1
fi

if [ -n "$extraCommands" ] && naughtyConstraints="$(../naughty-constraints.sh "${repos[@]}")" && [ -n "$naughtyConstraints" ]; then
echo >&2
echo >&2 "Invalid 'FROM' + 'Constraints' combinations detected:"
echo >&2
echo >&2 "$naughtyConstraints"
echo >&2
exit 1
fi

_bashbrew() {
echo $'\n\n$ bashbrew' "$@" "${repos[@]}"
bashbrew "$@" "${repos[@]}"
Expand Down
142 changes: 142 additions & 0 deletions naughty-constraints.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,142 @@
#!/usr/bin/env bash
set -Eeuo pipefail

: "${BASHBREW_CACHE:=$HOME/.cache/bashbrew}"
export BASHBREW_CACHE BASHBREW_ARCH=

if [ ! -d "$BASHBREW_CACHE/git" ]; then
# initialize the "bashbrew cache"
bashbrew --arch amd64 from --uniq --apply-constraints hello-world:linux > /dev/null
fi

if [ "$#" -eq 0 ]; then
set -- '--all'
fi

_windows_constraint() {
local from="$1"; shift
local repo="${from%:*}"
local tag="${from#$repo:}"

local constraint
case "$repo" in
mcr.microsoft.com/windows/nanoserver | microsoft/nanoserver) constraint='nanoserver' ;;
mcr.microsoft.com/windows/servercore | microsoft/windowsservercore) constraint='windowsservercore' ;;
*) echo >&2 "error: unknown Windows image: $from"; exit 1 ;;
esac

if [ "$tag" != 'latest' ]; then
constraint+="-$tag"
fi

echo "$constraint"
}

_expected_constraints() {
local from="$1"; shift

local fromConstraints
if fromConstraints="$(bashbrew cat --format '{{ .TagEntry.Constraints | join "\n" }}' "$from" 2>/dev/null)" && [ -n "$fromConstraints" ]; then
echo "$fromConstraints"
return
fi

case "$from" in
*microsoft*) _windows_constraint "$from" ;;
esac

return
}

_arches() {
bashbrew cat --format '
{{- range .TagEntries -}}
{{- .Architectures | join "\n" -}}
{{- "\n" -}}
{{- end -}}
' "$@" | sort -u
}

_froms() {
bashbrew cat --format '
{{- range .TagEntries -}}
{{- $.DockerFroms . | join "\n" -}}
{{- "\n" -}}
{{- end -}}
' "$@" | sort -u
}

declare -A naughtyFromsArches=(
#[img:tag=from:tag]='arch arch ...'
)
naughtyFroms=()
declare -A allNaughty=(
#[img:tag]=1
)

tags="$(bashbrew list --uniq "$@" | sort -u)"
naughtyCount=0
for img in $tags; do
arches="$(_arches "$img")"
constraints="$(bashbrew cat --format '{{ .TagEntry.Constraints | join "\n" }}' "$img" | sort -u)"
declare -A imgMissing=()
declare -A imgExtra=()
for BASHBREW_ARCH in $arches; do
export BASHBREW_ARCH

if ! froms="$(_froms "$img" 2>/dev/null)"; then
# if we can't fetch the tags from their real locations, let's try the warehouse
refsList="$(
bashbrew list --uniq "$img" \
| sed \
-e 's!:!/!' \
-e "s!^!refs/tags/$BASHBREW_ARCH/!" \
-e 's!$!:!'
)"
[ -n "$refsList" ]
git -C "$BASHBREW_CACHE/git" \
fetch --no-tags --quiet \
https://github.com/docker-library/commit-warehouse.git \
$refsList
froms="$(_froms "$img")"
fi

[ -n "$froms" ] # rough sanity check

for from in $froms; do
expected="$(_expected_constraints "$from" | sort -u)"
missing="$(comm -13 <(echo "$constraints") <(echo "$expected"))"
if [ -n "$missing" ]; then
imgMissing[$from]+=$'\n'"$missing"
fi
extra="$(comm -23 <(echo "$constraints") <(echo "$expected"))"
if [ "$from" = 'scratch' ]; then
# if a given image is "FROM scratch", then consider "!aufs" an acceptable constraint that doesn't show
extra="$(grep -vE '^!aufs$' <<<"$extra" || :)"
fi
if [ -n "$extra" ]; then
imgExtra[$from]+=$'\n'"$extra"
fi
done
done
if [ "${#imgMissing[@]}" -gt 0 ]; then
for from in $(IFS=$'\n'; sort -u <<<"${!imgMissing[*]}"); do
missing="${imgMissing[$from]}"
missing="$(sed '/^$/d' <<<"$missing" | sort -u)"
echo " - $img -- missing constraints (FROM $from):"
sed 's/^/ - /' <<<"$missing"
(( naughtyCount++ )) || :
done
fi
if [ "${#imgExtra[@]}" -gt 0 ]; then
for from in $(IFS=$'\n'; sort -u <<<"${!imgExtra[*]}"); do
extra="${imgExtra[$from]}"
extra="$(sed '/^$/d' <<<"$extra" | sort -u)"
echo " - $img -- extra constraints (FROM $from):"
sed 's/^/ - /' <<<"$extra"
(( naughtyCount++ )) || :
done
fi
done

exit "$naughtyCount"

0 comments on commit bcc52e4

Please sign in to comment.