From 4a77060805b14d5ac5d37b9fa4afc4ff4759845c Mon Sep 17 00:00:00 2001 From: sophia Date: Tue, 7 Jul 2020 14:41:57 -0500 Subject: [PATCH] Add linux cap --- lib/vagrant/util/caps.rb | 1 + plugins/hosts/linux/cap/fs_iso.rb | 54 ++++++++++++++++ plugins/hosts/linux/plugin.rb | 10 +++ .../plugins/hosts/linux/cap/fs_iso_test.rb | 63 +++++++++++++++++++ 4 files changed, 128 insertions(+) create mode 100644 plugins/hosts/linux/cap/fs_iso.rb create mode 100644 test/unit/plugins/hosts/linux/cap/fs_iso_test.rb diff --git a/lib/vagrant/util/caps.rb b/lib/vagrant/util/caps.rb index 04a9f2160..aed82aeb1 100644 --- a/lib/vagrant/util/caps.rb +++ b/lib/vagrant/util/caps.rb @@ -1,4 +1,5 @@ require "vagrant/util/directory" +require "vagrant/util/subprocess" module Vagrant module Util diff --git a/plugins/hosts/linux/cap/fs_iso.rb b/plugins/hosts/linux/cap/fs_iso.rb new file mode 100644 index 000000000..fc18cef1f --- /dev/null +++ b/plugins/hosts/linux/cap/fs_iso.rb @@ -0,0 +1,54 @@ +require "tempfile" +require 'fileutils' +require 'pathname' +require "vagrant/util/subprocess" +require "vagrant/util/directory" +require "vagrant/util/caps" + +module VagrantPlugins + module HostLinux + module Cap + class FsISO + extend Vagrant::Util::Caps::BuildISO + + @@logger = Log4r::Logger.new("vagrant::host::linux::fs_iso") + + BUILD_ISO_CMD = "mkisofs".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_file_destination(extra_opts[:file_destination]) + + # 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) + @@logger.info("Building ISO from source #{source_directory}") + iso_command = [BUILD_ISO_CMD] + iso_command << "-joliet" + iso_command.concat(["-volid", extra_opts[:volume_id]]) if extra_opts[:volume_id] + iso_command << "-o" + iso_command << file_destination.to_s + iso_command << source_directory.to_s + result = Vagrant::Util::Subprocess.execute(*iso_command) + + if result.exit_code != 0 + raise Vagrant::Errors::ISOBuildFailed, cmd: iso_command.join(" "), stdout: result.stdout, stderr: result.stderr + end + end + + @@logger.info("ISO available at #{file_destination}") + file_destination + end + end + end + end +end diff --git a/plugins/hosts/linux/plugin.rb b/plugins/hosts/linux/plugin.rb index a4c6311bf..e2fb24c67 100644 --- a/plugins/hosts/linux/plugin.rb +++ b/plugins/hosts/linux/plugin.rb @@ -11,6 +11,16 @@ module VagrantPlugins Host end + host_capability("linux", "isofs_available") do + require_relative "cap/fs_iso" + Cap::FsISO + end + + host_capability("linux", "create_iso") do + require_relative "cap/fs_iso" + Cap::FsISO + end + host_capability("linux", "nfs_export") do require_relative "cap/nfs" Cap::NFS diff --git a/test/unit/plugins/hosts/linux/cap/fs_iso_test.rb b/test/unit/plugins/hosts/linux/cap/fs_iso_test.rb new file mode 100644 index 000000000..d44c19b8c --- /dev/null +++ b/test/unit/plugins/hosts/linux/cap/fs_iso_test.rb @@ -0,0 +1,63 @@ +require "pathname" +require_relative "../../../../base" +require_relative "../../../../../../plugins/hosts/linux/cap/fs_iso" + +describe VagrantPlugins::HostLinux::Cap::FsISO do + include_context "unit" + + let(:subject){ VagrantPlugins::HostLinux::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 + + it "does not find iso building utility when not available" do + expect(Vagrant::Util::Which).to receive(:which).and_return(false) + expect(subject.isofs_available(env)).to eq(false) + 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( + "mkisofs", "-joliet", "-o", /.iso/, /\/foo\/src/ + ).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( + "mkisofs", "-joliet", "-volid", "cidata", "-o", /.iso/, /\/foo\/src/ + ).and_return(double(exit_code: 0)) + + output = subject.create_iso(env, "/foo/src", file_destination: file_destination, volume_id: "cidata") + 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( + "mkisofs", "-joliet", "-o", /.iso/, /\/foo\/src/ + ).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: "/woo/out.iso") }.to raise_error(Vagrant::Errors::ISOBuildFailed) + end + end +end