Refactor the provider config a bit to allow multiple blocks
This commit is contained in:
parent
661ccef6e2
commit
2eeccf085b
|
@ -36,7 +36,7 @@ module VagrantPlugins
|
|||
|
||||
# The providers hash defaults any key to a provider object
|
||||
@providers = Hash.new do |hash, key|
|
||||
hash[key] = VagrantConfigProvider.new(key, nil)
|
||||
hash[key] = VagrantConfigProvider.new(key)
|
||||
end
|
||||
end
|
||||
|
||||
|
@ -94,8 +94,7 @@ module VagrantPlugins
|
|||
#
|
||||
# @param [Symbol] name The name of the provider.
|
||||
def provider(name, &block)
|
||||
# TODO: Error if a provider is defined multiple times.
|
||||
@providers[name] = VagrantConfigProvider.new(name, block)
|
||||
@providers[name].add_config_block(block) if block_given?
|
||||
end
|
||||
|
||||
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
|
||||
# default VM which just inherits the rest of the configuration.
|
||||
define(DEFAULT_VM_NAME) if defined_vm_keys.empty?
|
||||
|
||||
# Compile all the provider configurations
|
||||
@providers.each do |name, config|
|
||||
config.finalize!
|
||||
end
|
||||
end
|
||||
|
||||
def validate(machine)
|
||||
|
|
|
@ -7,31 +7,53 @@ module VagrantPlugins
|
|||
# Represents a single configured provider for a VM. This may or may
|
||||
# not be a valid provider. Validation is deferred until later.
|
||||
class VagrantConfigProvider
|
||||
# This is the name of the provider, as a symbol.
|
||||
#
|
||||
# @return [Symbol]
|
||||
attr_reader :name
|
||||
|
||||
# The compiled configuration. This is only available after finalizing.
|
||||
#
|
||||
# @return [Object]
|
||||
attr_reader :config
|
||||
|
||||
# Initializes a new provider configuration for a VM. This should
|
||||
# only be instantiated internally by calling `config.vm.provider`.
|
||||
#
|
||||
# @param [Symbol] name The name of the provider that is registered.
|
||||
def initialize(name, block)
|
||||
def initialize(name)
|
||||
@name = name
|
||||
@config = nil
|
||||
@config_blocks = []
|
||||
@logger = Log4r::Logger.new("vagrant::config::vm::provider")
|
||||
|
||||
# Attempt to find the configuration class for this provider and
|
||||
# load the configuration.
|
||||
config_class = Vagrant.plugin("2").manager.provider_configs[@name]
|
||||
if !config_class
|
||||
@config_class = Vagrant.plugin("2").manager.provider_configs[@name]
|
||||
if !@config_class
|
||||
@logger.info("Provider config for #{@name} not found, ignoring config.")
|
||||
return
|
||||
end
|
||||
end
|
||||
|
||||
@logger.info("Configuring provider #{@name} with #{config_class}")
|
||||
@config = config_class.new
|
||||
block.call(@config) if block
|
||||
# This adds a configuration block to the list of configuration
|
||||
# blocks to execute when compiling the configuration.
|
||||
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!
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
Loading…
Reference in New Issue