Class: Baha::Image
- Inherits:
-
Object
- Object
- Baha::Image
- Defined in:
- lib/baha/image.rb
Constant Summary collapse
Instance Attribute Summary collapse
-
#bind ⇒ Object
readonly
Returns the value of attribute bind.
-
#command ⇒ Object
readonly
Returns the value of attribute command.
-
#image ⇒ Object
readonly
Returns the value of attribute image.
-
#maintainer ⇒ Object
readonly
Returns the value of attribute maintainer.
-
#name ⇒ Object
readonly
Returns the value of attribute name.
-
#options ⇒ Object
readonly
Returns the value of attribute options.
-
#parent ⇒ Object
readonly
Returns the value of attribute parent.
-
#pre_build ⇒ Object
readonly
Returns the value of attribute pre_build.
-
#run ⇒ Object
readonly
Returns the value of attribute run.
-
#tags ⇒ Object
readonly
Returns the value of attribute tags.
-
#timeout ⇒ Object
readonly
Returns the value of attribute timeout.
-
#workspace ⇒ Object
readonly
Returns the value of attribute workspace.
Class Method Summary collapse
- .get_image!(image) ⇒ Object
-
.parse_name(image) ⇒ Object
Parses an image name.
- .parse_with_default(image, repository) ⇒ Object
Instance Method Summary collapse
- #commit_config ⇒ Object
- #env ⇒ Object
-
#initialize(config, image) ⇒ Image
constructor
A new instance of Image.
- #inspect ⇒ Object
-
#needs_update? ⇒ Boolean
Checks if the image needs updating 1.
- #parent_id ⇒ Object
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 |
# File 'lib/baha/image.rb', line 70 def initialize(config,image) @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::(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 = config.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
#bind ⇒ Object (readonly)
Returns the value of attribute bind.
68 69 70 |
# File 'lib/baha/image.rb', line 68 def bind @bind end |
#command ⇒ Object (readonly)
Returns the value of attribute command.
68 69 70 |
# File 'lib/baha/image.rb', line 68 def command @command end |
#image ⇒ Object (readonly)
Returns the value of attribute image.
68 69 70 |
# File 'lib/baha/image.rb', line 68 def image @image end |
#maintainer ⇒ Object (readonly)
Returns the value of attribute maintainer.
68 69 70 |
# File 'lib/baha/image.rb', line 68 def maintainer @maintainer end |
#name ⇒ Object (readonly)
Returns the value of attribute name.
68 69 70 |
# File 'lib/baha/image.rb', line 68 def name @name end |
#options ⇒ Object (readonly)
Returns the value of attribute options.
68 69 70 |
# File 'lib/baha/image.rb', line 68 def @options end |
#parent ⇒ Object (readonly)
Returns the value of attribute parent.
68 69 70 |
# File 'lib/baha/image.rb', line 68 def parent @parent end |
#pre_build ⇒ Object (readonly)
Returns the value of attribute pre_build.
68 69 70 |
# File 'lib/baha/image.rb', line 68 def pre_build @pre_build end |
#run ⇒ Object (readonly)
Returns the value of attribute run.
68 69 70 |
# File 'lib/baha/image.rb', line 68 def run @run end |
#tags ⇒ Object (readonly)
Returns the value of attribute tags.
68 69 70 |
# File 'lib/baha/image.rb', line 68 def @tags end |
#timeout ⇒ Object (readonly)
Returns the value of attribute timeout.
68 69 70 |
# File 'lib/baha/image.rb', line 68 def timeout @timeout end |
#workspace ⇒ Object (readonly)
Returns the value of attribute workspace.
68 69 70 |
# File 'lib/baha/image.rb', line 68 def workspace @workspace 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_config ⇒ Object
109 110 111 112 113 114 |
# File 'lib/baha/image.rb', line 109 def commit_config @options.values.reduce({}) do |memo,option| option.apply(memo) memo end end |
#env ⇒ Object
93 94 95 96 97 98 99 100 101 102 |
# File 'lib/baha/image.rb', line 93 def env { :parent => @parent, :maintainer => @maintainer, :bind => @bind, :name => @image[:name], :tag => @image[:tag], :workspace => @workspace..to_s } end |
#inspect ⇒ Object
140 141 142 143 144 145 146 147 148 149 150 151 152 153 |
# File 'lib/baha/image.rb', line 140 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
-
If it’s parent image has changed
-
If the desired tag is not found
Will raise Baha::ImageNotFoundError if the parent image can not be found
120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 |
# File 'lib/baha/image.rb', line 120 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'] = 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 ( .include?(local_tag) or .include?(remote_tag) ) rescue true end end |
#parent_id ⇒ Object
104 105 106 107 |
# File 'lib/baha/image.rb', line 104 def parent_id parent = Baha::Image.get_image!(@parent) parent.id end |