Last active
August 29, 2015 14:24
-
-
Save menski/8a9b64e5cac5032bb5cc to your computer and use it in GitHub Desktop.
Remove unused images from docker registry v1 directory
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
#!/usr/bin/env python2 | |
from __future__ import print_function | |
from glob import iglob | |
from json import load | |
from os import listdir | |
from sys import argv, exit | |
from shutil import rmtree | |
def get_tag_ids(path): | |
files = iglob('{}/repositories/*/*/tag_*'.format(path)) | |
return read_ids(files) | |
def read_ids(files): | |
ids = set() | |
for file in files: | |
with open(file, 'r') as tag: | |
ids.add(tag.readline()) | |
return ids | |
def get_ancestors(path, tag_ids): | |
ancestors = set() | |
for tag_id in tag_ids: | |
ancestors.update(get_ancestors_for_tag(path, tag_id)) | |
return ancestors | |
def get_ancestors_for_tag(path, tag_id): | |
with open('{}/images/{}/ancestry'.format(path, tag_id)) as ancestry: | |
return load(ancestry) | |
def get_unused(path, used): | |
images = listdir('{}/images/'.format(path)) | |
return [image for image in images if image not in used] | |
def delete_images(path, images): | |
for image in images: | |
rmtree('{}/images/{}'.format(path, image)) | |
if __name__ == '__main__': | |
if len(argv) != 2: | |
print('Usage: {} REGISTRY_PATH'.format(argv[0])) | |
exit(1) | |
registry_path = argv[1] | |
tags = get_tag_ids(registry_path) | |
used = get_ancestors(registry_path, tags) | |
unused = get_unused(registry_path, used) | |
print('Unused images:', '\n'.join(unused), sep='\n') | |
answer = '' | |
while unused: | |
if answer == 'y': | |
delete_images(registry_path, unused) | |
break | |
elif answer == 'n': | |
break | |
else: | |
answer = raw_input('Delete images [y/n]: ').lower() |
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
#!/usr/bin/env python3 | |
from glob import iglob | |
from json import load | |
from os import listdir | |
from sys import argv, exit | |
from shutil import rmtree | |
def get_tag_ids(path): | |
files = iglob('{}/repositories/*/*/tag_*'.format(path)) | |
return read_ids(files) | |
def read_ids(files): | |
ids = set() | |
for file in files: | |
with open(file, 'r') as tag: | |
ids.add(tag.readline()) | |
return ids | |
def get_ancestors(path, tag_ids): | |
ancestors = set() | |
for tag_id in tag_ids: | |
ancestors.update(get_ancestors_for_tag(path, tag_id)) | |
return ancestors | |
def get_ancestors_for_tag(path, tag_id): | |
with open('{}/images/{}/ancestry'.format(path, tag_id)) as ancestry: | |
return load(ancestry) | |
def get_unused(path, used): | |
images = listdir('{}/images/'.format(path)) | |
return [image for image in images if image not in used] | |
def delete_images(path, images): | |
for image in images: | |
rmtree('{}/images/{}'.format(path, image)) | |
if __name__ == '__main__': | |
if len(argv) != 2: | |
print('Usage: {} REGISTRY_PATH'.format(argv[0])) | |
exit(1) | |
registry_path = argv[1] | |
tags = get_tag_ids(registry_path) | |
used = get_ancestors(registry_path, tags) | |
unused = get_unused(registry_path, used) | |
print('Unused images:', '\n'.join(unused), sep='\n') | |
answer = '' | |
while unused: | |
if answer == 'y': | |
delete_images(registry_path, unused) | |
break | |
elif answer == 'n': | |
break | |
else: | |
answer = input('Delete images [y/n]: ').lower() | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment