Class: VmCollection

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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(uri) ⇒ VmCollection

Returns a new instance of VmCollection.



19
20
21
# File 'lib/ovfparse/vmcollection.rb', line 19

def initialize
   @package_details = Array.new
end

Instance Attribute Details

#base_pathObject

Returns the value of attribute base_path.



17
18
19
# File 'lib/ovfparse/vmcollection.rb', line 17

def base_path
  @base_path
end

#diskSectionObject

Returns the value of attribute diskSection.



17
18
19
# File 'lib/ovfparse/vmcollection.rb', line 17

def diskSection
  @diskSection
end

#nameObject

Returns the value of attribute name.



17
18
19
# File 'lib/ovfparse/vmcollection.rb', line 17

def name
  @name
end

#networkSectionObject

Returns the value of attribute networkSection.



17
18
19
# File 'lib/ovfparse/vmcollection.rb', line 17

def networkSection
  @networkSection
end

#package_detailsObject

Returns the value of attribute package_details.



17
18
19
# File 'lib/ovfparse/vmcollection.rb', line 17

def package_details
  @package_details
end

#protocolObject

Returns the value of attribute protocol.



17
18
19
# File 'lib/ovfparse/vmcollection.rb', line 17

def protocol
  @protocol
end

#referencesObject

Returns the value of attribute references.



17
18
19
# File 'lib/ovfparse/vmcollection.rb', line 17

def references
  @references
end

#sizeObject

Returns the value of attribute size.



17
18
19
# File 'lib/ovfparse/vmcollection.rb', line 17

def size
  @size
end

#stateObject

Returns the value of attribute state.



17
18
19
# File 'lib/ovfparse/vmcollection.rb', line 17

def state
  @state
end

#urlObject

Returns the value of attribute url.



17
18
19
# File 'lib/ovfparse/vmcollection.rb', line 17

def url
  @url
end

#versionObject

Returns the value of attribute version.



17
18
19
# File 'lib/ovfparse/vmcollection.rb', line 17

def version
  @version
end

#virtualSystemCollectionObject

Returns the value of attribute virtualSystemCollection.



17
18
19
# File 'lib/ovfparse/vmcollection.rb', line 17

def virtualSystemCollection
  @virtualSystemCollection
end

#xmlObject

Returns the value of attribute xml.



17
18
19
# File 'lib/ovfparse/vmcollection.rb', line 17

def xml
  @xml
end

Class Method Details

.constructFromVmPackages(packages) ⇒ Object



150
151
152
153
154
# File 'lib/ovfparse/vmcollection.rb', line 150

def self.constructFromVmPackages(packages)
   packages.each{ |package|
      addVmPackage(package)
   }
end

.constructSkeletonObject



297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
# File 'lib/ovfparse/vmcollection.rb', line 297

def self.constructSkeleton
   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", 'xmlns:cops' => 'http://cops.mitre.org/1.2', 'xmlns:cpe' => 'http://cpe.mitre.org/dictionary/2.0') {
         xml.References{}
         xml.DiskSection{
            xml.Info "Virtual disk information"
         }
         xml.NetworkSection{
            xml.Info "List of logical networks"
         }
         xml.VirtualSystemCollection('ovf:id' => "vm_collection"){
            xml.Info "A collection of virtual machines"
         }
      }

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

   newPackage = NewVmCollection.new
   newPackage.xml = builder.doc
   newPackage.loadElementRefs
   return newPackage
end

.create(uri) ⇒ Object



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

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)
   if @protocol=='http'
      HttpVmCollection.new(uri)
   #elsif @protocol=='https'
   #   HttpsVmPackage.new(uri)
   elsif @protocol=='file'
      FileVmCollection.new(uri)
   end
end

Instance Method Details

#addDisk(newDisk, childPackage, details) ⇒ Object



208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
# File 'lib/ovfparse/vmcollection.rb', line 208

def addDisk(newDisk, childPackage, details)
   # Always add a disk, even if it's a clone, cause this VM will need its own copy
   isNewDisk = true
   currentDisks = getChildrenByName(diskSection, 'Disk')
   #currentDisks.each{ |oldDisk|
   #   if(compareDisks(oldDisk, newDisk))
   #      isNewDisk = false
   #      break
   #   end
   #}

   if(isNewDisk)
      newDiskID = "vmdisk" + (currentDisks.length + 1).to_s
      newDiskNode = diskSection.add_child(xml.create_element('Disk', {
         'capacity' => newDisk['size'],
         'diskId' => newDiskID,
         'fileRef' => getChildrenByName(references, 'File').detect{ |ref| ref['href'] == newDisk['location'] }['id'],
         'format' => "http://www.vmware.com/interfaces/specifications/vmdk.html#streamOptimized",
         'populatedSize' => newDisk['thin_size']
      }))
      newDiskNode.attribute("capacity").namespace = @ovf_namespace
      newDiskNode.attribute("diskId").namespace = @ovf_namespace
      newDiskNode.attribute("fileRef").namespace = @ovf_namespace
      newDiskNode.attribute("format").namespace = @ovf_namespace
      newDiskNode.attribute("populatedSize").namespace = @ovf_namespace
      findReplace(childPackage, newDisk['name'], newDiskID)
      details.disks.push(newDiskID)
   end
