Overwrite outputfile if exists and update available

This commit is contained in:
sophia 2020-06-15 11:14:44 -05:00
parent 1ff6582fff
commit decd489a6d
2 changed files with 29 additions and 29 deletions

View File

@ -31,8 +31,22 @@ module VagrantPlugins
# @note If file_destination exists, source_directory will be checked
# for recent modifications and a new ISO will be generated if requried.
def self.create_iso(env, source_directory, file_destination=nil, extra_opts={})
file_destination = output_file(file_destination)
source_directory = Pathname.new(source_directory)
if file_destination.nil?
@@logger.info("No file destination specified, creating temp location")
tmpfile = Tempfile.new(["vagrant", ".iso"])
file_destination = Pathname.new(tmpfile.path)
tmpfile.delete
else
file_destination = Pathname.new(file_destination.to_s)
# If the file destination path is a folder, target the output to a randomly named
# file in that dir
if file_destination.extname != ".iso"
file_destination = file_destination.join("#{rand(36**6).to_s(36)}_vagrant.iso")
end
end
# Ensure destination directory is available
FileUtils.mkdir_p(File.dirname(file_destination.to_s))
# If the destrination does not exist or there have been changes in the source directory since the last build, then build
if !file_destination.exist? || Vagrant::Util::Directory.directory_changed?(source_directory, file_destination.mtime)
@ -41,6 +55,7 @@ module VagrantPlugins
iso_command << "-hfs"
iso_command << "-iso"
iso_command << "-joliet"
iso_command << "-ov"
iso_command.concat(Vagrant::Util::MapCommandOptions.map_to_command_options(extra_opts, cmd_flag="-"))
iso_command << "-o"
iso_command << file_destination.to_s
@ -55,29 +70,6 @@ module VagrantPlugins
@@logger.info("ISO available at #{file_destination}")
file_destination
end
# Determines a valid file path for an output file
# and ensures parent directory exists
#
# @param [String, nil] (optional) path to output file
# @return [Pathname] path to output file
def self.output_file(file_destination=nil)
if file_destination.nil?
@@logger.info("No file destination specified, creating temp location")
tmpfile = Tempfile.new("vagrant-iso")
file_destination = Pathname.new(tmpfile.path)
tmpfile.delete
else
file_destination = Pathname.new(file_destination.to_s)
if file_destination.extname != ".iso"
file_destination = file_destination.join("#{rand(36**6).to_s(36)}_vagrant-iso")
end
end
@@logger.info("Targeting to create ISO at #{file_destination}")
# Ensure destination directory is available
FileUtils.mkdir_p(File.dirname(file_destination.to_s))
file_destination
end
end
end
end

View File

@ -22,16 +22,15 @@ describe VagrantPlugins::HostDarwin::Cap::FsISO do
describe ".create_iso" do
let(:file_destination) { "/woo/out.iso" }
let(:file_destination_path) { Pathname.new(file_destination)}
before do
allow(subject).to receive(:output_file).with(any_args).and_return(file_destination_path)
allow(file_destination_path).to receive(:exist?).and_return(false)
allow(file_destination).to receive(:nil?).and_return(false)
allow(FileUtils).to receive(:mkdir_p)
end
it "builds an iso" do
expect(Vagrant::Util::Subprocess).to receive(:execute).with(
"hdiutil", "makehybrid", "-hfs", "-iso", "-joliet", "-o", /.iso/, /\/foo\/src/
"hdiutil", "makehybrid", "-hfs", "-iso", "-joliet", "-ov", "-o", /.iso/, /\/foo\/src/
).and_return(double(exit_code: 0))
output = subject.create_iso(env, "/foo/src", "/woo/out.iso")
@ -40,13 +39,22 @@ describe VagrantPlugins::HostDarwin::Cap::FsISO do
it "builds an iso with args" do
expect(Vagrant::Util::Subprocess).to receive(:execute).with(
"hdiutil", "makehybrid", "-hfs", "-iso", "-joliet", "-default-volume-name", "cidata", "-o", /.iso/, /\/foo\/src/
"hdiutil", "makehybrid", "-hfs", "-iso", "-joliet", "-ov", "-default-volume-name", "cidata", "-o", /.iso/, /\/foo\/src/
).and_return(double(exit_code: 0))
output = subject.create_iso(env, "/foo/src", "/woo/out.iso", extra_opts={"default-volume-name" => "cidata"})
expect(output.to_s).to eq("/woo/out.iso")
end
it "builds an iso given a file destination without an extension" do
expect(Vagrant::Util::Subprocess).to receive(:execute).with(
"hdiutil", "makehybrid", "-hfs", "-iso", "-joliet", "-ov", "-o", /.iso/, /\/foo\/src/
).and_return(double(exit_code: 0))
output = subject.create_iso(env, "/foo/src", "/woo/out_dir")
expect(output.to_s).to match(/\/woo\/out_dir\/[\w]{6}_vagrant.iso/)
end
it "raises an error if iso build failed" do
allow(Vagrant::Util::Subprocess).to receive(:execute).with(any_args).and_return(double(stdout: "nope", stderr: "nope", exit_code: 1))
expect{ subject.create_iso(env, "/foo/src", "/woo/out.iso") }.to raise_error(Vagrant::Errors::ISOBuildFailed)