Class: VmPackage

Inherits:
Object
  • Object
show all
Defined in:
lib/ovfparse/vmpackage.rb

Constant Summary collapse

OVA =
0
OVF =
1
ISO =
2
DEBUG_MODE =
false
PRODUCT_ATTRIBUTES =

List of attributes in an OVF product that we will extract / set

[ {'full_name' => 'ovf:instance', 'node_ref' => 'instance', 'attribute_ref' => 'instance'},
{'full_name' => 'ovf:class', 'node_ref' => 'class', 'attribute_ref' => 'product_class'},
{'full_name' => 'cops:cpetag', 'node_ref' => 'cpetag', 'attribute_ref' => 'cpe_tag'} ]
PRODUCT_ELEMENTS =

List of elements in an OVF product that we will extract / set

[ {'full_name' => 'Info', 'node_ref' => 'Info', 'element_ref' => 'description', 'required' => false},
{'full_name' => 'Product', 'node_ref' => 'Product', 'element_ref' => 'name', 'required' => false},
{'full_name' => 'Vendor', 'node_ref' => 'Vendor', 'element_ref' => 'vendor', 'required' => false},
{'full_name' => 'Version', 'node_ref' => 'Version', 'element_ref' => 'version', 'required' => false} ]
PROPERTY_ATTRIBUTES =

List of attributes in an OVF property that we will extract / set

[ {'full_name' => 'ovf:value', 'node_ref' => 'value', 'attribute_ref' => 'value'},
{'full_name' => 'ovf:key', 'node_ref' => 'key', 'attribute_ref' => 'key'},
{'full_name' => 'ovf:userConfigurable', 'node_ref' => 'userConfigurable', 'attribute_ref' => 'userConfigurable'},
{'full_name' => 'ovf:password', 'node_ref' => 'password', 'attribute_ref' => 'password'},
{'full_name' => 'ovf:required', 'node_ref' => 'required', 'attribute_ref' => 'required'},
{'full_name' => 'ovf:type', 'node_ref' => 'type', 'attribute_ref' => 'value_basetype'},
{'full_name' => 'cops:valueType', 'node_ref' => 'valueType', 'attribute_ref' => 'valueType'}, # @todo refactor to cops extension module
{'full_name' => 'cops:uuid', 'node_ref' => 'uuid', 'attribute_ref' => 'uuid'} ]
PROPERTY_ELEMENTS =

List of elements in an OVF property that we will extract / set

[ {'full_name' => 'Label', 'node_ref' => 'Label', 'element_ref' => 'name', 'required' => false},
{'full_name' => 'Description', 'node_ref' => 'Description', 'element_ref' => 'description', 'required' => false},
{'full_name' => 'Example', 'node_ref' => 'cops:Example', 'element_ref' => 'example', 'required' => true}, # @todo refactor to cops extension module
{'full_name' => 'NoneType', 'node_ref' => 'cops:NoneType', 'element_ref' => 'nonetype', 'required' => true} ]
OVF_NAMESPACE =
{'ovf' => 'http://schemas.dmtf.org/ovf/envelope/1'}

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(uri) ⇒ VmPackage

Returns a new instance of VmPackage.



60
61
# File 'lib/ovfparse/vmpackage.rb', line 60

def initialize 
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method) ⇒ Object



150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
# File 'lib/ovfparse/vmpackage.rb', line 150

def method_missing(method)
  if DEBUG_MODE
    puts "WARNING: NoSuchMethod Error: " + method.to_s + " ...trying XPath query \n"
  end 

  # try with namespace
  data = @xml.xpath("//ovf:" + method.to_s)


  # try without namespace
  if nil===data then
    data = @xml.xpath("//" + method.to_s)
  end

  # try changing method name without namespace
  # i.e. egg_and_ham.classify #=> "EggAndHam"
  if nil==data then
    data = @xml.xpath("//" + method.to_s.classify)
  end

  # try changing method name with namespace
  # i.e. egg_and_ham.classify #=> "EggAndHam"
  if nil==data then
    data = @xml.xpath("//ovf:" + method.to_s.classify)
  end

  return data

end

Instance Attribute Details

#base_pathObject

Returns the value of attribute base_path.



57
58
59
# File 'lib/ovfparse/vmpackage.rb', line 57

def base_path
  @base_path
end

#diskSectionObject

Returns the value of attribute diskSection.



57
58
59
# File 'lib/ovfparse/vmpackage.rb', line 57

def diskSection
  @diskSection
end

#nameObject

Returns the value of attribute name.



57
58
59
# File 'lib/ovfparse/vmpackage.rb', line 57

def name
  @name
end

#networkSectionObject

Returns the value of attribute networkSection.



57
58
59
# File 'lib/ovfparse/vmpackage.rb', line 57

def networkSection
  @networkSection
end

#protocolObject

Returns the value of attribute protocol.



57
58
59
# File 'lib/ovfparse/vmpackage.rb', line 57

def protocol
  @protocol
end

#referencesObject

Returns the value of attribute references.



57
58
59
# File 'lib/ovfparse/vmpackage.rb', line 57

def references
  @references
end

#sizeObject

Returns the value of attribute size.



57
58
59
# File 'lib/ovfparse/vmpackage.rb', line 57

def size
  @size
end

#stateObject

Returns the value of attribute state.



57
58
59
# File 'lib/ovfparse/vmpackage.rb', line 57

def state
  @state
end

#urlObject

Returns the value of attribute url.



57
58
59
# File 'lib/ovfparse/vmpackage.rb', line 57

def url
  @url
end

#versionObject

Returns the value of attribute version.



57
58
59
# File 'lib/ovfparse/vmpackage.rb', line 57

def version
  @version
end

#virtualSystemObject

Returns the value of attribute virtualSystem.



57
58
59
# File 'lib/ovfparse/vmpackage.rb', line 57

def virtualSystem
  @virtualSystem
end

#xmlObject

Returns the value of attribute xml.



57
58
59
# File 'lib/ovfparse/vmpackage.rb', line 57

def xml
  @xml
end

Class Method Details

.construct_skeletonObject



635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
# File 'lib/ovfparse/vmpackage.rb', line 635

def self.construct_skeleton
   builder = Nokogiri::XML::Builder.new(:encoding => 'UTF-8') do |xml|
    xml.Envelope('xmlns' => 'http://schemas.dmtf.org/ovf/envelope/1', 'xmlns:cim' => "http://schemas.dmtf.org/wbem/wscim/1/common", 'xmlns:ovf' => "http://schemas.dmtf.org/ovf/envelope/1", 'xmlns:rasd' => "http://schemas.dmtf.org/wbem/wscim/1/cim-schema/2/CIM_ResourceAllocationSettingData", 'xmlns:vmw' => "http://www.vmware.com/schema/ovf", 'xmlns:vssd' => "http://schemas.dmtf.org/wbem/wscim/1/cim-schema/2/CIM_VirtualSystemSettingData", 'xmlns:xsi' => "http://www.w3.org/2001/XMLSchema-instance") {
        xml.References{}
        xml.DiskSection{
           xml.Info "Virtual disk information"
        }
        xml.NetworkSection{
           xml.Info "List of logical networks"
        }
        xml.VirtualSystem('id' => "vm"){
           xml.Info "A virtual machine"
           xml.Name "New Virtual Machine"
           xml.OperatingSystemSection('id' => "94"){
               xml.Info "The kind of guest operating system"
           }
           xml.VirtualHardwareSection{
               xml.Info "Virtual hardware requirements"
               xml.System{
                   xml['vssd'].ElementName "Virtual Hardware Family"
                   xml['vssd'].InstanceID "0"
                   xml['vssd'].VirtualSystemIdentifier "New Virtual Machine"
               }
               xml.Item{
                   xml['rasd'].AllocationUnits "herts * 10^6"
                   xml['rasd'].Description "Number of Virtual CPUs"
                   xml['rasd'].ElementName "1 Virtual CPU(s)"
                   xml['rasd'].InstanceID "1"
                   xml['rasd'].ResourceType "3"
                   xml['rasd'].VirtualQuantity "1"
               }
               xml.Item{
                   xml['rasd'].AllocationUnits "byte * 2^20"
                   xml['rasd'].Description "Memory Size"
                   xml['rasd'].ElementName "512MB of memory"
                   xml['rasd'].InstanceID "2"
                   xml['rasd'].ResourceType "4"
                   xml['rasd'].VirtualQuantity "512"
               }
           }
        }
    }

    node = Nokogiri::XML::Comment.new(xml.doc, ' skeleton framework constructed by OVFparse ')
    xml.doc.children[0].add_previous_sibling(node)
  end

  builder.doc.root.children[3].attribute("id").namespace = builder.doc.root.namespace_definitions.detect{ |ns| ns.prefix == "ovf"}
  builder.doc.root.children[3].children[2].attribute("id").namespace = builder.doc.root.namespace_definitions.detect{ |ns| ns.prefix == "ovf"}
    
  newPackage = NewVmPackage.new
  newPackage.xml = builder.doc
  newPackage.loadElementRefs
  return newPackage
end

.create(uri) ⇒ Object



88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
# File 'lib/ovfparse/vmpackage.rb', line 88

def self.create uri
  (@protocol, @url) = uri.split(":", 2) unless !uri
  @url.sub!(/^\/{0,2}/, '')
  @protocol.downcase
  @url.downcase
  if @protocol=='ftp'
    FtpVmPackage.new(uri)
  elsif @protocol=='http'
    HttpVmPackage.new(uri)
  elsif @protocol=='https'
    HttpsVmPackage.new(uri)
  elsif @protocol=='file'
    FileVmPackage.new(uri)
  elsif @protocol.match(/esx/)
    if @protocol.match(/esx4/)
      Esx4VmPackage.new(uri)
    else
      raise NotImplementedError, "Cannot handle this version of ESX: " + @protocol + "\n"
    end
  elsif @protocol.match(/vc/)
    if @protocol.match(/vc4/)
      Vc4VmPackage.new(uri)
    else
      raise NotImplementedError, "Cannot handle this version of VirtualCenter: " + @protocol + "\n"
    end
  else
    raise NotImplementedError, "Unknown Protocol: " + @protocol + " (bad URI string?)\n"
    VmRepository.new(uri)
  end
end

Instance Method Details

#buildNewIDEController(vhs, rasdNamespace, newID, newAddress) ⇒ Object



437
438
439
440
441
442
443
444
# File 'lib/ovfparse/vmpackage.rb', line 437

def buildNewIDEController(vhs, rasdNamespace, newID, newAddress)
   new_controller = vhs.add_child(xml.create_element('Item', {}))
   new_controller.add_child(xml.create_element('Address', newAddress)).namespace = rasdNamespace
   new_controller.add_child(xml.create_element('Description', "IDE Controller " + newAddress)).namespace = rasdNamespace
   new_controller.add_child(xml.create_element('ElementName', "IDEController" + newAddress)).namespace = rasdNamespace
   new_controller.add_child(xml.create_element('InstanceID', newID)).namespace = rasdNamespace
   new_controller.add_child(xml.create_element('ResourceType', "5")).namespace = rasdNamespace
end

#checkschema(schema) ⇒ Object



180
181
182
183
184
185
186
187
188
189
190
# File 'lib/ovfparse/vmpackage.rb', line 180

def checkschema(schema)
  response = ""

  isValid = true    
  schema.validate(@xml).each do |error|
    response << error.message + "\n"
    isValid = false
  end

  return [isValid, response]
end

#fetchObject



120
121
# File 'lib/ovfparse/vmpackage.rb', line 120

def fetch
end

#getChildByName(node, childName) ⇒ Object

Returns the first child node of the passed node whose name matches the passed name.



137
138
139
# File 'lib/ovfparse/vmpackage.rb', line 137

def getChildByName(node, childName)
   return node.nil? ? nil : node.children.detect{ |element| element.name == childName}
end

#getChildrenByName(node, childName) ⇒ Object

Returns every child node of the passed node whose name matches the passed name.



142
143
144
# File 'lib/ovfparse/vmpackage.rb', line 142

def getChildrenByName(node, childName)
   return node.nil? ? [] : node.children.select{ |element| element.name == childName}
end

#getFirstOpenIDEAddress(vhs, rasdNamespace, maxID) ⇒ Object



446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
# File 'lib/ovfparse/vmpackage.rb', line 446

