Advertisement
Guest User

Untitled

a guest
Oct 27th, 2016
58
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.73 KB | None | 0 0
  1. #!/usr/bin/env ruby
  2. # prints the Docker Image Labels for ECR images matching a given tag expression
  3. require 'aws-sdk'
  4.  
  5. repo = 'mdsol/operationalanalytics/build'
  6. tag_match = /feature-sprint3\.maps/
  7. sort_field = 'com.mdsol.12f-buildDate'
  8. reverse_sort = true # reverse is nicer when date sorting
  9. label_filter = /\Acom\.mdsol\.12f-appDesc/ # values in these labels are ignored
  10. date_fields = /\Acom\.mdsol\.12f-.*Date\Z/ # these values get DateTime parsed
  11. region = 'us-east-1'
  12.  
  13. # get all images for repo (<= 100)
  14. ecr = Aws::ECR::Client.new(region: region)
  15. STDERR.printf "Fetching all images from Repo %s in %s...\n", repo, region
  16. all_imgs = ecr.describe_images(repository_name: repo).image_details
  17.  
  18. # filter images by tag_match
  19. img_info = all_imgs.select{|i| !(i.image_tags.select {|t| t =~ tag_match}).empty?}
  20.  
  21. # get image details with another API call
  22. STDERR.printf "Fetching image details for %d matching images...\n", img_info.count
  23. details = ecr.batch_get_image(
  24. repository_name: repo,
  25. image_ids: img_info.map{|i| { image_digest: i.image_digest}}).images
  26.  
  27. # extract Docker Labels, excluding label_filter, and parsing DateTime fields
  28. STDERR.printf "Extracting image manifests...\n"
  29. manifests = details.map{|d| JSON.parse(JSON.parse(
  30. d.image_manifest)['history'].first['v1Compatibility'])}
  31. labels = manifests.map{|m| m['config']['Labels'].select{|k,v| k !~ label_filter}}
  32.  
  33. # sort as needed
  34. if (! sort_field.empty?) && (labels.first.has_key?(sort_field))
  35. labels.sort! do |a,b|
  36. if a =~ date_fields
  37. DateTime.parse(a[sort_field]) <=> DateTime.parse(b[sort_field])
  38. else
  39. a[sort_field] <=> b[sort_field]
  40. end
  41. end
  42. end
  43. labels.reverse! if reverse_sort
  44.  
  45. puts JSON.pretty_generate(labels) # output
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement