naughty-from.sh 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  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. declare -A naughtyFromsArches=(
  13. #[img:tag=from:tag]='arch arch ...'
  14. )
  15. naughtyFroms=()
  16. tags="$(bashbrew list --uniq "$@" | sort -u)"
  17. for img in $tags; do
  18. for BASHBREW_ARCH in $(bashbrew cat --format '{{ join " " .TagEntry.Architectures }}' "$img"); do
  19. export BASHBREW_ARCH
  20. if ! from="$(bashbrew cat --format '{{ $.DockerFrom .TagEntry }}' "$img" 2>/dev/null)"; then
  21. # if we can't fetch the tags from their real locations, let's try the warehouse
  22. refsList="$(
  23. bashbrew list --uniq "$img" \
  24. | sed \
  25. -e 's!:!/!' \
  26. -e "s!^!refs/tags/$BASHBREW_ARCH/!" \
  27. -e 's!$!:!'
  28. )"
  29. [ -n "$refsList" ]
  30. git -C "$BASHBREW_CACHE/git" \
  31. fetch --no-tags --quiet \
  32. https://github.com/docker-library/commit-warehouse.git \
  33. $refsList
  34. from="$(bashbrew cat --format '{{ $.DockerFrom .TagEntry }}' "$img")"
  35. fi
  36. case "$BASHBREW_ARCH=$from" in
  37. # a few explicitly permissible exceptions to Santa's naughty list
  38. *=scratch \
  39. | amd64=docker.elastic.co/elasticsearch/elasticsearch:* \
  40. | amd64=docker.elastic.co/kibana/kibana:* \
  41. | amd64=docker.elastic.co/logstash/logstash:* \
  42. | windows-*=mcr.microsoft.com/windows/nanoserver:* \
  43. | windows-*=mcr.microsoft.com/windows/servercore:* \
  44. | windows-*=microsoft/nanoserver:* \
  45. | windows-*=microsoft/windowsservercore:* \
  46. ) continue ;;
  47. esac
  48. if ! listOutput="$(bashbrew cat --format '{{ .TagEntry.HasArchitecture arch | ternary arch "" }}' "$from")" || [ -z "$listOutput" ]; then
  49. if [ -z "${naughtyFromsArches["$img=$from"]:-}" ]; then
  50. naughtyFroms+=( "$img=$from" )
  51. else
  52. naughtyFromsArches["$img=$from"]+=', '
  53. fi
  54. naughtyFromsArches["$img=$from"]+="$BASHBREW_ARCH"
  55. fi
  56. done
  57. done
  58. for naughtyFrom in "${naughtyFroms[@]:-}"; do
  59. [ -n "$naughtyFrom" ] || continue # https://mywiki.wooledge.org/BashFAQ/112#BashFAQ.2F112.line-8 (empty array + "set -u" + bash 4.3 == sad day)
  60. img="${naughtyFrom%%=*}"
  61. from="${naughtyFrom#$img=}"
  62. arches="${naughtyFromsArches[$naughtyFrom]}"
  63. echo " - $img (FROM $from) [$arches]"
  64. done