core: HandleBox reloads the box

This commit is contained in:
Mitchell Hashimoto 2014-01-24 11:17:23 -08:00
parent f2c4adc4ee
commit b23ad41c40
2 changed files with 43 additions and 97 deletions

View File

@ -33,20 +33,17 @@ module Vagrant
end
lock.synchronize do
if !machine.config.vm.box_url
handle_metadata_box(env)
else
handle_direct_box(env)
end
handle_box(env)
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
def handle_direct_box(env)
end
def handle_metadata_box(env)
def handle_box(env)
machine = env[:machine]
if machine.box
@ -68,7 +65,8 @@ module Vagrant
begin
env[:action_runner].run(Vagrant::Action.action_box_add, env.merge({
box_url: machine.config.vm.box,
box_name: machine.config.vm.box,
box_url: machine.config.vm.box_url || machine.config.vm.box,
box_provider: box_formats,
box_version: machine.config.vm.box_version,
box_client_cert: box_download_client_cert,
@ -80,83 +78,6 @@ module Vagrant
# This can happen in a multi-threaded environment.
end
end
=begin
def call(env)
if !env[:machine].config.vm.box || !env[:machine].config.vm.box_url
@logger.info("Skipping HandleBoxUrl because box or box_url not set.")
@app.call(env)
return
end
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_ca_cert = env[:machine].config.vm.box_download_ca_cert
box_download_checksum = env[:machine].config.vm.box_download_checksum
box_download_checksum_type = env[:machine].config.vm.box_download_checksum_type
box_download_client_cert = env[:machine].config.vm.box_download_client_cert
box_download_insecure = env[:machine].config.vm.box_download_insecure
# Expand the CA cert file relative to the Vagrantfile path, if
# there is one.
if box_download_ca_cert
box_download_ca_cert = File.expand_path(
box_download_ca_cert, env[:machine].env.root_path)
end
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_checksum => box_download_checksum,
:box_checksum_type => box_download_checksum_type,
:box_client_cert => box_download_client_cert,
:box_download_ca_cert => box_download_ca_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
end
end
end

View File

@ -37,24 +37,25 @@ describe Vagrant::Action::Builtin::HandleBox do
subject.call(env)
end
context "without a box_url" do
it "doesn't do anything if a box exists" do
machine.stub(box: box)
action_runner.should_receive(:run).never
app.should_receive(:call).with(env)
subject.call(env)
end
context "with a box set and no box_url" do
before do
machine.stub(box: nil)
machine.config.vm.box = "foo"
end
it "doesn't do anything if a box exists" do
machine.stub(box: box)
action_runner.should_receive(:run).never
app.should_receive(:call).with(env)
subject.call(env)
end
it "adds a box that doesn't exist" do
action_runner.should_receive(:run).with do |action, opts|
expect(opts[:box_name]).to eq(machine.config.vm.box)
expect(opts[:box_url]).to eq(machine.config.vm.box)
expect(opts[:box_provider]).to eq(:dummy)
expect(opts[:box_version]).to eq(machine.config.vm.box_version)
@ -70,6 +71,7 @@ describe Vagrant::Action::Builtin::HandleBox do
machine.provider_options[:box_format] = [:foo, :bar]
action_runner.should_receive(:run).with do |action, opts|
expect(opts[:box_name]).to eq(machine.config.vm.box)
expect(opts[:box_url]).to eq(machine.config.vm.box)
expect(opts[:box_provider]).to eq([:foo, :bar])
expect(opts[:box_version]).to eq(machine.config.vm.box_version)
@ -81,4 +83,27 @@ describe Vagrant::Action::Builtin::HandleBox do
subject.call(env)
end
end
context "with a box and box_url set" do
before do
machine.stub(box: nil)
machine.config.vm.box = "foo"
machine.config.vm.box_url = "bar"
end
it "adds a box that doesn't exist" do
action_runner.should_receive(:run).with do |action, opts|
expect(opts[:box_name]).to eq(machine.config.vm.box)
expect(opts[:box_url]).to eq(machine.config.vm.box_url)
expect(opts[:box_provider]).to eq(:dummy)
expect(opts[:box_version]).to eq(machine.config.vm.box_version)
true
end
app.should_receive(:call).with(env)
subject.call(env)
end
end
end