core: Box can store a metadata URL
This commit is contained in:
parent
e9afe386c1
commit
bd9f375263
|
@ -39,17 +39,24 @@ module Vagrant
|
||||||
# @return [Hash]
|
# @return [Hash]
|
||||||
attr_reader :metadata
|
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.
|
# This is used to initialize a box.
|
||||||
#
|
#
|
||||||
# @param [String] name Logical name of the box.
|
# @param [String] name Logical name of the box.
|
||||||
# @param [Symbol] provider The provider that this box implements.
|
# @param [Symbol] provider The provider that this box implements.
|
||||||
# @param [Pathname] directory The directory where this box exists on
|
# @param [Pathname] directory The directory where this box exists on
|
||||||
# disk.
|
# disk.
|
||||||
def initialize(name, provider, version, directory)
|
def initialize(name, provider, version, directory, **opts)
|
||||||
@name = name
|
@name = name
|
||||||
@version = version
|
@version = version
|
||||||
@provider = provider
|
@provider = provider
|
||||||
@directory = directory
|
@directory = directory
|
||||||
|
@metadata_url = opts[:metadata_url]
|
||||||
|
|
||||||
metadata_file = directory.join("metadata.json")
|
metadata_file = directory.join("metadata.json")
|
||||||
raise Errors::BoxMetadataFileNotFound, :name => @name if !metadata_file.file?
|
raise Errors::BoxMetadataFileNotFound, :name => @name if !metadata_file.file?
|
||||||
|
|
|
@ -151,7 +151,8 @@ module Vagrant
|
||||||
provider = box_provider.to_sym
|
provider = box_provider.to_sym
|
||||||
|
|
||||||
# Create the directory for this box, not including the provider
|
# 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
|
box_dir.mkpath
|
||||||
@logger.debug("Box directory: #{box_dir}")
|
@logger.debug("Box directory: #{box_dir}")
|
||||||
|
|
||||||
|
@ -173,6 +174,12 @@ module Vagrant
|
||||||
@logger.debug("Moving: #{f} => #{destination}")
|
@logger.debug("Moving: #{f} => #{destination}")
|
||||||
FileUtils.mv(f, destination)
|
FileUtils.mv(f, destination)
|
||||||
end
|
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
|
end
|
||||||
end
|
end
|
||||||
|
@ -244,9 +251,10 @@ module Vagrant
|
||||||
end
|
end
|
||||||
|
|
||||||
versions = box_directory.children(true).map do |versiondir|
|
versions = box_directory.children(true).map do |versiondir|
|
||||||
|
next if !versiondir.directory?
|
||||||
version = versiondir.basename.to_s
|
version = versiondir.basename.to_s
|
||||||
Gem::Version.new(version)
|
Gem::Version.new(version)
|
||||||
end
|
end.compact
|
||||||
|
|
||||||
# Traverse through versions with the latest version first
|
# Traverse through versions with the latest version first
|
||||||
versions.sort.reverse.each do |v|
|
versions.sort.reverse.each do |v|
|
||||||
|
@ -260,7 +268,15 @@ module Vagrant
|
||||||
provider_dir = versiondir.join(provider.to_s)
|
provider_dir = versiondir.join(provider.to_s)
|
||||||
next if !provider_dir.directory?
|
next if !provider_dir.directory?
|
||||||
@logger.info("Box found: #{name} (#{provider})")
|
@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
|
end
|
||||||
end
|
end
|
||||||
|
|
|
@ -97,7 +97,7 @@ describe Vagrant::BoxCollection do
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
describe "adding" do
|
describe "#add" do
|
||||||
it "should add a valid box to the system" do
|
it "should add a valid box to the system" do
|
||||||
box_path = environment.box2_file(:virtualbox)
|
box_path = environment.box2_file(:virtualbox)
|
||||||
|
|
||||||
|
@ -134,6 +134,17 @@ describe Vagrant::BoxCollection do
|
||||||
expect(box.provider).to eq(:vmware)
|
expect(box.provider).to eq(:vmware)
|
||||||
end
|
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
|
it "should add a V1 box" do
|
||||||
# Create a V1 box.
|
# Create a V1 box.
|
||||||
box_path = environment.box1_file
|
box_path = environment.box1_file
|
||||||
|
|
|
@ -15,6 +15,8 @@ describe Vagrant::Box do
|
||||||
let(:directory) { environment.box3("foo", "1.0", :virtualbox) }
|
let(:directory) { environment.box3("foo", "1.0", :virtualbox) }
|
||||||
subject { described_class.new(name, provider, version, directory) }
|
subject { described_class.new(name, provider, version, directory) }
|
||||||
|
|
||||||
|
its(:metadata_url) { should be_nil }
|
||||||
|
|
||||||
it "provides the name" do
|
it "provides the name" do
|
||||||
subject.name.should == name
|
subject.name.should == name
|
||||||
end
|
end
|
||||||
|
@ -39,6 +41,16 @@ describe Vagrant::Box do
|
||||||
subject.metadata.should == data
|
subject.metadata.should == data
|
||||||
end
|
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
|
context "with a corrupt metadata file" do
|
||||||
before do
|
before do
|
||||||
directory.join("metadata.json").open("w") do |f|
|
directory.join("metadata.json").open("w") do |f|
|
||||||
|
|
Loading…
Reference in New Issue