This commit is contained in:
Chris Roberts 2018-07-24 14:03:53 -07:00 committed by sophia
parent aef97fa061
commit f5a957f949
4 changed files with 131 additions and 3 deletions

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -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