Sha256: dd26f6d0e829a5d520facb9745e30067e50889c9901aa70094b69388c91a1dbb

Contents?: true

Size: 1.55 KB

Versions: 3

Compression:

Stored size: 1.55 KB

Contents

require 'highline/import'
require 'rake/tasklib'

# A Task Generator for Docker images
#
# Example:
#   require "lux/dockertasks"
#
#   DockerImageTask.new('myimage') do
#      puts "Here you can build the image"
#   end
#
#   task :run => 'myimage' do
#     sh "docker run myimage"
#   end
#
class DockerImageTask < Rake::TaskLib

  # The name of the task (defaults to the image)
  attr_accessor :name

  # The name of the image (if different to the task name)
  attr_accessor :image

  # Name of tag (default is latest)
  attr_accessor :tag

  # Task Description (default is 'Build Docker Image <imagename>')
  attr_accessor :description

  # The block to execute if this needs building
  attr_accessor :build

  # The image may include a tag, but the tag is never part of the name
  # If the tag is omitted it defaults to 'latest'
  # If a name is specified it becomes the task name.
  #
  def initialize(image, name=nil, &block)
    @image, sep, @tag = image.partition(':')
    @tag = 'latest' if @tag.empty?
    @name = name || @image
    @description = "Build Docker Image #{@image}:#{@tag}"
    @build = block
    define
  end

  def define
    desc @description
    task @name do
      local_images = `docker images #{@image}`.split("\n")[1..-1].map{|l| l.split(/\s+/)}
      local_images.select!{|i| i[1] == @tag}
      if local_images.size == 0
        if @build.nil?
          HighLine.say "Build Docker Image <%=BOLD%>#{@image}:#{@tag}<%=CLEAR%> before proceeding"
          exit 2
        else
          @build.call self
        end
      end
    end
    self
  end

end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
lux-0.9 lib/lux/dockertasks.rb
lux-0.7 lib/lux/dockertasks.rb
lux-0.6 lib/lux/dockertasks.rb