Get rid of the old Environment#vms calls. Use #machine everywhere.

This commit is contained in:
Mitchell Hashimoto 2012-11-08 19:14:42 -08:00
parent 5adcb0fc43
commit 1559f7b7a7
3 changed files with 30 additions and 71 deletions

View File

@ -170,47 +170,23 @@ module Vagrant
config.vms
end
# Returns the VMs associated with this environment.
#
# @return [Hash<Symbol,VM>]
def vms
load! if !loaded?
@vms ||= load_vms!
end
# Returns the VMs associated with this environment, in the order
# that they were defined.
#
# @return [Array<VM>]
def vms_ordered
return @vms.values if !multivm?
@vms_enum ||= config.global.vm.defined_vm_keys.map { |name| @vms[name] }
end
# Returns the primary VM associated with this environment. This
# method is only applicable for multi-VM environments. This can
# potentially be nil if no primary VM is specified.
#
# @return [VM]
def primary_vm
return vms.values.first if !multivm?
def primary_machine
if machine_names.length == 1
return machine(machine_names[0], :virtualbox)
end
config.global.vm.defined_vms.each do |name, subvm|
return vms[name] if subvm.options[:primary]
return machine(name, :virtualbox) if subvm.options[:primary]
end
nil
end
# Returns a boolean whether this environment represents a multi-VM
# environment or not. This will work even when called on child
# environments.
#
# @return [Bool]
def multivm?
vms.length > 1
end
# Makes a call to the CLI with the given arguments as if they
# came from the real command line (sometimes they do!). An example:
#
@ -472,25 +448,6 @@ module Vagrant
@config = Config::Container.new(global, vm_configs)
end
# Loads the persisted VM (if it exists) for this environment.
def load_vms!
# This is hardcoded for now.
provider = Vagrant.plugin("2").manager.providers[:virtualbox]
raise "VirtualBox provider not found." if !provider
# Load all the virtual machine instances.
result = {}
config.vms.each do |name|
vm_config = config.for_vm(name)
box = boxes.find(vm_config.vm.box, :virtualbox)
result[name] = Vagrant::Machine.new(name, provider, vm_config, box, self)
end
result
end
# This sets the `@home_path` variable properly.
#
# @return [Pathname]

View File

@ -75,7 +75,7 @@ module Vagrant
names = [names] if !names.is_a?(Array)
# First determine the proper array of VMs.
vms = []
machines = []
if names.length > 0
names.each do |name|
if pattern = name[/^\/(.+?)\/$/, 1]
@ -83,38 +83,40 @@ module Vagrant
# expression and allow that sort of matching.
regex = Regexp.new(pattern)
@env.vms.each do |vm_name, vm|
vms << vm if vm_name =~ regex
@env.machine_names.each do |machine_name|
if machine_name =~ regex
machines << @env.machine(machine_name, :virtualbox)
end
end
raise Errors::VMNoMatchError if vms.empty?
raise Errors::VMNoMatchError if machines.empty?
else
# String name, just look for a specific VM
vms << @env.vms[name.to_sym]
raise Errors::VMNotFoundError, :name => name if !vms[0]
machines << @env.machine(name.to_sym, :virtualbox)
raise Errors::VMNotFoundError, :name => name if !machines[0]
end
end
else
vms = @env.vms_ordered
# No name was given, so we return every VM in the order
# configured.
machines = @env.machine_names.map do |machine_name|
@env.machine(machine_name, :virtualbox)
end
end
# Make sure we're only working with one VM if single target
if options[:single_target] && vms.length != 1
vm = @env.primary_vm
raise Errors::MultiVMTargetRequired if !vm
vms = [vm]
if options[:single_target] && machines.length != 1
primary = @env.primary_machine
raise Errors::MultiVMTargetRequired if !primary
machines = [primary]
end
# If we asked for reversed ordering, then reverse it
vms.reverse! if options[:reverse]
machines.reverse! if options[:reverse]
# Go through each VM and yield it!
vms.each do |old_vm|
# We get a new VM from the environment here to avoid potentially
# stale VMs (if there was a config reload on the environment
# or something).
vm = @env.vms[old_vm.name]
yield vm
machines.each do |machine|
yield machine
end
end

View File

@ -103,12 +103,12 @@ describe Vagrant::Environment do
end
end
describe "primary VM" do
it "should be the only VM if not a multi-VM environment" do
instance.primary_vm.should == instance.vms.values.first
describe "primary machine" do
it "should be the only machine if not a multi-machine environment" do
instance.primary_machine.name.should == instance.machine_names.first
end
it "should be the VM marked as the primary" do
it "should be the machine marked as the primary" do
environment = isolated_environment do |env|
env.vagrantfile(<<-VF)
Vagrant.configure("2") do |config|
@ -120,7 +120,7 @@ VF
end
env = environment.create_vagrant_env
env.primary_vm.should == env.vms[:bar]
env.primary_machine.name.should == :bar
end
end