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