Better handling of merging sub-vms

This commit is contained in:
Mitchell Hashimoto 2013-03-08 21:23:04 -08:00
parent cd36088d2f
commit ad0e6deb3e
2 changed files with 42 additions and 7 deletions

View File

@ -32,6 +32,8 @@ module VagrantPlugins
# Internal state
@__compiled_provider_configs = {}
@__defined_vm_keys = []
@__defined_vms = {}
@__finalized = false
@__networks = {}
@__providers = {}
@ -46,6 +48,32 @@ module VagrantPlugins
result.instance_variable_set(:@synced_folders, @synced_folders.merge(other.synced_folders))
result.instance_variable_set(:@provisioners, @provisioners + other.provisioners)
# Merge defined VMs by first merging the defined VM keys,
# preserving the order in which they were defined.
other_defined_vm_keys = other.instance_variable_get(:@__defined_vm_keys)
other_defined_vm_keys -= @__defined_vm_keys
new_defined_vm_keys = @__defined_vm_keys + other_defined_vm_keys
result.instance_variable_set(:@__defined_vm_keys, new_defined_vm_keys)
# Merge the actual defined VMs.
other_defined_vms = other.instance_variable_get(:@__defined_vms)
new_defined_vms = {}
@__defined_vms.each do |key, subvm|
new_defined_vms[key] = subvm.clone
end
other_defined_vms.each do |key, subvm|
if !new_defined_vms.has_key?(key)
new_defined_vms[key] = subvm.clone
else
new_defined_vms[key].config_procs.concat(subvm.config_procs)
new_defined_vms[key].options.merge!(subvm.options)
end
end
result.instance_variable_set(:@__defined_vms, new_defined_vms)
# Merge the providers by prepending any configuration blocks we
# have for providers onto the new configuration.
other_providers = other.instance_variable_get(:@__providers)
@ -125,13 +153,13 @@ module VagrantPlugins
end
def defined_vms
@defined_vms ||= {}
@__defined_vms
end
# This returns the keys of the sub-vms in the order they were
# defined.
def defined_vm_keys
@defined_vm_keys ||= []
@__defined_vm_keys
end
def define(name, options=nil, &block)
@ -141,15 +169,15 @@ module VagrantPlugins
# Add the name to the array of VM keys. This array is used to
# preserve the order in which VMs are defined.
defined_vm_keys << name
@__defined_vm_keys << name
# Add the SubVM to the hash of defined VMs
if !defined_vms[name]
defined_vms[name] ||= VagrantConfigSubVM.new
if !@__defined_vms[name]
@__defined_vms[name] = VagrantConfigSubVM.new
end
defined_vms[name].options.merge!(options)
defined_vms[name].config_procs << [options[:config_version], block] if block
@__defined_vms[name].options.merge!(options)
@__defined_vms[name].config_procs << [options[:config_version], block] if block
end
#-------------------------------------------------------------------

View File

@ -18,6 +18,13 @@ module VagrantPlugins
@config_procs = []
@options = {}
end
def initialize_copy(other)
super
@config_procs = other.config_procs.clone
@options = other.options.clone
end
end
end
end