Add lock around storage pool creation; closes #278

This commit is contained in:
Brian Pitts 2014-12-07 12:16:05 -06:00
parent 916ec6798f
commit 47a21433a5

View File

@ -6,38 +6,42 @@ module VagrantPlugins
class HandleStoragePool class HandleStoragePool
include VagrantPlugins::ProviderLibvirt::Util::ErbTemplate include VagrantPlugins::ProviderLibvirt::Util::ErbTemplate
@@lock = Mutex.new
def initialize(app, env) def initialize(app, env)
@logger = Log4r::Logger.new("vagrant_libvirt::action::handle_storage_pool") @logger = Log4r::Logger.new("vagrant_libvirt::action::handle_storage_pool")
@app = app @app = app
end end
def call(env) def call(env)
# Get config options. @@lock.synchronize do
config = env[:machine].provider_config # Get config options.
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 = ProviderLibvirt::Util::Collection.find_matching(
env[:libvirt_compute].pools.all, config.storage_pool_name) env[:libvirt_compute].pools.all, config.storage_pool_name)
return @app.call(env) if fog_pool return @app.call(env) if fog_pool
@logger.info("No storage pool '#{config.storage_pool_name}' is available.") @logger.info("No storage pool '#{config.storage_pool_name}' is available.")
# If user specified other pool than default, don't create default # If user specified other pool than default, don't create default
# storage pool, just write error message. # storage pool, just write error message.
raise Errors::NoStoragePool if config.storage_pool_name != 'default' raise Errors::NoStoragePool if config.storage_pool_name != 'default'
@logger.info("Creating storage pool 'default'") @logger.info("Creating storage pool 'default'")
# Fog libvirt currently doesn't support creating pools. Use # Fog libvirt currently doesn't support creating pools. Use
# ruby-libvirt client directly. # ruby-libvirt client directly.
begin begin
libvirt_pool = env[:libvirt_compute].client.create_storage_pool_xml( libvirt_pool = env[:libvirt_compute].client.create_storage_pool_xml(
to_xml('default_storage_pool')) to_xml('default_storage_pool'))
rescue => e rescue => e
raise Errors::CreatingStoragePoolError, raise Errors::CreatingStoragePoolError,
:error_message => e.message :error_message => e.message
end
raise Errors::NoStoragePool if !libvirt_pool
end end
raise Errors::NoStoragePool if !libvirt_pool
@app.call(env) @app.call(env)
end end