Better locking within handle_box_url
This improves locking in the face of parallel providers and handling box_url parameters. This avoids downloading a box multiple times.
This commit is contained in:
parent
c29e4d6334
commit
14d70776ca
|
@ -1,3 +1,5 @@
|
||||||
|
require "thread"
|
||||||
|
|
||||||
module Vagrant
|
module Vagrant
|
||||||
module Action
|
module Action
|
||||||
module Builtin
|
module Builtin
|
||||||
|
@ -6,34 +8,60 @@ module Vagrant
|
||||||
# sequence for a provider after configuration validation but before
|
# sequence for a provider after configuration validation but before
|
||||||
# you attempt to use any box.
|
# you attempt to use any box.
|
||||||
class HandleBoxUrl
|
class HandleBoxUrl
|
||||||
|
@@big_lock = Mutex.new
|
||||||
|
@@handle_box_url_locks = Hash.new { |h,k| h[k] = Mutex.new }
|
||||||
|
|
||||||
def initialize(app, env)
|
def initialize(app, env)
|
||||||
@app = app
|
@app = app
|
||||||
end
|
end
|
||||||
|
|
||||||
def call(env)
|
def call(env)
|
||||||
if !env[:machine].box
|
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
|
# We can assume a box URL is set because the Vagrantfile
|
||||||
# validation should do this for us. If not, though, we do
|
# validation should do this for us. If not, though, we do
|
||||||
# raise a terrible runtime error.
|
# raise a terrible runtime error.
|
||||||
box_name = env[:machine].config.vm.box
|
box_name = env[:machine].config.vm.box
|
||||||
box_url = env[:machine].config.vm.box_url
|
box_url = env[:machine].config.vm.box_url
|
||||||
|
|
||||||
# Add the box then reload the box collection so that it becomes
|
lock.synchronize do
|
||||||
# aware of it.
|
# First see if we actually have the box now.
|
||||||
env[:ui].info I18n.t(
|
has_box = false
|
||||||
"vagrant.actions.vm.check_box.not_found",
|
|
||||||
:name => box_name,
|
|
||||||
:provider => env[:machine].provider_name)
|
|
||||||
|
|
||||||
begin
|
formats = env[:machine].provider_options[:box_format] ||
|
||||||
env[:action_runner].run(Vagrant::Action.action_box_add, {
|
env[:machine].provider_name
|
||||||
:box_name => box_name,
|
[formats].flatten.each do |format|
|
||||||
:box_provider => env[:machine].provider_name,
|
if env[:box_collection].find(box_name, format)
|
||||||
:box_url => box_url
|
has_box = true
|
||||||
})
|
break
|
||||||
rescue Errors::BoxAlreadyExists
|
end
|
||||||
# Just ignore this, since it means the next part will succeed!
|
end
|
||||||
# This can happen in a multi-threaded environment.
|
|
||||||
|
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_name => box_name,
|
||||||
|
:box_provider => env[:machine].provider_name,
|
||||||
|
: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
|
end
|
||||||
|
|
||||||
# Reload the environment and set the VM to be the new loaded VM.
|
# Reload the environment and set the VM to be the new loaded VM.
|
||||||
|
|
Loading…
Reference in New Issue