Class: DockerImageTask

Inherits:
Rake::TaskLib
  • Object
show all
Defined in:
lib/lux/dockertasks.rb

Overview

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(image, name = nil, &block) ⇒ DockerImageTask

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.



38
39
40
41
42
43
44
45
# File 'lib/lux/dockertasks.rb', line 38

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

Instance Attribute Details

#buildObject

The block to execute if this needs building



32
33
34
# File 'lib/lux/dockertasks.rb', line 32

def build
  @build
end

#descriptionObject

Task Description (default is ‘Build Docker Image <imagename>’)



29
30
31
# File 'lib/lux/dockertasks.rb', line 29

def description
  @description
end

#imageObject

The name of the image (if different to the task name)



23
24
25
# File 'lib/lux/dockertasks.rb', line 23

def image
  @image
end

#nameObject

The name of the task (defaults to the image)



20
21
22
# File 'lib/lux/dockertasks.rb', line 20

def name
  @name
end

#tagObject

Name of tag (default is latest)



26
27
28
# File 'lib/lux/dockertasks.rb', line 26

def tag
  @tag
end

Instance Method Details

#defineObject



47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/lux/dockertasks.rb', line 47

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