Skip to content

Commit

Permalink
L #-: Fix linting issues
Browse files Browse the repository at this point in the history
  • Loading branch information
tinova committed Sep 12, 2022
1 parent a06b822 commit 2f21cd1
Show file tree
Hide file tree
Showing 6 changed files with 68 additions and 75 deletions.
8 changes: 4 additions & 4 deletions src/cli/onevm
Original file line number Diff line number Diff line change
Expand Up @@ -917,17 +917,17 @@ CommandParser::CmdParser.new(ARGV) do
nic_alias = options[:alias]
nic_name = options[:nic_name]

is_pci = [:pci, :pci_device, :pci_vendor, :pci_class].any? { |o|
is_pci = [:pci, :pci_device, :pci_vendor, :pci_class].any? do |o|
!options[o].nil?
}
end

if is_pci
pcia = options[:pci]
pcid = options[:pci_device]
pcic = options[:pci_class]
pciv = options[:pci_vendor]

template = "PCI = [ TYPE = NIC"
template = 'PCI = [ TYPE = NIC'
template << ", NETWORK_ID = #{network_id}"
template << ", SHORT_ADDRESS = \"#{pcia}\"" if pcia
template << ", DEVICE = \"#{pcid}\"" if pcid
Expand All @@ -942,7 +942,7 @@ CommandParser::CmdParser.new(ARGV) do

template << ", IP = #{ip}" if ip
template << ", NAME = #{nic_name}" if nic_name
template << "]"
template << ']'
end

helper.perform_action(args[0], options, 'Attaching NIC') do |vm|
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,8 @@
domcap_xml = REXML::Document.new(domcapabilities)
domcap_xml = domcap_xml.root

cpu_mode_custom_elem = domcap_xml.elements["cpu/mode[@name='custom',@supported='yes']"]
cpu_mode_xpath = "cpu/mode[@name='custom',@supported='yes']"
cpu_mode_custom_elem = domcap_xml.elements[cpu_mode_xpath]

cpu_mode_custom_elem.elements.each("model[@usable='no']") do |m|
models.delete(m.text)
Expand Down
13 changes: 6 additions & 7 deletions src/vmm_mad/remotes/kvm/attach_nic
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,6 @@ include VirtualMachineManagerKVM

# ------------------------------------------------------------------------------
# ------------------------------------------------------------------------------
#load_env("#{__dir__}/../../etc/vmm/kvm/kvmrc")
load_local_env

domain = ARGV[0]
Expand All @@ -72,14 +71,14 @@ else
end

script =<<~EOS
#{virsh} attach-device #{domain} <(
cat <<EOT
#{dev_xml}
EOT
)
#{virsh} attach-device #{domain} <(
cat <<EOT
#{dev_xml}
EOT
)
EOS

rc, _out, err = Command.execute("bash -s", false, 0, :stdin_data => script)
rc, _out, err = Command.execute('bash -s', false, 0, :stdin_data => script)

if rc != 0
STDERR.puts "Could not attach NIC to #{domain}: #{err}"
Expand Down
21 changes: 9 additions & 12 deletions src/vmm_mad/remotes/kvm/detach_nic
Original file line number Diff line number Diff line change
Expand Up @@ -61,14 +61,14 @@ include VirtualMachineManagerKVM
# ------------------------------------------------------------------------------
def attached?(dom, mac)
cmd = "#{virsh} domiflist #{dom} | grep #{mac} > /dev/null 2>&1"
rc, _o, _e = Command.execute("bash -s", false, 0, :stdin_data => cmd)
rc, _o, _e = Command.execute('bash -s', false, 0, :stdin_data => cmd)

rc == 0
end

def detach_interface(dom, mac)
cmd = "#{virsh} detach-interface --domain #{dom} --type bridge --mac #{mac}"
rc, _o, e = Command.execute("bash -s", false, 0, :stdin_data => cmd)
rc, _o, e = Command.execute('bash -s', false, 0, :stdin_data => cmd)

STDERR.puts "Error detaching interface (#{mac}): #{e}" if rc != 0

Expand All @@ -79,27 +79,25 @@ def detach_nic(dom, mac)
tries = ENV['VIRSH_RETRIES']
tries ||= 3

rc = tries.to_i.times do
tries.to_i.times do
detach_interface(dom, mac)

break 0 unless attached?(dom, mac)
end

rc
end

def detach_pci(dom, vm)
dev_xml = vm.hostdev_xml(true)

cmd =<<~EOS
#{virsh} detach-device #{dom} <(
cat <<EOT
#{dev_xml}
EOT
)
#{virsh} detach-device #{dom} <(
cat <<EOT
#{dev_xml}
EOT
)
EOS

