Validate that before/after provisioner exists in machines config

This commit is contained in:
Brian Cain 2019-08-19 14:48:06 -07:00
parent 8ecd32de53
commit d15bac7fb7
No known key found for this signature in database
GPG Key ID: 9FC4639B2E4510A0
3 changed files with 33 additions and 10 deletions

View File

@ -767,7 +767,8 @@ module VagrantPlugins
next next
end end
provisioner_errors = vm_provisioner.validate(machine) provisioner_names = @provisioners.map { |i| i.name if i.name != vm_provisioner.name }.reject(&:blank?)
provisioner_errors = vm_provisioner.validate(machine, provisioner_names)
if provisioner_errors if provisioner_errors
errors = Vagrant::Config::V2::Util.merge_errors(errors, provisioner_errors) errors = Vagrant::Config::V2::Util.merge_errors(errors, provisioner_errors)
end end

View File

@ -105,23 +105,43 @@ module VagrantPlugins
@config.finalize! @config.finalize!
end end
# @param [Vagrant::Machine] machine - machine to validate against
# @param [Array<Symbol>] provisioner_names - Names of provisioners for a given machine
# @return [Array] array of strings of error messages from config option validation # @return [Array] array of strings of error messages from config option validation
def validate(machine) def validate(machine, provisioner_names)
errors = _detected_errors errors = _detected_errors
if @before if @before
if @before.is_a?(Symbol) && !VALID_BEFORE_AFTER_TYPES.include?(@before) if !VALID_BEFORE_AFTER_TYPES.include?(@before)
errors << I18n.t("vagrant.provisioners.base.invalid_alias_value", opt: "before", alias: VALID_BEFORE_AFTER_TYPES.join(", ")) if @before.is_a?(Symbol) && !VALID_BEFORE_AFTER_TYPES.include?(@before)
elsif !@before.is_a?(String) && !VALID_BEFORE_AFTER_TYPES.include?(@before) errors << I18n.t("vagrant.provisioners.base.invalid_alias_value", opt: "before", alias: VALID_BEFORE_AFTER_TYPES.join(", "))
errors << I18n.t("vagrant.provisioners.base.wrong_type", opt: "before") 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
end end
end end
if @after if @after
if @after.is_a?(Symbol) && !VALID_BEFORE_AFTER_TYPES.include?(@after) if !VALID_BEFORE_AFTER_TYPES.include?(@after)
errors << I18n.t("vagrant.provisioners.base.invalid_alias_value", opt: "after", alias: VALID_BEFORE_AFTER_TYPES.join(", ")) if @after.is_a?(Symbol)
elsif !@after.is_a?(String) && !VALID_BEFORE_AFTER_TYPES.include?(@after) errors << I18n.t("vagrant.provisioners.base.invalid_alias_value", opt: "after", alias: VALID_BEFORE_AFTER_TYPES.join(", "))
errors << I18n.t("vagrant.provisioners.base.wrong_type", opt: "after") 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
end end
end end

View File

@ -2489,6 +2489,8 @@ en:
base: base:
invalid_alias_value: |- invalid_alias_value: |-
Provisioner option `%{opt}` is not set as a valid type. Must be a string, or one of the alias shortcuts: %{alias} Provisioner option `%{opt}` is not set as a valid type. Must be a string, or one of the alias shortcuts: %{alias}
missing_provisioner_name: |-
Could not find provisioner name `%{name}` defined for machine `%{machine_name}` to run provisioner `%{action}`.
wrong_type: |- wrong_type: |-
Provisioner option `%{opt}` is not set as a valid type. Must be a string. Provisioner option `%{opt}` is not set as a valid type. Must be a string.
chef: chef: