download-frozen-image-v1.sh 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. #!/usr/bin/env 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. usage() {
  12. echo "usage: $0 dir image[:tag][@image-id] ..."
  13. echo " ie: $0 /tmp/hello-world hello-world"
  14. echo " $0 /tmp/debian-jessie debian:jessie"
  15. echo " $0 /tmp/old-hello-world hello-world@ef872312fe1bbc5e05aae626791a47ee9b032efa8f3bda39cc0be7b56bfe59b9"
  16. echo " $0 /tmp/old-debian debian:latest@f6fab3b798be3174f45aa1eb731f8182705555f89c9026d8c1ef230cbf8301dd"
  17. [ -z "$1" ] || exit "$1"
  18. }
  19. dir="$1" # dir for building tar in
  20. shift || usage 1 >&2
  21. [ $# -gt 0 -a "$dir" ] || usage 2 >&2
  22. mkdir -p "$dir"
  23. # hacky workarounds for Bash 3 support (no associative arrays)
  24. images=()
  25. rm -f "$dir"/tags-*.tmp
  26. # repositories[busybox]='"latest": "...", "ubuntu-14.04": "..."'
  27. while [ $# -gt 0 ]; do
  28. imageTag="$1"
  29. shift
  30. image="${imageTag%%[:@]*}"
  31. tag="${imageTag#*:}"
  32. imageId="${tag##*@}"
  33. [ "$imageId" != "$tag" ] || imageId=
  34. [ "$tag" != "$imageTag" ] || tag='latest'
  35. tag="${tag%@*}"
  36. imageFile="${image//\//_}" # "/" can't be in filenames :)
  37. token="$(curl -sSL -o /dev/null -D- -H 'X-Docker-Token: true' "https://index.docker.io/v1/repositories/$image/images" | tr -d '\r' | awk -F ': *' '$1 == "X-Docker-Token" { print $2 }')"
  38. if [ -z "$imageId" ]; then
  39. imageId="$(curl -sSL -H "Authorization: Token $token" "https://registry-1.docker.io/v1/repositories/$image/tags/$tag")"
  40. imageId="${imageId//\"/}"
  41. fi
  42. ancestryJson="$(curl -sSL -H "Authorization: Token $token" "https://registry-1.docker.io/v1/images/$imageId/ancestry")"
  43. if [ "${ancestryJson:0:1}" != '[' ]; then
  44. echo >&2 "error: /v1/images/$imageId/ancestry returned something unexpected:"
  45. echo >&2 " $ancestryJson"
  46. exit 1
  47. fi
  48. IFS=','
  49. ancestry=( ${ancestryJson//[\[\] \"]/} )
  50. unset IFS
  51. if [ -s "$dir/tags-$imageFile.tmp" ]; then
  52. echo -n ', ' >> "$dir/tags-$imageFile.tmp"
  53. else
  54. images=( "${images[@]}" "$image" )
  55. fi
  56. echo -n '"'"$tag"'": "'"$imageId"'"' >> "$dir/tags-$imageFile.tmp"
  57. echo "Downloading '$imageTag' (${#ancestry[@]} layers)..."
  58. for imageId in "${ancestry[@]}"; do
  59. mkdir -p "$dir/$imageId"
  60. echo '1.0' > "$dir/$imageId/VERSION"
  61. curl -sSL -H "Authorization: Token $token" "https://registry-1.docker.io/v1/images/$imageId/json" -o "$dir/$imageId/json"
  62. # TODO figure out why "-C -" doesn't work here
  63. # "curl: (33) HTTP server doesn't seem to support byte ranges. Cannot resume."
  64. # "HTTP/1.1 416 Requested Range Not Satisfiable"
  65. if [ -f "$dir/$imageId/layer.tar" ]; then
  66. # TODO hackpatch for no -C support :'(
  67. echo "skipping existing ${imageId:0:12}"
  68. continue
  69. fi
  70. curl -SL --progress -H "Authorization: Token $token" "https://registry-1.docker.io/v1/images/$imageId/layer" -o "$dir/$imageId/layer.tar" # -C -
  71. done
  72. echo
  73. done
  74. echo -n '{' > "$dir/repositories"
  75. firstImage=1
  76. for image in "${images[@]}"; do
  77. imageFile="${image//\//_}" # "/" can't be in filenames :)
  78. [ "$firstImage" ] || echo -n ',' >> "$dir/repositories"
  79. firstImage=
  80. echo -n $'\n\t' >> "$dir/repositories"
  81. echo -n '"'"$image"'": { '"$(cat "$dir/tags-$imageFile.tmp")"' }' >> "$dir/repositories"
  82. done
  83. echo -n $'\n}\n' >> "$dir/repositories"
  84. rm -f "$dir"/tags-*.tmp
  85. echo "Download of images into '$dir' complete."
  86. echo "Use something like the following to load the result into a Docker daemon:"
  87. echo " tar -cC '$dir' . | docker load"