Last active
August 29, 2015 14:23
-
-
Save nextrevision/186e2c685c6e2605b618 to your computer and use it in GitHub Desktop.
CLI tool to interact with private docker registries
This file contains 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 | |
# requirements: | |
# brew install gawk gsed | |
# settings file ~/.registry_settings | |
# REGISTRY="registry.acme.com" | |
# URL="https://${REGISTRY}:5000/v1" | |
# USERNAME="admin" | |
# PASSWORD="password" | |
source ~/.registry_settings | |
ACTION=$1 | |
AUTH="${USERNAME}:${PASSWORD}" | |
function _usage() { | |
echo "$0 - interact with docker registry (${REGISTRY})" | |
echo "Commands:" | |
echo " env # prints script environment vars" | |
echo " images # lists all images" | |
echo " tags image_name # lists all tags for an image" | |
echo " info image_name [tag] # prints image/tag info" | |
echo " pull image_name [tag] # pulls an image via docker cli (default tag: latest)" | |
exit | |
} | |
function _error() { | |
echo "[!] ERROR: ${1}" | |
exit 1 | |
} | |
function _curl_json() { | |
local url=$1 | |
[ -z $url ] && _error "Must pass a URL to func _curl_json" | |
curl --silent -u $AUTH $url | python -m json.tool | |
} | |
function _env() { | |
echo "Registry: ${REGISTRY}" | |
echo "Base URL: ${URL}" | |
echo "Username: ${USERNAME}" | |
} | |
function _images() { | |
_curl_json ${URL}/search \ | |
| awk '/name/ { print $2 }' \ | |
| gsed -r 's/(["\/]|library)//g' | |
} | |
function _tags() { | |
local image=$1 | |
[ -z $image ] && _error "Must pass an image to tags command" | |
_curl_json ${URL}/repositories/${image}/tags \ | |
| gsed -r 's/[\{\}",]//g' \ | |
| gsed -r 's/^[ ]+//g' | |
} | |
function _info() { | |
local image=$1 | |
local tag=$2 | |
local req="${image}/json" | |
[ -z $image ] && _error "Must pass an image to info command" | |
[ -z $tag ] || req="${image}/tags/${tag}/json" | |
_curl_json ${URL}/repositories/${req} \ | |
| gsed -r 's/[\{\}",]//g' \ | |
| gsed -r 's/^[ ]+//g' \ | |
| gawk '{if ($2 ~ /[0-9]{10}/) {{$2=strftime("%c",$2)} {print "last_update:",$2}} else {print}}' | |
} | |
function _pull() { | |
local image=$1 | |
local tag=${2:-latest} | |
[ -z $image ] && _error "Must pass an image to pull command" | |
docker pull ${REGISTRY}/${image}:${tag} | |
} | |
[ -z $1 ] && _usage | |
while ! [ -z $1 ]; do | |
case $1 in | |
env) _env;; | |
images) _images;; | |
tags) shift; _tags $1;; | |
info) shift; i=$1; shift; t=$1; _info $i $t;; | |
pull) shift; i=$1; shift; t=$1; _pull $i $t;; | |
*) _usage;; | |
esac | |
shift | |
done |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment