From 6bb621026faaf01d4d9ba9a5d4705fb7bb7b49a2 Mon Sep 17 00:00:00 2001 From: Mitchell Hashimoto Date: Mon, 9 Jul 2012 20:09:54 -0700 Subject: [PATCH] `vagrant box add` uses the new API. This assumes VirtualBox boxes for now, which is fine. --- lib/vagrant/action.rb | 3 +- lib/vagrant/action/box/add.rb | 21 ++++++++++ lib/vagrant/action/box/destroy.rb | 25 ------------ lib/vagrant/action/box/download.rb | 8 ++-- lib/vagrant/action/box/unpackage.rb | 61 ----------------------------- lib/vagrant/action/box/verify.rb | 3 +- lib/vagrant/action/builtin.rb | 2 +- plugins/commands/box/command/add.rb | 11 ++++-- templates/locales/en.yml | 13 ++---- 9 files changed, 40 insertions(+), 107 deletions(-) create mode 100644 lib/vagrant/action/box/add.rb delete mode 100644 lib/vagrant/action/box/destroy.rb delete mode 100644 lib/vagrant/action/box/unpackage.rb diff --git a/lib/vagrant/action.rb b/lib/vagrant/action.rb index 1ad7cc3ce..93e41d776 100644 --- a/lib/vagrant/action.rb +++ b/lib/vagrant/action.rb @@ -8,10 +8,9 @@ module Vagrant autoload :Warden, 'vagrant/action/warden' module Box - autoload :Destroy, 'vagrant/action/box/destroy' + autoload :Add, 'vagrant/action/box/add' autoload :Download, 'vagrant/action/box/download' autoload :Package, 'vagrant/action/box/package' - autoload :Unpackage, 'vagrant/action/box/unpackage' autoload :Verify, 'vagrant/action/box/verify' end diff --git a/lib/vagrant/action/box/add.rb b/lib/vagrant/action/box/add.rb new file mode 100644 index 000000000..bc9835ad7 --- /dev/null +++ b/lib/vagrant/action/box/add.rb @@ -0,0 +1,21 @@ +module Vagrant + module Action + module Box + # Adds a downloaded box file to the environment's box collection. + # This handles unpacking the box. See {BoxCollection#add} for more + # information. + class Add + def initialize(app, env) + @app = app + @env = env + end + + def call(env) + @env[:ui].info I18n.t("vagrant.actions.box.add.adding", :name => env[:box_name]) + env[:box_collection].add(env[:box_download_temp_path], env[:box_name]) + @app.call(env) + end + end + end + end +end diff --git a/lib/vagrant/action/box/destroy.rb b/lib/vagrant/action/box/destroy.rb deleted file mode 100644 index d06d9ea07..000000000 --- a/lib/vagrant/action/box/destroy.rb +++ /dev/null @@ -1,25 +0,0 @@ -require 'fileutils' - -module Vagrant - module Action - module Box - class Destroy - def initialize(app, env) - @app = app - @env = env - end - - def call(env) - # Delete the existing box - env[:ui].info I18n.t("vagrant.actions.box.destroy.destroying", :name => env[:box_name]) - FileUtils.rm_rf(env[:box_directory]) - - # Reload the box collection - env[:box_collection].reload! - - @app.call(env) - end - end - end - end -end diff --git a/lib/vagrant/action/box/download.rb b/lib/vagrant/action/box/download.rb index 283d2eb55..144546f27 100644 --- a/lib/vagrant/action/box/download.rb +++ b/lib/vagrant/action/box/download.rb @@ -36,7 +36,7 @@ module Vagrant # Use the class if it matches the given URI or if this # is the last class... - if classes.length == (i + 1) || klass.match?(@env["box_url"]) + if classes.length == (i + 1) || klass.match?(@env[:box_url]) @env[:ui].info I18n.t("vagrant.actions.box.download.with", :class => klass.to_s) @downloader = klass.new(@env[:ui]) break @@ -47,14 +47,14 @@ module Vagrant # just in case for now. raise Errors::BoxDownloadUnknownType if !@downloader - @downloader.prepare(@env["box_url"]) + @downloader.prepare(@env[:box_url]) true end def download with_tempfile do |tempfile| download_to(tempfile) - @temp_path = @env["download.temp_path"] = tempfile.path + @temp_path = @env[:box_download_temp_path] = tempfile.path end end @@ -76,7 +76,7 @@ module Vagrant end def download_to(f) - @downloader.download!(@env["box_url"], f) + @downloader.download!(@env[:box_url], f) end end end diff --git a/lib/vagrant/action/box/unpackage.rb b/lib/vagrant/action/box/unpackage.rb deleted file mode 100644 index 02dad75c6..000000000 --- a/lib/vagrant/action/box/unpackage.rb +++ /dev/null @@ -1,61 +0,0 @@ -require 'fileutils' -require 'archive/tar/minitar' - -module Vagrant - module Action - module Box - # Unpackages a downloaded box to a given directory with a given - # name. - # - # # Required Variables - # - # * `download.temp_path` - A location for the downloaded box. This is - # set by the {Download} action. - # * `box` - A {Vagrant::Box} object. - # - class Unpackage - attr_reader :box_directory - - def initialize(app, env) - @app = app - @env = env - end - - def call(env) - @env = env - - setup_box_directory - decompress - - @app.call(@env) - end - - def recover(env) - if box_directory && File.directory?(box_directory) - FileUtils.rm_rf(box_directory) - end - end - - def setup_box_directory - if File.directory?(@env["box_directory"]) - raise Errors::BoxAlreadyExists, :name => @env["box_name"] - end - - FileUtils.mkdir_p(@env["box_directory"]) - @box_directory = @env["box_directory"] - end - - def decompress - Dir.chdir(@env["box_directory"]) do - @env[:ui].info I18n.t("vagrant.actions.box.unpackage.extracting") - begin - Archive::Tar::Minitar.unpack(@env["download.temp_path"], @env["box_directory"].to_s) - rescue SystemCallError - raise Errors::BoxUnpackageFailure - end - end - end - end - end - end -end diff --git a/lib/vagrant/action/box/verify.rb b/lib/vagrant/action/box/verify.rb index 67232db05..082cf4fc0 100644 --- a/lib/vagrant/action/box/verify.rb +++ b/lib/vagrant/action/box/verify.rb @@ -10,8 +10,9 @@ module Vagrant def call(env) @env[:ui].info I18n.t("vagrant.actions.box.verify.verifying") + box = env[:box_collection].find(env[:box_name], :virtualbox) driver = Driver::VirtualBox.new(nil) - if !driver.verify_image(env["box_directory"].join("box.ovf").to_s) + if !driver.verify_image(box.directory.join("box.ovf").to_s) raise Errors::BoxVerificationFailed end diff --git a/lib/vagrant/action/builtin.rb b/lib/vagrant/action/builtin.rb index 9f402dd47..f7ec000d7 100644 --- a/lib/vagrant/action/builtin.rb +++ b/lib/vagrant/action/builtin.rb @@ -147,7 +147,7 @@ module Vagrant Builder.new do use General::CheckVirtualbox use Box::Download - use Box::Unpackage + use Box::Add use Box::Verify end end diff --git a/plugins/commands/box/command/add.rb b/plugins/commands/box/command/add.rb index 9c9f549eb..996074b26 100644 --- a/plugins/commands/box/command/add.rb +++ b/plugins/commands/box/command/add.rb @@ -24,11 +24,16 @@ module VagrantPlugins # If we're force adding, then be sure to destroy any existing box if it # exists. if options[:force] - existing = @env.boxes.find(argv[0]) - existing.destroy if existing + existing = @env.boxes.find(argv[0], :virtualbox) + existing.destroy! if existing end - @env.boxes.add(argv[0], argv[1]) + # Invoke the "box_add" middleware sequence. + @env.action_runner.run(:box_add, { + :box_name => argv[0], + :box_provider => :virtualbox, + :box_url => argv[1] + }) # Success, exit status 0 0 diff --git a/templates/locales/en.yml b/templates/locales/en.yml index 98031702c..d5325b4cd 100644 --- a/templates/locales/en.yml +++ b/templates/locales/en.yml @@ -571,22 +571,15 @@ en: suspending: Saving VM state and suspending execution... box: + add: + adding: |- + Extracting box... destroy: destroying: "Deleting box '%{name}'..." download: with: "Downloading with %{class}..." cleaning: "Cleaning up downloaded box..." unknown_type: "Unknown or unsupported URI type given for box download." - unpackage: - extracting: "Extracting box..." - already_exists: |- - A box already exists under the name of '%{name}'. This may or may - not be the same box you are trying to add. Please use another name - or remove the previous box then try to add it again. - untar_failure: |- - Failed to untar the box file. This is usually because you're - attempting to add a box that isn't a valid box file. Please - double check that the box file is properly packaged. verify: verifying: "Verifying box..." failed: |-