diff --git a/lib/vagrant/plugin/remote/synced_folder.rb b/lib/vagrant/plugin/remote/synced_folder.rb index 645145ab4..40415360c 100644 --- a/lib/vagrant/plugin/remote/synced_folder.rb +++ b/lib/vagrant/plugin/remote/synced_folder.rb @@ -52,6 +52,14 @@ module Vagrant end end + # @param [Machine] machine + # @param [Hash] folders The folders to remove. This will not contain + # any folders that should remain. + # @param [Hash] opts Any options for the synced folders. + def prepare(machine, folders, opts) + client.prepare(machine.to_proto, folders, opts) + end + # @param [Machine] machine # @param [Hash] folders The folders to remove. This will not contain # any folders that should remain. diff --git a/plugins/commands/serve/client/synced_folder.rb b/plugins/commands/serve/client/synced_folder.rb index c0ba328fd..0801e5d07 100644 --- a/plugins/commands/serve/client/synced_folder.rb +++ b/plugins/commands/serve/client/synced_folder.rb @@ -22,6 +22,28 @@ module VagrantPlugins run_func(machine) end + # Generate callback and spec for required arguments + # + # @return [SDK::FuncSpec, Proc] + def prepare_func + spec = client.prepare_spec(Empty.new) + cb = proc do |args| + client.prepare(args) + end + [spec, cb] + end + + # Prepare synced folders on guest + # + # @param machine [Vagrant::Machine] Guest machine + # @param folders [Array] Synced folders + # @param opts [Hash] Options for folders + def prepare(machine, folders, opts) + spec, cb = prepare_func + cb.call(generate_funcspec_args(spec, + machine, folders, Type::Direct.new(value: opts))) + end + # Generate callback and spec for required arguments # # @return [SDK::FuncSpec, Proc] diff --git a/plugins/commands/serve/service/synced_folder_service.rb b/plugins/commands/serve/service/synced_folder_service.rb index e54f8140a..98c430d2f 100644 --- a/plugins/commands/serve/service/synced_folder_service.rb +++ b/plugins/commands/serve/service/synced_folder_service.rb @@ -37,20 +37,55 @@ module VagrantPlugins end def usable(req, ctx) - with_info(ctx, broker: broker) do |info| - plugin_name = info.plugin_name + plugins = Vagrant.plugin("2").local_manager.synced_folders + with_plugin(ctx, plugins, broker: broker) do |plugin| target = mapper.funcspec_map(req) project = target.project env = Vagrant::Environment.new({client: project}) machine = env.machine(target.name.to_sym, target.provider_name.to_sym) - sf = get_synced_folder_plugin(plugin_name) - logger.debug("got sf #{sf}") - usable = sf.usable?(machine) - logger.debug("usable: #{usable}") - SDK::SyncedFolder::UsableResp.new( - usable: usable, + sf = plugin.new + usable = sf.usable?(machine) + logger.debug("usable: #{usable}") + SDK::SyncedFolder::UsableResp.new( + usable: usable, + ) + end + end + + + def prepare_spec(*_) + SDK::FuncSpec.new( + name: "prepare_spec", + args: [ + SDK::FuncSpec::Value.new( + type: "hashicorp.vagrant.sdk.Args.Target.Machine", + name: "", + ), + SDK::FuncSpec::Value.new( + type: "hashicorp.vagrant.sdk.Args.Hash", + name: "", + ), + SDK::FuncSpec::Value.new( + type: "hashicorp.vagrant.sdk.Args.Direct", + name: "", + ), + ], + ) + end + + def prepare(req, ctx) + plugins = Vagrant.plugin("2").local_manager.synced_folders + with_plugin(ctx, plugins, broker: broker) do |plugin| + machine, folders, opts = mapper.funcspec_map( + req, + expect: [Vagrant::Machine, Hash, Type::Direct] ) + # change the top level folders hash key to a string + folders.transform_keys!(&:to_s) + sf = plugin.new + sf.prepare(machine, folders, opts) + Empty.new end end @@ -75,14 +110,15 @@ module VagrantPlugins end def enable(req, ctx) - with_info(ctx, broker: broker) do |info| - plugin_name = info.plugin_name + plugins = Vagrant.plugin("2").local_manager.synced_folders + with_plugin(ctx, plugins, broker: broker) do |plugin| machine, folders, opts = mapper.funcspec_map( req.func_args, expect: [Vagrant::Machine, Hash, Type::Direct] ) - - sf = get_synced_folder_plugin(plugin_name) + # change the top level folders hash key to a string + folders.transform_keys!(&:to_s) + sf = plugin.new sf.enable(machine, folders, opts) Empty.new end @@ -97,7 +133,7 @@ module VagrantPlugins name: "", ), SDK::FuncSpec::Value.new( - type: "hashicorp.vagrant.sdk.Args.Folder", + type: "hashicorp.vagrant.sdk.Args.Hash", name: "", ), SDK::FuncSpec::Value.new( @@ -109,14 +145,15 @@ module VagrantPlugins end def disable(req, ctx) - with_info(ctx, broker: broker) do |info| - plugin_name = info.plugin_name + plugins = Vagrant.plugin("2").local_manager.synced_folders + with_plugin(ctx, plugins, broker: broker) do |plugin| machine, folders, opts = mapper.funcspec_map( req.func_args, expect: [Vagrant::Machine, Hash, Type::Direct] ) - - sf = get_synced_folder_plugin(plugin_name) + # change the top level folders hash key to a string + folders.transform_keys!(&:to_s) + sf = plugin.new sf.disable(machine, folders, opts) Empty.new end @@ -151,18 +188,6 @@ module VagrantPlugins Empty.new end end - - private - - def get_synced_folder_plugin(plugin_name) - synced_folders = Vagrant.plugin("2").local_manager.synced_folders - logger.debug("got synced folders #{synced_folders}") - plugin = [plugin_name.to_s.to_sym].to_a.first - logger.debug("got plugin #{plugin}") - sf = plugin.new - logger.debug("got sf #{sf}") - sf - end end end end