naughty-from.sh 2.8 KB

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