Update provisioner enhancements from pull request feedback

This commit is contained in:
Brian Cain 2019-08-29 13:50:22 -07:00
parent 07bcfc6077
commit 7b0dc8d528
No known key found for this signature in database
GPG Key ID: 9FC4639B2E4510A0
4 changed files with 34 additions and 16 deletions

View File

@ -50,7 +50,7 @@ module Vagrant
final_provs = []
root_provs = []
# extract root provisioners
root_provs = pvs.map { |p,o| [p,o] if o[:before].nil? && o[:after].nil? }.reject(&:nil?)
root_provs = pvs.find_all { |_, o| o[:before].nil? && o[:after].nil? }
if root_provs.size == pvs.size
# no dependencies found
@ -63,21 +63,21 @@ module Vagrant
all_provs = []
# extract dependency provisioners
dep_provs = pvs.map { |p,o| [p,o] if (!o[:before].nil? && !o[:before].is_a?(Symbol)) || (!o[:after].nil? && !o[:after].is_a?(Symbol)) }.reject(&:nil?)
dep_provs = pvs.find_all { |_, o| o[:before].is_a?(String) || o[:after].is_a?(String) }
# extract each provisioners
each_provs = pvs.map { |p,o| [p,o] if o[:before] == :each || o[:after] == :each }.reject(&:nil?)
each_provs = pvs.find_all { |_,o| o[:before] == :each || o[:after] == :each }
# extract all provisioners
all_provs = pvs.map { |p,o| [p,o] if o[:before] == :all || o[:after] == :all }.reject(&:nil?)
all_provs = pvs.find_all { |_,o| o[:before] == :all || o[:after] == :all }
# insert provisioners in order
final_provs = root_provs
dep_provs.each do |p,options|
idx = 0
if options[:before]
idx = final_provs.each_with_index.map { |(p,o), i| i if o[:name].to_s == options[:before] }.reject(&:nil?).first
idx = final_provs.index { |_, o| o[:name].to_s == options[:before] }
final_provs.insert(idx, [p, options])
elsif options[:after]
idx = final_provs.each_with_index.map { |(p,o), i| i if o[:name].to_s == options[:after] }.reject(&:nil?).first
idx = final_provs.index { |_, o| o[:name].to_s == options[:after] }
idx += 1
final_provs.insert(idx, [p, options])
end
@ -85,7 +85,7 @@ module Vagrant
# Add :each and :all provisioners in reverse to preserve order in Vagrantfile
tmp_final_provs = []
final_provs.each_with_index.map do |(prv,o), i|
final_provs.each_with_index do |(prv,o), i|
tmp_before = []
tmp_after = []

View File

@ -340,7 +340,8 @@ module VagrantPlugins
end
if Vagrant::Util::Experimental.feature_enabled?("dependency_provisioners")
prov = VagrantConfigProvisioner.new(name, type.to_sym, before, after)
opts = {before: before, after: after}
prov = VagrantConfigProvisioner.new(name, type.to_sym, opts)
else
prov = VagrantConfigProvisioner.new(name, type.to_sym)
end

View File

@ -53,7 +53,7 @@ module VagrantPlugins
# @return [String, Symbol]
attr_accessor :after
def initialize(name, type, before=nil, after=nil)
def initialize(name, type, **options)
@logger = Log4r::Logger.new("vagrant::config::vm::provisioner")
@logger.debug("Provisioner defined: #{name}")
@ -64,8 +64,8 @@ module VagrantPlugins
@preserve_order = false
@run = nil
@type = type
@before = before
@after = after
@before = options[:before]
@after = options[:after]
# Attempt to find the provisioner...
if !Vagrant.plugin("2").manager.provisioners[type]
@ -113,7 +113,7 @@ module VagrantPlugins
def validate(machine, provisioners)
errors = _detected_errors
provisioner_names = provisioners.map { |i| i.name if i.name != name }.reject(&:nil?)
provisioner_names = provisioners.map { |i| i.name.to_s if i.name != name }.reject(&:nil?)
if @before && @after
errors << I18n.t("vagrant.provisioners.base.both_before_after_set")
@ -127,7 +127,7 @@ module VagrantPlugins
errors << I18n.t("vagrant.provisioners.base.wrong_type", opt: "before")
end
if !provisioner_names.include?(@before.to_sym)
if !provisioner_names.include?(@before)
errors << I18n.t("vagrant.provisioners.base.missing_provisioner_name",
name: @before,
machine_name: machine.name,
@ -135,7 +135,7 @@ module VagrantPlugins
provisioner_name: @name)
end
dep_prov = provisioners.map { |i| i if i.name.to_s == @before && (i.before || i.after) }.reject(&:nil?)
dep_prov = provisioners.find_all { |i| i.name.to_s == @before && (i.before || i.after) }
if !dep_prov.empty?
errors << I18n.t("vagrant.provisioners.base.dependency_provisioner_dependency",
@ -153,7 +153,7 @@ module VagrantPlugins
errors << I18n.t("vagrant.provisioners.base.wrong_type", opt: "after")
end
if !provisioner_names.include?(@after.to_sym)
if !provisioner_names.include?(@after)
errors << I18n.t("vagrant.provisioners.base.missing_provisioner_name",
name: @after,
machine_name: machine.name,
@ -161,7 +161,8 @@ module VagrantPlugins
provisioner_name: @name)
end
dep_prov = provisioners.map { |i| i if i.name.to_s == @after && (i.before || i.after) }.reject(&:nil?)
dep_prov = provisioners.find_all { |i| i.name.to_s == @after && (i.before || i.after) }
if !dep_prov.empty?
errors << I18n.t("vagrant.provisioners.base.dependency_provisioner_dependency",
name: @name,

View File

@ -260,6 +260,22 @@ end
safely skip this.
</div>
<div class="alert alert-warning">
<strong>Warning!</strong> This feature is still experimental and may break or
change in between releases. Use at your own risk.
This feature currently reqiures the experimental flag to be used. To explicitly enable this feature, you can set the experimental flag to:
```
VAGRANT_EXPERIMENTAL="dependency_provisioners"
```
Please note that `VAGRANT_EXPERIMENTAL` is an environment variable. For more
information about this flag visit the [Experimental docs page](/docs/experimental/)
for more info. Without this flag enabled, provisioners with the `before` and
`after` option will be ignored.
</div>
If a provisioner has been configured using the `before` or `after` options, it
is considered a _Dependency Provisioner_. This means it has been configured to
run before or after a _Root Provisioner_, which does not have the `before` or