Skip to content

Instantly share code, notes, and snippets.

@ake-persson
Last active June 2, 2021 18:19
Show Gist options
  • Select an option

  • Save ake-persson/1ca682f258d4ab43569b5b550bc0e66e to your computer and use it in GitHub Desktop.

Select an option

Save ake-persson/1ca682f258d4ab43569b5b550bc0e66e to your computer and use it in GitHub Desktop.

Revisions

  1. Michael Persson revised this gist Sep 17, 2016. No changes.
  2. Michael Persson revised this gist Sep 17, 2016. 1 changed file with 55 additions and 6 deletions.
    61 changes: 55 additions & 6 deletions pull.sh
    Original file line number Diff line number Diff line change
    @@ -1,19 +1,68 @@
    #!/bin/bash

    set -eu

    reg="registry.hub.docker.com"
    repo="gliderlabs"
    image="alpine"
    name="${repo}/${image}"
    tag="latest"
    parallel=4

    INFO="\033[1;32m"
    WARN="\033[0;33m"
    FATAL="\033[0;31m"
    CLEAR='\033[0m'

    info() {
    if [ -n "${QUIET:-}" ]; then
    return
    fi

    printf "* ${INFO}${1}${CLEAR}\n"
    }

    warn() {
    printf "! ${WARN}${1}${CLEAR}\n"
    }

    fatal() {
    printf "!! ${FATAL}${1}${CLEAR}\n"
    exit 1
    }

    # Get auth token
    token=$( curl -s "https://auth.docker.io/token?service=registry.docker.io&scope=repository:${name}:pull" | jq -r .token )
    echo "$token"

    # Get layers
    layers=$(curl -s -H "Authorization: Bearer $token" "https://${reg}/v2/${name}/manifests/${tag}" | jq -r .fsLayers[].blobSum )
    resp=$(curl -s -H "Authorization: Bearer $token" "https://${reg}/v2/${name}/manifests/${tag}" | jq -r .fsLayers[].blobSum )
    layers=( $( echo $resp | tr ' ' '\n' | sort -u ) )

    prun() {
    while (( "$#" )); do
    for (( i=0; i<$parallel; i++ )); do
    if [ -n "${1:-}" ]; then
    if [ -f "${1}.tar.gz" ]; then
    checksum=$( shasum -a 256 $1.tar.gz | awk '{ print $1 }' )
    if [ "$checksum" != "${1##sha256:}" ]; then
    warn "File exist checksum doesn't match, download again: ${1}.tar.gz"
    curl -s -o $1.tar.gz -L -H "Authorization: Bearer $token" "https://${reg}/v2/${name}/blobs/${1}" &
    else
    info "Skip file: ${1}.tar.gz"
    fi
    else
    info "Download: ${1}.tar.gz"
    curl -s -o $1.tar.gz -L -H "Authorization: Bearer $token" "https://${reg}/v2/${name}/blobs/${1}" &
    fi
    shift
    fi
    done
    wait
    done
    }

    prun ${layers[@]}

    # Download layers
    for layer in $layers; do
    curl -s -o $layer.tar.gz -L -H "Authorization: Bearer $token" "https://${reg}/v2/${name}/blobs/${layer}"
    done
    # Run twice in-case of failure
    QUIET="true"
    prun ${layers[@]}
  3. Michael Persson created this gist Sep 16, 2016.
    19 changes: 19 additions & 0 deletions pull.sh
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,19 @@
    #!/bin/bash

    reg="registry.hub.docker.com"
    repo="gliderlabs"
    image="alpine"
    name="${repo}/${image}"
    tag="latest"

    # Get auth token
    token=$( curl -s "https://auth.docker.io/token?service=registry.docker.io&scope=repository:${name}:pull" | jq -r .token )
    echo "$token"

    # Get layers
    layers=$(curl -s -H "Authorization: Bearer $token" "https://${reg}/v2/${name}/manifests/${tag}" | jq -r .fsLayers[].blobSum )

    # Download layers
    for layer in $layers; do
    curl -s -o $layer.tar.gz -L -H "Authorization: Bearer $token" "https://${reg}/v2/${name}/blobs/${layer}"
    done