download-frozen-image-v2.sh 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. #!/bin/bash
  2. set -e
  3. # hello-world latest ef872312fe1b 3 months ago 910 B
  4. # hello-world latest ef872312fe1bbc5e05aae626791a47ee9b032efa8f3bda39cc0be7b56bfe59b9 3 months ago 910 B
  5. # debian latest f6fab3b798be 10 weeks ago 85.1 MB
  6. # debian latest f6fab3b798be3174f45aa1eb731f8182705555f89c9026d8c1ef230cbf8301dd 10 weeks ago 85.1 MB
  7. if ! command -v curl &> /dev/null; then
  8. echo >&2 'error: "curl" not found!'
  9. exit 1
  10. fi
  11. if ! command -v jq &> /dev/null; then
  12. echo >&2 'error: "jq" not found!'
  13. exit 1
  14. fi
  15. usage() {
  16. echo "usage: $0 dir image[:tag][@digest] ..."
  17. echo " $0 /tmp/old-hello-world hello-world:latest@sha256:8be990ef2aeb16dbcb9271ddfe2610fa6658d13f6dfb8bc72074cc1ca36966a7"
  18. [ -z "$1" ] || exit "$1"
  19. }
  20. dir="$1" # dir for building tar in
  21. shift || usage 1 >&2
  22. [ $# -gt 0 -a "$dir" ] || usage 2 >&2
  23. mkdir -p "$dir"
  24. # hacky workarounds for Bash 3 support (no associative arrays)
  25. images=()
  26. rm -f "$dir"/tags-*.tmp
  27. # repositories[busybox]='"latest": "...", "ubuntu-14.04": "..."'
  28. while [ $# -gt 0 ]; do
  29. imageTag="$1"
  30. shift
  31. image="${imageTag%%[:@]*}"
  32. imageTag="${imageTag#*:}"
  33. digest="${imageTag##*@}"
  34. tag="${imageTag%%@*}"
  35. # add prefix library if passed official image
  36. if [[ "$image" != *"/"* ]]; then
  37. image="library/$image"
  38. fi
  39. imageFile="${image//\//_}" # "/" can't be in filenames :)
  40. token="$(curl -sSL "https://auth.docker.io/token?service=registry.docker.io&scope=repository:$image:pull" | jq --raw-output .token)"
  41. manifestJson="$(curl -sSL -H "Authorization: Bearer $token" "https://registry-1.docker.io/v2/$image/manifests/$digest")"
  42. if [ "${manifestJson:0:1}" != '{' ]; then
  43. echo >&2 "error: /v2/$image/manifests/$digest returned something unexpected:"
  44. echo >&2 " $manifestJson"
  45. exit 1
  46. fi
  47. layersFs=$(echo "$manifestJson" | jq --raw-output '.fsLayers | .[] | .blobSum')
  48. IFS=$'\n'
  49. # bash v4 on Windows CI requires CRLF separator
  50. if [ "$(go env GOHOSTOS)" = 'windows' ]; then
  51. major=$(echo ${BASH_VERSION%%[^0.9]} | cut -d. -f1)
  52. if [ "$major" -ge 4 ]; then
  53. IFS=$'\r\n'
  54. fi
  55. fi
  56. layers=( ${layersFs} )
  57. unset IFS
  58. history=$(echo "$manifestJson" | jq '.history | [.[] | .v1Compatibility]')
  59. imageId=$(echo "$history" | jq --raw-output .[0] | jq --raw-output .id)
  60. if [ -s "$dir/tags-$imageFile.tmp" ]; then
  61. echo -n ', ' >> "$dir/tags-$imageFile.tmp"
  62. else
  63. images=( "${images[@]}" "$image" )
  64. fi
  65. echo -n '"'"$tag"'": "'"$imageId"'"' >> "$dir/tags-$imageFile.tmp"
  66. echo "Downloading '${image}:${tag}@${digest}' (${#layers[@]} layers)..."
  67. for i in "${!layers[@]}"; do
  68. imageJson=$(echo "$history" | jq --raw-output .[${i}])
  69. imageId=$(echo "$imageJson" | jq --raw-output .id)
  70. imageLayer=${layers[$i]}
  71. mkdir -p "$dir/$imageId"
  72. echo '1.0' > "$dir/$imageId/VERSION"
  73. echo "$imageJson" > "$dir/$imageId/json"
  74. # TODO figure out why "-C -" doesn't work here
  75. # "curl: (33) HTTP server doesn't seem to support byte ranges. Cannot resume."
  76. # "HTTP/1.1 416 Requested Range Not Satisfiable"
  77. if [ -f "$dir/$imageId/layer.tar" ]; then
  78. # TODO hackpatch for no -C support :'(
  79. echo "skipping existing ${imageId:0:12}"
  80. continue
  81. fi
  82. token="$(curl -sSL "https://auth.docker.io/token?service=registry.docker.io&scope=repository:$image:pull" | jq --raw-output .token)"
  83. curl -SL --progress -H "Authorization: Bearer $token" "https://registry-1.docker.io/v2/$image/blobs/$imageLayer" -o "$dir/$imageId/layer.tar" # -C -
  84. done
  85. echo
  86. done
  87. echo -n '{' > "$dir/repositories"
  88. firstImage=1
  89. for image in "${images[@]}"; do
  90. imageFile="${image//\//_}" # "/" can't be in filenames :)
  91. image="${image#library\/}"
  92. [ "$firstImage" ] || echo -n ',' >> "$dir/repositories"
  93. firstImage=
  94. echo -n $'\n\t' >> "$dir/repositories"
  95. echo -n '"'"$image"'": { '"$(cat "$dir/tags-$imageFile.tmp")"' }' >> "$dir/repositories"
  96. done
  97. echo -n $'\n}\n' >> "$dir/repositories"
  98. rm -f "$dir"/tags-*.tmp
  99. echo "Download of images into '$dir' complete."
  100. echo "Use something like the following to load the result into a Docker daemon:"
  101. echo " tar -cC '$dir' . | docker load"