Builtin: ConfigValidate will validate your configuration

This commit is contained in:
Mitchell Hashimoto 2013-01-18 13:26:14 -08:00
parent 37e36010e0
commit 7f55d5eac8
7 changed files with 44 additions and 26 deletions

View File

@ -12,6 +12,7 @@ module Vagrant
autoload :BoxAdd, "vagrant/action/builtin/box_add"
autoload :Call, "vagrant/action/builtin/call"
autoload :Confirm, "vagrant/action/builtin/confirm"
autoload :ConfigValidate, "vagrant/action/builtin/config_validate"
autoload :EnvSet, "vagrant/action/builtin/env_set"
autoload :Provision, "vagrant/action/builtin/provision"
autoload :SSHExec, "vagrant/action/builtin/ssh_exec"
@ -20,7 +21,6 @@ module Vagrant
module General
autoload :Package, 'vagrant/action/general/package'
autoload :Validate, 'vagrant/action/general/validate'
end
# This is the action that will add a box from a URL. This middleware

View File

@ -0,0 +1,30 @@
require "vagrant/util/template_renderer"
module Vagrant
module Action
module Builtin
# This class validates the configuration and raises an exception
# if there are any validation errors.
class ConfigValidate
def initialize(app, env)
@app = app
end
def call(env)
if !env.has_key?(:config_validate) || env[:config_validate]
errors = env[:machine].config.validate(env[:machine])
if errors
raise Errors::ConfigInvalid,
:errors => Util::TemplateRenderer.render(
"config/validation_failed",
:errors => errors)
end
end
@app.call(env)
end
end
end
end
end

View File

@ -1,21 +0,0 @@
module Vagrant
module Action
module General
# Simply validates the configuration of the current Vagrant
# environment.
class Validate
def initialize(app, env)
@app = app
end
def call(env)
if !env.has_key?(:validate) || env[:validate]
env[:machine].config.validate!(env[:machine].env)
end
@app.call(env)
end
end
end
end
end

View File

@ -150,6 +150,10 @@ module Vagrant
error_key(:cli_invalid_options)
end
class ConfigInvalid < VagrantError
error_key(:config_invalid)
end
class DestroyRequiresForce < VagrantError
status_code(74)
error_key(:destroy_requires_force)

View File

@ -40,7 +40,7 @@ module VagrantPlugins
# validate the configuration here, and we don't want to confirm
# we want to destroy.
destroy_env = env.clone
destroy_env[:validate] = false
destroy_env[:config_validate] = false
destroy_env[:force_confirm_destroy] = true
env[:action_runner].run(Action.action_destroy, destroy_env)
end

View File

@ -1,6 +1,6 @@
<% errors.each do |key, container| -%>
<%= key %>:
<% container.errors.each do |error| -%>
<% errors.each do |section, list| -%>
<%= section %>:
<% list.each do |error| -%>
* <%= error %>
<% end -%>

View File

@ -56,6 +56,11 @@ en:
available below.
%{help}
config_invalid: |-
There are errors in the configuration of this machine. Please fix
the following errors and try again:
%{errors}
destroy_requires_force: |-
Destroy doesn't have a TTY to ask for confirmation. Please pass the
`--force` flag to force a destroy, otherwise attach a TTY so that