Use same list_all_networks and filter (#1638)

Ensure the same filtering for networks supported by vagrant-libvirt is
done for both driver and util by moving to call the same function with
filtering.

This avoids calls for the list of host devices from failing to parse
some networks that are not supported.

Fixes: #599
This commit is contained in:
Darragh Bailey
2022-10-11 17:54:41 +01:00
committed by GitHub
parent 736190ae41
commit 59c7d9f18e
6 changed files with 65 additions and 39 deletions

View File

@@ -61,7 +61,7 @@ module VagrantPlugins
# We have slot for interface, fill it with interface configuration.
adapters[free_slot] = options
adapters[free_slot][:network_name] = interface_network(
env[:machine].provider.driver.connection.client, adapters[free_slot]
env[:machine].provider.driver, adapters[free_slot]
)
end
@@ -282,7 +282,7 @@ module VagrantPlugins
end
# Return network name according to interface options.
def interface_network(libvirt_client, options)
def interface_network(driver, options)
# no need to get interface network for tcp tunnel config
return 'tunnel_interface' if options.fetch(:tunnel_type, nil)
@@ -292,7 +292,7 @@ module VagrantPlugins
end
# Get list of all (active and inactive) Libvirt networks.
available_networks = libvirt_networks(libvirt_client)
available_networks = libvirt_networks(driver)
return 'public' if options[:iface_type] == :public_network

View File

@@ -35,9 +35,7 @@ module VagrantPlugins
# for VMs using sessions. It is likely that this should be done
# to determine the correct virtual device for the management
# network for sessions instead of assuming the default of virbr0.
@available_networks = libvirt_networks(
env[:machine].provider.driver.system_connection
)
@available_networks = libvirt_networks(env[:machine].provider.driver)
@app.call(env)
return
@@ -61,9 +59,7 @@ module VagrantPlugins
# Get a list of all (active and inactive) Libvirt networks. This
# list is used throughout this class and should be easier to
# process than Libvirt API calls.
@available_networks = libvirt_networks(
env[:machine].provider.driver.connection.client
)
@available_networks = libvirt_networks(env[:machine].provider.driver)
current_network = @available_networks.detect { |network| network[:name] == @options[:network_name] }

View File

@@ -197,6 +197,19 @@ module VagrantPlugins
domain
end
def list_all_networks
system_connection.list_all_networks.select do |net|
begin
net.bridge_name
rescue Libvirt::Error
# there does not appear to be a mechanism to determine the type of network, only by
# querying the attribute and catching the error is it possible to ignore unsupported.
@logger.debug "Ignoring #{net.name} as it does not support retrieval of bridge_name attribute"
next
end
end
end
def host_devices
@host_devices ||= begin
cmd = []
@@ -216,7 +229,7 @@ module VagrantPlugins
(
info.map { |iface| iface['ifname'] } +
connection.client.list_all_interfaces.map { |iface| iface.name } +
connection.client.list_all_networks.map { |net| net.bridge_name }
list_all_networks.map { |net| net.bridge_name }
).uniq.reject(&:empty?)
end
end
@@ -234,7 +247,7 @@ module VagrantPlugins
def get_ipaddress_from_system(mac)
ip_address = nil
system_connection.list_all_networks.each do |net|
list_all_networks.each do |net|
leases = net.dhcp_leases(mac, 0)
# Assume the lease expiring last is the current IP address
ip_address = leases.max_by { |lse| lse['expirytime'] }['ipaddr'] unless leases.empty?

View File

@@ -149,19 +149,11 @@ module VagrantPlugins
# Return a list of all (active and inactive) Libvirt networks as a list
# of hashes with their name, network address and status (active or not)
def libvirt_networks(libvirt_client)
def libvirt_networks(driver)
libvirt_networks = []
# Iterate over all (active and inactive) networks.
libvirt_client.list_all_networks.each do |libvirt_network|
begin
bridge_name = libvirt_network.bridge_name
rescue Libvirt::Error
# there does not appear to be a mechanism to determine the type of network, only by
# querying the attribute and catching the error is it possible to ignore unsupported.
@logger.debug "Ignoring #{libvirt_network.name} as it does not support retrieval of bridge_name attribute"
next
end
driver.list_all_networks.each do |libvirt_network|
# Parse ip address and netmask from the network xml description.
xml = Nokogiri::XML(libvirt_network.xml_desc)
@@ -189,7 +181,7 @@ module VagrantPlugins
netmask: netmask,
network_address: network_address,
dhcp_enabled: dhcp_enabled,
bridge_name: bridge_name,
bridge_name: libvirt_network.bridge_name,
domain_name: domain_name,
created: true,
active: libvirt_network.active?,