1
0
mirror of https://github.com/OpenNebula/one.git synced 2025-03-22 18:50:08 +03:00

B #3029: Avoid running nic_query in network pre script. Make use of pre_action detection in other drivers.

Co-authored-by: Daniel Clavijo Coca <dclavijo@opennebula.systems>
This commit is contained in:
Ruben S. Montero 2019-03-25 14:43:14 +01:00
parent 2bb8957cb6
commit d2a33ecbb5
No known key found for this signature in database
GPG Key ID: A0CEA6FA880A1D87
8 changed files with 127 additions and 110 deletions

View File

@ -34,35 +34,36 @@ class EbtablesVLAN < VNMMAD::NoVLANDriver
# Activates ebtables rules
#
def activate(pre_action=false)
if pre_action
def activate
if VNMMAD.pre_action?
super()
else
lock
process do |nic|
tap = nic[:tap]
if tap
iface_mac = nic[:mac]
mac = iface_mac.split(':')
mac[-1] = '00'
net_mac = mac.join(':')
in_rule="FORWARD -s ! #{net_mac}/ff:ff:ff:ff:ff:00 " <<
"-o #{tap} -j DROP"
out_rule="FORWARD -s ! #{iface_mac} -i #{tap} -j DROP"
ebtables(in_rule) if nic[:filter_mac_spoofing] =~ /yes/i
ebtables(out_rule)
end
end
unlock
return 0
end
return 0
lock
process do |nic|
tap = nic[:tap]
if tap
iface_mac = nic[:mac]
mac = iface_mac.split(':')
mac[-1] = '00'
net_mac = mac.join(':')
in_rule="FORWARD -s ! #{net_mac}/ff:ff:ff:ff:ff:00 " <<
"-o #{tap} -j DROP"
out_rule="FORWARD -s ! #{iface_mac} -i #{tap} -j DROP"
ebtables(in_rule) if nic[:filter_mac_spoofing] =~ /yes/i
ebtables(out_rule)
end
end
unlock
0
end
def deactivate

View File

@ -26,4 +26,4 @@ deploy_id = ARGV[0]
xpath_filter = EbtablesVLAN::XPATH_FILTER
onevlan = EbtablesVLAN.from_base64(template64, xpath_filter, deploy_id)
onevlan.activate(pre_action=true)
onevlan.activate

View File

@ -14,6 +14,8 @@
# limitations under the License. #
#--------------------------------------------------------------------------- #
require 'open3'
module VNMMAD
module VNMNetwork
@ -110,12 +112,17 @@ module VNMMAD
end
if deploy_id && vm.vm_info[:dumpxml].nil?
cmd = "lxc config show #{deploy_id} 2>/dev/null"
cmd = "lxc config show #{deploy_id}"
config = YAML.safe_load(`#{cmd}`)
config = YAML.safe_load(`sudo #{cmd}`) if config.nil?
config, e, s = Open3.capture3(cmd)
vm.vm_info[:dumpxml] = config
if s.exitstatus != 0 && e.include?('cannot create'\
'user data directory')
cmd.prepend('sudo')
config, _e, _s = Open3.capture3(cmd)
end
vm.vm_info[:dumpxml] = YAML.safe_load(config)
vm.vm_info.each_key do |k|
vm.vm_info[k] = nil if vm.vm_info[k].to_s.strip.empty?

View File

@ -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 !VNMMAD.pre_action?
nic.get_info(self)
nic.get_tap(self)
end
nics << nic
nics << nic
end
@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
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
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
if nic_attribute.has_elements?
data = {}
nic_build_hash(nic_attribute,data)
else
data = nic_attribute.text
# 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
if nic[key]
if nic[key].instance_of?(Array)
nic[key] << data
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
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
if nic_attribute.has_elements?
data = {}
nic_build_hash(nic_attribute, data)
else
nic[key] = [nic[key], data]
data = nic_attribute.text
end
if nic[key]
if nic[key].instance_of?(Array)
nic[key] << data
else
nic[key] = [nic[key], data]
end
else
nic[key] = data
end
else
nic[key] = data
end
end
end
end
end
end

View File

@ -188,5 +188,12 @@ module VNMMAD
options
end
# Returns true if the driver is executing action pre
def self.pre_action?
File.basename($PROGRAM_NAME) == 'pre'
end
end
end

View File

@ -34,7 +34,7 @@ class OpenvSwitchVLAN < VNMMAD::VNMDriver
end
end
def activate(pre_action=false)
def activate
lock
@bridges = get_bridges
@ -65,13 +65,11 @@ class OpenvSwitchVLAN < VNMMAD::VNMDriver
# In net/pre action, we just need to ensure the bridge is
# created so the libvirt/QEMU can add VM interfaces into that.
# Any other driver actions are done in net/post action.
if pre_action
next
else
STDERR.puts "No tap device found for nic #{@nic[:nic_id]}"
unlock
exit 1
end
next if VNMMAD.pre_action?
STDERR.puts "No tap device found for nic #{@nic[:nic_id]}"
unlock
exit 1
end
# Apply VLAN

View File

@ -27,4 +27,4 @@ xpath_filter = OpenvSwitchVLAN::XPATH_FILTER
ovs = OpenvSwitchVLAN.from_base64(template64, xpath_filter, deploy_id)
ovs.activate(pre_action=true)
ovs.activate

View File

@ -27,4 +27,4 @@ xpath_filter = OpenvSwitchVXLAN::XPATH_FILTER
ovs = OpenvSwitchVXLAN.from_base64(template64, xpath_filter, deploy_id)
ovs.activate(pre_action=true)
ovs.activate