class Awful::ECR

Public Instance Methods

auth(*registries) click to toggle source
# File lib/awful/ecr.rb, line 71
def auth(*registries)
  registries = nil if registries.empty?
  ecr.get_authorization_token(registry_ids: registries).authorization_data.output do |auths|
    auths.each do |auth|
      puts YAML.dump(stringify_keys(auth.to_h))
    end
  end
end
create(repository) click to toggle source
# File lib/awful/ecr.rb, line 58
def create(repository)
  ecr.create_repository(repository_name: repository)
end
date(repository, *tags) click to toggle source
# File lib/awful/ecr.rb, line 137
def date(repository, *tags)
  ecr.batch_get_image(repository_name: repository, image_ids: image_tags(*tags)).images.output do |imgs|
    imgs.map do |img|
      parse_created(img)
    end.output do |dates|
      puts dates
    end
  end
end
delete(repository) click to toggle source
# File lib/awful/ecr.rb, line 64
def delete(repository)
  if yes? "Really delete repository #{repository}?", :yellow
    ecr.delete_repository(repository_name: repository, force: options[:force])
  end
end
describe_images(repository, tag_status = nil) click to toggle source
# File lib/awful/ecr.rb, line 24
def describe_images(repository, tag_status = nil)
  paginate(:image_details) do |next_token|
    ecr.describe_images(
      repository_name: repository,
      filter: { tag_status: tag_status },
      next_token: next_token,
    )
  end
end
dump(*repos) click to toggle source
# File lib/awful/ecr.rb, line 48
def dump(*repos)
  repos = nil if repos.empty? # omit this arg to show all repos
  ecr.describe_repositories(repository_names: repos).repositories.output do |list|
    list.each do |repo|
      puts YAML.dump(stringify_keys(repo.to_h))
    end
  end
end
ecr() click to toggle source
# File lib/awful/ecr.rb, line 13
def ecr
  @ecr ||= Aws::ECR::Client.new
end
exists(repository, tag) click to toggle source
# File lib/awful/ecr.rb, line 148
def exists(repository, tag)
  imgs = ecr.batch_get_image(repository_name: repository, image_ids: image_tags(tag)).images
  (imgs.empty? ? false : true).output(&method(:puts))
end
get(repository, *tags) click to toggle source
# File lib/awful/ecr.rb, line 117
def get(repository, *tags)
  ecr.batch_get_image(repository_name: repository, image_ids: image_tags(*tags)).images.output do |imgs|
    imgs.each do |img|
      puts YAML.dump(stringify_keys(img.to_h))
    end
  end
end
image_tags(*tags) click to toggle source

get array of image_tag hashes in form expected by get methods

# File lib/awful/ecr.rb, line 18
def image_tags(*tags)
  tags.map do |tag|
    {image_tag: tag}
  end
end
images(repository) click to toggle source
# File lib/awful/ecr.rb, line 99
def images(repository)
  tag_status = 'TAGGED'   if options[:tagged]
  tag_status = 'UNTAGGED' if options[:untagged]

  describe_images(repository, tag_status).output do |list|
    if options[:long]
      print_table list.map { |i|
        tags = i.image_tags || []
        size = "%.2f MiB" % (i.image_size_in_bytes/(1024*1024))
        [tags.join(','), i.image_pushed_at, size, i.image_digest]
      }
    else
      puts list.map(&:image_tags).flatten
    end
  end
end
inspect(repository, *tags) click to toggle source
# File lib/awful/ecr.rb, line 126
def inspect(repository, *tags)
  ecr.batch_get_image(repository_name: repository, image_ids: image_tags(*tags)).images.output do |imgs|
    imgs.map do |img|
      JSON.parse(JSON.parse(img.image_manifest)['history'].first['v1Compatibility'])
    end.output do |list|
      puts YAML.dump(list)
    end
  end
end
login(*registries) click to toggle source
# File lib/awful/ecr.rb, line 83
def login(*registries)
  cmd = options[:print] ? :puts : :system
  registries = nil if registries.empty?
  email = options[:email] ? "-e #{options[:email]}" : ''  # -e deprecated as of 1.14, here for older docker clients
  ecr.get_authorization_token(registry_ids: registries).authorization_data.output do |auths|
    auths.each do |auth|
      user, pass = Base64.decode64(auth.authorization_token).split(':')
      send(cmd, "docker login -u #{user} -p #{pass} #{email} #{auth.proxy_endpoint}")
    end
  end
end
ls() click to toggle source
# File lib/awful/ecr.rb, line 37
def ls
  ecr.describe_repositories.repositories.output do |repos|
    if options[:long]
      print_table repos.map { |r| [r.repository_name, r.registry_id, r.repository_arn] }.sort
    else
      puts repos.map(&:repository_name).sort
    end
  end
end
reap(repository, days, token: nil) click to toggle source
# File lib/awful/ecr.rb, line 157
def reap(repository, days, token: nil)
  verbose = options[:verbose] || options[:dry_run]
  next_token = token
  now = Time.now.utc

  deleted = failures = 0
  loop do
    response = ecr.describe_images(repository_name: repository, next_token: next_token, max_results: options[:batch])

    old_images = response.image_details.select do |image|
      date = image.image_pushed_at.utc
      age = ((now - date)/(24*60*60)).to_i
      if age > days.to_i
        puts "#{date} #{age} #{image.image_digest} #{image.image_tags}" if verbose
        true
      else
        false
      end
    end

    ## delete old images
    unless options[:dry_run] || old_images.empty?
      r = ecr.batch_delete_image(
        repository_name: repository,
        image_ids: old_images.map { |i| {image_digest: i.image_digest} }
      )
      deleted  += r.image_ids.count
      failures += r.failures.count
    end

    next_token = response.next_token
    break unless next_token
  end

  puts "deleted: #{deleted}, failures: #{failures}"
end