Class: Awful::ECR

Inherits:
Cli show all
Defined in:
lib/awful/ecr.rb

Instance Method Summary collapse

Methods inherited from Cli

#initialize, #ll, #version

Constructor Details

This class inherits a constructor from Awful::Cli

Instance Method Details

#auth(*registries) ⇒ Object



72
73
74
75
76
77
78
79
# File 'lib/awful/ecr.rb', line 72

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) ⇒ Object



59
60
61
# File 'lib/awful/ecr.rb', line 59

def create(repository)
  ecr.create_repository(repository_name: repository)
end

#date(repository, *tags) ⇒ Object



138
139
140
141
142
143
144
145
146
# File 'lib/awful/ecr.rb', line 138

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) ⇒ Object



65
66
67
68
69
# File 'lib/awful/ecr.rb', line 65

def delete(repository)
  if yes? "Really delete repository #{repository}?", :yellow
    ecr.delete_repository(repository_name: repository, force: options[:force])
  end
end

#dump(*repos) ⇒ Object



49
50
51
52
53
54
55
56
# File 'lib/awful/ecr.rb', line 49

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

#exists(repository, tag) ⇒ Object



149
150
151
152
# File 'lib/awful/ecr.rb', line 149

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) ⇒ Object



118
119
120
121
122
123
124
# File 'lib/awful/ecr.rb', line 118

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

#images(repository) ⇒ Object



100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
# File 'lib/awful/ecr.rb', line 100

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) ⇒ Object



127
128
129
130
131
132
133
134
135
# File 'lib/awful/ecr.rb', line 127

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) ⇒ Object



84
85
86
87
88
89
90
91
92
93
94
# File 'lib/awful/ecr.rb', line 84

def (*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

#lsObject



38
39
40
41
42
43
44
45
46
# File 'lib/awful/ecr.rb', line 38

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) ⇒ Object



158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
# File 'lib/awful/ecr.rb', line 158

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