From a20016ba11d214e585274cd8ea28f4dcb9c0713e Mon Sep 17 00:00:00 2001 From: "Ruben S. Montero" Date: Wed, 18 Nov 2020 11:09:12 +0100 Subject: [PATCH] B #4903: Do not fail link if exists. Linting This reverts commit 5b030d2869cfe960f6a2cdb61d52f502ebf0972c. This reverts commit fd03fafb8f7ddf4da29f8d4ff38cc4ae8819003e. --- src/vnm_mad/remotes/802.1Q/vlan_tag_driver.rb | 30 +++++++++---------- src/vnm_mad/remotes/lib/vnm_driver.rb | 21 ++++--------- 2 files changed, 19 insertions(+), 32 deletions(-) diff --git a/src/vnm_mad/remotes/802.1Q/vlan_tag_driver.rb b/src/vnm_mad/remotes/802.1Q/vlan_tag_driver.rb index 99183e23ca..cbd8fce920 100644 --- a/src/vnm_mad/remotes/802.1Q/vlan_tag_driver.rb +++ b/src/vnm_mad/remotes/802.1Q/vlan_tag_driver.rb @@ -43,7 +43,13 @@ class VLANTagDriver < VNMMAD::VLANDriver # This function creates and activate a VLAN device ############################################################################ def create_vlan_dev - @nic[:mtu] ? mtu = "mtu #{@nic[:mtu]}" : mtu = "mtu #{CONF[:vlan_mtu]}" + mtu = '' + + if @nic[:mtu] + mtu = "mtu #{@nic[:mtu]}" + else + mtu = "mtu #{CONF[:vlan_mtu]}" + end ip_link_conf = '' @@ -58,30 +64,23 @@ class VLANTagDriver < VNMMAD::VLANDriver ip_link_conf << "#{option} #{value} " end - # Delete vlan if it stuck in another bridge. - if nic_exist?(@nic[:vlan_dev])[0] - cmd = "#{command(:ip)} link delete #{@nic[:vlan_dev]}" - OpenNebula.exec_and_log(cmd) - end - + # Do not fail if the device exists to prevent race conditions. + # ip link add returns 2 on "RTNETLINK answers: File exists" OpenNebula.exec_and_log("#{command(:ip)} link add link"\ " #{@nic[:phydev]} name #{@nic[:vlan_dev]} #{mtu} type vlan id"\ - " #{@nic[:vlan_id]} #{ip_link_conf}") + " #{@nic[:vlan_id]} #{ip_link_conf}", nil, 2) OpenNebula.exec_and_log("#{command(:ip)} link set #{@nic[:vlan_dev]} up") end def delete_vlan_dev - return unless @nic[:vlan_dev] != @nic[:phydev] - - OpenNebula.exec_and_log("\ - #{command(:ip)} link delete #{@nic[:vlan_dev]}") + OpenNebula.exec_and_log("#{command(:ip)} link delete"\ + " #{@nic[:vlan_dev]}") if @nic[:vlan_dev] != @nic[:phydev] end def list_interface_vlan(name) - text, status = nic_exist?(name) - - return if status == false + text = %x(#{command(:ip_unpriv)} -d link show #{name}) + return nil if $?.exitstatus != 0 text.each_line do |line| m = line.match(/vlan protocol 802.1Q id (\d+)/) @@ -91,5 +90,4 @@ class VLANTagDriver < VNMMAD::VLANDriver nil end - end diff --git a/src/vnm_mad/remotes/lib/vnm_driver.rb b/src/vnm_mad/remotes/lib/vnm_driver.rb index d8951fc6a2..0209cfc330 100644 --- a/src/vnm_mad/remotes/lib/vnm_driver.rb +++ b/src/vnm_mad/remotes/lib/vnm_driver.rb @@ -38,7 +38,7 @@ module VNMMAD # @param xpath_filter [String] to get relevant NICs for the driver # @param deploy_id [String] def initialize(vm_tpl, xpath_filter, deploy_id = nil) - @locking = false + @locking ||= false @vm = VNMNetwork::VM.new(REXML::Document.new(vm_tpl).root, xpath_filter, deploy_id) @@ -47,8 +47,8 @@ module VNMMAD # Creates a new VNMDriver using: # @param vm_64 [String] Base64 encoded XML String from oned # @param deploy_id [String] - def self.from_base64(vm64, xpath_filter = nil, deploy_id = nil) - vm_xml = Base64.decode64(vm64) + def self.from_base64(vm_64, xpath_filter = nil, deploy_id = nil) + vm_xml = Base64.decode64(vm_64) new(vm_xml, xpath_filter, deploy_id) end @@ -156,8 +156,8 @@ module VNMMAD # Returns a filter object based on the contents of the template # # @return SGDriver object - def self.filter_driver(vm64, xpath_filter, deploy_id) - SGDriver.new(vm64, xpath_filter, deploy_id) + def self.filter_driver(vm_64, xpath_filter, deploy_id) + SGDriver.new(vm_64, xpath_filter, deploy_id) end # Returns the associated command including sudo and other configuration @@ -214,17 +214,6 @@ module VNMMAD 0 end - # Checks wether a NIC exist or not. Returns true/false and the NIC info - def nic_exist?(name) - cmd = "#{command(:ip)} link show #{name}" - - _o, _e, s = Open3.capture3(cmd) - - return true, text if s.exitstatus.zero? - - [false, text] - end - private # returns files sorted alphabetically