Merge pull request #281 from sciurus/fix/lock-pool-creation

Fixes for storage pool creation and base box uploads
This commit is contained in:
Brian Pitts
2014-12-15 10:20:17 -06:00
3 changed files with 72 additions and 61 deletions

View File

@@ -70,8 +70,8 @@ module VagrantPlugins
autostart_network if !@interface_network[:autostart]
activate_network if !@interface_network[:active]
end
end
end
@app.call(env)
end

View File

@@ -4,6 +4,9 @@ module VagrantPlugins
module ProviderLibvirt
module Action
class HandleBoxImage
@@lock = Mutex.new
def initialize(app, env)
@logger = Log4r::Logger.new('vagrant_libvirt::action::handle_box_image')
@app = app
@@ -34,49 +37,51 @@ module VagrantPlugins
env[:box_volume_name] = env[:machine].box.name.to_s.dup.gsub("/", "-VAGRANTSLASH-")
env[:box_volume_name] << '_vagrant_box_image.img'
# Don't continue if image already exists in storage pool.
return @app.call(env) if ProviderLibvirt::Util::Collection.find_matching(
env[:libvirt_compute].volumes.all, env[:box_volume_name])
@@lock.synchronize do
# Don't continue if image already exists in storage pool.
return @app.call(env) if ProviderLibvirt::Util::Collection.find_matching(
env[:libvirt_compute].volumes.all, env[:box_volume_name])
# Box is not available as a storage pool volume. Create and upload
# it as a copy of local box image.
env[:ui].info(I18n.t('vagrant_libvirt.uploading_volume'))
# Box is not available as a storage pool volume. Create and upload
# it as a copy of local box image.
env[:ui].info(I18n.t('vagrant_libvirt.uploading_volume'))
# Create new volume in storage pool
box_image_size = File.size(box_image_file) # B
message = "Creating volume #{env[:box_volume_name]}"
message << " in storage pool #{config.storage_pool_name}."
@logger.info(message)
begin
fog_volume = env[:libvirt_compute].volumes.create(
name: env[:box_volume_name],
allocation: "#{box_image_size/1024/1024}M",
capacity: "#{box_virtual_size}G",
format_type: box_format,
pool_name: config.storage_pool_name)
rescue Fog::Errors::Error => e
raise Errors::FogCreateVolumeError,
:error_message => e.message
end
# Upload box image to storage pool
ret = upload_image(box_image_file, config.storage_pool_name,
env[:box_volume_name], env) do |progress|
env[:ui].clear_line
env[:ui].report_progress(progress, box_image_size, false)
end
# Clear the line one last time since the progress meter doesn't
# disappear immediately.
env[:ui].clear_line
# If upload failed or was interrupted, remove created volume from
# storage pool.
if env[:interrupted] || !ret
# Create new volume in storage pool
box_image_size = File.size(box_image_file) # B
message = "Creating volume #{env[:box_volume_name]}"
message << " in storage pool #{config.storage_pool_name}."
@logger.info(message)
begin
fog_volume.destroy
rescue
nil
fog_volume = env[:libvirt_compute].volumes.create(
name: env[:box_volume_name],
allocation: "#{box_image_size/1024/1024}M",
capacity: "#{box_virtual_size}G",
format_type: box_format,
pool_name: config.storage_pool_name)
rescue Fog::Errors::Error => e
raise Errors::FogCreateVolumeError,
:error_message => e.message
end
# Upload box image to storage pool
ret = upload_image(box_image_file, config.storage_pool_name,
env[:box_volume_name], env) do |progress|
env[:ui].clear_line
env[:ui].report_progress(progress, box_image_size, false)
end
# Clear the line one last time since the progress meter doesn't
# disappear immediately.
env[:ui].clear_line
# If upload failed or was interrupted, remove created volume from
# storage pool.
if env[:interrupted] || !ret
begin
fog_volume.destroy
rescue
nil
end
end
end

View File

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