Created
January 17, 2017 13:44
-
-
Save drankard/3dcb172f68d82d2f950ed460bfa77900 to your computer and use it in GitHub Desktop.
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 | |
HELP=false | |
LIST=false | |
DELETE=false | |
TAG=EMPTY | |
REPOSITORY=EMPTY | |
ACCEPT_HEADER="application/vnd.docker.distribution.manifest.v2+json" | |
OPTS=$(getopt -n 'parse-options' -o hrtld -l help:,repository:,tag,:list-tags:delete-tag: -- "$@"); | |
if [ $? != 0 ] ; then echo "Failed parsing options." >&2 ; exit 1 ; fi | |
while true; do | |
case "$1" in | |
-h|--help ) HELP=true; shift ;; | |
-l|--list-tags ) LIST=true; shift ;; | |
-d|--delete-tag ) DELETE=true; shift ;; | |
-r|--repository ) REPOSITORY="$2"; shift; shift ;; | |
-t|--tag ) TAG="$2"; shift; shift ;; | |
-- ) shift; break ;; | |
* ) break ;; | |
esac | |
done | |
echo "HELP:$HELP" | |
echo "LIST:$LIST" | |
echo "DELETE:$DELETE" | |
echo "TAG:$TAG" | |
echo "REPOSITORY:$REPOSITORY" | |
if [[ $REPOSITORY = "" ]] ; then echo "Failed parsing options."; exit 1; fi | |
if [[ $TAG = "" ]] ; then echo "Failed parsing options."; exit 1; fi | |
function print_help { | |
echo "usage: | |
[-h | --help] Print this help | |
[-l | --list-tags] list all tags in repository, requires -r <repository> | |
[-d | --delete-tag] Delete a tag, requires -r <repository> -t <tag> | |
[-r | --repository <repository>] Docker repository | |
[-t | --t <tag>] Docker tag | |
" | |
} | |
function list_tags { | |
local REPOSITORY=$1 | |
echo "Listing tags in ${REPOSITORY}" | |
curl -sk --header "Accept: ${ACCEPT_HEADER}" http://docker.ice-skm.dk:80/v2/skm/datomic/tags/list | jq | |
} | |
function delete_tag { | |
local REPOSITORY=$1 | |
local TAG=$2 | |
echo "Deleting tag < ${TAG} > in Repository < ${REPOSITORY} >" | |
local DIGEST=$(curl -sk --head --header "Accept: ${ACCEPT_HEADER}" "${REPOSITORY}/manifests/${TAG}" | grep Docker-Content-Digest | awk '{print $2}' | tr -d "[:cntrl:]") | |
RESP=$(curl -sk -X DELETE --header "Accept: ${ACCEPT_HEADER}" "${REPOSITORY}/manifests/${DIGEST}") | |
echo $RESP | |
} | |
if [ $HELP = true ] ; then | |
printl_help | |
exit 0 | |
fi | |
if [ $LIST = true ] && [[ $REPOSITORY != "EMPTY" ]] ; then | |
list_tags $REPOSITORY | |
exit 0 | |
fi | |
if [ $DELETE = true ] && [[ $REPOSITORY != "EMPTY" ]] && [[ $TAG != "EMPTY" ]] ; then | |
delete_tag $REPOSITORY $TAG | |
exit 0 | |
else | |
print_help | |
exit 1 | |
fi |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment