diff --git a/lib/vagrant/command/helpers.rb b/lib/vagrant/command/helpers.rb index d5af59d4a..6fb9dadd2 100644 --- a/lib/vagrant/command/helpers.rb +++ b/lib/vagrant/command/helpers.rb @@ -17,7 +17,7 @@ module Vagrant @target_vms ||= begin if env.multivm? - return env.vms.values if !name + return env.vms_ordered if !name vm = env.vms[name.to_sym] raise Errors::VMNotFoundError.new(:name => name) if !vm else diff --git a/lib/vagrant/config/vm.rb b/lib/vagrant/config/vm.rb index 2a9374e9c..8a4b9e249 100644 --- a/lib/vagrant/config/vm.rb +++ b/lib/vagrant/config/vm.rb @@ -88,8 +88,15 @@ module Vagrant @defined_vms ||= {} end + # This returns the keys of the sub-vms in the order they were + # defined. + def defined_vm_keys + @defined_vm_keys ||= [] + end + def define(name, options=nil, &block) options ||= {} + defined_vm_keys << name defined_vms[name.to_sym] ||= SubVM.new defined_vms[name.to_sym].options.merge!(options) defined_vms[name.to_sym].push_proc(&block) diff --git a/lib/vagrant/environment.rb b/lib/vagrant/environment.rb index 40824c43a..f3fe5c6e5 100644 --- a/lib/vagrant/environment.rb +++ b/lib/vagrant/environment.rb @@ -126,13 +126,21 @@ module Vagrant # Returns the VMs associated with this environment. # - # @return [Array] + # @return [Hash] def vms return parent.vms if parent load! if !loaded? @vms ||= load_vms! end + # Returns the VMs associated with this environment, in the order + # that they were defined. + # + # @return [Array] + def vms_ordered + @vms_enum ||= config.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. @@ -352,7 +360,7 @@ module Vagrant # For any VMs which aren't created, create a blank VM instance for # them - all_keys = config.vm.defined_vms.keys + all_keys = config.vm.defined_vm_keys all_keys = [DEFAULT_VM] if all_keys.empty? all_keys.each do |name| result[name] = Vagrant::VM.new(:name => name, :env => self) if !result.has_key?(name) diff --git a/test/vagrant/command/helpers_test.rb b/test/vagrant/command/helpers_test.rb index 9b6350361..8f9a5ea0a 100644 --- a/test/vagrant/command/helpers_test.rb +++ b/test/vagrant/command/helpers_test.rb @@ -39,7 +39,7 @@ class CommandHelpersTest < Test::Unit::TestCase context "without multivm" do setup do - @env.stubs(:vms).returns({ :one => 1, :two => 2 }) + @env.stubs(:vms_ordered => [1, 2], :vms => {:one => 1, :two => 2}) end should "raise an exception if a name is specified" do @@ -59,7 +59,7 @@ class CommandHelpersTest < Test::Unit::TestCase context "with multivm" do setup do - @env.stubs(:vms).returns(:one => 1, :two => 2) + @env.stubs(:vms_ordered => [1, 2], :vms => {:one => 1, :two => 2}) end should "return all the VMs if no name is specified" do diff --git a/test/vagrant/config/vm_test.rb b/test/vagrant/config/vm_test.rb index d9f304486..0033ae4e1 100644 --- a/test/vagrant/config/vm_test.rb +++ b/test/vagrant/config/vm_test.rb @@ -32,6 +32,14 @@ class ConfigVMTest < Test::Unit::TestCase @config.define(:foo) {} assert @config.has_multi_vms? end + + should "retain vm definition order" do + @config.define(:a) {} + @config.define(:b) {} + @config.define(:c) {} + + assert_equal [:a, :b, :c], @config.defined_vm_keys + end end context "customizing" do