Use fog-libvirt filters when querying libvirt host

This commit replaces the pattern where the metadata for all volumes,
servers, or pools was downloaded and then searched. Instead a filter
argument is passed to the connection and only the metadata for the named
resource is returned, if it exists. This results in significant speed
increases for libvirt hosts that are offsite or have many volumes.
This commit is contained in:
Chris Crebolder
2020-02-23 15:35:12 -05:00
committed by Darragh Bailey
parent 304a2a055c
commit 86aac4ce9c
6 changed files with 29 additions and 34 deletions

View File

@@ -126,13 +126,9 @@ module VagrantPlugins
pool_name = @storage_pool_name
end
@logger.debug "Search for volume in pool: #{pool_name}"
actual_volumes =
env[:machine].provider.driver.connection.volumes.all.select do |x|
x.pool_name == pool_name
end
domain_volume = ProviderLibvirt::Util::Collection.find_matching(
actual_volumes, "#{@name}.img"
)
domain_volume = env[:machine].provider.driver.connection.volumes.all(
name: "#{@name}.img"
).find { |x| x.pool_name == pool_name }
raise Errors::DomainVolumeExists if domain_volume.nil?
@domain_volume_path = domain_volume.path
end

View File

@@ -25,15 +25,15 @@ module VagrantPlugins
@name = "#{env[:domain_name]}.img"
# Verify the volume doesn't exist already.
domain_volume = ProviderLibvirt::Util::Collection.find_matching(
env[:machine].provider.driver.connection.volumes.all, @name
)
raise Errors::DomainVolumeExists if domain_volume
domain_volume = env[:machine].provider.driver.connection.volumes.all(
name: @name
).first
raise Errors::DomainVolumeExists if domain_volume.id
# Get path to backing image - box volume.
box_volume = ProviderLibvirt::Util::Collection.find_matching(
env[:machine].provider.driver.connection.volumes.all, env[:box_volume_name]
)
box_volume = env[:machine].provider.driver.connection.volumes.all(
name: env[:box_volume_name]
).first
@backing_file = box_volume.path
# Virtual size of image. Take value worked out by HandleBoxImage

View File

@@ -63,9 +63,9 @@ module VagrantPlugins
# locking all subsequent actions as well.
@@lock.synchronize do
# Don't continue if image already exists in storage pool.
break if ProviderLibvirt::Util::Collection.find_matching(
env[:machine].provider.driver.connection.volumes.all, env[:box_volume_name]
)
break if env[:machine].provider.driver.connection.volumes.all(
name: env[:box_volume_name]
).first.id
# Box is not available as a storage pool volume. Create and upload
# it as a copy of local box image.

View File

@@ -24,9 +24,9 @@ module VagrantPlugins
# locking all subsequent actions as well.
@@lock.synchronize do
# Check for storage pool, where box image should be created
break if ProviderLibvirt::Util::Collection.find_matching(
env[:machine].provider.driver.connection.pools.all, config.storage_pool_name
)
break unless env[:machine].provider.driver.connection.pools.all(
name: config.storage_pool_name
).empty?
@logger.info("No storage pool '#{config.storage_pool_name}' is available.")

View File

@@ -18,9 +18,9 @@ module VagrantPlugins
# Remove stale server volume
config = env[:machine].provider_config
# Check for storage pool, where box image should be created
fog_pool = ProviderLibvirt::Util::Collection.find_matching(
env[:machine].provider.driver.connection.pools.all, config.storage_pool_name
)
fog_pool = env[:machine].provider.driver.connection.pools.all(
name: config.storage_pool_name
).first
env[:result] = nil
@@ -36,14 +36,16 @@ module VagrantPlugins
@logger.debug("**** Volume name #{name}")
# remove root storage
box_volume = ProviderLibvirt::Util::Collection.find_matching(
env[:machine].provider.driver.connection.volumes.all, name
)
if box_volume && box_volume.pool_name == fog_pool.name
box_volume = env[:machine].provider.driver.connection.volumes.all(
name: name
).find { |x| x.pool_name == fog_pool.name }
if box_volume && box_volume.id
env[:ui].info(I18n.t('vagrant_libvirt.remove_stale_volume'))
@logger.info("Deleting volume #{box_volume.key}")
box_volume.destroy
env[:result] = box_volume
else
@logger.debug("**** Volume #{name} not found in pool #{fog_pool.name}")
end
# Continue the middleware chain.
@app.call(env)

View File

@@ -13,20 +13,17 @@ module VagrantPlugins
env[:domain_name] = build_domain_name(env)
begin
@logger.info("Looking for domain #{env[:domain_name]} through list " \
"#{env[:machine].provider.driver.connection.servers.all}")
@logger.info("Looking for domain #{env[:domain_name]}")
# Check if the domain name is not already taken
domain = ProviderLibvirt::Util::Collection.find_matching(
env[:machine].provider.driver.connection.servers.all, env[:domain_name]
domain = env[:machine].provider.driver.connection.servers.all(
name: env[:domain_name]
)
rescue Fog::Errors::Error => e
rescue Libvirt::RetrieveError => e
@logger.info(e.to_s)
domain = nil
end
@logger.info("Looking for domain #{env[:domain_name]}")
unless domain.nil?
raise ProviderLibvirt::Errors::DomainNameExists,
domain_name: env[:domain_name]