rc, _o, e = Command.execute("bash -s", false, 0, :stdin_data => cmd)
rc, _o, e = Command.execute('bash -s', false, 0, :stdin_data => cmd)

if rc != 0
STDERR.puts "Could not attach NIC to #{dom}: #{e}"
Expand All @@ -110,7 +108,6 @@ end
# ------------------------------------------------------------------------------
# ------------------------------------------------------------------------------

#load_env("#{__dir__}/../../etc/vmm/kvm/kvmrc")
load_local_env

domain = ARGV[0]
Expand Down
84 changes: 37 additions & 47 deletions src/vmm_mad/remotes/lib/kvm/opennebula_vm.rb
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,8 @@ module VirtualMachineManagerKVM

# Default locations for kvmrc file on the front-end (local) or
# hypervisor (remote)
KVMRC_LOCAL = "/var/lib/one/remotes/etc/vmm/kvm/kvmrc"
KVMRC_REMOTE = "/var/tmp/one/etc/vmm/kvm/kvmrc"
KVMRC_LOCAL = '/var/lib/one/remotes/etc/vmm/kvm/kvmrc'
KVMRC_REMOTE = '/var/tmp/one/etc/vmm/kvm/kvmrc'

# Loads env from the default local (front-end) path
def load_local_env
Expand Down Expand Up @@ -74,7 +74,7 @@ class KvmVM < OpenNebulaVM
def initialize(xml_action)
super(xml_action, {})

#if set, it will scope VM element access
# if set, it will scope VM element access
@xpath_prefix = ''
end

Expand Down Expand Up @@ -119,7 +119,7 @@ def interface_xml
dev << '<virtualport type="openvswitch"/>'
end

dev << xputs("<source bridge=%s/>", 'BRIDGE')
dev << xputs('<source bridge=%s/>', 'BRIDGE')
else
dev = '<interface type="ethernet">'
end
Expand All @@ -132,45 +132,46 @@ def interface_xml
dev << "<model type=#{model}/>" unless model.empty?

if model == 'virtio'
dev << xputs('<driver name="vhost" queues=%s/>', 'VIRTIO_QUEUES')
dev << xputs('<driver name="vhost" queues=%s/>',
'VIRTIO_QUEUES')
end

if exist?('IP') && !filter.empty?
dev << "<filterref filter=#{filter}>"
dev << xputs('<parameter name="IP" value=%s/>', 'IP')
dev << xputs('<parameter name="IP" value=%s/>', 'VROUTER_IP')
dev << "</filterref>"
dev << '</filterref>'
end

inb_keys = %w[ INBOUND_AVG_BW INBOUND_PEAK_BW INBOUND_PEAK_KB ]
inbound = inb_keys.any? { |e| exist? e }
inb_keys = %w[INBOUND_AVG_BW INBOUND_PEAK_BW INBOUND_PEAK_KB]
inbound = inb_keys.any? {|e| exist? e }

outb_keys = %w[ OUTBOUND_AVG_BW OUTBOUND_PEAK_BW OUTBOUND_PEAK_KB ]
outbound = outb_keys.any? { |e| exist? e }
outb_keys = %w[OUTBOUND_AVG_BW OUTBOUND_PEAK_BW OUTBOUND_PEAK_KB]
outbound = outb_keys.any? {|e| exist? e }

if inbound || outbound
dev << "<bandwidth>"
dev << '<bandwidth>'

if inbound
dev << "<inbound"
dev << xputs(" average=%s", 'INBOUND_AVG_BW')
dev << xputs(" peak=%s", 'INBOUND_PEAK_BW')
dev << xputs(" burst=%s", 'INBOUND_PEAK_KB')
dev << "/>"
dev << '<inbound'
dev << xputs(' average=%s', 'INBOUND_AVG_BW')
dev << xputs(' peak=%s', 'INBOUND_PEAK_BW')
dev << xputs(' burst=%s', 'INBOUND_PEAK_KB')
dev << '/>'
end

if outbound
dev << "<outbound"
dev << xputs(" average=%s", 'OUTBOUND_AVG_BW')
dev << xputs(" peak=%s", 'OUTBOUND_PEAK_BW')
dev << xputs(" burst=%s", 'OUTBOUND_PEAK_KB')
dev << "/>"
dev << '<outbound'
dev << xputs(' average=%s', 'OUTBOUND_AVG_BW')
dev << xputs(' peak=%s', 'OUTBOUND_PEAK_BW')
dev << xputs(' burst=%s', 'OUTBOUND_PEAK_KB')
dev << '/>'
end

dev << "</bandwidth>"
dev << '</bandwidth>'
end

