Class: Ufo::DockerCleaner

Inherits:
Object
  • Object
show all
Includes:
Execute
Defined in:
lib/ufo/docker_cleaner.rb

Instance Method Summary collapse

Methods included from Execute

#execute

Constructor Details

#initialize(docker_image_name, options) ⇒ DockerCleaner

Returns a new instance of DockerCleaner.



5
6
7
8
9
10
11
12
# File 'lib/ufo/docker_cleaner.rb', line 5

def initialize(docker_image_name, options)
  # docker_image_name does not containg the tag
  # Example: 123456789.dkr.ecr.us-east-1.amazonaws.com/image
  @docker_image_name = docker_image_name
  @options = options
  @keep = options[:keep] || 3
  @tag_prefix = options[:tag_prefix] || "ufo"
end

Instance Method Details

#cleanupObject



14
15
16
17
18
19
20
21
# File 'lib/ufo/docker_cleaner.rb', line 14

def cleanup
  return if delete_list.empty?
  command = "docker rmi #{delete_list}"
  say "Cleaning up docker images...".green
  say "Running: #{"docker rmi #{delete_list}"}"
  return if @options[:noop]
  execute(command, use_system: false) # to use_system: false silence output
end

#delete_listObject



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/ufo/docker_cleaner.rb', line 23

def delete_list
  return @delete_list if @delete_list

  out = execute("docker images", live: true) # live to override the noop cli options
  name_regexp = Regexp.new(@docker_image_name)
  # Example tag: ufo-2016-10-19T00-36-47-211b63a
  tag_string = "#{@tag_prefix}-\\d{4}-\\d{2}-\\d{2}T\\d{2}-\\d{2}-\\d{2}-.{7}"
  tag_regexp = Regexp.new(tag_string)
  filtered_out = out.split("\n").select do |line|
    name,tag = line.split(' ')
    name =~ name_regexp && tag =~ tag_regexp
  end

  tags = filtered_out.map { |l| l.split(' ')[1] } # 2nd column is tag
  tags = tags.sort.reverse  # ordered by most recent images first
  delete_tags = tags[@keep..-1]
  if delete_tags.nil?
    @delete_list = []
  else
    @delete_list = delete_tags.map { |t| "#{@docker_image_name}:#{t}" }.join(' ')
  end
end

#say(msg) ⇒ Object



46
47
48
49
# File 'lib/ufo/docker_cleaner.rb', line 46

def say(msg)
  msg = "NOOP #{msg}" if @options[:noop]
  puts msg unless @options[:mute]
end