Skip to content

Commit

Permalink
OpenNebula#3029 Avoid running nic_query in network pre script
Browse files Browse the repository at this point in the history
  • Loading branch information
dann1 committed Mar 22, 2019
1 parent 0d158cf commit 7ddc098
Showing 1 changed file with 69 additions and 65 deletions.
134 changes: 69 additions & 65 deletions src/vnm_mad/remotes/lib/vm.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,100 +16,104 @@

module VNMMAD

module VNMNetwork
module VNMNetwork

############################################################################
# This class represents the VM abstraction. It provides basic methods
# to interact with its network interfaces.
############################################################################
class VM
attr_accessor :nics, :vm_info, :deploy_id, :vm_root
########################################################################
# This class represents the VM abstraction. It provides basic methods
# to interact with its network interfaces.
########################################################################
class VM

attr_accessor :nics, :vm_info, :deploy_id, :vm_root

# Creates a new VM object, and bootstrap the NICs array
# @param vm_root [REXML] XML document representing the VM
# @param xpath_filer [String] to get the VM NICs
# @param deploy_id [String] refers to the VM in the hypervisor
def initialize(vm_root, xpath_filter, deploy_id)
@vm_root = vm_root
@deploy_id = deploy_id
# Creates a new VM object, and bootstrap the NICs array
# @param vm_root [REXML] XML document representing the VM
# @param xpath_filer [String] to get the VM NICs
# @param deploy_id [String] refers to the VM in the hypervisor
def initialize(vm_root, xpath_filter, deploy_id)
@vm_root = vm_root
@deploy_id = deploy_id

@vm_info = Hash.new
@vm_info = {}

@deploy_id = nil if deploy_id == "-"
@deploy_id = nil if deploy_id == '-'

nics = VNMNetwork::Nics.new(hypervisor)
nics = VNMNetwork::Nics.new(hypervisor)

@vm_root.elements.each(xpath_filter) do |nic_element|
nic = nics.new_nic
@vm_root.elements.each(xpath_filter) do |nic_element|
nic = nics.new_nic

nic_build_hash(nic_element,nic)
nic_build_hash(nic_element, nic)

nic.get_info(self)
nic.get_tap(self)
if File.basename($PROGRAM_NAME) != 'pre'
nic.get_info(self)
nic.get_tap(self)
end

nics << nic
end

nics << nic
@nics = nics
end

@nics = nics
end
# Iterator on each NIC of the VM
def each_nic(block)
return if @nics.nil?

# Iterator on each NIC of the VM
def each_nic(block)
if @nics != nil
@nics.each do |the_nic|
block.call(the_nic)
end
end
end

# Access an XML Element of the VM
# @param element [String] element name
# @return [String] value of the element or nil if not found
def [](element)
if @vm_root
val = @vm_root.elements[element]
return val.text if !val.nil? && val.text
end
# Access an XML Element of the VM
# @param element [String] element name
# @return [String] value of the element or nil if not found
def [](element)
if @vm_root
val = @vm_root.elements[element]
return val.text if !val.nil? && val.text
end

nil
end
nil
end

# Gets the Hypervisor VM_MAD from the Template
# @return [String] name of the hypervisor driver
def hypervisor
xpath = 'HISTORY_RECORDS/HISTORY/VM_MAD'
@vm_root.root.elements[xpath].text
end
# Gets the Hypervisor VM_MAD from the Template
# @return [String] name of the hypervisor driver
def hypervisor
xpath = 'HISTORY_RECORDS/HISTORY/VM_MAD'
@vm_root.root.elements[xpath].text
end

private
private

# Method to build the associated Hash from a NIC
# @param nic_element [REXML] for the NIC
# @param nic [Nic] class representation
def nic_build_hash(nic_element,nic)
nic_element.elements.each('*') do |nic_attribute|
key = nic_attribute.name.downcase.to_sym
# Method to build the associated Hash from a NIC
# @param nic_element [REXML] for the NIC
# @param nic [Nic] class representation
def nic_build_hash(nic_element, nic)
nic_element.elements.each('*') do |nic_attribute|
key = nic_attribute.name.downcase.to_sym

if nic_attribute.has_elements?
data = {}
nic_build_hash(nic_attribute,data)
else
data = nic_attribute.text
end
if nic_attribute.has_elements?
data = {}
nic_build_hash(nic_attribute, data)
else
data = nic_attribute.text
end

if nic[key]
if nic[key].instance_of?(Array)
nic[key] << data
if nic[key]
if nic[key].instance_of?(Array)
nic[key] << data
else
nic[key] = [nic[key], data]
end
else
nic[key] = [nic[key], data]
nic[key] = data
end
else
nic[key] = data
end
end

end

end
end

end

0 comments on commit 7ddc098

Please sign in to comment.