Only write box metadata if guest has box object

Prior to this commit, providers like docker would fail to be brought up
because they do not store box objects like virtualbox or vmware
provider guests. This commit fixes that by making sure the box object
exists before writing the metadata file to disk.
This commit is contained in:
Brian Cain 2018-08-17 09:39:34 -07:00
parent 64acd68c64
commit e5364e7798
No known key found for this signature in database
GPG Key ID: 43D51080D357A001
2 changed files with 31 additions and 10 deletions

View File

@ -10,19 +10,25 @@ module VagrantPlugins
class StoreBoxMetadata
def initialize(app, env)
@app = app
@logger = Log4r::Logger.new("vagrant::up::storeboxmetadata")
end
def call(env)
box = env[:machine].box
box_meta = {
name: box.name,
version: box.version,
provider: box.provider,
directory: box.directory.sub(Vagrant.user_data_path.to_s + "/", "")
}
meta_file = env[:machine].data_dir.join("box_meta")
File.open(meta_file.to_s, "w+") do |file|
file.write(JSON.dump(box_meta))
if env[:machine].box
box = env[:machine].box
box_meta = {
name: box.name,
version: box.version,
provider: box.provider,
directory: box.directory.sub(Vagrant.user_data_path.to_s + "/", "")
}
meta_file = env[:machine].data_dir.join("box_meta")
@logger.debug("Writing box metadata file to #{meta_file}")
File.open(meta_file.to_s, "w+") do |file|
file.write(JSON.dump(box_meta))
end
else
@logger.debug("No box data found for #{env[:machine].name} with provider #{env[:machine].provider_name}")
end
@app.call(env)
end

View File

@ -26,6 +26,21 @@ describe VagrantPlugins::CommandUp::StoreBoxMetadata do
let(:subject) { described_class.new(app, env) }
describe "#call" do
context "with no box file" do
let(:machine) { double("machine", name: "guest", provider_name: "provider") }
let(:env) { {machine: machine} }
before do
allow(machine).to receive(:box).and_return(nil)
expect(app).to receive(:call).with(env)
end
it "does not write the metadata file" do
expect(File).to_not receive(:open)
subject.call(env)
end
end
let(:meta_file) { double("meta_file") }