naughty-constraints.sh 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. #!/usr/bin/env bash
  2. set -Eeuo pipefail
  3. : "${BASHBREW_CACHE:=$HOME/.cache/bashbrew}"
  4. export BASHBREW_CACHE BASHBREW_ARCH=
  5. if [ ! -d "$BASHBREW_CACHE/git" ]; then
  6. # initialize the "bashbrew cache"
  7. bashbrew --arch amd64 from --uniq --apply-constraints hello-world:linux > /dev/null
  8. fi
  9. if [ "$#" -eq 0 ]; then
  10. set -- '--all'
  11. fi
  12. _windows_constraint() {
  13. local from="$1"; shift
  14. local repo="${from%:*}"
  15. local tag="${from#$repo:}"
  16. local constraint
  17. case "$repo" in
  18. mcr.microsoft.com/windows/nanoserver | microsoft/nanoserver) constraint='nanoserver' ;;
  19. mcr.microsoft.com/windows/servercore | microsoft/windowsservercore) constraint='windowsservercore' ;;
  20. *) echo >&2 "error: unknown Windows image: $from"; exit 1 ;;
  21. esac
  22. if [ "$tag" != 'latest' ]; then
  23. constraint+="-$tag"
  24. fi
  25. echo "$constraint"
  26. }
  27. _expected_constraints() {
  28. local from="$1"; shift
  29. local fromConstraints
  30. if fromConstraints="$(bashbrew cat --format '{{ .TagEntry.Constraints | join "\n" }}' "$from" 2>/dev/null)" && [ -n "$fromConstraints" ]; then
  31. echo "$fromConstraints"
  32. return
  33. fi
  34. case "$from" in
  35. *microsoft*) _windows_constraint "$from" ;;
  36. esac
  37. return
  38. }
  39. _arches() {
  40. bashbrew cat --format '
  41. {{- range .TagEntries -}}
  42. {{- .Architectures | join "\n" -}}
  43. {{- "\n" -}}
  44. {{- end -}}
  45. ' "$@" | sort -u
  46. }
  47. _froms() {
  48. bashbrew cat --format '
  49. {{- range .TagEntries -}}
  50. {{- $.DockerFroms . | join "\n" -}}
  51. {{- "\n" -}}
  52. {{- end -}}
  53. ' "$@" | sort -u
  54. }
  55. declare -A naughtyFromsArches=(
  56. #[img:tag=from:tag]='arch arch ...'
  57. )
  58. naughtyFroms=()
  59. declare -A allNaughty=(
  60. #[img:tag]=1
  61. )
  62. tags="$(bashbrew --namespace '' list --uniq "$@" | sort -u)"
  63. for img in $tags; do
  64. arches="$(_arches "$img")"
  65. constraints="$(bashbrew cat --format '{{ .TagEntry.Constraints | join "\n" }}' "$img" | sort -u)"
  66. declare -A imgMissing=()
  67. declare -A imgExtra=()
  68. for BASHBREW_ARCH in $arches; do
  69. export BASHBREW_ARCH
  70. froms="$(_froms "$img")"
  71. [ -n "$froms" ] # rough sanity check
  72. allExpected=
  73. for from in $froms; do
  74. expected="$(_expected_constraints "$from")"
  75. allExpected="$(sort -u <<<"$allExpected"$'\n'"$expected")"
  76. done
  77. missing="$(comm -13 <(echo "$constraints") <(echo "$allExpected"))"
  78. if [ -n "$missing" ]; then
  79. imgMissing[$from]+=$'\n'"$missing"
  80. fi
  81. extra="$(comm -23 <(echo "$constraints") <(echo "$allExpected"))"
  82. if [ -n "$extra" ]; then
  83. imgExtra[$from]+=$'\n'"$extra"
  84. fi
  85. done
  86. if [ "${#imgMissing[@]}" -gt 0 ]; then
  87. for from in $(IFS=$'\n'; sort -u <<<"${!imgMissing[*]}"); do
  88. missing="${imgMissing[$from]}"
  89. missing="$(sed '/^$/d' <<<"$missing" | sort -u)"
  90. echo " - $img -- missing constraints (FROM $from):"
  91. sed 's/^/ - /' <<<"$missing"
  92. done
  93. fi
  94. if [ "${#imgExtra[@]}" -gt 0 ]; then
  95. for from in $(IFS=$'\n'; sort -u <<<"${!imgExtra[*]}"); do
  96. extra="${imgExtra[$from]}"
  97. extra="$(sed '/^$/d' <<<"$extra" | sort -u)"
  98. echo " - $img -- extra constraints (FROM $from):"
  99. sed 's/^/ - /' <<<"$extra"
  100. done
  101. fi
  102. done