naughty-from.sh 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  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. _is_naughty() {
  13. local from="$1"; shift
  14. case "$BASHBREW_ARCH=$from" in
  15. # a few images that no longer exist (and are thus not permissible)
  16. # https://techcommunity.microsoft.com/t5/Containers/Removing-the-latest-Tag-An-Update-on-MCR/ba-p/393045
  17. *=mcr.microsoft.com/windows/nanoserver:latest \
  18. | *=mcr.microsoft.com/windows/servercore:latest \
  19. | *=microsoft/nanoserver:latest \
  20. | *=microsoft/windowsservercore:latest \
  21. ) return 0 ;;
  22. # a few explicitly permissible exceptions to Santa's naughty list
  23. *=scratch \
  24. | amd64=docker.elastic.co/elasticsearch/elasticsearch:* \
  25. | amd64=docker.elastic.co/kibana/kibana:* \
  26. | amd64=docker.elastic.co/logstash/logstash:* \
  27. | windows-*=mcr.microsoft.com/windows/nanoserver:* \
  28. | windows-*=mcr.microsoft.com/windows/servercore:* \
  29. | windows-*=microsoft/nanoserver:* \
  30. | windows-*=microsoft/windowsservercore:* \
  31. ) return 1 ;;
  32. # "x/y" and not an approved exception
  33. */*) return 0 ;;
  34. esac
  35. # must be some other official image AND support our current architecture
  36. local archSupported
  37. if archSupported="$(bashbrew cat --format '{{ .TagEntry.HasArchitecture arch | ternary arch "" }}' "$from")" && [ -n "$archSupported" ]; then
  38. return 1
  39. fi
  40. return 0
  41. }
  42. _arches() {
  43. bashbrew cat --format '
  44. {{- range .TagEntries -}}
  45. {{- .Architectures | join "\n" -}}
  46. {{- "\n" -}}
  47. {{- end -}}
  48. ' "$@" | sort -u
  49. }
  50. _froms() {
  51. bashbrew cat --format '
  52. {{- range .TagEntries -}}
  53. {{- $.DockerFrom . -}}
  54. {{- "\n" -}}
  55. {{- end -}}
  56. ' "$@" | sort -u
  57. }
  58. declare -A naughtyFromsArches=(
  59. #[img:tag=from:tag]='arch arch ...'
  60. )
  61. naughtyFroms=()
  62. declare -A allNaughty=(
  63. #[img:tag]=1
  64. )
  65. tags="$(bashbrew list --uniq "$@" | sort -u)"
  66. for img in $tags; do
  67. arches="$(_arches "$img")"
  68. hasNice= # do we have _any_ arches that aren't naughty? (so we can make the message better if not)
  69. for BASHBREW_ARCH in $arches; do
  70. export BASHBREW_ARCH
  71. if ! froms="$(_froms "$img" 2>/dev/null)"; then
  72. # if we can't fetch the tags from their real locations, let's try the warehouse
  73. refsList="$(
  74. bashbrew list --uniq "$img" \
  75. | sed \
  76. -e 's!:!/!' \
  77. -e "s!^!refs/tags/$BASHBREW_ARCH/!" \
  78. -e 's!$!:!'
  79. )"
  80. [ -n "$refsList" ]
  81. git -C "$BASHBREW_CACHE/git" \
  82. fetch --no-tags --quiet \
  83. https://github.com/docker-library/commit-warehouse.git \
  84. $refsList
  85. froms="$(_froms "$img")"
  86. fi
  87. [ -n "$froms" ] # rough sanity check
  88. for from in $froms; do
  89. if _is_naughty "$from"; then
  90. if [ -z "${naughtyFromsArches["$img=$from"]:-}" ]; then
  91. naughtyFroms+=( "$img=$from" )
  92. else
  93. naughtyFromsArches["$img=$from"]+=', '
  94. fi
  95. naughtyFromsArches["$img=$from"]+="$BASHBREW_ARCH"
  96. else
  97. hasNice=1
  98. fi
  99. done
  100. done
  101. if [ -z "$hasNice" ]; then
  102. allNaughty["$img"]=1
  103. fi
  104. done
  105. for naughtyFrom in "${naughtyFroms[@]:-}"; do
  106. [ -n "$naughtyFrom" ] || continue # https://mywiki.wooledge.org/BashFAQ/112#BashFAQ.2F112.line-8 (empty array + "set -u" + bash 4.3 == sad day)
  107. img="${naughtyFrom%%=*}"
  108. from="${naughtyFrom#$img=}"
  109. if [ -n "${allNaughty["$img"]:-}" ]; then
  110. echo " - $img (FROM $from) -- completely unsupported base!"
  111. else
  112. arches="${naughtyFromsArches[$naughtyFrom]}"
  113. echo " - $img (FROM $from) [$arches]"
  114. fi
  115. done