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

View File

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

View File

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

View File

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

View File

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

View File

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