2012-11-07 05:28:44 +00:00
|
|
|
require 'log4r'
|
|
|
|
|
|
|
|
module VagrantPlugins
|
|
|
|
module Kernel_V2
|
|
|
|
# Represents a single configured provisioner for a VM.
|
2019-08-06 23:18:58 +00:00
|
|
|
class VagrantConfigProvisioner < Vagrant.plugin("2", :config)
|
2019-07-26 17:42:32 +00:00
|
|
|
# Defaults
|
|
|
|
VALID_BEFORE_AFTER_TYPES = [:each, :all].freeze
|
|
|
|
|
2014-10-24 01:40:14 +00:00
|
|
|
# Unique name for this provisioner
|
2014-02-03 15:56:39 +00:00
|
|
|
#
|
|
|
|
# @return [String]
|
2014-10-24 01:40:14 +00:00
|
|
|
attr_reader :name
|
2014-02-03 15:56:39 +00:00
|
|
|
|
2017-06-13 18:26:12 +00:00
|
|
|
# Internal unique name for this provisioner
|
|
|
|
# Set to the given :name if exists, otherwise
|
|
|
|
# it's set as a UUID.
|
|
|
|
#
|
|
|
|
# Note: This is for internal use only.
|
|
|
|
#
|
|
|
|
# @return [String]
|
|
|
|
attr_reader :id
|
|
|
|
|
2014-10-24 01:40:14 +00:00
|
|
|
# The type of the provisioner that should be registered
|
2013-01-14 00:02:48 +00:00
|
|
|
# as a plugin.
|
|
|
|
#
|
|
|
|
# @return [Symbol]
|
2014-10-24 01:40:14 +00:00
|
|
|
attr_reader :type
|
2013-01-14 00:02:48 +00:00
|
|
|
|
|
|
|
# The configuration associated with the provisioner, if there is any.
|
|
|
|
#
|
|
|
|
# @return [Object]
|
2014-02-03 20:30:01 +00:00
|
|
|
attr_accessor :config
|
2012-11-07 05:28:44 +00:00
|
|
|
|
2019-04-04 15:55:32 +00:00
|
|
|
# When to run this provisioner. Either "once", "always", or "never"
|
2014-04-09 22:28:44 +00:00
|
|
|
#
|
|
|
|
# @return [String]
|
|
|
|
attr_accessor :run
|
|
|
|
|
2014-02-03 21:03:20 +00:00
|
|
|
# Whether or not to preserve the order when merging this with a
|
|
|
|
# parent scope.
|
|
|
|
#
|
|
|
|
# @return [Boolean]
|
|
|
|
attr_accessor :preserve_order
|
|
|
|
|
2019-07-26 16:37:56 +00:00
|
|
|
# The name of a provisioner to run before it has started
|
|
|
|
#
|
2019-08-19 18:33:30 +00:00
|
|
|
# @return [String, Symbol]
|
2019-07-26 16:37:56 +00:00
|
|
|
attr_accessor :before
|
|
|
|
|
|
|
|
# The name of a provisioner to run after it is finished
|
|
|
|
#
|
2019-08-19 18:33:30 +00:00
|
|
|
# @return [String, Symbol]
|
2019-07-26 16:37:56 +00:00
|
|
|
attr_accessor :after
|
|
|
|
|
2019-08-06 23:18:58 +00:00
|
|
|
def initialize(name, type, before=nil, after=nil)
|
2012-11-07 05:28:44 +00:00
|
|
|
@logger = Log4r::Logger.new("vagrant::config::vm::provisioner")
|
2013-01-14 00:02:48 +00:00
|
|
|
@logger.debug("Provisioner defined: #{name}")
|
|
|
|
|
2017-06-13 18:26:12 +00:00
|
|
|
@id = name || SecureRandom.uuid
|
2013-03-28 22:38:32 +00:00
|
|
|
@config = nil
|
|
|
|
@invalid = false
|
|
|
|
@name = name
|
2014-02-03 21:03:20 +00:00
|
|
|
@preserve_order = false
|
2014-04-09 22:28:44 +00:00
|
|
|
@run = nil
|
2014-10-24 01:40:14 +00:00
|
|
|
@type = type
|
2019-08-19 18:36:54 +00:00
|
|
|
@before = before
|
2019-08-06 23:18:58 +00:00
|
|
|
@after = after
|
2012-11-07 05:28:44 +00:00
|
|
|
|
2013-03-30 21:39:29 +00:00
|
|
|
# Attempt to find the provisioner...
|
2014-10-24 01:40:14 +00:00
|
|
|
if !Vagrant.plugin("2").manager.provisioners[type]
|
|
|
|
@logger.warn("Provisioner '#{type}' not found.")
|
2013-03-30 21:39:29 +00:00
|
|
|
@invalid = true
|
|
|
|
end
|
|
|
|
|
2013-01-14 00:02:48 +00:00
|
|
|
# Attempt to find the configuration class for this provider
|
|
|
|
# if it exists and load the configuration.
|
2014-02-03 15:56:39 +00:00
|
|
|
@config_class = Vagrant.plugin("2").manager.
|
2014-10-24 01:40:14 +00:00
|
|
|
provisioner_configs[@type]
|
2014-02-03 15:56:39 +00:00
|
|
|
if !@config_class
|
|
|
|
@logger.info(
|
2014-10-24 01:40:14 +00:00
|
|
|
"Provisioner config for '#{@type}' not found. Ignoring config.")
|
2014-03-18 18:36:40 +00:00
|
|
|
@config_class = Vagrant::Config::V2::DummyConfig
|
2012-11-07 05:28:44 +00:00
|
|
|
end
|
2014-02-03 15:56:39 +00:00
|
|
|
end
|
|
|
|
|
2014-02-03 20:30:01 +00:00
|
|
|
def initialize_copy(orig)
|
|
|
|
super
|
|
|
|
@config = @config.dup if @config
|
|
|
|
end
|
|
|
|
|
2014-02-03 15:56:39 +00:00
|
|
|
def add_config(**options, &block)
|
|
|
|
return if invalid?
|
|
|
|
|
|
|
|
current = @config_class.new
|
|
|
|
current.set_options(options) if options
|
2014-02-04 03:41:11 +00:00
|
|
|
block.call(current) if block
|
2014-02-03 15:56:39 +00:00
|
|
|
current = @config.merge(current) if @config
|
|
|
|
@config = current
|
|
|
|
end
|
|
|
|
|
|
|
|
def finalize!
|
|
|
|
return if invalid?
|
2012-11-07 05:28:44 +00:00
|
|
|
|
2013-01-14 00:02:48 +00:00
|
|
|
@config.finalize!
|
2012-11-07 05:28:44 +00:00
|
|
|
end
|
2013-03-28 22:38:32 +00:00
|
|
|
|
2019-08-19 21:48:06 +00:00
|
|
|
# @param [Vagrant::Machine] machine - machine to validate against
|
|
|
|
# @param [Array<Symbol>] provisioner_names - Names of provisioners for a given machine
|
2019-07-26 17:42:32 +00:00
|
|
|
# @return [Array] array of strings of error messages from config option validation
|
2019-08-19 21:48:06 +00:00
|
|
|
def validate(machine, provisioner_names)
|
2019-07-26 17:42:32 +00:00
|
|
|
errors = _detected_errors
|
|
|
|
|
|
|
|
if @before
|
2019-08-19 21:48:06 +00:00
|
|
|
if !VALID_BEFORE_AFTER_TYPES.include?(@before)
|
|
|
|
if @before.is_a?(Symbol) && !VALID_BEFORE_AFTER_TYPES.include?(@before)
|
|
|
|
errors << I18n.t("vagrant.provisioners.base.invalid_alias_value", opt: "before", alias: VALID_BEFORE_AFTER_TYPES.join(", "))
|
|
|
|
elsif !@before.is_a?(String) && !VALID_BEFORE_AFTER_TYPES.include?(@before)
|
|
|
|
errors << I18n.t("vagrant.provisioners.base.wrong_type", opt: "before")
|
|
|
|
end
|
|
|
|
|
|
|
|
if !provisioner_names.include?(@before.to_sym)
|
|
|
|
errors << I18n.t("vagrant.provisioners.base.missing_provisioner_name",
|
|
|
|
name: @before,
|
|
|
|
machine_name: machine.name,
|
|
|
|
action: "before")
|
|
|
|
end
|
2019-07-26 17:42:32 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
if @after
|
2019-08-19 21:48:06 +00:00
|
|
|
if !VALID_BEFORE_AFTER_TYPES.include?(@after)
|
|
|
|
if @after.is_a?(Symbol)
|
|
|
|
errors << I18n.t("vagrant.provisioners.base.invalid_alias_value", opt: "after", alias: VALID_BEFORE_AFTER_TYPES.join(", "))
|
|
|
|
elsif !@after.is_a?(String)
|
|
|
|
errors << I18n.t("vagrant.provisioners.base.wrong_type", opt: "after")
|
|
|
|
end
|
|
|
|
|
|
|
|
if !provisioner_names.include?(@after.to_sym)
|
|
|
|
errors << I18n.t("vagrant.provisioners.base.missing_provisioner_name",
|
|
|
|
name: @after,
|
|
|
|
machine_name: machine.name,
|
|
|
|
action: "after")
|
|
|
|
end
|
2019-07-26 17:42:32 +00:00
|
|
|
end
|
|
|
|
end
|
2019-08-19 18:23:06 +00:00
|
|
|
|
|
|
|
{"provisioner" => errors}
|
2019-07-26 17:42:32 +00:00
|
|
|
end
|
|
|
|
|
2013-03-28 22:38:32 +00:00
|
|
|
# Returns whether the provisioner used was invalid or not. A provisioner
|
|
|
|
# is invalid if it can't be found.
|
|
|
|
#
|
|
|
|
# @return [Boolean]
|
|
|
|
def invalid?
|
|
|
|
@invalid
|
|
|
|
end
|
2012-11-07 05:28:44 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|