def getFirstOpenIDEAddress(vhs, rasdNamespace, maxID)
   items = getChildrenByName(vhs, 'Item')
   ide_controllers = items.select{ |item| getChildByName(item, 'ResourceType').content == '5' }

   if(ide_controllers.length == 0)
      buildNewIDEController(vhs, rasdNamespace, maxID, '0')
      return [maxID, '0']

   elsif(ide_controllers.length == 1)
      controller = ide_controllers[0]
      controllerAddress = getChildByName(controller, 'Address').content
      open_address = getOpenChannelOnIDEController(controller, items)
      if(open_address == '0' || open_address == '1')
         return [getChildByName(controller, 'InstanceID').content, open_address]
      elsif(!open_address && controllerAddress == '0')
         buildNewIDEController(vhs, rasdNamespace, maxID, '1')
         return [maxID, '0']
      else
         buildNewIDEController(vhs, rasdNamespace, maxID, '0')
         return [maxID, '0']
      end

   else
      controller = ide_controllers[0]
      controllerAddress = getChildByName(controller, 'Address').content
      open_address = getOpenChannelOnIDEController(controller, items)
      if(open_address == '0' || open_address == '1')
         return [getChildByName(controller, 'InstanceID').content, open_address]
      else
         controller = ide_controllers[1]
         controllerAddress = getChildByName(controller, 'Address').content
         open_address = getOpenChannelOnIDEController(controller, items)
         if(open_address == '0' || open_address == '1')
            return [getChildByName(controller, 'InstanceID').content, open_address]
         else
            return false
         end
      end
   end
end

#getOpenChannelOnIDEController(controller, items) ⇒ Object



416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
# File 'lib/ovfparse/vmpackage.rb', line 416

def getOpenChannelOnIDEController(controller, items)
   currentAddress = getChildByName(controller, 'InstanceID').content
   controllerChildren = items.select{ |item| 
      parentNode = getChildByName(item, 'Parent')
      unless(parentNode.nil?)
         parentNode.content == currentAddress
      end
   }
   childAddresses = Array.new
   controllerChildren.each{ |child|
      childAddresses.push(getChildByName(child, 'AddressOnParent').content)
   }
   if(childAddresses.length == 0 || (childAddresses.length == 1 && childAddresses[0] == '1'))
      return '0'
   elsif(childAddresses.length == 1)
      return '1'
   else
      return false
   end
end

#getVirtualQuantity(resource) ⇒ Object



303
304
305
306
307
308
# File 'lib/ovfparse/vmpackage.rb', line 303

def getVirtualQuantity(resource)
  getChildrenByName(getChildByName(virtualSystem, 'VirtualHardwareSection'), 'Item').each{ |node|
    resourceType = node.xpath('rasd:ResourceType')[0].text
    resourceType == resource.to_s ? (return node.xpath('rasd:VirtualQuantity')[0].text) : next
  }
end

#getVmAttributesObject



240
241
242
243
244
245
246
247
248
249
# File 'lib/ovfparse/vmpackage.rb', line 240

def getVmAttributes
   return {
      'name' => getVmName,
      'description' => getVmDescription,
      'OS' => getVmOS_ID,
      'patch_level' => getVmPatchLevel,
      'CPUs' => getVmCPUs,
      'RAM' => getVmRAM
   }
end

#getVmCPUsObject



295
296
297
# File 'lib/ovfparse/vmpackage.rb', line 295

def getVmCPUs
  return getVirtualQuantity(3)
end

#getVmDescriptionObject



196
197
198
199
# File 'lib/ovfparse/vmpackage.rb', line 196

def getVmDescription
  descNode = getChildByName(virtualSystem, 'Info')
  return descNode.nil? ? '' : descNode.content
end

#getVmDisksObject



251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
# File 'lib/ovfparse/vmpackage.rb', line 251

def getVmDisks
  disks = Array.new
  filenames = Hash.new
  getChildrenByName(references, 'File').each { |node|
    filenames[node['id']] = node['href']
  }

  getChildrenByName(diskSection, 'Disk').each { |node|
    capacity = node['capacity']
    units = node['capacityAllocationUnits']
    if(units == "byte * 2^40")
       capacity = (capacity.to_i * 1099511627776).to_s
    elsif(units == "byte * 2^30")
       capacity = (capacity.to_i * 1073741824).to_s
    elsif(units == "byte * 2^20")
       capacity = (capacity.to_i * 1048576).to_s
    elsif(units == "byte * 2^10")
       capacity = (capacity.to_i * 1024).to_s
    end
    thin_size = node['populatedSize']
    disks.push({ 'name' => node['diskId'], 'location' => filenames[node['fileRef']], 'size' => capacity, 'thin_size' => (thin_size || "-1") })
  }

  return disks
end

#getVmNameObject



192
193
194
# File 'lib/ovfparse/vmpackage.rb', line 192

def getVmName
  return virtualSystem['id'] || ''
end

#getVmNetworksObject



277
278
279
280
281
282
283
284
285
# File 'lib/ovfparse/vmpackage.rb', line 277

def getVmNetworks
  networks = Array.new
  getChildrenByName(networkSection, 'Network').each { |node|
    descriptionNode = getChildByName(node, 'Description')
    text = descriptionNode.nil? ? '' : descriptionNode.text
    networks.push({'location' => node['name'], 'notes' => text })
  }
  return networks
end

#getVmOSObject



226
227
228
229
# File 'lib/ovfparse/vmpackage.rb', line 226

def getVmOS
  os = getVmOS_ID
  return os == '' ? '' : OS_ID_TABLE[os.to_i]
end

#getVmOS_IDObject



201
202
203
204
# File 'lib/ovfparse/vmpackage.rb', line 201

def getVmOS_ID
  osNode = getChildByName(virtualSystem, 'OperatingSystemSection')
  return osNode.nil? ? '' : osNode['id']
end

#getVmOS_PasswordObject



216
217
218
219
220
221
222
223
224
# File 'lib/ovfparse/vmpackage.rb', line 216

def getVmOS_Password
  osNode = getChildByName(virtualSystem, 'OperatingSystemSection')
  if osNode == nil then
    return ''
  else
    passNode = getChildByName(osNode, 'Password')
    return passNode.nil? ? '' : passNode.content
  end
end

#getVmOS_UsernameObject



206
207
208
209
210
211
212
213
214
# File 'lib/ovfparse/vmpackage.rb', line 206

def getVmOS_Username
  osNode = getChildByName(virtualSystem, 'OperatingSystemSection')
  if osNode == nil then
    return ''
  else
    userNode = getChildByName(osNode, 'Username')
    return userNode.nil? ? '' : userNode.content
  end
end

#getVmPatchLevelObject

note this is not part of the OVF spec. Specific users could overwrite this method to store/retrieve patch level in the description field, for example.



234
235
# File 'lib/ovfparse/vmpackage.rb', line 234

def getVmPatchLevel
end

#getVmRAMObject



299
300
301
# File 'lib/ovfparse/vmpackage.rb', line 299

def getVmRAM
  return getVirtualQuantity(4)
end

#getVmReferencesObject



287
288
289
290
291
292
293
# File 'lib/ovfparse/vmpackage.rb', line 287

def getVmReferences
  refs = Array.new
  getChildrenByName(references, 'File').each { |node|
     refs.push({'href' => node['href'], 'id' => node['id'], 'size' => node['size']})
  }
  return refs
end

#loadElementRefsObject

Caches all of the base elements inside Envelope for fast access



125
126
127
128
129
130
131
132
133
134
# File 'lib/ovfparse/vmpackage.rb', line 125

def loadElementRefs
   children = @xml.root.children

   @references = getChildByName(xml.root, 'References')
   @virtualSystem = getChildByName(xml.root, 'VirtualSystem')

   @diskSection = getChildByName(xml.root, 'DiskSection') || @virtualSystem.add_previous_sibling(xml.create_element('DiskSection', {}))
   @networkSection = getChildByName(xml.root, 'NetworkSection') || @virtualSystem.add_previous_sibling(xml.create_element('NetworkSection', {}))

end

#referenced_file(element) ⇒ Object



146
147
148
# File 'lib/ovfparse/vmpackage.rb', line 146

def referenced_file(element) 
  @xml.xpath("//ovf:References/ovf:File[@ovf:id='#{element['fileRef']}']", OVF_NAMESPACE).first
end

#removeDisksFromVirtualHardwareSectionObject



405
406
407
408
409
410
411
412
413
414
# File 'lib/ovfparse/vmpackage.rb', line 405

def removeDisksFromVirtualHardwareSection
   vhs = getChildByName(virtualSystem, 'VirtualHardwareSection') || virtualSystem.add_child(xml.create_element('VirtualHardwareSection', {}))
   items = getChildrenByName(vhs, 'Item')
   items.each { |item|
      id = getChildByName(item, 'ResourceType')
      if(id.content == '17')
         item.unlink
      end
   }
end

#removeNetworksFromVirtualHardwareSectionObject



340
341
342
343
344
345
346
347
348
349
# File 'lib/ovfparse/vmpackage.rb', line 340

def removeNetworksFromVirtualHardwareSection
   vhs = getChildByName(virtualSystem, 'VirtualHardwareSection') || virtualSystem.add_child(xml.create_element('VirtualHardwareSection', {}))
   items = getChildrenByName(vhs, 'Item')
   items.each { |item|
      id = getChildByName(item, 'ResourceType')
      if(id.content == '10')
         item.unlink
      end
   }
end

#setAttributes(updated_element, parent_node, attribute_list) ⇒ Object



625
626
627
628
629
630
631
# File 'lib/ovfparse/vmpackage.rb', line 625

def setAttributes(updated_element, parent_node, attribute_list)
   attribute_list.each { |attribute_details|
      updated_value = updated_element[attribute_details['attribute_ref']]
     # (updated_value == '' || updated_value.nil?) ? parent_node.delete(attribute_details['node_ref']) :
      parent_node[attribute_details['full_name']] = updated_value || ''
   }
end

#setElements(updated_element, parent_node, element_list) ⇒ Object



609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
# File 'lib/ovfparse/vmpackage.rb', line 609

def setElements(updated_element, parent_node, element_list)
   element_list.each { |element_details|
      updated_value = updated_element[element_details['element_ref']]
      element_node = getChildByName(parent_node, element_details['full_name'])
      #if((updated_value == '' || updated_value.nil?) && !element_node.nil?)
      #   element_node.unlink
      #elsif(updated_value != '' && !updated_value.nil?)
         element_node = element_node.nil? ? parent_node.add_child(xml.create_element(element_details['node_ref'], {})) : parent_node.add_child(element_node)
         element_node.content = updated_value || ''
         if(element_details['required'])
            element_node['ovf:required'] = 'false'
         end
     # end
   }
end

#setProductIcon(new_icon, productNode) ⇒ Object



579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
# File 'lib/ovfparse/vmpackage.rb', line 579

def setProductIcon(new_icon, productNode)
   iconNode = getChildByName(productNode, 'Icon')
   if((new_icon == '' || new_icon.nil?) && !iconNode.nil?)
      getChildrenByName(references, 'File').detect { |fileNode| fileNode['id'] == iconNode['fileRef']}.unlink
      iconNode.unlink
   elsif(new_icon != '' && !new_icon.nil?)
      if(iconNode.nil?)
         productNode.add_child(xml.create_element('Icon', {'ovf:fileRef' => productNode['class'] + '_icon'}))
         iconRef = getChildrenByName(references, 'File').detect { |fileNode| fileNode['href'] == new_icon} ||
            references.add_child(xml.create_element('File', {'ovf:href' => new_icon}))
         iconRef['ovf:id'] = productNode['class'] + '_icon'
      else
         productNode.add_child(iconNode)
         getChildrenByName(references, 'File').detect { |fileNode| fileNode['id'] == iconNode['fileRef']}['ovf:href'] = new_icon
      end
   end
end

#setPropertyDefault(key, newVal) ⇒ Object



597
598
599
600
601
602
603
604
605
606
607
# File 'lib/ovfparse/vmpackage.rb', line 597

def setPropertyDefault(key, newVal)
   getChildrenByName(virtualSystem, "ProductSection").each{ |product|
      getChildrenByName(product, "Property").each{ |property|
         if(property['key'] == key)
            property['value'] = newVal
            property['ovf:value'] = newVal
            return
         end
      }
   }
end

#setVirtualQuantity(resource, newValue) ⇒ Object



333
334
335
336
337
338
# File 'lib/ovfparse/vmpackage.rb', line 333

def setVirtualQuantity(resource, newValue)
  getChildrenByName(getChildByName(virtualSystem, 'VirtualHardwareSection'), 'Item').each { |node|
    resourceType = node.xpath('rasd:ResourceType')[0].text
    resourceType == resource.to_s ? (node.xpath('rasd:VirtualQuantity')[0].content = newValue) : next
  }
end

#setVmAttributes(attributes) ⇒ Object



558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
# File 'lib/ovfparse/vmpackage.rb', line 558

def setVmAttributes(attributes)
  if attributes['name']
    setVmName(attributes['name'])
  end
  if attributes['description']
    setVmDescription(attributes['description'])
  end
  if attributes['OS']
    setVmOS_ID(attributes['OS'])
  end
  if attributes['patch_level']
    setVmPatchLevel(attributes['patch_level'])
  end
  if attributes['CPUs']
    setVmCPUs(attributes['CPUs'])
  end
  if attributes['RAM']
    setVmRAM(attributes['RAM'])
  end
end

#setVmCPUs(newValue) ⇒ Object



325
326
327
# File 'lib/ovfparse/vmpackage.rb', line 325

def setVmCPUs(newValue)
  setVirtualQuantity(3, newValue)
end

#setVmDescription(newValue) ⇒ Object



317
318
319
# File 'lib/ovfparse/vmpackage.rb', line 317

def setVmDescription(newValue)
  getChildByName(virtualSystem, 'Info').content = newValue
end

#setVmDisks(disks) ⇒ Object



487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
# File 'lib/ovfparse/vmpackage.rb', line 487

def setVmDisks(disks)
   removeDisksFromVirtualHardwareSection

   fileNodes = getChildrenByName(references, 'File')
   diskNodes = getChildrenByName(diskSection, 'Disk')
   vhs = getChildByName(virtualSystem, 'VirtualHardwareSection')

   icons = Array.new
   getChildrenByName(getChildByName(virtualSystem, 'ProductSection'), 'Icon').each { |node|
      icons.push(node['fileRef'])
   }

   fileNodes.each { |file_node|
      updated_disk = disks.detect { |disk| disk.location == file_node['href'] }
      old_disk_node = diskNodes.detect { |old_node| old_node['id'] == file_node['fileRef'] }

      if(updated_disk.nil?)
         if((icons.detect { |fileRef| fileRef == file_node['id'] }).nil?)
            file_node.unlink
            if(!old_disk_node.nil?)
               old_disk_node.unlink
            end
         end
      else
         file_node['ovf:id'] = updated_disk.name + '_disk'
         old_disk_node = old_disk_node || diskSection.add_child(xml.create_element('Disk', {}))
         old_disk_node['ovf:fileRef'] = updated_disk.name + '_disk'
         old_disk_node['ovf:capacity'] = updated_disk.size.to_s
         old_disk_node['ovf:diskId'] = updated_disk.name
         old_disk_node['ovf:capacityAllocationUnits'] = "byte * 2^30"
         old_disk_node['ovf:format'] = "http://www.vmware.com/interfaces/specifications/vmdk.html#streamOptimized" 
      end
   }

   # Find the highest instance ID
   maxID = 0
   items = getChildrenByName(vhs, 'Item')
   items.each { |item|
      itemID = getChildByName(item, 'InstanceID').content.to_i
      if(itemID > maxID)
         maxID = itemID
      end
   }

   rasdNamespace = xml.root.namespace_definitions.detect{ |ns| ns.prefix == 'rasd' }

   disks.each { |disk|
      if( (fileNodes.detect { |node| disk.location == node['href'] }).nil?)
         diskSection.add_child(xml.create_element('Disk', {'ovf:capacity' => disk.size.to_s, 'ovf:capacityAllocationUnits' => "byte * 2^30", 'ovf:diskId' => disk.name, 'ovf:fileRef' => disk.name + '_disk', 'ovf:format' => "http://www.vmware.com/interfaces/specifications/vmdk.html#streamOptimized" }))
         references.add_child(xml.create_element('File', {'ovf:href' => disk.location, 'ovf:id' => disk.name + '_disk'}))
      end

      maxID += 1
      address = getFirstOpenIDEAddress(vhs, rasdNamespace, maxID)
      if(!address)
         # PANIC BECAUSE THIS IS BAD MAN, NO AVAILABLE IDE SLOTS
         raise "No IDE slots available"
      else
         maxID += 1
         newDisk = vhs.add_child(xml.create_element('Item', {}))
         newDisk.add_child(xml.create_element('AddressOnParent', address[1])).namespace = rasdNamespace
         newDisk.add_child(xml.create_element('ElementName', disk.name)).namespace = rasdNamespace
         newDisk.add_child(xml.create_element('HostResource', "ovf:/disk/" + disk.name)).namespace = rasdNamespace
         newDisk.add_child(xml.create_element('InstanceID', maxID.to_s)).namespace = rasdNamespace
         newDisk.add_child(xml.create_element('Parent', address[0])).namespace = rasdNamespace
         newDisk.add_child(xml.create_element('ResourceType', "17")).namespace = rasdNamespace
      end
   }

end

#setVmName(newValue) ⇒ Object



310
311
312
313
314
315
# File 'lib/ovfparse/vmpackage.rb', line 310

def setVmName(newValue)
  virtualSystem['ovf:id'] = newValue
  nameNode = getChildByName(virtualSystem, 'Name') ||
     getChildByName(virtualSystem, 'Info').add_next_sibling(xml.create_element('Name', {}))
  nameNode.content = newValue
end

#setVmNetworks(networks) ⇒ Object



351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
# File 'lib/ovfparse/vmpackage.rb', line 351

def setVmNetworks(networks)
   removeNetworksFromVirtualHardwareSection

   networkNodes = getChildrenByName(networkSection, 'Network')
   vhs = getChildByName(virtualSystem, 'VirtualHardwareSection')

   networkNodes.each { |node|
      updated_network = networks.detect { |network| network.location == node['name'] }
      if(updated_network.nil?)
         node.unlink
      else
         descriptionNode = getChildByName(node, 'Description')
         if((updated_network.notes == '' || updated_network.notes.nil?) && !descriptionNode.nil?)
            descriptionNode.unlink
         elsif(updated_network.notes != '' && !updated_network.notes.nil?)
descriptionNode = descriptionNode || descriptionNode.add_child(xml.create_element("Description", {}))
            descriptionNode.content = updated_network.notes
         end
      end
   }

   # Find the highest instance ID
   maxID = 0
   items = getChildrenByName(vhs, 'Item')
   items.each { |item|
      itemID = getChildByName(item, 'InstanceID').content.to_i
      if(itemID > maxID)
         maxID = itemID
      end
   }

   rasdNamespace = xml.root.namespace_definitions.detect{ |ns| ns.prefix == 'rasd' }
   netCount = 0

   networks.each { |network|
      if( (networkNodes.detect { |node| network.location == node['name'] }).nil?)
         networkNode = networkSection.add_child(xml.create_element('Network', {'ovf:name' => network.location}))
         if(network.notes != '' && !network.notes.nil?)
            networkNode.add_child(xml.create_element('Description', network.notes))
         end
      end

      maxID += 1
      newNetwork = vhs.add_child(xml.create_element('Item', {}))
      newNetwork.add_child(xml.create_element('AutomaticAllocation', "true")).namespace = rasdNamespace
      newNetwork.add_child(xml.create_element('Connection', network.location)).namespace = rasdNamespace
      newNetwork.add_child(xml.create_element('ElementName', "ethernet" + netCount.to_s)).namespace = rasdNamespace
      newNetwork.add_child(xml.create_element('InstanceID', maxID.to_s)).namespace = rasdNamespace
      newNetwork.add_child(xml.create_element('ResourceSubType', "PCNet32")).namespace = rasdNamespace
      newNetwork.add_child(xml.create_element('ResourceType', "10")).namespace = rasdNamespace
      netCount += 1
   }
end

#setVmOS_ID(newValue) ⇒ Object



321
322
323
# File 'lib/ovfparse/vmpackage.rb', line 321

def setVmOS_ID(newValue)
  getChildByName(virtualSystem, 'OperatingSystemSection')['ovf:id'] = newValue.to_s
end

#setVmPatchlevelObject



237
238
# File 'lib/ovfparse/vmpackage.rb', line 237

def setVmPatchlevel
end

#setVmRAM(newValue) ⇒ Object



329
330
331
# File 'lib/ovfparse/vmpackage.rb', line 329

def setVmRAM(newValue)
  setVirtualQuantity(4, newValue)
end

#sign(signature) ⇒ Object



698
699
700
701
# File 'lib/ovfparse/vmpackage.rb', line 698

def sign(signature)
  node = Nokogiri::XML::Comment.new(xml, signature)
  xml.children[0].add_next_sibling(node)
end

#to_sObject



63
64
65
66
# File 'lib/ovfparse/vmpackage.rb', line 63

def to_s 
#    (@name + " from " + @url + "\n")
  self.uri 
end

#uriObject



68
69
70
71
72
73
74
# File 'lib/ovfparse/vmpackage.rb', line 68

def uri 
  if (nil==@protocol) then
    return @url
  else 
    return (@protocol + "://" + @url)
  end
end

#writeXML(filename) ⇒ Object



691
692
693
694
695
# File 'lib/ovfparse/vmpackage.rb', line 691

def writeXML(filename)
   file = File.new(filename, "w")
   file.puts(xml.to_s)
   file.close
end

#xpath(string) ⇒ Object



703
704
705
# File 'lib/ovfparse/vmpackage.rb', line 703

def xpath(string)
  puts @xml.xpath(string)
end