Support refreshing the machine cache

This commit is contained in:
Mitchell Hashimoto 2013-01-30 10:37:40 -08:00
parent 64042a5d35
commit 7eec127704
3 changed files with 33 additions and 2 deletions

View File

@ -217,14 +217,21 @@ module Vagrant
# Vagrantfile).
# @param [Symbol] provider The provider that this machine should be
# backed by.
# @param [Boolean] refresh If true, then if there is a cached version
# it is reloaded.
# @return [Machine]
def machine(name, provider)
def machine(name, provider, refresh=false)
@logger.info("Getting machine: #{name} (#{provider})")
# Compose the cache key of the name and provider, and return from
# the cache if we have that.
cache_key = [name, provider]
@machines ||= {}
if refresh
@logger.info("Refreshing machine (busting cache): #{name} (#{provider})")
@machines.delete(cache_key)
end
if @machines.has_key?(cache_key)
@logger.info("Returning cached machine: #{name} (#{provider})")
return @machines[cache_key]

View File

@ -25,7 +25,7 @@ module VagrantPlugins
# 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)
env[:machine].name, env[:machine].provider_name, true)
end
@app.call(env)

View File

@ -454,6 +454,30 @@ VF
machine.config.ssh.port.should == 100
end
it "should reload the cache if refresh is set" do
# Create a provider
foo_provider = register_provider("foo")
# Create the configuration
isolated_env = isolated_environment do |e|
e.vagrantfile(<<-VF)
Vagrant.configure("2") do |config|
config.vm.box = "base"
end
VF
e.box2("base", :foo)
end
env = isolated_env.create_vagrant_env
vm1 = env.machine(:default, :foo)
vm2 = env.machine(:default, :foo, true)
vm3 = env.machine(:default, :foo)
vm1.should_not eql(vm2)
vm2.should eql(vm3)
end
it "should raise an error if the VM is not found" do
expect { instance.machine("i-definitely-dont-exist", :virtualbox) }.
to raise_error(Vagrant::Errors::MachineNotFound)