From 7f55d5eac8f27f09aa9d93f46c5d19ed85461e02 Mon Sep 17 00:00:00 2001 From: Mitchell Hashimoto Date: Fri, 18 Jan 2013 13:26:14 -0800 Subject: [PATCH] Builtin: ConfigValidate will validate your configuration --- lib/vagrant/action.rb | 2 +- lib/vagrant/action/builtin/config_validate.rb | 30 +++++++++++++++++++ lib/vagrant/action/general/validate.rb | 21 ------------- lib/vagrant/errors.rb | 4 +++ plugins/providers/virtualbox/action/import.rb | 2 +- templates/config/validation_failed.erb | 6 ++-- templates/locales/en.yml | 5 ++++ 7 files changed, 44 insertions(+), 26 deletions(-) create mode 100644 lib/vagrant/action/builtin/config_validate.rb delete mode 100644 lib/vagrant/action/general/validate.rb diff --git a/lib/vagrant/action.rb b/lib/vagrant/action.rb index bb75519d2..e9606e8a8 100644 --- a/lib/vagrant/action.rb +++ b/lib/vagrant/action.rb @@ -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 diff --git a/lib/vagrant/action/builtin/config_validate.rb b/lib/vagrant/action/builtin/config_validate.rb new file mode 100644 index 000000000..87603b860 --- /dev/null +++ b/lib/vagrant/action/builtin/config_validate.rb @@ -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 diff --git a/lib/vagrant/action/general/validate.rb b/lib/vagrant/action/general/validate.rb deleted file mode 100644 index f566b793f..000000000 --- a/lib/vagrant/action/general/validate.rb +++ /dev/null @@ -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 diff --git a/lib/vagrant/errors.rb b/lib/vagrant/errors.rb index ed62e8617..4fdc5ce2c 100644 --- a/lib/vagrant/errors.rb +++ b/lib/vagrant/errors.rb @@ -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) diff --git a/plugins/providers/virtualbox/action/import.rb b/plugins/providers/virtualbox/action/import.rb index e63dfc950..d7f287e90 100644 --- a/plugins/providers/virtualbox/action/import.rb +++ b/plugins/providers/virtualbox/action/import.rb @@ -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 diff --git a/templates/config/validation_failed.erb b/templates/config/validation_failed.erb index 3e8d27b09..f8de6bb2a 100644 --- a/templates/config/validation_failed.erb +++ b/templates/config/validation_failed.erb @@ -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 -%> diff --git a/templates/locales/en.yml b/templates/locales/en.yml index a5751cdc1..f76cebe55 100644 --- a/templates/locales/en.yml +++ b/templates/locales/en.yml @@ -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