From 165dd63f7883707ad25fdbbd1425d8270b0bc1b3 Mon Sep 17 00:00:00 2001 From: Mitchell Hashimoto Date: Fri, 1 Feb 2013 11:28:51 -0800 Subject: [PATCH] New HandleBoxUrl built-in middleware to handlet the "box_url" config --- lib/vagrant/action.rb | 1 + lib/vagrant/action/builtin/handle_box_url.rb | 43 +++++++++++++++++++ lib/vagrant/util/subprocess.rb | 4 +- plugins/providers/virtualbox/action.rb | 3 +- .../providers/virtualbox/action/check_box.rb | 36 ---------------- templates/locales/en.yml | 7 ++- 6 files changed, 53 insertions(+), 41 deletions(-) create mode 100644 lib/vagrant/action/builtin/handle_box_url.rb delete mode 100644 plugins/providers/virtualbox/action/check_box.rb diff --git a/lib/vagrant/action.rb b/lib/vagrant/action.rb index 09625f1b7..2dafceaa7 100644 --- a/lib/vagrant/action.rb +++ b/lib/vagrant/action.rb @@ -15,6 +15,7 @@ module Vagrant autoload :ConfigValidate, "vagrant/action/builtin/config_validate" autoload :EnvSet, "vagrant/action/builtin/env_set" autoload :GracefulHalt, "vagrant/action/builtin/graceful_halt" + autoload :HandleBoxUrl, "vagrant/action/builtin/handle_box_url" autoload :Lock, "vagrant/action/builtin/lock" autoload :Provision, "vagrant/action/builtin/provision" autoload :SSHExec, "vagrant/action/builtin/ssh_exec" diff --git a/lib/vagrant/action/builtin/handle_box_url.rb b/lib/vagrant/action/builtin/handle_box_url.rb new file mode 100644 index 000000000..d6e99a74d --- /dev/null +++ b/lib/vagrant/action/builtin/handle_box_url.rb @@ -0,0 +1,43 @@ +module Vagrant + module Action + module Builtin + # This built-in middleware handles the `box_url` setting, downloading + # the box if necessary. You should place this early in your middleware + # sequence for a provider after configuration validation but before + # you attempt to use any box. + class HandleBoxUrl + def initialize(app, env) + @app = app + end + + def call(env) + if !env[:machine].box + # We can assume a box URL is set because the Vagrantfile + # validation should do this for us. If not, though, we do + # raise a terrible runtime error. + box_name = env[:machine].config.vm.box + box_url = env[:machine].config.vm.box_url + + # Add the box then reload the box collection so that it becomes + # aware of it. + env[:ui].info I18n.t( + "vagrant.actions.vm.check_box.not_found", + :name => box_name, + :provider => env[:machine].provider_name) + env[:action_runner].run(Vagrant::Action.action_box_add, { + :box_name => box_name, + :box_provider => env[:machine].provider_name, + :box_url => box_url + }) + + # Reload the environment and set the VM to be the new loaded VM. + env[:machine] = env[:machine].env.machine( + env[:machine].name, env[:machine].provider_name, true) + end + + @app.call(env) + end + end + end + end +end diff --git a/lib/vagrant/util/subprocess.rb b/lib/vagrant/util/subprocess.rb index dfee6e5a3..a8bcb5cf9 100644 --- a/lib/vagrant/util/subprocess.rb +++ b/lib/vagrant/util/subprocess.rb @@ -119,7 +119,7 @@ module Vagrant next if data.empty? io_name = r == stdout ? :stdout : :stderr - @logger.debug("#{io_name}: #{data}") + @logger.debug("#{io_name}: #{data.chomp}") io_data[io_name] += data yield io_name, data if block_given? && notify_table[io_name] @@ -161,7 +161,7 @@ module Vagrant # Log it out and accumulate io_name = io == stdout ? :stdout : :stderr io_data[io_name] += extra_data - @logger.debug("#{io_name}: #{extra_data}") + @logger.debug("#{io_name}: #{extra_data.chomp}") # Yield to any listeners any remaining data yield io_name, extra_data if block_given? diff --git a/plugins/providers/virtualbox/action.rb b/plugins/providers/virtualbox/action.rb index 9a27af844..542c7ee9d 100644 --- a/plugins/providers/virtualbox/action.rb +++ b/plugins/providers/virtualbox/action.rb @@ -5,7 +5,6 @@ module VagrantPlugins module Action autoload :Boot, File.expand_path("../action/boot", __FILE__) autoload :CheckAccessible, File.expand_path("../action/check_accessible", __FILE__) - autoload :CheckBox, File.expand_path("../action/check_box", __FILE__) autoload :CheckCreated, File.expand_path("../action/check_created", __FILE__) autoload :CheckGuestAdditions, File.expand_path("../action/check_guest_additions", __FILE__) autoload :CheckPortCollisions, File.expand_path("../action/check_port_collisions", __FILE__) @@ -293,7 +292,7 @@ module VagrantPlugins # If the VM is NOT created yet, then do the setup steps if !env[:result] b2.use CheckAccessible - b2.use CheckBox + b2.use HandleBoxUrl b2.use Import b2.use CheckGuestAdditions b2.use MatchMACAddress diff --git a/plugins/providers/virtualbox/action/check_box.rb b/plugins/providers/virtualbox/action/check_box.rb deleted file mode 100644 index b5ee90efb..000000000 --- a/plugins/providers/virtualbox/action/check_box.rb +++ /dev/null @@ -1,36 +0,0 @@ -module VagrantPlugins - module ProviderVirtualBox - module Action - class CheckBox - def initialize(app, env) - @app = app - end - - def call(env) - box_name = env[:machine].config.vm.box - raise Vagrant::Errors::BoxNotSpecified if !box_name - - if !env[:box_collection].find(box_name, :virtualbox) - box_url = env[:machine].config.vm.box_url - raise Vagrant::Errors::BoxSpecifiedDoesntExist, :name => box_name if !box_url - - # Add the box then reload the box collection so that it becomes - # aware of it. - env[:ui].info I18n.t("vagrant.actions.vm.check_box.not_found", :name => box_name) - env[:action_runner].run(Vagrant::Action.action_box_add, { - :box_name => box_name, - :box_provider => env[:machine].provider_name, - :box_url => box_url - }) - - # Reload the environment and set the VM to be the new loaded VM. - env[:machine] = env[:machine].env.machine( - env[:machine].name, env[:machine].provider_name, true) - end - - @app.call(env) - end - end - end - end -end diff --git a/templates/locales/en.yml b/templates/locales/en.yml index 3713b3f46..6d1ae9fee 100644 --- a/templates/locales/en.yml +++ b/templates/locales/en.yml @@ -500,7 +500,12 @@ en: Specific bridge '%{bridge}' not found. You may be asked to specify which network to bridge to. check_box: - not_found: Box %{name} was not found. Fetching box from specified URL... + not_found: |- + Box '%{name}' was not found. Fetching box from specified URL for + the provider '%{provider}'. Note that if the URL does not have + a box for this provider, you should interrupt Vagrant now and add + the box yourself. Otherwise Vagrant will attempt to download the + full box prior to discovering this error. not_specified: |- No base box was specified! A base box is required as a staring point for every vagrant virtual machine. Please specify one in your Vagrantfile