naughty-from.sh 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  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/*:latest \
  18. ) return 0 ;;
  19. # https://docs.microsoft.com/en-us/virtualization/windowscontainers/deploy-containers/base-image-lifecycle
  20. # "11/12/2019"
  21. *=mcr.microsoft.com/windows/*:1803* \
  22. ) return 0 ;;
  23. # https://docs.microsoft.com/en-us/virtualization/windowscontainers/deploy-containers/base-image-lifecycle
  24. # "04/09/2019"
  25. *=mcr.microsoft.com/windows/*:1709* \
  26. ) return 0 ;;
  27. # https://docs.microsoft.com/en-us/virtualization/windowscontainers/deploy-containers/base-image-lifecycle
  28. # "10/09/2018"
  29. *=mcr.microsoft.com/windows/nanoserver:sac2016 \
  30. ) return 0 ;;
  31. # a few explicitly permissible exceptions to Santa's naughty list
  32. *=scratch \
  33. | amd64=docker.elastic.co/elasticsearch/elasticsearch:* \
  34. | amd64=docker.elastic.co/kibana/kibana:* \
  35. | amd64=docker.elastic.co/logstash/logstash:* \
  36. | arm64v8=docker.elastic.co/elasticsearch/elasticsearch:* \
  37. | arm64v8=docker.elastic.co/kibana/kibana:* \
  38. | arm64v8=docker.elastic.co/logstash/logstash:* \
  39. | windows-*=mcr.microsoft.com/windows/nanoserver:* \
  40. | windows-*=mcr.microsoft.com/windows/servercore:* \
  41. ) return 1 ;;
  42. # "x/y" and not an approved exception
  43. */*) return 0 ;;
  44. esac
  45. # must be some other official image AND support our current architecture
  46. local archSupported
  47. if archSupported="$(bashbrew cat --format '{{ .TagEntry.HasArchitecture arch | ternary arch "" }}' "$from")" && [ -n "$archSupported" ]; then
  48. return 1
  49. fi
  50. return 0
  51. }
  52. _arches() {
  53. bashbrew cat --format '
  54. {{- range .TagEntries -}}
  55. {{- .Architectures | join "\n" -}}
  56. {{- "\n" -}}
  57. {{- end -}}
  58. ' "$@" | sort -u
  59. }
  60. _froms() {
  61. bashbrew cat --format '
  62. {{- range .TagEntries -}}
  63. {{- $.DockerFroms . | join "\n" -}}
  64. {{- "\n" -}}
  65. {{- end -}}
  66. ' "$@" | sort -u
  67. }
  68. declare -A naughtyFromsArches=(
  69. #[img:tag=from:tag]='arch arch ...'
  70. )
  71. naughtyFroms=()
  72. declare -A allNaughty=(
  73. #[img:tag]=1
  74. )
  75. tags="$(bashbrew --namespace '' list --uniq "$@" | sort -u)"
  76. for img in $tags; do
  77. arches="$(_arches "$img")"
  78. hasNice= # do we have _any_ arches that aren't naughty? (so we can make the message better if not)
  79. for BASHBREW_ARCH in $arches; do
  80. export BASHBREW_ARCH
  81. froms="$(_froms "$img")"
  82. [ -n "$froms" ] # rough sanity check
  83. for from in $froms; do
  84. if _is_naughty "$from"; then
  85. if [ -z "${naughtyFromsArches["$img=$from"]:-}" ]; then
  86. naughtyFroms+=( "$img=$from" )
  87. else
  88. naughtyFromsArches["$img=$from"]+=', '
  89. fi
  90. naughtyFromsArches["$img=$from"]+="$BASHBREW_ARCH"
  91. else
  92. hasNice=1
  93. fi
  94. done
  95. done
  96. if [ -z "$hasNice" ]; then
  97. allNaughty["$img"]=1
  98. fi
  99. done
  100. for naughtyFrom in "${naughtyFroms[@]:-}"; do
  101. [ -n "$naughtyFrom" ] || continue # https://mywiki.wooledge.org/BashFAQ/112#BashFAQ.2F112.line-8 (empty array + "set -u" + bash 4.3 == sad day)
  102. img="${naughtyFrom%%=*}"
  103. from="${naughtyFrom#$img=}"
  104. if [ -n "${allNaughty["$img"]:-}" ]; then
  105. echo " - $img (FROM $from) -- completely unsupported base!"
  106. else
  107. arches="${naughtyFromsArches[$naughtyFrom]}"
  108. echo " - $img (FROM $from) [$arches]"
  109. fi
  110. done