Add synced_folder_capability

This commit is contained in:
sophia 2020-07-30 14:35:44 -05:00
parent 2063111ab5
commit afd2a28f60
8 changed files with 74 additions and 1 deletions

View File

@ -1,4 +1,5 @@
require_relative "util/ssh"
require_relative "action/builtin/mixin_synced_folders"
require "digest/md5"
require "thread"
@ -10,6 +11,8 @@ module Vagrant
# API for querying the state and making state changes to the machine, which
# is backed by any sort of provider (VirtualBox, VMware, etc.).
class Machine
include Vagrant::Action::Builtin::MixinSyncedFolders
# The box that is backing this machine.
#
# @return [Box]

View File

@ -64,6 +64,11 @@ module Vagrant
# @return [Registry<Symbol, Array<Class, Integer>>]
attr_reader :synced_folders
# This contains all the registered synced folder capabilities.
#
# @return [Hash<Symbol, Registry>]
attr_reader :synced_folder_capabilities
def initialize
# The action hooks hash defaults to []
@action_hooks = Hash.new { |h, k| h[k] = [] }
@ -78,6 +83,7 @@ module Vagrant
@provider_capabilities = Hash.new { |h, k| h[k] = Registry.new }
@pushes = Registry.new
@synced_folders = Registry.new
@synced_folder_capabilities = Hash.new { |h, k| h[k] = Registry.new }
end
end
end

View File

@ -258,6 +258,20 @@ module Vagrant
end
end
# This returns all the registered synced folder capabilities.
#
# @return [Hash]
def synced_folder_capabilities
results = Hash.new { |h, k| h[k] = Registry.new }
@registered.each do |plugin|
plugin.components.synced_folder_capabilities.each do |synced_folder, caps|
results[synced_folder].merge!(caps)
end
end
results
end
# This registers a plugin. This should _NEVER_ be called by the public
# and should only be called from within Vagrant. Vagrant will
# automatically register V2 plugins when a name is set on the

View File

@ -247,6 +247,18 @@ module Vagrant
nil
end
# Defines a capability for the given synced folder. The block should return
# a class/module that has a method with the capability name, ready
# to be executed. This means that if it is an instance method,
# the block should return an instance of the class.
#
# @param [String] host The name of the synced folder
# @param [String] cap The name of the capability
def self.synced_folder_capability(synced_folder, cap, &block)
components.synced_folder_capabilities[synced_folder.to_sym].register(cap.to_sym, &block)
nil
end
# Returns the internal data associated with this plugin. This
# should NOT be called by the general public.
#

View File

@ -3,6 +3,8 @@ module Vagrant
module V2
# This is the base class for a synced folder implementation.
class SyncedFolder
include CapabilityHost
# This is called early when the synced folder is set to determine
# if this implementation can be used for this machine. This should
# return true or false.
@ -54,6 +56,12 @@ module Vagrant
# @param [Hash] opts
def cleanup(machine, opts)
end
def _initialize(machine, synced_folder_type)
plugins = Vagrant.plugin("2").manager.synced_folders
capabilities = Vagrant.plugin("2").manager.synced_folder_capabilities
initialize_capabilities!(synced_folder_type, plugins, capabilities, machine)
end
end
end
end

View File

@ -508,7 +508,6 @@ module VagrantPlugins
def finalize!
# Defaults
@allow_fstab_modification = true if @allow_fstab_modification == UNSET_VALUE
@allowed_synced_folder_types = nil if @allowed_synced_folder_types == UNSET_VALUE
@base_mac = nil if @base_mac == UNSET_VALUE
@base_address = nil if @base_address == UNSET_VALUE
@ -726,6 +725,21 @@ module VagrantPlugins
def validate(machine, ignore_provider=nil)
errors = _detected_errors
if @allow_fstab_modification == UNSET_VALUE
plugins = Vagrant.plugin("2").manager.synced_folders
machine.synced_folders.each do |type, _|
instance = plugins[type.to_sym][0].new
instance._initialize(machine, type)
if instance.capability?(:default_fstab_modification)
if instance.capability(:default_fstab_modification) == false
@allow_fstab_modification = false
break
end
end
end
@allow_fstab_modification = true if @allow_fstab_modification == UNSET_VALUE
end
if !box && !clone && !machine.provider_options[:box_optional]
errors << I18n.t("vagrant.config.vm.box_missing")
end

View File

@ -0,0 +1,11 @@
module VagrantPlugins
module SyncedFolderSMB
module Cap
module DefaultFstabModification
def self.default_fstab_modification(machine)
return false
end
end
end
end
end

View File

@ -23,6 +23,11 @@ module VagrantPlugins
SyncedFolder
end
synced_folder_capability("smb", "default_fstab_modification") do
require_relative "cap/default_fstab_modification"
Cap::DefaultFstabModification
end
protected
def self.init!