dev << "</interface>"
dev << '</interface>'

@xpath_prefix = prefix_old

Expand All @@ -197,17 +198,18 @@ def vf?(short_address)
# Example:
#
# <hostdev mode='subsystem' type='pci' managed='yes'>
# <source>
# <address domain='0x0000' bus='0x05' slot='0x02' function='0x0'/>
# </source>
# <address type='pci' domain='0x0' bus='0x01' slot='0x01' function='0'/>
# <source>
# <address domain='0x0000' bus='0x05' slot='0x02' function='0x0'/>
# </source>
# <address type='pci' domain='0x0' bus='0x01'
# slot='0x01' function='0'/>
# </hostdev>
#
# NOTE: Libvirt/QEMU seems to have a race condition accesing vfio device
# and the permission check/set that makes <hostdev> not work for VF.
#
# NOTE: On detach (as we are manging MAC/VLAN through ip link vf) devices
# needs to use <hostdev> format
# NOTE: On detach (as we are managing MAC/VLAN through ip link vf)
# devices needs to use <hostdev> format
#-----------------------------------------------------------------------
def hostdev_xml(force_hostdev = false)
prefix_old = @xpath_prefix
Expand All @@ -216,7 +218,7 @@ def hostdev_xml(force_hostdev = false)
if exist? 'UUID'
dev = '<hostdev mode="subsystem" type="mdev" model="vfio-pci">'
dev << xputs('<source><address uuid=%s/></source>', 'UUID')
dev << "</hostdev>"
dev << '</hostdev>'
else
if force_hostdev
is_vf = false
Expand All @@ -239,22 +241,6 @@ def hostdev_xml(force_hostdev = false)
dev << xputs(' slot=%s', 'SLOT', :hex => true)
dev << xputs(' function=%s', 'FUNCTION', :hex => true)
dev << '/></source>'
=begin
#Setting Bus address needs to check that a PCI contoller is
#present for Bus 1
vm_addr = %w[VM_DOMAIN VM_BUS VM_SLOT VM_FUNCTION].all? {|e|
exist? e
}
if vm_addr
dev << '<address type="pci"'
dev << xputs(' domain=%s', 'VM_DOMAIN')
dev << xputs(' bus=%s', 'VM_BUS')
dev << xputs(' slot=%s', 'VM_SLOT')
dev << xputs(' function=%s', 'VM_FUNCTION')
dev << '/>'
end
=end
dev << dev_end
end

Expand All @@ -276,7 +262,7 @@ def xputs(format, name, opts = {})

value = "0x#{value}" if opts[:hex]

sprintf(format, value.encode(:xml => :attr))
format(format, value.encode(:xml => :attr))
end

# @return true if the given VM element exists (considers xpath_prefix)
Expand All @@ -287,9 +273,13 @@ def exist?(name)
# @return a copy of an env variable or '' if not defined
def env(name)
return '' if ENV[name].nil?

ENV[name].dup
end

end

end

# rubocop:enable Style/ClassAndModuleChildren
# rubocop:enable Style/ClassVars
14 changes: 10 additions & 4 deletions src/vnm_mad/remotes/lib/vf.rb
Original file line number Diff line number Diff line change
Expand Up @@ -62,11 +62,12 @@ def activate_vf(vm)
end
end

# rubocop:disable Style/CombinableLoops
vm.each_pci do |pci|
next if pci[:short_address].nil?
next if is_attach && pci[:attach] != 'YES'

#Look for the associated PF
# Look for the associated PF
cmd = "find /sys/devices -type l -name 'virtfn*' -printf '%p#'"\
" -exec readlink -f '{}' \\;"

Expand All @@ -81,9 +82,12 @@ def activate_vf(vm)

virtfn, _vf = line.split('#')

#Matched line is in the form:
#virtfn /sys/devices/pci0000:80/0000:80:03.2/0000:85:00.0/virtfn3
#_vf /sys/devices/pci0000:80/0000:80:03.2/0000:85:02.3
# rubocop:disable Layout/LineLength
# Matched line is in the form:
# virtfn /sys/devices/pci0000:80/0000:80:03.2/0000:85:00.0/virtfn3
# _vf /sys/devices/pci0000:80/0000:80:03.2/0000:85:02.3
# rubocop:enable Layout/LineLength

m = virtfn.match(/virtfn([0-9]+)/)

next if m.nil?
Expand All @@ -102,6 +106,8 @@ def activate_vf(vm)
OpenNebula.exec_and_log(cmd)
end
end
# rubocop:enable Style/CombinableLoops
end

end
# rubocop:enable Style/ClassAndModuleChildren

0 comments on commit 2f21cd1

Please sign in to comment.