From 21bed5c0aaf1882b2bebfee5da2a98c207a414ea Mon Sep 17 00:00:00 2001 From: Mitchell Hashimoto Date: Mon, 25 Nov 2013 19:11:26 -0800 Subject: [PATCH] core: cleanup HandleBoxURL --- lib/vagrant/action/builtin/handle_box_url.rb | 103 +++++++++---------- 1 file changed, 50 insertions(+), 53 deletions(-) diff --git a/lib/vagrant/action/builtin/handle_box_url.rb b/lib/vagrant/action/builtin/handle_box_url.rb index db0d162a3..e352ef74b 100644 --- a/lib/vagrant/action/builtin/handle_box_url.rb +++ b/lib/vagrant/action/builtin/handle_box_url.rb @@ -25,61 +25,58 @@ module Vagrant return end - if !env[:machine].box - # Get a "big lock" to make sure that our more fine grained - # lock access is thread safe. - lock = nil - @@big_lock.synchronize do - lock = @@handle_box_url_locks[env[:machine].config.vm.box] - end - - # We can assume a box URL is set because the Vagrantfile - # validation should do this for us. If not, though, we do - # raise a terrible runtime error. - box_name = env[:machine].config.vm.box - box_url = env[:machine].config.vm.box_url - box_download_client_cert = env[:machine].config.vm.box_download_client_cert - box_download_insecure = env[:machine].config.vm.box_download_insecure - - lock.synchronize do - # First see if we actually have the box now. - has_box = false - - box_formats = env[:machine].provider_options[:box_format] || - env[:machine].provider_name - if env[:box_collection].find(box_name, box_formats) - has_box = true - break - end - - if !has_box - # Add the box then reload the box collection so that it becomes - # aware of it. - env[:ui].info I18n.t( - "vagrant.actions.vm.check_box.not_found", - :name => box_name, - :provider => env[:machine].provider_name) - - begin - env[:action_runner].run(Vagrant::Action.action_box_add, { - :box_client_cert => box_download_client_cert, - :box_download_insecure => box_download_insecure, - :box_name => box_name, - :box_provider => box_formats, - :box_url => box_url, - }) - rescue Errors::BoxAlreadyExists - # Just ignore this, since it means the next part will succeed! - # This can happen in a multi-threaded environment. - end - end - end - - # Reload the environment and set the VM to be the new loaded VM. - env[:machine] = env[:machine].env.machine( - env[:machine].name, env[:machine].provider_name, true) + if env[:machine].box + @logger.info("Skipping HandleBoxUrl because box is already available") + @app.call(env) + return end + # Get a "big lock" to make sure that our more fine grained + # lock access is thread safe. + lock = nil + @@big_lock.synchronize do + lock = @@handle_box_url_locks[env[:machine].config.vm.box] + end + + box_name = env[:machine].config.vm.box + box_url = env[:machine].config.vm.box_url + box_download_client_cert = env[:machine].config.vm.box_download_client_cert + box_download_insecure = env[:machine].config.vm.box_download_insecure + + lock.synchronize do + # Check that we don't already have the box, which can happen + # if we're slow to acquire the lock because of another thread + box_formats = env[:machine].provider_options[:box_format] || + env[:machine].provider_name + if env[:box_collection].find(box_name, box_formats) + break + end + + # Add the box then reload the box collection so that it becomes + # aware of it. + env[:ui].info I18n.t( + "vagrant.actions.vm.check_box.not_found", + :name => box_name, + :provider => env[:machine].provider_name) + + begin + env[:action_runner].run(Vagrant::Action.action_box_add, { + :box_client_cert => box_download_client_cert, + :box_download_insecure => box_download_insecure, + :box_name => box_name, + :box_provider => box_formats, + :box_url => box_url, + }) + rescue Errors::BoxAlreadyExists + # Just ignore this, since it means the next part will succeed! + # This can happen in a multi-threaded environment. + end + end + + # Reload the environment and set the VM to be the new loaded VM. + env[:machine] = env[:machine].env.machine( + env[:machine].name, env[:machine].provider_name, true) + @app.call(env) end end