Class: DockerTools::Image

Inherits:
Object
  • Object
show all
Defined in:
lib/docker_tools/image.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, registry, tag, dir = nil, lookup = true) ⇒ Image

Returns a new instance of Image.



11
12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/docker_tools/image.rb', line 11

def initialize(name, registry, tag, dir=nil, lookup=true)
  @name = name
  @registry = registry
  @tag = tag
  @full_name = "#{registry}/#{name}:#{tag}" unless registry.nil?
  @full_name = "#{name}:#{tag}" if registry.nil?
  @image_name = "#{registry}/#{name}" unless registry.nil?
  @image_name = name if registry.nil?
  @dir = dir
  @dockerfile = "#{dir}/Dockerfile.template" unless dir.nil?
  @image = lookup_image if lookup
  # Set the read timeout for the connection
  Excon.defaults[:read_timeout] = DockerTools.image_timeout
end

Instance Attribute Details

#dirObject

Returns the value of attribute dir.



8
9
10
# File 'lib/docker_tools/image.rb', line 8

def dir
  @dir
end

#full_nameObject

Returns the value of attribute full_name.



8
9
10
# File 'lib/docker_tools/image.rb', line 8

def full_name
  @full_name
end

#imageObject

Returns the value of attribute image.



8
9
10
# File 'lib/docker_tools/image.rb', line 8

def image
  @image
end

#nameObject

Returns the value of attribute name.



8
9
10
# File 'lib/docker_tools/image.rb', line 8

def name
  @name
end

#registryObject

Returns the value of attribute registry.



8
9
10
# File 'lib/docker_tools/image.rb', line 8

def registry
  @registry
end

#tag(new_tag) ⇒ Object

Returns the value of attribute tag.



8
9
10
# File 'lib/docker_tools/image.rb', line 8

def tag
  @tag
end

Instance Method Details

#build(registry: @registry, tag: @tag, method: 'image', distro: 'precise', fallback_tag: DockerTools.dependency_fallback_tag, no_pull: DockerTools.no_pull, template_vars: {}, rm: false, no_cache: false) ⇒ Object



32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/docker_tools/image.rb', line 32

def build(registry: @registry, tag: @tag, method: 'image', distro: 'precise', fallback_tag: DockerTools.dependency_fallback_tag, no_pull: DockerTools.no_pull, template_vars: {}, rm: false, no_cache: false)
  case method
  when 'image'
    dependency_tag = dependency['tag']
    dependency_tag = DockerTools::Dependency.new(dependency['repository'], dependency['registry'], dependency['tag'], fallback_tag).run unless no_pull

    dockerfile_path = "#{@dir}/Dockerfile"
    dockerfile_contents = dockerfile(@name, registry, dependency_tag, template_vars)
    File.open(dockerfile_path, 'w') { | file | file.write(dockerfile_contents) }
    @image = Docker::Image.build_from_dir(@dir, { 'rm' => rm, 'nocache' => no_cache }) do | chunk | 
      DockerTools::Util.parse_output(chunk) do | output |
        if output.kind_of?(Hash)
          if output.has_key?('error')
            puts output['error']
            raise output['error']
          end
          puts output['stream'] if output.has_key?('stream')
        else
          puts output
        end
      end
    end
    File.delete(dockerfile_path)
  when 'debootstrap'
    debootstrap = DockerTools::Debootstrap.new(@name, distro)
    debootstrap.run
    @image = Docker::Image.import(debootstrap.archive)
    debootstrap.cleanup
  else
    raise "Invalid value for method: #{method}"
  end
  @image.tag('repo' => "#{@registry}/#{@name}", 'tag' => @tag, 'force' => true)
  @image
end

#dependencyObject



71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
# File 'lib/docker_tools/image.rb', line 71

def dependency
  if @dockerfile.nil?
    return nil
  else
    template = dockerfile
    dependency = {}
    if template =~ /(FROM|from)\s+(\S+)\/(\S+):(\S+)/
      dependency['registry'] = $2
      dependency['repository'] = $3
      dependency['tag'] = $4
      return dependency
    elsif template =~ /(FROM|from)\s+(\S+):(\S+)/
      dependency['registry'] = nil
      dependency['repository'] = $2
      dependency['tag'] = $3
    elsif template =~ /(FROM|from)\s+(\S+)$/
      dependency['registry'] = nil
      dependency['repository'] = $2
      dependency['tag'] = nil
    end
    dependency
  end
end

#pullObject



26
27
28
29
30
# File 'lib/docker_tools/image.rb', line 26

def pull
  puts "Pulling image #{@image_name}"
  Docker::Image.create('fromImage' => @image_name, 'tag' => @tag)
  @image = lookup_image
end