class UC3Code::ECRImagesClient
Query for repository images by tag
Public Class Methods
Source
# File app/lib/client/code/ecr_images.rb, line 10 def initialize @client = Aws::ECR::Client.new(region: UC3::UC3Client.region) @client.describe_registry super(enabled: enabled) rescue StandardError => e super(enabled: false, message: e.to_s) end
Calls superclass method
UC3::UC3Client::new
Public Instance Methods
Source
# File app/lib/client/code/ecr_images.rb, line 183 def archive_images [] end
Source
# File app/lib/client/code/ecr_images.rb, line 22 def cleanup_archive_images(image_repo) images = [] @client.list_images( repository_name: image_repo ).image_ids.each do |img| tag = img.image_tag next if tag.nil? match = /^archive-.*-(\d{8,8})$/.match(tag) next if match.nil? status = 'Keep' deltag = Date.strptime(match[1], '%Y%m%d') < Date.today - 14 if deltag begin @client.batch_delete_image( repository_name: image_repo, image_ids: [ { image_tag: tag } ] ) status = 'Deleted' rescue StandardError status = 'Delete Failed' end end images << { image: image_repo, tag: tag, status: status } end images end
Source
# File app/lib/client/code/ecr_images.rb, line 187 def delete_image(tag, image) @client.batch_delete_image( repository_name: image, image_ids: [ { image_tag: tag } ] ) end
Source
# File app/lib/client/code/ecr_images.rb, line 223 def get_manifest_tag(image) tag = UC3S3::ConfigObjectsClient.client.get_ecs_release_manifest_stack_tag(image) return '' if tag.empty? options = { repository_name: image, image_ids: [ { image_tag: tag } ] } options[:registry_id] = ENV.fetch('ECR_ACCOUNT', '') unless ENV.fetch('ECR_ACCOUNT', '').empty? resp = @client.describe_images(options) return '' if resp.image_details.nil? date = '' resp.image_details.each do |img| date = img.image_pushed_at if img.image_pushed_at end return tag if date.nil? { title: "#{tag} (#{date_format(date)})", value: "#{tag}*" } end
Source
# File app/lib/client/code/ecr_images.rb, line 154 def image_table(res) table = AdminUI::FilterTable.new( columns: [ AdminUI::Column.new(:tag, header: 'Image Tag'), AdminUI::Column.new(:image, header: 'Image'), AdminUI::Column.new(:digest, header: 'Digest'), AdminUI::Column.new(:pushed, header: 'Pushed At'), AdminUI::Column.new(:matching_tags, header: 'Matching Image Tags'), AdminUI::Column.new(:actions, header: 'Actions') ], description: "#### Tag deletion rules:\n\n" \ "- Tags matching Merritt stack names cannot be deleted\n" \ '- Tags registered in the [ECS Release Manifest](/merritt_manifest) cannot be deleted' ) res.each_key do |tag| next if UC3::UC3Client.semantic_prefix_tag?(tag) res.fetch(tag, []).each do |rec| table.add_row( AdminUI::Row.make_row( table.columns, rec ) ) end end table end
Source
# File app/lib/client/code/ecr_images.rb, line 57 def list_images(repohash: {}) res = {} dig = {} return res unless enabled tagimages = repohash.fetch(:image_repos, []) tagimages.each do |image| begin imglist = @client.list_images( repository_name: image ) rescue StandardError # puts "Client ERR: #{e}: #{@client}" return res end imglist.image_ids.each do |img| tag = img.image_tag next if tag.nil? next if tag =~ /^archive/ rec = { tag: tag, digest: img.image_digest, image: image, pushed: nil, pulled: nil, matching_tags: [], actions: [] } dig[img.image_digest] = dig.fetch(img.image_digest, []) dig[img.image_digest] << tag @client.describe_images( repository_name: image, image_ids: [ { image_tag: tag, image_digest: img.image_digest } ] ).image_details.each do |imgdet| rec[:pushed] = date_format(imgdet.image_pushed_at) end res[tag] = res.fetch(tag, []) res[tag] << rec end end res.each do |tag, arr| arr.each do |rec| image = rec[:image] dig[rec[:digest]].each do |t| next if t == tag rec[:matching_tags] << t end rec[:digest] = { value: rec[:digest], title: rec[:digest] } rec[:deployed] = UC3::UC3Client.deployed_tag?(tag, rec[:matching_tags]) if rec[:deployed] rec[:actions] << { value: 'STACK TAG', cssclass: 'button', disabled: true, href: '#', title: 'This image tag is a special tag that corresponds to a Merritt stack name. ' \ 'This should not be deleted.' } next end reposhort = File.basename(repohash.fetch(:repo, '')) deployed_tags = UC3S3::ConfigObjectsClient.client.get_release_manifest_deploy_tags(reposhort) if deployed_tags.include?(tag) rec[:actions] << { value: 'DEPLOYED', cssclass: 'button', disabled: true, href: '#', title: 'This image tag is deployed in a release manifest' } next end rec[:actions] << [ { value: 'Delete', href: "/source/images/delete/#{tag}", cssclass: 'button', post: true, disabled: false, data: image } ] end end res end
Source
# File app/lib/client/code/ecr_images.rb, line 198 def retag_image(tag, newtag, image) ecracct = ENV.fetch('ECR_ACCOUNT', '') options = { repository_name: image, image_ids: [ { image_tag: tag } ] } options[:registry_id] = ecracct unless ecracct.empty? resp = @client.batch_get_image(options) options = { repository_name: image, image_manifest: resp.images[0].image_manifest, image_tag: newtag } options[:registry_id] = ecracct unless ecracct.empty? @client.put_image(options) end
Source
# File app/lib/client/code/ecr_images.rb, line 219 def untag_image(tag, image) delete_image(tag, image) end