diff --git a/CHANGELOG.md b/CHANGELOG.md index da04624b9..a4ae97063 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -56,6 +56,8 @@ BUG FIXES: [GH-2792] - providers/virtualbox: Enabling internal networks by just setting "true" works properly. [GH-2751] + - synced\_folders/nfs: NFS entries are pruned on every `vagrant up`, + if there are any to prune. [GH-2738] ## 1.4.3 (January 2, 2014) diff --git a/plugins/synced_folders/nfs/action_cleanup.rb b/plugins/synced_folders/nfs/action_cleanup.rb new file mode 100644 index 000000000..6c1b72d76 --- /dev/null +++ b/plugins/synced_folders/nfs/action_cleanup.rb @@ -0,0 +1,24 @@ +require "log4r" + +module VagrantPlugins + module SyncedFolderNFS + class ActionCleanup + def initialize(app, env) + @app = app + @logger = Log4r::Logger.new("vagrant::synced_folders::nfs") + end + + def call(env) + if !env[:nfs_valid_ids] + @logger.warn("nfs_valid_ids not set, cleanup cannot occur") + return @app.call(env) + end + + @logger.info("NFS pruning. Valid IDs: #{env[:nfs_valid_ids].inspect}") + env[:machine].env.host.capability( + :nfs_prune, env[:machine].ui, env[:nfs_valid_ids]) + @app.call(env) + end + end + end +end diff --git a/plugins/synced_folders/nfs/plugin.rb b/plugins/synced_folders/nfs/plugin.rb index b6f1fa491..6751b7905 100644 --- a/plugins/synced_folders/nfs/plugin.rb +++ b/plugins/synced_folders/nfs/plugin.rb @@ -2,6 +2,22 @@ require "vagrant" module VagrantPlugins module SyncedFolderNFS + # This plugin implements NFS synced folders. In order to take advantage + # of NFS synced folders, some provider-specific assistance is required. + # Within the middleware sequences, some data must be put into the + # environment state bag: + # + # * `nfs_host_ip` (string) - The IP of the host machine that the NFS + # client in the machine should talk to. + # * `nfs_machine_ip` (string) - The IP of the guest machine that the NFS + # server should serve the folders to. + # * `nfs_valid_ids` (array of strings) - A list of IDs that are "valid" + # and should not be pruned. The synced folder implementation will + # regularly prune NFS exports of invalid IDs. + # + # If any of these variables are not set, an internal exception will be + # raised. + # class Plugin < Vagrant.plugin("2") name "NFS synced folders" description <<-EOF @@ -10,14 +26,21 @@ module VagrantPlugins EOF config("nfs") do - require File.expand_path("../config", __FILE__) + require_relative "config" Config end synced_folder("nfs", 5) do - require File.expand_path("../synced_folder", __FILE__) + require_relative "synced_folder" SyncedFolder end + + action_hook("nfs_cleanup") do |hook| + require_relative "action_cleanup" + hook.before( + Vagrant::Action::Builtin::SyncedFolderCleanup, + ActionCleanup) + end end end end diff --git a/test/unit/plugins/synced_folders/nfs/action_cleanup_test.rb b/test/unit/plugins/synced_folders/nfs/action_cleanup_test.rb new file mode 100644 index 000000000..4f2cd489e --- /dev/null +++ b/test/unit/plugins/synced_folders/nfs/action_cleanup_test.rb @@ -0,0 +1,42 @@ +require_relative "../../../base" + +require Vagrant.source_root.join("plugins/synced_folders/nfs/action_cleanup") + +describe VagrantPlugins::SyncedFolderNFS::ActionCleanup do + include_context "unit" + + let(:iso_env) do + # We have to create a Vagrantfile so there is a root path + env = isolated_environment + env.vagrantfile("") + env.create_vagrant_env + end + + let(:host) { double("host") } + let(:machine) { iso_env.machine(iso_env.machine_names[0], :virtualbox) } + + let(:app) { lambda {} } + let(:env) { { + machine: machine, + } } + + subject { described_class.new(app, env) } + + before do + machine.env.stub(host: host) + end + + it "does nothing if there are no valid IDs" do + app.should_receive(:call).with(env) + subject.call(env) + end + + it "prunes the NFS entries if valid IDs are given" do + env[:nfs_valid_ids] = [1,2,3] + + host.should_receive(:capability).with(:nfs_prune, machine.ui, [1,2,3]).ordered + app.should_receive(:call).with(env).ordered + + subject.call(env) + end +end