Sha256: 94e5ed458c4a9456d8cb27d914e51156e7a9b7cd2afe1c5bd64ae00273e1dd30

Contents?: true

Size: 1.73 KB

Versions: 2

Compression:

Stored size: 1.73 KB

Contents

module Ufo
  class DockerCleaner
    include Execute

    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

    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

    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?
        say "No images found that matched #{@docker_image_name}:#{tag_string}"
        @delete_list = []
      else
        @delete_list = delete_tags.map { |t| "#{@docker_image_name}:#{t}" }.join(' ')
      end
    end

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

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
ufo-0.1.0 lib/ufo/docker_cleaner.rb
ufo-0.0.6 lib/ufo/docker_cleaner.rb