From 7a093340bf369fc33373c8d6d867d57283280388 Mon Sep 17 00:00:00 2001 From: Mitchell Hashimoto Date: Thu, 3 Jun 2010 19:20:15 -0700 Subject: [PATCH] VM definition blocks are now stackable. [closes GH-94] --- lib/vagrant/config.rb | 14 +++++++++++++- lib/vagrant/environment.rb | 11 +++++++---- test/vagrant/config_test.rb | 4 ++-- 3 files changed, 22 insertions(+), 7 deletions(-) diff --git a/lib/vagrant/config.rb b/lib/vagrant/config.rb index 577ebafde..08721c297 100644 --- a/lib/vagrant/config.rb +++ b/lib/vagrant/config.rb @@ -98,6 +98,16 @@ module Vagrant attr_accessor :shared_folder_gid attr_accessor :system + # Represents a SubVM. This class is only used here in the VMs + # hash. + class SubVM + include Util::StackedProcRunner + + def options + @options ||= {} + end + end + def initialize @forwarded_ports = {} @shared_folders = {} @@ -170,7 +180,9 @@ module Vagrant def define(name, options=nil, &block) options ||= {} - defined_vms[name.to_sym] = options.merge({:config_proc => block}) + defined_vms[name.to_sym] ||= SubVM.new + defined_vms[name.to_sym].options.merge!(options) + defined_vms[name.to_sym].push_proc(&block) end def shift(orig, rsync) diff --git a/lib/vagrant/environment.rb b/lib/vagrant/environment.rb index f6dbe1377..7a931d6ec 100644 --- a/lib/vagrant/environment.rb +++ b/lib/vagrant/environment.rb @@ -106,8 +106,8 @@ module Vagrant return vms.values.first if !multivm? return parent.primary_vm if parent - config.vm.defined_vms.each do |name, options| - return vms[name] if options[:primary] + config.vm.defined_vms.each do |name, subvm| + return vms[name] if subvm.options[:primary] end nil @@ -178,10 +178,13 @@ module Vagrant # If this environment represents some VM in a multi-VM environment, # we push that VM's configuration onto the config_queue. if vm_name - vm_data = parent.config.vm.defined_vms[vm_name] || {} - config_queue << vm_data[:config_proc] + subvm = parent.config.vm.defined_vms[vm_name] + config_queue << subvm.proc_stack if subvm end + # Flatten the config queue so any nested procs are flattened + config_queue.flatten! + # Clear out the old data Config.reset!(self) diff --git a/test/vagrant/config_test.rb b/test/vagrant/config_test.rb index a4ffc6d5d..866cf3247 100644 --- a/test/vagrant/config_test.rb +++ b/test/vagrant/config_test.rb @@ -232,12 +232,12 @@ class ConfigTest < Test::Unit::TestCase proc = Proc.new { foo.call } @config.define(:name, &proc) - assert_equal proc, @config.defined_vms[:name][:config_proc] + assert @config.defined_vms[:name].proc_stack.include?(proc) end should "store the options" do @config.define(:name, :set => true) - assert @config.defined_vms[:name][:set] + assert @config.defined_vms[:name].options[:set] end should "not have multi-VMs by default" do