From 7b0dc8d528002d6aee73485822f0668b3c9c8389 Mon Sep 17 00:00:00 2001 From: Brian Cain Date: Thu, 29 Aug 2019 13:50:22 -0700 Subject: [PATCH] Update provisioner enhancements from pull request feedback --- .../action/builtin/mixin_provisioners.rb | 14 +++++++------- plugins/kernel_v2/config/vm.rb | 3 ++- plugins/kernel_v2/config/vm_provisioner.rb | 17 +++++++++-------- .../docs/provisioning/basic_usage.html.md | 16 ++++++++++++++++ 4 files changed, 34 insertions(+), 16 deletions(-) diff --git a/lib/vagrant/action/builtin/mixin_provisioners.rb b/lib/vagrant/action/builtin/mixin_provisioners.rb index 35ca0aebb..ac5c89cab 100644 --- a/lib/vagrant/action/builtin/mixin_provisioners.rb +++ b/lib/vagrant/action/builtin/mixin_provisioners.rb @@ -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 = [] diff --git a/plugins/kernel_v2/config/vm.rb b/plugins/kernel_v2/config/vm.rb index e7196a8ea..3169b0552 100644 --- a/plugins/kernel_v2/config/vm.rb +++ b/plugins/kernel_v2/config/vm.rb @@ -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 diff --git a/plugins/kernel_v2/config/vm_provisioner.rb b/plugins/kernel_v2/config/vm_provisioner.rb index a8e106f7a..da57d1808 100644 --- a/plugins/kernel_v2/config/vm_provisioner.rb +++ b/plugins/kernel_v2/config/vm_provisioner.rb @@ -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, diff --git a/website/source/docs/provisioning/basic_usage.html.md b/website/source/docs/provisioning/basic_usage.html.md index c514b7663..43c20cb55 100644 --- a/website/source/docs/provisioning/basic_usage.html.md +++ b/website/source/docs/provisioning/basic_usage.html.md @@ -260,6 +260,22 @@ end safely skip this. +
+ Warning! 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. +
+ 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