From bd9f375263caecaa5c4f4cc2b93822bfad24e140 Mon Sep 17 00:00:00 2001 From: Mitchell Hashimoto Date: Fri, 24 Jan 2014 13:40:11 -0800 Subject: [PATCH] core: Box can store a metadata URL --- lib/vagrant/box.rb | 9 ++++++++- lib/vagrant/box_collection.rb | 22 +++++++++++++++++++--- test/unit/vagrant/box_collection_test.rb | 13 ++++++++++++- test/unit/vagrant/box_test.rb | 12 ++++++++++++ 4 files changed, 51 insertions(+), 5 deletions(-) diff --git a/lib/vagrant/box.rb b/lib/vagrant/box.rb index 254c0c393..2b848da7c 100644 --- a/lib/vagrant/box.rb +++ b/lib/vagrant/box.rb @@ -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? diff --git a/lib/vagrant/box_collection.rb b/lib/vagrant/box_collection.rb index 3765d2f47..fc5e1eda6 100644 --- a/lib/vagrant/box_collection.rb +++ b/lib/vagrant/box_collection.rb @@ -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 diff --git a/test/unit/vagrant/box_collection_test.rb b/test/unit/vagrant/box_collection_test.rb index d2ac1dac1..7cdb242bd 100644 --- a/test/unit/vagrant/box_collection_test.rb +++ b/test/unit/vagrant/box_collection_test.rb @@ -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 diff --git a/test/unit/vagrant/box_test.rb b/test/unit/vagrant/box_test.rb index a540476f7..6832e3ca4 100644 --- a/test/unit/vagrant/box_test.rb +++ b/test/unit/vagrant/box_test.rb @@ -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|