Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/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
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement