Class: Ufo::DockerCleaner
- Inherits:
-
Object
- Object
- Ufo::DockerCleaner
- Includes:
- Execute
- Defined in:
- lib/ufo/docker_cleaner.rb
Instance Method Summary collapse
- #cleanup ⇒ Object
- #delete_list ⇒ Object
-
#initialize(docker_image_name, options) ⇒ DockerCleaner
constructor
A new instance of DockerCleaner.
- #say(msg) ⇒ Object
Methods included from 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, ) # 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 = @keep = [:keep] || 3 @tag_prefix = [:tag_prefix] || "ufo" end |
Instance Method Details
#cleanup ⇒ Object
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_list ⇒ Object
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 = filtered_out.map { |l| l.split(' ')[1] } # 2nd column is tag = .sort.reverse # ordered by most recent images first = [@keep..-1] if .nil? @delete_list = [] else @delete_list = .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 |