Environment#primary_machine_name and use it for with_target_vms

This makes the single-provider and default provider semantics work with
primary VMs.
This commit is contained in:
Mitchell Hashimoto 2012-12-30 11:12:56 -10:00
parent 24c3c9a7ae
commit 4c46091746
4 changed files with 59 additions and 18 deletions

View File

@ -289,21 +289,22 @@ module Vagrant
config_global.vm.defined_vm_keys.dup config_global.vm.defined_vm_keys.dup
end end
# Returns the primary VM associated with this environment. This # This returns the name of the machine that is the "primary." In the
# method is only applicable for multi-VM environments. This can # case of a single-machine environment, this is just the single machine
# potentially be nil if no primary VM is specified. # name. In the case of a multi-machine environment, then this can
# potentially be nil if no primary machine is specified.
# #
# @param [Symbol] provider The provider to back the primary machine. # @return [Symbol]
# @return [VM] def primary_machine_name
def primary_machine(provider) # If it is a single machine environment, then return the name
if machine_names.length == 1 return machine_names.first if machine_names.length == 1
return machine(machine_names[0], provider)
end
# If it is a multi-machine environment, then return the primary
config_global.vm.defined_vms.each do |name, subvm| config_global.vm.defined_vms.each do |name, subvm|
return machine(name, provider) if subvm.options[:primary] return name if subvm.options[:primary]
end end
# If no primary was specified, nil it is
nil nil
end end

View File

@ -167,9 +167,9 @@ module Vagrant
# Make sure we're only working with one VM if single target # Make sure we're only working with one VM if single target
if options[:single_target] && machines.length != 1 if options[:single_target] && machines.length != 1
@logger.debug("Using primary machine since single target") @logger.debug("Using primary machine since single target")
primary = @env.primary_machine(provider) primary_name = @env.primary_machine_name
raise Errors::MultiVMTargetRequired if !primary raise Errors::MultiVMTargetRequired if !primary_name
machines = [primary] machines = [get_machine.call(primary_name)]
end end
# If we asked for reversed ordering, then reverse it # If we asked for reversed ordering, then reverse it

View File

@ -205,9 +205,9 @@ describe Vagrant::Environment do
end end
end end
describe "primary machine" do describe "primary machine name" do
it "should be the only machine if not a multi-machine environment" do it "should be the only machine if not a multi-machine environment" do
instance.primary_machine(:dummy).name.should == instance.machine_names.first instance.primary_machine_name.should == instance.machine_names.first
end end
it "should be the machine marked as the primary" do it "should be the machine marked as the primary" do
@ -224,7 +224,24 @@ VF
end end
env = environment.create_vagrant_env env = environment.create_vagrant_env
env.primary_machine(:dummy).name.should == :bar env.primary_machine_name.should == :bar
end
it "should be nil if no primary is specified in a multi-machine environment" do
environment = isolated_environment do |env|
env.vagrantfile(<<-VF)
Vagrant.configure("2") do |config|
config.vm.box = "base"
config.vm.define :foo
config.vm.define :bar
end
VF
env.box2("base", :virtualbox)
end
env = environment.create_vagrant_env
env.primary_machine_name.should be_nil
end end
end end

View File

@ -171,11 +171,34 @@ describe Vagrant::Plugin::V2::Command do
end end
it "should use the primary machine with the active provider" do it "should use the primary machine with the active provider" do
pending name = :foo
provider = :vmware
vmware_vm = double("vmware_vm")
environment.stub(:active_machines => [[name, provider]])
environment.stub(:machine).with(name, provider).and_return(vmware_vm)
environment.stub(:machine_names => [])
environment.stub(:primary_machine_name => name)
vmware_vm.stub(:name => name, :provider => provider)
vms = []
instance.with_target_vms(nil, :single_target => true) { |vm| vms << vm }
vms.should == [vmware_vm]
end end
it "should use the primary machine with the default provider" do it "should use the primary machine with the default provider" do
pending name = :foo
machine = double("machine")
environment.stub(:active_machines => [])
environment.stub(:machine).with(name, default_provider).and_return(machine)
environment.stub(:machine_names => [])
environment.stub(:primary_machine_name => name)
machine.stub(:name => name, :provider => default_provider)
vms = []
instance.with_target_vms(nil, :single_target => true) { |vm| vms << machine }
vms.should == [machine]
end end
end end