naughty-constraints.sh 2.7 KB

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