Refactor the provider config a bit to allow multiple blocks

This commit is contained in:
Mitchell Hashimoto 2013-02-05 12:08:17 -08:00
parent 661ccef6e2
commit 2eeccf085b
2 changed files with 37 additions and 11 deletions

View File

@ -36,7 +36,7 @@ module VagrantPlugins
# The providers hash defaults any key to a provider object # The providers hash defaults any key to a provider object
@providers = Hash.new do |hash, key| @providers = Hash.new do |hash, key|
hash[key] = VagrantConfigProvider.new(key, nil) hash[key] = VagrantConfigProvider.new(key)
end end
end end
@ -94,8 +94,7 @@ module VagrantPlugins
# #
# @param [Symbol] name The name of the provider. # @param [Symbol] name The name of the provider.
def provider(name, &block) def provider(name, &block)
# TODO: Error if a provider is defined multiple times. @providers[name].add_config_block(block) if block_given?
@providers[name] = VagrantConfigProvider.new(name, block)
end end
def provision(name, options=nil, &block) def provision(name, options=nil, &block)
@ -134,6 +133,11 @@ module VagrantPlugins
# If we haven't defined a single VM, then we need to define a # If we haven't defined a single VM, then we need to define a
# default VM which just inherits the rest of the configuration. # default VM which just inherits the rest of the configuration.
define(DEFAULT_VM_NAME) if defined_vm_keys.empty? define(DEFAULT_VM_NAME) if defined_vm_keys.empty?
# Compile all the provider configurations
@providers.each do |name, config|
config.finalize!
end
end end
def validate(machine) def validate(machine)

View File

@ -7,31 +7,53 @@ module VagrantPlugins
# Represents a single configured provider for a VM. This may or may # Represents a single configured provider for a VM. This may or may
# not be a valid provider. Validation is deferred until later. # not be a valid provider. Validation is deferred until later.
class VagrantConfigProvider class VagrantConfigProvider
# This is the name of the provider, as a symbol.
#
# @return [Symbol]
attr_reader :name attr_reader :name
# The compiled configuration. This is only available after finalizing.
#
# @return [Object]
attr_reader :config attr_reader :config
# Initializes a new provider configuration for a VM. This should # Initializes a new provider configuration for a VM. This should
# only be instantiated internally by calling `config.vm.provider`. # only be instantiated internally by calling `config.vm.provider`.
# #
# @param [Symbol] name The name of the provider that is registered. # @param [Symbol] name The name of the provider that is registered.
def initialize(name, block) def initialize(name)
@name = name @name = name
@config = nil @config = nil
@config_blocks = []
@logger = Log4r::Logger.new("vagrant::config::vm::provider") @logger = Log4r::Logger.new("vagrant::config::vm::provider")
# Attempt to find the configuration class for this provider and # Attempt to find the configuration class for this provider and
# load the configuration. # load the configuration.
config_class = Vagrant.plugin("2").manager.provider_configs[@name] @config_class = Vagrant.plugin("2").manager.provider_configs[@name]
if !config_class if !@config_class
@logger.info("Provider config for #{@name} not found, ignoring config.") @logger.info("Provider config for #{@name} not found, ignoring config.")
return end
end end
@logger.info("Configuring provider #{@name} with #{config_class}") # This adds a configuration block to the list of configuration
@config = config_class.new # blocks to execute when compiling the configuration.
block.call(@config) if block def add_config_block(block)
@config_blocks << block
end
# This is called to compile the configuration
def finalize!
if @config_class
@logger.info("Configuring provider #{@name} with #{@config_class}")
# Call each block in order with the config object
@config = @config_class.new
@config_blocks.each { |b| b.call(@config) }
# Finalize the configuration
@config.finalize! @config.finalize!
end end
end end
end end
end
end end