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 config.vms
end 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 # Returns the primary VM associated with this environment. This
# method is only applicable for multi-VM environments. This can # method is only applicable for multi-VM environments. This can
# potentially be nil if no primary VM is specified. # potentially be nil if no primary VM is specified.
# #
# @return [VM] # @return [VM]
def primary_vm def primary_machine
return vms.values.first if !multivm? if machine_names.length == 1
return machine(machine_names[0], :virtualbox)
end
config.global.vm.defined_vms.each do |name, subvm| 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 end
nil nil
end 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 # Makes a call to the CLI with the given arguments as if they
# came from the real command line (sometimes they do!). An example: # came from the real command line (sometimes they do!). An example:
# #
@ -472,25 +448,6 @@ module Vagrant
@config = Config::Container.new(global, vm_configs) @config = Config::Container.new(global, vm_configs)
end 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. # This sets the `@home_path` variable properly.
# #
# @return [Pathname] # @return [Pathname]

View File

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

View File

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