variant.sh 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. #!/bin/bash
  2. set -eo pipefail
  3. repo="$1"
  4. if [ -z "$repo" ]; then
  5. echo >&2 "usage: $0 repo"
  6. echo >&2 " ie: $0 hylang"
  7. exit 1
  8. fi
  9. dir="$(dirname "$(readlink -f "$BASH_SOURCE")")"
  10. repoDir="$dir/../$repo"
  11. # prints "$2$1$3$1...$N"
  12. join() {
  13. local sep="$1"; shift
  14. local out; printf -v out "${sep//%/%%}%s" "$@"
  15. echo "${out#$sep}"
  16. }
  17. commaJoin() {
  18. local items=( $(xargs -n1 <<<"$1" | sort -u) ); shift
  19. local sep=', '
  20. case "${#items[@]}" in
  21. 0)
  22. return
  23. ;;
  24. 1)
  25. echo "$items"
  26. return
  27. ;;
  28. 2)
  29. sep=' '
  30. ;;
  31. esac
  32. items[-1]="or ${items[-1]}"
  33. join "$sep" "${items[@]}"
  34. }
  35. tagFiles() {
  36. local tag="$1"; shift
  37. local tagUltimate="${tag##*-}" # 3.6-stretch -> stretch
  38. local tagPenultimate="${tag%-*}" # 2.7.15-windowsservercore-1803 -> 2.7.15-windowsservercore
  39. tagPenultimate="${tagPenultimate##*-}" # 2.7.15-windowsservercore -> windowsservercore
  40. echo \
  41. "$repoDir/variant-$tag.md" \
  42. "$repoDir/variant-$tagUltimate.md" \
  43. "$repoDir/variant-$tagPenultimate.md" \
  44. "$dir/variant-$tag.md" \
  45. "$dir/variant-$tagUltimate.md" \
  46. "$dir/variant-$tagPenultimate.md"
  47. }
  48. _repo() {
  49. local repo=$1; shift
  50. # if we haven't set BASHBREW_LIBRARY explicitly (like Jenkins does, for example), don't trust the local library
  51. if [ -z "${BASHBREW_LIBRARY:-}" ]; then
  52. repo="https://github.com/docker-library/official-images/raw/master/library/$repo"
  53. fi
  54. echo "$repo"
  55. }
  56. bbRepo="$(_repo "$repo")"
  57. IFS=$'\n'
  58. tags=( $(bashbrew cat -f '
  59. {{- $archSpecific := getenv "ARCH_SPECIFIC_DOCS" -}}
  60. {{- range ($archSpecific | ternary (archFilter arch .Entries) .Entries) -}}
  61. {{- join "\n" .Tags -}}
  62. {{- "\n" -}}
  63. {{- end -}}
  64. ' "$bbRepo") )
  65. unset IFS
  66. text=
  67. declare -A includedFiles=()
  68. for tag in "${tags[@]}"; do
  69. for f in $(tagFiles "$tag"); do
  70. if [ -n "${includedFiles[$f]}" ]; then
  71. # make sure we don't duplicate variant sections
  72. break
  73. fi
  74. if [ -f "$f" ]; then
  75. includedFiles[$f]=1
  76. if [ -s "$f" ]; then
  77. # an empty file can be used to disable a specific "variant" section for an image
  78. text+=$'\n' # give a little space
  79. text+="$(< "$f")"
  80. text+=$'\n' # parameter expansion eats the trailing newline
  81. fi
  82. break
  83. fi
  84. done
  85. done
  86. if [ -n "$text" ]; then
  87. default="$([ -f "$repoDir/variant.md" ] && cat "$repoDir/variant.md" || cat "$dir/variant.md")"
  88. default+=$'\n' # parameter expansion eats the trailing newline
  89. if [ "$repo" != 'debian' ] && [ "$repo" != 'ubuntu' ]; then
  90. # what is 'jessie', 'stretch' and 'sid'
  91. # https://github.com/docker-library/python/issues/343
  92. debian=( $(bashbrew list --uniq "$(_repo 'debian')" | grep -vE 'stable|slim|backports|experimental|testing' | cut -d: -f2) )
  93. ubuntu=( $(bashbrew list "$(_repo 'ubuntu')" | grep -vE 'devel|latest|[0-9]' | cut -d: -f2) )
  94. foundDebianTags=
  95. foundUbuntuTags=
  96. for tag in ${tags[@]}; do
  97. for suite in "${debian[@]}"; do
  98. case "$tag" in
  99. *-"$suite" | "$suite"-* | *-"$suite"-* | "$suite" )
  100. foundDebianTags+=" $suite"
  101. ;;
  102. esac
  103. done
  104. for suite in "${ubuntu[@]}"; do
  105. case "$tag" in
  106. *-"$suite" | "$suite"-* | *-"$suite"-* | "$suite" )
  107. foundUbuntuTags+=" $suite"
  108. ;;
  109. esac
  110. done
  111. done
  112. if [ -n "$foundDebianTags" ]; then
  113. default+=$'\n' # give a little space
  114. default+="$( sed -e 's/%%DEB-SUITES%%/'"$(commaJoin "$foundDebianTags")"'/' "$dir/variant-default-debian.md" )"
  115. default+=$'\n' # parameter expansion eats the trailing newline
  116. fi
  117. if [ -n "$foundUbuntuTags" ]; then
  118. default+=$'\n' # give a little space
  119. default+="$( sed -e 's/%%DEB-SUITES%%/'"$(commaJoin "$foundUbuntuTags")"'/' "$dir/variant-default-ubuntu.md" )"
  120. default+=$'\n' # parameter expansion eats the trailing newline
  121. fi
  122. fi
  123. # buildpack-deps text
  124. potentialTags="$(bashbrew list --uniq "$bbRepo" | cut -d: -f2)"
  125. for tag in $potentialTags; do
  126. baseImage="$(bashbrew cat -f '{{ .ArchLastStageFrom (.TagEntry.Architectures | first) .TagEntry }}' "$bbRepo:$tag" 2>/dev/null)"
  127. case "$baseImage" in
  128. buildpack-deps:*-*) ;; # "scm", "curl" -- not large images
  129. buildpack-deps:*)
  130. default+=$'\n' # give a little space
  131. default+="$(< "$dir/variant-default-buildpack-deps.md")"
  132. default+=$'\n' # parameter expansion eats the trailing newline
  133. break
  134. ;;
  135. esac
  136. done
  137. echo
  138. echo
  139. echo -n "$default"
  140. echo "$text"
  141. fi