Skip to content

Instantly share code, notes, and snippets.

@kebyn
Forked from alex-bender/get_image_manifest.sh
Created April 24, 2018 12:50
Show Gist options
  • Save kebyn/ec9e1f797c3b3ab943582f40d7bb21d7 to your computer and use it in GitHub Desktop.
Save kebyn/ec9e1f797c3b3ab943582f40d7bb21d7 to your computer and use it in GitHub Desktop.
Docker Registry v2 get manifest and push\pull
#!/bin/bash
#
# Shell scripts for get image manifest from v2 registry
#
# Tested on Debian 8, curl 7.38.0, jq-1.5
#
set -e -u
# Default tag is latest
tag=latest
getLogin() {
read -p "Please enter username: " username
read -p "Please enter reponame: " repo
reponame=${username}/${repo}
if [ -n "$username" ]; then
read -s -p "password: " password
echo
fi
}
getBearerToken() {
local HEADERS
local RESPONSE
if [ -n "$username" ]; then
HEADERS="Authorization: Basic $(echo -n "${username}:${password}" | base64)"
fi
echo [+] Logging in
curl -s -H "$HEADERS" "https://auth.docker.io/token?service=registry.docker.io&scope=repository:${reponame}:pull" | jq '.token' -r > token
echo [+] Got Bearer token
}
getManifestV2() {
local ACCEPT="Accept: application/vnd.docker.distribution.manifest.v2+json"
local REGISTER_URI="https://registry-1.docker.io/v2"
curl -s -H "${ACCEPT}" -H "Authorization: Bearer $(cat token)" "${REGISTER_URI}/${reponame}/manifests/${tag}" -o manifest.json
echo
echo [+] Got Manifest in \`manifest.json\'
}
doClean() {
rm token
echo [+] Token file removed
}
getLogin
getBearerToken ${reponame}
getManifestV2
doClean
#!/bin/bash
# Shell scripts for push/pull to v2 registry
#
# - Get a token.
# - Push a blob.
# - Pull that blob repeatedly.
#
# Tested on OS X 10.10.3, curl 7.38.0, jq-1.4
#
reponame="jlhawn/test-script"
getLogin() {
read -p "Please enter username (or empty for anonymous): " username
if [ -n "$username" ]; then
read -s -p "password: " password
echo
fi
}
getToken() {
local reponame=$1
local actions=$2
local headers
local response
if [ -n "$username" ]; then
headers="Authorization: Basic $(echo -n "${username}:${password}" | base64)"
fi
response=$(curl -s -H "$headers" "https://auth.docker.io/token?service=registry.docker.io&scope=repository:$reponame:$actions")
echo $response | jq '.token' | xargs echo
}
uploadBlob() {
local reponame=$1
local token=$(getToken $reponame "push,pull")
local uploadURL
local numBytes=10000000 # 10 Megabytes
uploadURL=$(curl -siL -H "Authorization: Bearer $token" -X POST "https://registry-1.docker.io/v2/$reponame/blobs/uploads/" | grep 'Location:' | cut -d ' ' -f 2 | tr -d '[:space:]')
blobDigest="sha256:$(head -c $numBytes /dev/urandom | tee upload.tmp | shasum -a 256 | cut -d ' ' -f 1)"
echo "Uploading Blob of 10 Random Megabytes"
time curl -T upload.tmp --progress-bar -H "Authorization: Bearer $token" "$uploadURL&digest=$blobDigest" > /dev/null
}
downloadBlob() {
local reponame=$1
local blobDigest=$2
local token=$(getToken $reponame "pull")
echo "Downloading Blob"
time curl -L --progress-bar -H "Authorization: Bearer $token" "https://registry-1.docker.io/v2/$reponame/blobs/$blobDigest" > download.tmp
}
getLogin
uploadBlob $reponame
for i in {1..10}; do
downloadBlob $reponame $blobDigest
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment