Created
May 18, 2016 00:24
-
-
Save mavencode01/a5717c12e7b423607f13a4926f11f12b to your computer and use it in GitHub Desktop.
Registry v2 Push and Pull Script
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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