class ContainedMr::Cleaner

Cleans up left over Docker images and containers.

Attributes

name_prefix[R]

Public Class Methods

new(name_prefix) click to toggle source

Sets up a cleaner.

@param {String} name_prefix should match the value given to Template

instances
# File lib/contained_mr/cleaner.rb, line 11
def initialize(name_prefix)
  @name_prefix = name_prefix
  @label_value = name_prefix
end

Public Instance Methods

container_filters() click to toggle source

@return { Hash<Symbol, Array<String>> } filters used to identify Docker

containers started by this controller
# File lib/contained_mr/cleaner.rb, line 58
def container_filters
  { label: [ "contained_mr.ctl=#{@label_value}" ] }
end
destroy_all!() click to toggle source

Removes all images and containers matching this cleaner’s name prefix.

@return {Array<Docker::Container, Docker::Image>} the removed objects

# File lib/contained_mr/cleaner.rb, line 19
def destroy_all!
  containers = destroy_all_containers!
  images = destroy_all_images!
  containers + images
end
destroy_all_containers!() click to toggle source

@return {Array<Docker::Container>} the removed containers

# File lib/contained_mr/cleaner.rb, line 26
def destroy_all_containers!
  containers = Docker::Container.all all: true,
                                     filters: container_filters.to_json
  containers.each do |container|
    begin
      container.delete force: false, volumes: true
    rescue Docker::Error::NotFoundError
      # Workaround for https://github.com/docker/docker/issues/14474
    end
  end
end
destroy_all_images!() click to toggle source

@return {Array<Docker::Image>} the removed images

# File lib/contained_mr/cleaner.rb, line 39
def destroy_all_images!
  tag_prefix = "#{@name_prefix}/"
  images = Docker::Image.all
  deleted_images = []
  images.each do |image|
    next unless image_tags = image.info['RepoTags']
    image_tags.each do |image_tag|
      next unless image_tag.start_with? tag_prefix
      # HACK(pwnall): Trick docker-api into issuing a DELETE request by tag.
      tag_image = Docker::Image.new Docker.connection, 'id' => image_tag
      tag_image.delete
      deleted_images << image
    end
  end
  deleted_images
end