`vagrant box add` uses the new API.

This assumes VirtualBox boxes for now, which is fine.
This commit is contained in:
Mitchell Hashimoto 2012-07-09 20:09:54 -07:00
parent 5b18a6525d
commit 6bb621026f
9 changed files with 40 additions and 107 deletions

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -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: |-