From f5a957f949d77a18cc33db28c056e66897195ff2 Mon Sep 17 00:00:00 2001 From: Chris Roberts Date: Tue, 24 Jul 2018 14:03:53 -0700 Subject: [PATCH] WIP --- plugins/hosts/darwin/cap/fs_iso.rb | 63 ++++++++++++++++++++++++++ plugins/hosts/darwin/plugin.rb | 10 ++++ plugins/kernel_v2/config/cloud_init.rb | 50 ++++++++++++++++++++ plugins/kernel_v2/plugin.rb | 11 +++-- 4 files changed, 131 insertions(+), 3 deletions(-) create mode 100644 plugins/hosts/darwin/cap/fs_iso.rb create mode 100644 plugins/kernel_v2/config/cloud_init.rb diff --git a/plugins/hosts/darwin/cap/fs_iso.rb b/plugins/hosts/darwin/cap/fs_iso.rb new file mode 100644 index 000000000..8a2d858eb --- /dev/null +++ b/plugins/hosts/darwin/cap/fs_iso.rb @@ -0,0 +1,63 @@ +require "tempfile" + +module VagrantPlugins + module HostDarwin + module Cap + class FsISO + @@logger = Log4r::Logger.new("vagrant::host::darwin::fs_iso") + + # Check that the host has the ability to generate ISOs + # + # @param [Vagrant::Environment] env + # @return [Boolean] + def self.isofs_available(env) + !!Vagrant::Util::Which.which("hdiutil") + end + + # Generate an ISO file of the given source directory + # + # @param [Vagrant::Environment] env + # @param [String, Pathname] source_directory Contents of ISO + # @param [String, Pathname, nil] file_destination Location to store ISO + # @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, file_destination=nil) + if file_destination.nil? + tmpfile = Tempfile.new("vagrant-iso") + file_destination = Pathname.new(tmpfile.path) + tmpfile.delete + else + file_destination = Pathname.new(file_destination.to_s) + end + source_directory = Pathname.new(source_directory) + if iso_update_required?(file_destination, source_directory) + # Ensure destination directory is available + FileUtils.mkdir_p(file_destination.to_s) + result = Vagrant::Util::Subprocess.execute("hdiutil", "makehybrid", "-o", + file_destination.to_s, "-hfs", "-joliet", "-iso", "-default-volume-name", + "cidata", source_directory.to_s) + if result.exit_code != 0 + raise "Failed to create ISO!" + end + end + file_destination + end + + # Check if source directory has any new updates + # + # @param [Pathname] iso_path Path to ISO file + # @param [Pathname] dir_path Path to source directory + # @return [Boolean] + def self.iso_update_required?(iso_path, dir_path) + Dir.glob(dir_path.join("**/**/*")).each do |path| + if File.mtime > iso_path.mtime + return true + end + end + false + end + end + end + end +end diff --git a/plugins/hosts/darwin/plugin.rb b/plugins/hosts/darwin/plugin.rb index d27e2eda4..cbc1fe1d7 100644 --- a/plugins/hosts/darwin/plugin.rb +++ b/plugins/hosts/darwin/plugin.rb @@ -11,6 +11,16 @@ module VagrantPlugins Host end + host_capability("darwin", "iso_available") do + require_relative "cap/fs_iso" + Cap::FsISO + end + + host_capability("darwin", "create_iso") do + require_relative "cap/fs_iso" + Cap::FsISO + end + host_capability("darwin", "provider_install_virtualbox") do require_relative "cap/provider_install_virtualbox" Cap::ProviderInstallVirtualBox diff --git a/plugins/kernel_v2/config/cloud_init.rb b/plugins/kernel_v2/config/cloud_init.rb new file mode 100644 index 000000000..271e9df83 --- /dev/null +++ b/plugins/kernel_v2/config/cloud_init.rb @@ -0,0 +1,50 @@ +require "vagrant" + +module VagrantPlugins + module Kernel_V2 + class CloudInit < Vagrant.plugin("2", :config) + + DEFAULT_SOURCE_PATTERN = "**/**/*".freeze + + attr_accessor :iso_path + attr_accessor :source_files_pattern + attr_accessor :source_directory + + def initialize + @iso_path = UNSET_VALUE + @source_files_pattern = UNSET_VALUE + @source_directory = UNSET_VALUE + end + + def finalize! + if @iso_path != UNSET_VALUE + @iso_path = Pathname.new(@iso_path.to_s) + else + @iso_path = nil + end + if @source_files_pattern == UNSET_VALUE + @source_files_pattern = DEFAULT_SOURCE_PATTERN + end + if @source_directory != UNSET_VALUE + @source_directory = Pathname.new(@source_directory.to_s) + else + @source_directory = nil + end + end + + def validate(machine) + errors = _detected_errors + if @source_directory.nil? || !@source_directory.exist? + errors << I18n.t("vagrant.config.cloud_init.invalid_source_directory", + directory: source_directory.to_s) + end + + {"cloud_init" => errors} + end + + def to_s + "CloudInit" + end + end + end +end diff --git a/plugins/kernel_v2/plugin.rb b/plugins/kernel_v2/plugin.rb index 280470d19..846106828 100644 --- a/plugins/kernel_v2/plugin.rb +++ b/plugins/kernel_v2/plugin.rb @@ -15,9 +15,9 @@ module VagrantPlugins # "kernel_v1", none of these configuration classes are upgradable. # This is by design, since we can't be sure if they're upgradable # until another version is available. - config("ssh") do - require File.expand_path("../config/ssh", __FILE__) - SSHConfig + config("cloud_init") do + require File.expand_path("../config/cloud_init", __FILE__) + CloudInit end config("package") do @@ -30,6 +30,11 @@ module VagrantPlugins PushConfig end + config("ssh") do + require File.expand_path("../config/ssh", __FILE__) + SSHConfig + end + config("vagrant") do require File.expand_path("../config/vagrant", __FILE__) VagrantConfig