Skip to content

Instantly share code, notes, and snippets.

@benton
Created October 26, 2016 18:10
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 benton/93fcb9576c82cc6fc92049ebb8bbd4bd to your computer and use it in GitHub Desktop.
Save benton/93fcb9576c82cc6fc92049ebb8bbd4bd to your computer and use it in GitHub Desktop.
prints the Docker Image Labels for ECR images matching a given tag expression
#!/usr/bin/env ruby
# prints the Docker Image Labels for ECR images matching a given tag expression
require 'aws-sdk'
repo = 'mdsol/operationalanalytics/build'
tag_match = /feature-sprint3\.maps/
sort_field = 'com.mdsol.12f-buildDate'
reverse_sort = true # reverse is nicer when date sorting
label_filter = /\Acom\.mdsol\.12f-appDesc/ # values in these labels are ignored
date_fields = /\Acom\.mdsol\.12f-.*Date\Z/ # these values get DateTime parsed
region = 'us-east-1'
# get all images for repo (<= 100)
ecr = Aws::ECR::Client.new(region: region)
STDERR.printf "Fetching all images from Repo %s in %s...\n", repo, region
all_imgs = ecr.describe_images(repository_name: repo).image_details
# filter images by tag_match
img_info = all_imgs.select{|i| !(i.image_tags.select {|t| t =~ tag_match}).empty?}
# get image details with another API call
STDERR.printf "Fetching image details for %d matching images...\n", img_info.count
details = ecr.batch_get_image(
repository_name: repo,
image_ids: img_info.map{|i| { image_digest: i.image_digest}}).images
# extract Docker Labels, excluding label_filter, and parsing DateTime fields
STDERR.printf "Extracting image manifests...\n"
manifests = details.map{|d| JSON.parse(JSON.parse(
d.image_manifest)['history'].first['v1Compatibility'])}
labels = manifests.map{|m| m['config']['Labels'].select{|k,v| k !~ label_filter}}
# sort as needed
if (! sort_field.empty?) && (labels.first.has_key?(sort_field))
labels.sort! do |a,b|
if a =~ date_fields
DateTime.parse(a[sort_field]) <=> DateTime.parse(b[sort_field])
else
a[sort_field] <=> b[sort_field]
end
end
end
labels.reverse! if reverse_sort
puts JSON.pretty_generate(labels) # output
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment