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