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



123
124
125
126
127
# File 'lib/ovfparse/vmcollection.rb', line 123

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

.constructSkeletonObject

TODO:

any need to make this a general purpose “writer” ?



270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
# File 'lib/ovfparse/vmcollection.rb', line 270

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('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

   builder.doc.root.children[3].attribute("id").namespace = builder.doc.root.namespace_definitions.detect{ |ns| ns.prefix == "ovf"}
   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



181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
# File 'lib/ovfparse/vmcollection.rb', line 181

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



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

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



216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
# File 'lib/ovfparse/vmcollection.rb', line 216

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



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

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

#addVmPackage(package) ⇒ Object



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

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



211
212
213
214
# File 'lib/ovfparse/vmcollection.rb', line 211

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

#compareFileReferences(oldRef, newRef) ⇒ Object



177
178
179
# File 'lib/ovfparse/vmcollection.rb', line 177

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

#compareNetworks(oldNetwork, newNetwork) ⇒ Object



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

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



244
245
246
247
248
249
250
251
252
253
# File 'lib/ovfparse/vmcollection.rb', line 244

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

#getCollectionDescriptionObject



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

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

#getCollectionNameObject



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

def getCollectionName
   return virtualSystemCollection['id'] || ''
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



341
342
343
344
345
346
347
348
349
350
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
# File 'lib/ovfparse/vmcollection.rb', line 341

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



255
256
257
258
259
260
261
262
263
264
265
266
267
# File 'lib/ovfparse/vmcollection.rb', line 255

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

#setCollectionDescription(newValue) ⇒ Object



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

def setCollectionDescription(newValue)
   getChildByName(virtualSystemCollection, 'Info').content = newValue
end

#setCollectionName(newValue) ⇒ Object



112
113
114
115
116
117
# File 'lib/ovfparse/vmcollection.rb', line 112

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

#splitIntoPackagesObject



302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
# File 'lib/ovfparse/vmcollection.rb', line 302

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['ovf:id'] = virtualSystem['id']
      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



296
297
298
299
300
# File 'lib/ovfparse/vmcollection.rb', line 296

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