end

#addFileReference(pendingRef, childPackage, details) ⇒ Object



179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
# File 'lib/ovfparse/vmcollection.rb', line 179

def addFileReference(pendingRef, childPackage, details)
   # Compare this one to all the existing ones to prevent duplicates
   isNewRef = true
   currentFileRefs = getChildrenByName(references, 'File')
   currentFileRefs.each{ |oldRef|
      if(compareFileReferences(oldRef, pendingRef))
         isNewRef = false
         break
      end
   }

   # If it's not a dup, add it
   if(isNewRef)
      newFileID = "file" + (currentFileRefs.length + 1).to_s
      newFile = references.add_child(xml.create_element('File', {'href' => pendingRef['href'], 'id' => newFileID, 'size' => pendingRef['size']}))
      newFile.attribute("href").namespace = @ovf_namespace
      newFile.attribute("id").namespace = @ovf_namespace
      newFile.attribute("size").namespace = @ovf_namespace
      findReplace(childPackage, pendingRef['id'], newFileID)
      details.files.push(newFileID)
   else
      details.files.push(pendingRef['href'])
   end
end

#addNetwork(newNetwork, childPackage, details) ⇒ Object



243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
# File 'lib/ovfparse/vmcollection.rb', line 243

def addNetwork(newNetwork, childPackage, details)
   isNewNetwork = true
   currentNetworks = getChildrenByName(networkSection, 'Network')
   currentNetworks.each{ |oldNetwork|
      if(compareNetworks(oldNetwork, newNetwork))
         isNewNetwork = false
         break
      end
   }

   if(isNewNetwork)
      newNode = networkSection.add_child(xml.create_element('Network', {'name' => newNetwork['location']}))
      newNode.attribute("name").namespace = @ovf_namespace
      newNode.add_child(xml.create_element('Description', newNetwork['notes']))
   end
   details.networks.push(newNetwork['location'])
end

#addVirtualSystem(newSystem) ⇒ Object



265
266
267
268
269
# File 'lib/ovfparse/vmcollection.rb', line 265

def addVirtualSystem(newSystem)
   ovfNamespace = xml.root.namespace
   newNode = virtualSystemCollection.add_child(newSystem.clone)
   newNode.namespace = ovfNamespace
end

#addVmPackage(package) ⇒ Object



156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
# File 'lib/ovfparse/vmcollection.rb', line 156

def addVmPackage(package)
   details = PackageDetails.new()
   details.id = package.getVmName

   newRefs = package.getVmReferences
   newRefs.each { |newRef|
      addFileReference(newRef, package, details)
   }

   newDisks = package.getVmDisks
   newDisks.each { |newDisk|
      addDisk(newDisk, package, details)
   }

   newNetworks = package.getVmNetworks
   newNetworks.each { |newNetwork|
      addNetwork(newNetwork, package, details)
   }
   
   package_details.push(details)
   addVirtualSystem(package.virtualSystem)
end

#checkschema(schema) ⇒ Object



91
92
93
94
95
96
97
98
99
100
101
# File 'lib/ovfparse/vmcollection.rb', line 91

def checkschema(schema)
   response = ""

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

   return [isValid, response]
end

#compareDisks(oldDisk, newDisk) ⇒ Object



238
239
240
241
# File 'lib/ovfparse/vmcollection.rb', line 238

def compareDisks(oldDisk, newDisk)
   filename = getChildrenByName(references, 'File').detect{ |ref| ref['id'] == oldDisk['fileRef'] }['href']
   return (filename == newDisk['location'])
end

#compareFileReferences(oldRef, newRef) ⇒ Object



204
205
206
# File 'lib/ovfparse/vmcollection.rb', line 204

def compareFileReferences(oldRef, newRef)
   return (oldRef['href'] == newRef['href']) #&& (oldRef['size'] == newRef['size'])
end

#compareNetworks(oldNetwork, newNetwork) ⇒ Object



261
262
263
# File 'lib/ovfparse/vmcollection.rb', line 261

def compareNetworks(oldNetwork, newNetwork)
   return (oldNetwork['name'] == newNetwork['location'])
end

#fetchObject



65
66
# File 'lib/ovfparse/vmcollection.rb', line 65

def fetch
end

#findReplace(package, oldval, newval) ⇒ Object



271
272
273
274
275
276
277
278
279
280
# File 'lib/ovfparse/vmcollection.rb', line 271

def findReplace(package, oldval, newval)
   package.xml.xpath("//*").each{ |node|
      if(node.children.length == 1 && node.children[0].text? && node.content.match(oldval) != nil)
         node.content = node.content.gsub(oldval, newval)
      end
      node.attributes.each{ |key, attr|
         attr.value = attr.value.gsub(oldval, newval)
      }
   }
end

#getChildByName(node, childName) ⇒ Object

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



82
83
84
# File 'lib/ovfparse/vmcollection.rb', line 82

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.



87
88
89
# File 'lib/ovfparse/vmcollection.rb', line 87

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

#getVirtualSystemsObject



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

def getVirtualSystems
end

#getVmDescriptionObject



107
108
109
# File 'lib/ovfparse/vmcollection.rb', line 107

def getVmDescription
   # ???
end

#getVmDisksObject



111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
# File 'lib/ovfparse/vmcollection.rb', line 111

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



103
104
105
# File 'lib/ovfparse/vmcollection.rb', line 103

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

#getVmNetworksObject



137
138
139
140
141
142
143
144
145
# File 'lib/ovfparse/vmcollection.rb', line 137

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

#loadElementRefsObject

Caches all of the base elements inside Envelope for fast access



69
70
71
72
73
74
75
76
77
78
79
# File 'lib/ovfparse/vmcollection.rb', line 69

def loadElementRefs
   children = @xml.root.children
   @ovf_namespace = xml.root.namespace_definitions.detect{|ns| ns.prefix == "ovf"}
   @default_namespace = xml.root.namespace

   @references = getChildByName(xml.root, 'References')
   @virtualSystemCollection = getChildByName(xml.root, 'VirtualSystemCollection')

   @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

#parseXMLObject



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
404
405
406
407
408
409
410
411
# File 'lib/ovfparse/vmcollection.rb', line 366

def parseXML
   fileRefs = Hash.new
   fileNames = Array.new
   getChildrenByName(references, "File").each{ |fileRef|
      fileNames.push(fileRef['id'])
   }

   diskNames = Array.new
   getChildrenByName(diskSection, "Disk").each{ |disk|
      diskNames.push(disk['diskId'])
      fileRefs[disk['diskId']] = disk['fileRef']
   }

   networkNames = Array.new
   getChildrenByName(networkSection, "Network").each{ |network|
      networkNames.push(network['name'])
   } 

   getChildrenByName(virtualSystemCollection, "VirtualSystem").each{ |virtualSystem|
      details = PackageDetails.new()
      details.id = virtualSystem['id']

      fileNames.each{ |fileName|
         if(search(virtualSystem, fileName))
            details.files.push(fileName)
         end
      }

      diskNames.each{ |diskName|
         if(search(virtualSystem, diskName))
            details.disks.push(diskName)
            if(!details.files.include?(fileRefs[diskName]))
               details.files.push(fileRefs[diskName])
            end
         end
      }

      networkNames.each{ |networkName|
         if(search(virtualSystem, networkName))
            details.networks.push(networkName)
         end
      }

      package_details.push(details)
   }
end

#search(virtualSystem, value) ⇒ Object



282
283
284
285
286
287
288
289
290
291
292
293
294
# File 'lib/ovfparse/vmcollection.rb', line 282

def search(virtualSystem, value)
   virtualSystem.xpath(".//*").each{ |node|
      if(node.children.length == 1 && node.children[0].text? && node.content.match(value) != nil)
         return true
      end
      node.attributes.each{ |key, attr|
         if(attr.value.match(value) != nil)
            return true
         end
      }
   }
   return false
end

#splitIntoPackagesObject



328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
# File 'lib/ovfparse/vmcollection.rb', line 328

def splitIntoPackages
   packages = Array.new

   i = 0
   virtualSystems = getChildrenByName(virtualSystemCollection, "VirtualSystem")
   virtualSystems.each{ |virtualSystem|
      details = package_details[i]
      new_package = VmPackage.construct_skeleton
      new_package.loadElementRefs

      details.files.each{ |file|
         node = getChildrenByName(references, "File").detect{ |node| node['id'] == file }
         new_package.references.add_child(node.clone)
      }
         
      details.disks.each{ |disk|
         node = getChildrenByName(diskSection, "Disk").detect{ |node| node['diskId'] == disk }
         new_package.diskSection.add_child(node.clone)
      }

      details.networks.each{ |network|
         node = getChildrenByName(networkSection, "Network").detect{ |node| node['name'] == network }
         new_package.networkSection.add_child(node.clone)
      }

      new_package.virtualSystem.children.unlink
      new_package.virtualSystem.add_child(virtualSystem.clone.children)
      new_package.virtualSystem.children.each{ |child|
         child.namespace = new_package.virtualSystem.namespace
      }
      packages.push(new_package)

      i += 1
   }

   return packages
end

#to_sObject



23
24
25
# File 'lib/ovfparse/vmcollection.rb', line 23

def to_s 
   self.uri 
end

#uriObject



27
28
29
30
31
32
33
# File 'lib/ovfparse/vmcollection.rb', line 27

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

#writeXML(filename) ⇒ Object



322
323
324
325
326
# File 'lib/ovfparse/vmcollection.rb', line 322

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