diff --git a/plugins/hosts/windows/cap/fs_iso.rb b/plugins/hosts/windows/cap/fs_iso.rb new file mode 100644 index 000000000..dba6e4d24 --- /dev/null +++ b/plugins/hosts/windows/cap/fs_iso.rb @@ -0,0 +1,40 @@ +require "pathname" +require "vagrant/util/caps" + +module VagrantPlugins + module HostWindows + module Cap + class FsISO + extend Vagrant::Util::Caps::BuildISO + + @@logger = Log4r::Logger.new("vagrant::host::windows::fs_iso") + + BUILD_ISO_CMD = "oscdimg".freeze + + # Generate an ISO file of the given source directory + # + # @param [Vagrant::Environment] env + # @param [String] source_directory Contents of ISO + # @param [Map] extra arguments to pass to the iso building command + # :file_destination (string) location to store ISO + # :volume_id (String) to set the volume name + # @return [Pathname] ISO location + # @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, **extra_opts) + source_directory = Pathname.new(source_directory) + file_destination = self.ensure_output_iso(extra_opts[:file_destination]) + + iso_command = [BUILD_ISO_CMD, "-j1", "-o", "-m"] + iso_command << "-l#{extra_opts[:volume_id]}" if extra_opts[:volume_id] + iso_command << source_directory.to_s + iso_command << file_destination.to_s + self.build_iso(iso_command, source_directory, file_destination) + + @@logger.info("ISO available at #{file_destination}") + file_destination + end + end + end + end +end diff --git a/plugins/hosts/windows/plugin.rb b/plugins/hosts/windows/plugin.rb index 5668141fb..f76d5c27d 100644 --- a/plugins/hosts/windows/plugin.rb +++ b/plugins/hosts/windows/plugin.rb @@ -11,6 +11,16 @@ module VagrantPlugins Host end + host_capability("windows", "isofs_available") do + require_relative "cap/fs_iso" + Cap::FsISO + end + + host_capability("windows", "create_iso") do + require_relative "cap/fs_iso" + Cap::FsISO + end + host_capability("windows", "provider_install_virtualbox") do require_relative "cap/provider_install_virtualbox" Cap::ProviderInstallVirtualBox diff --git a/test/unit/plugins/hosts/windows/cap/fs_iso_test.rb b/test/unit/plugins/hosts/windows/cap/fs_iso_test.rb new file mode 100644 index 000000000..5f2ed094a --- /dev/null +++ b/test/unit/plugins/hosts/windows/cap/fs_iso_test.rb @@ -0,0 +1,58 @@ +require "pathname" +require_relative "../../../../base" +require_relative "../../../../../../plugins/hosts/windows/cap/fs_iso" + +describe VagrantPlugins::HostWindows::Cap::FsISO do + include_context "unit" + + let(:subject){ VagrantPlugins::HostWindows::Cap::FsISO } + let(:env) { double("env") } + + describe ".isofs_available" do + it "finds iso building utility when available" do + expect(Vagrant::Util::Which).to receive(:which).and_return(true) + expect(subject.isofs_available(env)).to eq(true) + end + end + + describe ".create_iso" do + let(:file_destination) { "/woo/out.iso" } + + before do + 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( + "oscdimg", "-j1", "-o", "-m", /\/foo\/src/, /.iso/ + ).and_return(double(exit_code: 0)) + + output = subject.create_iso(env, "/foo/src", file_destination: file_destination) + expect(output.to_s).to eq(file_destination) + end + + it "builds an iso with volume_id" do + expect(Vagrant::Util::Subprocess).to receive(:execute).with( + "oscdimg", "-j1", "-o", "-m", "-ltest", /\/foo\/src/, /.iso/ + ).and_return(double(exit_code: 0)) + + output = subject.create_iso(env, "/foo/src", file_destination: file_destination, volume_id: "test") + expect(output.to_s).to eq(file_destination) + end + + it "builds an iso given a file destination without an extension" do + expect(Vagrant::Util::Subprocess).to receive(:execute).with( + "oscdimg", "-j1", "-o", "-m", /\/foo\/src/, /.iso/ + ).and_return(double(exit_code: 0)) + + output = subject.create_iso(env, "/foo/src", file_destination: "/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", file_destination: file_destination) }.to raise_error(Vagrant::Errors::ISOBuildFailed) + end + end +end diff --git a/test/unit/vagrant/util/caps_test.rb b/test/unit/vagrant/util/caps_test.rb index bccc5e9fc..757f73cfa 100644 --- a/test/unit/vagrant/util/caps_test.rb +++ b/test/unit/vagrant/util/caps_test.rb @@ -44,4 +44,4 @@ describe Vagrant::Util::Caps do end end end -end \ No newline at end of file +end