Created
January 17, 2018 07:09
-
-
Save retanoj/86f8699da77bc7d5c40b9e21a8701223 to your computer and use it in GitHub Desktop.
docker_registry_operation.py
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
# coding:utf-8 | |
import requests | |
import json | |
import sys | |
requests.packages.urllib3.disable_warnings() | |
headers = { | |
"Accept": "application/vnd.docker.distribution.manifest.v2+json" | |
} | |
baseapi = "https://registry.com/v2" | |
def list(): | |
r = requests.get(baseapi + "/_catalog", headers=headers, verify=False) | |
print json.dumps(json.loads(r.content), indent=2) | |
def tags(container): | |
r = requests.get(baseapi + "/{}/tags/list".format(container), | |
headers=headers, verify=False) | |
print json.dumps(json.loads(r.content), indent=2) | |
def delete(container, tag): | |
confirm = raw_input( | |
"Ready to Delete [{}:{}] ?? [Y/n] > ".format(container, tag)) | |
if confirm != 'Y' and confirm != 'y': | |
return | |
r = requests.get(baseapi + "/{}/manifests/{}".format(container, | |
tag), headers=headers, verify=False) | |
manifest = r.headers.get('Docker-Content-Digest', None) | |
if manifest: | |
print 'delete manifest: {}'.format(manifest) | |
r = requests.delete(baseapi + "/{}/manifests/{}".format(container, | |
manifest), headers=headers, verify=False) | |
if r.status_code == 202: | |
print '{}:{} Delete OK!'.format(container, tag) | |
else: | |
print '{}:{} Error'.format(container, tag) | |
else: | |
print '{}:{} Not Exist'.format(container, tag) | |
argl = len(sys.argv) | |
if argl == 1: | |
print "python docker_registry_exec.py list|tags|delete [container] [tag]" | |
elif argl > 1: | |
op = sys.argv[1] | |
if op == 'list': | |
list() | |
elif op == 'tags': | |
container = sys.argv[2] | |
tags(container) | |
elif op == 'delete': | |
container = sys.argv[2] | |
tag = sys.argv[3] | |
delete(container, tag) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment