kernel/v2: support overriding provisioner settings

This commit is contained in:
Mitchell Hashimoto 2014-02-03 16:56:39 +01:00
parent debc14f883
commit 10d5416a90
3 changed files with 52 additions and 10 deletions

View File

@ -221,8 +221,21 @@ module VagrantPlugins
end
end
def provision(name, options=nil, &block)
@provisioners << VagrantConfigProvisioner.new(name.to_sym, options, &block)
def provision(name, **options, &block)
options[:id] = options[:id].to_s if options[:id]
prov = nil
if options[:id]
prov = @provisioners.find { |p| p.id == options[:id] }
end
if !prov
prov = VagrantConfigProvisioner.new(options[:id], name.to_sym)
@provisioners << prov
end
prov.add_config(options, &block)
nil
end
def defined_vms

View File

@ -4,6 +4,11 @@ module VagrantPlugins
module Kernel_V2
# Represents a single configured provisioner for a VM.
class VagrantConfigProvisioner
# Unique ID name for this provisioner
#
# @return [String]
attr_reader :id
# The name of the provisioner that should be registered
# as a plugin.
#
@ -15,11 +20,12 @@ module VagrantPlugins
# @return [Object]
attr_reader :config
def initialize(name, options=nil, &block)
def initialize(id, name)
@logger = Log4r::Logger.new("vagrant::config::vm::provisioner")
@logger.debug("Provisioner defined: #{name}")
@config = nil
@id = id
@invalid = false
@name = name
@ -31,15 +37,27 @@ module VagrantPlugins
# Attempt to find the configuration class for this provider
# if it exists and load the configuration.
config_class = Vagrant.plugin("2").manager.provisioner_configs[@name]
if !config_class
@logger.info("Provisioner config for '#{@name}' not found. Ignoring config.")
return
@config_class = Vagrant.plugin("2").manager.
provisioner_configs[@name]
if !@config_class
@logger.info(
"Provisioner config for '#{@name}' not found. Ignoring config.")
end
end
@config = config_class.new
@config.set_options(options) if options
block.call(@config) if block
def add_config(**options, &block)
return if invalid?
current = @config_class.new
current.set_options(options) if options
current.call(@config) if block
current = @config.merge(current) if @config
@config = current
end
def finalize!
return if invalid?
@config.finalize!
end

View File

@ -17,6 +17,17 @@ describe VagrantPlugins::Kernel_V2::VMConfig do
expect(r[1].config.inline).to eql("bar")
end
it "allows provisioner settings to be overriden" do
subject.provision("shell", path: "foo", inline: "foo", id: "s")
subject.provision("shell", inline: "bar", id: "s")
subject.finalize!
r = subject.provisioners
expect(r.length).to eql(1)
expect(r[0].config.inline).to eql("bar")
expect(r[0].config.path).to eql("foo")
end
it "marks as invalid if a bad name" do
subject.provision("nope", inline: "foo")
subject.finalize!