Skip to content

Instantly share code, notes, and snippets.

@ten0s
Last active August 29, 2015 14:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ten0s/eba6f72dbfeda815a05e to your computer and use it in GitHub Desktop.
Save ten0s/eba6f72dbfeda815a05e to your computer and use it in GitHub Desktop.
Get comments history of image
#!/usr/bin/env python
import json
import sys, subprocess
def get_history_ids(image_id):
p = subprocess.Popen("docker history -q " + image_id,
stdout=subprocess.PIPE,
shell=True)
(out, _err) = p.communicate()
ids = out.split()
return ids
def get_image_info(image_id):
p = subprocess.Popen("docker inspect " + image_id,
stdout=subprocess.PIPE,
shell=True)
(out, _err) = p.communicate()
info = json.loads(out)[0]
full_info = get_full_image_info(info, image_id)
return full_info
def get_full_image_info(info, image_id_or_name):
"""Fill empty (in docker 1.3.1) Image field"""
if image_id_or_name == info['Id'][:len(image_id_or_name)]:
# id given, need to determine name
image_id = image_id_or_name
info['Image'] = get_image_name(image_id)
else:
# name given
image_name = image_id_or_name
info['Image'] = image_name
return info
def get_image_name(image_id):
p = subprocess.Popen("docker images | grep " + image_id,
stdout=subprocess.PIPE,
shell=True)
(out, _err) = p.communicate()
chunks = out.split()
if chunks:
return chunks[0] + ":" + chunks[1]
else:
return ''
def get_image_repo_tag(info):
repo_tag = info['Image'].split(':')
if repo_tag == ['']:
return ['<none>','<none>']
elif repo_tag == ['', '']:
return ['<none>','<none>']
else:
return repo_tag
def print_table(table):
col_width = [max(len(x) for x in col) for col in zip(*table)]
for line in table:
print " ".join("{:{}}".format(x, col_width[i])
for i, x in enumerate(line))
if __name__ == '__main__':
if len(sys.argv) > 1:
image_id = sys.argv[1].strip()
ids = get_history_ids(image_id)
table = [['REPOSITORY', 'TAG', 'IMAGE ID', 'COMMENT']]
for id in ids:
info = get_image_info(id)
[repo, tag] = get_image_repo_tag(info)
comment = info['Comment']
table.append([repo, tag, id, comment])
print_table(table)
else:
print 'Usage: docker-log IMAGE'
sys.exit(1)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment