core: Box can store a metadata URL

This commit is contained in:
Mitchell Hashimoto 2014-01-24 13:40:11 -08:00
parent e9afe386c1
commit bd9f375263
4 changed files with 51 additions and 5 deletions

View File

@ -39,17 +39,24 @@ module Vagrant
# @return [Hash]
attr_reader :metadata
# This is the URL to the version info and other metadata for this
# box.
#
# @return [String]
attr_reader :metadata_url
# This is used to initialize a box.
#
# @param [String] name Logical name of the box.
# @param [Symbol] provider The provider that this box implements.
# @param [Pathname] directory The directory where this box exists on
# disk.
def initialize(name, provider, version, directory)
def initialize(name, provider, version, directory, **opts)
@name = name
@version = version
@provider = provider
@directory = directory
@metadata_url = opts[:metadata_url]
metadata_file = directory.join("metadata.json")
raise Errors::BoxMetadataFileNotFound, :name => @name if !metadata_file.file?

View File

@ -151,7 +151,8 @@ module Vagrant
provider = box_provider.to_sym
# Create the directory for this box, not including the provider
box_dir = @directory.join(dir_name(name), version)
root_box_dir = @directory.join(dir_name(name))
box_dir = root_box_dir.join(version)
box_dir.mkpath
@logger.debug("Box directory: #{box_dir}")
@ -173,6 +174,12 @@ module Vagrant
@logger.debug("Moving: #{f} => #{destination}")
FileUtils.mv(f, destination)
end
if opts[:metadata_url]
root_box_dir.join("metadata_url").open("w") do |f|
f.write(opts[:metadata_url])
end
end
end
end
end
@ -244,9 +251,10 @@ module Vagrant
end
versions = box_directory.children(true).map do |versiondir|
next if !versiondir.directory?
version = versiondir.basename.to_s
Gem::Version.new(version)
end
end.compact
# Traverse through versions with the latest version first
versions.sort.reverse.each do |v|
@ -260,7 +268,15 @@ module Vagrant
provider_dir = versiondir.join(provider.to_s)
next if !provider_dir.directory?
@logger.info("Box found: #{name} (#{provider})")
return Box.new(name, provider, v.to_s, provider_dir)
metadata_url = nil
metadata_url_file = box_directory.join("metadata_url")
metadata_url = metadata_url_file.read if metadata_url_file.file?
return Box.new(
name, provider, v.to_s, provider_dir,
metadata_url: metadata_url,
)
end
end
end

View File

@ -97,7 +97,7 @@ describe Vagrant::BoxCollection do
end
end
describe "adding" do
describe "#add" do
it "should add a valid box to the system" do
box_path = environment.box2_file(:virtualbox)
@ -134,6 +134,17 @@ describe Vagrant::BoxCollection do
expect(box.provider).to eq(:vmware)
end
it "should store a metadata URL" do
box_path = environment.box2_file(:virtualbox)
subject.add(
box_path, "foo", "1.0",
metadata_url: "bar")
box = subject.find("foo", :virtualbox, "1.0")
expect(box.metadata_url).to eq("bar")
end
it "should add a V1 box" do
# Create a V1 box.
box_path = environment.box1_file

View File

@ -15,6 +15,8 @@ describe Vagrant::Box do
let(:directory) { environment.box3("foo", "1.0", :virtualbox) }
subject { described_class.new(name, provider, version, directory) }
its(:metadata_url) { should be_nil }
it "provides the name" do
subject.name.should == name
end
@ -39,6 +41,16 @@ describe Vagrant::Box do
subject.metadata.should == data
end
context "with a metadata URL" do
subject do
described_class.new(
name, provider, version, directory,
metadata_url: "foo")
end
its(:metadata_url) { should eq("foo") }
end
context "with a corrupt metadata file" do
before do
directory.join("metadata.json").open("w") do |f|