Revamp the configuration internal state for defining provisioners

This commit is contained in:
Mitchell Hashimoto 2013-01-13 16:02:48 -08:00
parent c8053c00a4
commit cf2cca3b7c
3 changed files with 33 additions and 42 deletions

View File

@ -91,6 +91,17 @@ module Vagrant
end end
end end
# This returns all the config classes for the various provisioners.
#
# @return [Registry]
def provisioner_configs
Registry.new.tap do |result|
@registered.each do |plugin|
result.merge!(plugin.components.configs[:provisioner])
end
end
end
# This returns all registered provisioners. # This returns all registered provisioners.
# #
# @return [Hash] # @return [Hash]

View File

@ -142,11 +142,6 @@ module VagrantPlugins
:name => name)) :name => name))
end end
end end
# Each provisioner can validate itself
provisioners.each do |prov|
prov.validate(env, errors)
end
end end
end end
end end

View File

@ -4,51 +4,36 @@ module VagrantPlugins
module Kernel_V2 module Kernel_V2
# Represents a single configured provisioner for a VM. # Represents a single configured provisioner for a VM.
class VagrantConfigProvisioner class VagrantConfigProvisioner
attr_reader :shortcut # The name of the provisioner that should be registered
attr_reader :provisioner # as a plugin.
#
# @return [Symbol]
attr_reader :name
# The configuration associated with the provisioner, if there is any.
#
# @return [Object]
attr_reader :config attr_reader :config
def initialize(shortcut, options=nil, &block) def initialize(name, options=nil, &block)
@logger = Log4r::Logger.new("vagrant::config::vm::provisioner") @logger = Log4r::Logger.new("vagrant::config::vm::provisioner")
@logger.debug("Provisioner config: #{shortcut}") @logger.debug("Provisioner defined: #{name}")
@shortcut = shortcut
@provisioner = shortcut
@config = nil @config = nil
@name = name
# If the shorcut is a symbol, we look through the registered # Attempt to find the configuration class for this provider
# plugins to see if any define a provisioner we want. # if it exists and load the configuration.
if shortcut.is_a?(Symbol) config_class = Vagrant.plugin("2").manager.provisioner_configs[@name]
@provisioner = Vagrant.plugin("2").manager.provisioners[shortcut] if !config_class
end @logger.info("Provisioner config for '#{@name}' not found. Ignoring config.")
@logger.info("Provisioner class: #{provisioner}")
configure(options, &block) if @provisioner
end
# Configures the provisioner if it can (if it is valid).
def configure(options=nil, &block)
config_class = @provisioner.config_class
return if !config_class
@logger.debug("Configuring provisioner with: #{config_class}")
@config = config_class.new
@config.set_options(options) if options
block.call(@config) if block
end
def validate(env, errors)
if !provisioner
# If we don't have a provisioner then the whole thing is invalid.
errors.add(I18n.t("vagrant.config.vm.provisioner_not_found", :shortcut => shortcut))
return return
end end
if !(provisioner <= Vagrant.plugin("2", :provisioner)) @config = config_class.new
errors.add(I18n.t("vagrant.config.vm.provisioner_invalid_class", :shortcut => shortcut)) @config.set_options(options) if options
end block.call(@config) if block
@config.finalize!
# Pass on validation to the provisioner config
config.validate(env, errors) if config
end end
end end
end end