Class: Baha::Image

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

Constant Summary collapse

LOG =
Baha::Log.for_name("Image")

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(config, image) ⇒ Image

Returns a new instance of Image.



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

def initialize(config,image)
  @config = config
  @parent = Baha::Image.parse_with_default(image['parent'] || config.defaults[:parent], config.defaults[:repository])
  @image  = Baha::Image.parse_with_default(image['name'], config.defaults[:repository])
  @image[:tag] = image['tag'] if image.has_key?('tag')
  @maintainer = image['maintainer'] || config.defaults[:maintainer]
  @options = Baha::ContainerOptions::parse_options(image['config'])
  @pre_build = image['pre_build']
  @bind = image['bind'] || config.defaults[:bind]
  @command = image['command'] || config.defaults[:command]
  @run = image['run']
  @timeout = image['timeout'] || config.defaults[:timeout]
  @workspace = image['workspace'] || @image[:name]
  @name = @image[:name]
  @tags = Set.new [
    "#{@image[:name]}:#{@image[:tag]}",
    "#{@image[:name]}:latest"
  ]
  if @image[:ns]
    @tags << "#{@image[:ns]}/#{@image[:name]}:#{@image[:tag]}"
    @tags << "#{@image[:ns]}/#{@image[:name]}:latest"
  end
end

Instance Attribute Details

#bindObject (readonly)

Returns the value of attribute bind.



68
69
70
# File 'lib/baha/image.rb', line 68

def bind
  @bind
end

#commandObject (readonly)

Returns the value of attribute command.



68
69
70
# File 'lib/baha/image.rb', line 68

def command
  @command
end

#imageObject (readonly)

Returns the value of attribute image.



68
69
70
# File 'lib/baha/image.rb', line 68

def image
  @image
end

#maintainerObject (readonly)

Returns the value of attribute maintainer.



68
69
70
# File 'lib/baha/image.rb', line 68

def maintainer
  @maintainer
end

#nameObject (readonly)

Returns the value of attribute name.



68
69
70
# File 'lib/baha/image.rb', line 68

def name
  @name
end

#optionsObject (readonly)

Returns the value of attribute options.



68
69
70
# File 'lib/baha/image.rb', line 68

def options
  @options
end

#parentObject (readonly)

Returns the value of attribute parent.



68
69
70
# File 'lib/baha/image.rb', line 68

def parent
  @parent
end

#pre_buildObject (readonly)

Returns the value of attribute pre_build.



68
69
70
# File 'lib/baha/image.rb', line 68

def pre_build
  @pre_build
end

#runObject (readonly)

Returns the value of attribute run.



68
69
70
# File 'lib/baha/image.rb', line 68

def run
  @run
end

#tagsObject (readonly)

Returns the value of attribute tags.



68
69
70
# File 'lib/baha/image.rb', line 68

def tags
  @tags
end

#timeoutObject (readonly)

Returns the value of attribute timeout.



68
69
70
# File 'lib/baha/image.rb', line 68

def timeout
  @timeout
end

Class Method Details

.get_image!(image) ⇒ Object



44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/baha/image.rb', line 44

def get_image!(image)
  LOG.debug { "get_image!(#{image.inspect})" }
  tag = image[:tag] || 'latest'
  repo = "#{image[:ns]}/#{image[:name]}"
  img = [
    lambda { Docker::Image.get("#{image[:name]}:#{image[:tag]}") },
    lambda { Docker::Image.create('fromImage'=> image[:name], 'tag' => tag) },
    lambda { Docker::Image.create('fromImage' => repo, 'tag' => tag) }
  ].reduce(nil) do |result,block|
    unless result
      begin
        result = block.call
        result = Docker::Image.get(result.id)
      rescue
        result = nil
      end
    end
    result
  end
  raise Baha::ImageNotFoundError.new(image) unless img
  img
end

.parse_name(image) ⇒ Object

Parses an image name



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/baha/image.rb', line 18

def parse_name(image)
  m = /(?:([a-z0-9\-._\/]+))(?::([a-zA-Z0-9\-._]+))?/.match(image)
  unless m
    raise ArgumentError.new("Unable to parse image name #{image}")
  end
  tag  = m.captures[1]
  tag  ||= 'latest'
  imagename = m.captures[0]
  m2 = /(?:(.+)\/)?(.+)/.match(imagename)
  ns = m2.captures[0]
  name = m2.captures[1]
  {
    :ns    => ns,
    :name  => name,
    :tag   => tag
  }
end

.parse_with_default(image, repository) ⇒ Object



36
37
38
39
40
41
42
# File 'lib/baha/image.rb', line 36

def parse_with_default(image,repository)
  i = parse_name(image)
  unless i[:ns]
    i[:ns] = repository
  end
  i
end

Instance Method Details

#commit_configObject



118
119
120
121
122
123
# File 'lib/baha/image.rb', line 118

def commit_config
  @options.values.reduce({}) do |memo,option|
    option.apply(memo)
    memo
  end
end

#envObject



102
103
104
105
106
107
108
109
110
111
# File 'lib/baha/image.rb', line 102

def env
  {
    :parent => @parent,
    :maintainer => @maintainer,
    :bind => @bind,
    :name => @image[:name],
    :tag  => @image[:tag],
    :workspace => workspace.expand_path.to_s
  }
end

#host_mountObject



94
95
96
# File 'lib/baha/image.rb', line 94

def host_mount
  @config.workspace_for(@workspace)
end

#inspectObject



149
150
151
152
153
154
155
156
157
158
159
160
161
162
# File 'lib/baha/image.rb', line 149

def inspect
  <<-eos.gsub(/\n?\s{2,}/,'')
  #{self.class.name}<
    @parent=#{@parent.inspect},
    @image=#{@image.inspect},
    @maintainer=#{@maintainer},
    @options=#{@options.inspect},
    @pre_build=#{@pre_build.inspect},
    @bind=#{@bind},
    @command=#{@command.inspect},
    @timeout=#{@timeout}
  >
  eos
end

#needs_update?Boolean

Checks if the image needs updating

  1. If it’s parent image has changed

  2. If the desired tag is not found

Will raise Baha::ImageNotFoundError if the parent image can not be found

Returns:

  • (Boolean)


129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
# File 'lib/baha/image.rb', line 129

def needs_update?
  LOG.debug { "needs_update?(#{@image.inspect})" }
  parent = Baha::Image.get_image!(@parent)
  LOG.debug { "got parent: #{parent.inspect}" }
  begin
    image = Baha::Image.get_image!(@image)
    LOG.debug { "got image: #{image.inspect}" }
    parent_id = image.info['Parent']
    this_tags = image.history[0]["Tags"]
    local_tag = "#{@image[:name]}:#{@image[:tag]}"
    remote_tag = "#{@image[:ns]}/#{local_tag}"
    LOG.debug { "current parent id = #{parent_id}" }
    LOG.debug { "current image tags = #{parent_id}" }
    LOG.debug { "current parent id = #{parent_id}" }
    parent_id != parent.id or not ( this_tags.include?(local_tag) or this_tags.include?(remote_tag) )
  rescue
    true
  end
end

#parent_idObject



113
114
115
116
# File 'lib/baha/image.rb', line 113

def parent_id
  parent = Baha::Image.get_image!(@parent)
  parent.id
end

#workspaceObject



98
99
100
# File 'lib/baha/image.rb', line 98

def workspace
  @config.workspace + @workspace
end