diff --git a/lib/vagrant/action/vm/clear_nfs_exports.rb b/lib/vagrant/action/vm/clear_nfs_exports.rb new file mode 100644 index 000000000..1f4043d9b --- /dev/null +++ b/lib/vagrant/action/vm/clear_nfs_exports.rb @@ -0,0 +1,20 @@ +require File.join(File.dirname(__FILE__), 'nfs_helpers') + +module Vagrant + class Action + module VM + class ClearNFSExports + include NFSHelpers + + def initialize(app, env) + @app = app + end + + def call(env) + clear_nfs_exports(env) + @app.call(env) + end + end + end + end +end diff --git a/lib/vagrant/action/vm/nfs.rb b/lib/vagrant/action/vm/nfs.rb index bd222faf2..4989a0696 100644 --- a/lib/vagrant/action/vm/nfs.rb +++ b/lib/vagrant/action/vm/nfs.rb @@ -1,3 +1,5 @@ +require File.join(File.dirname(__FILE__), 'nfs_helpers') + module Vagrant class Action module VM @@ -15,6 +17,7 @@ module Vagrant # class NFS include ExceptionCatcher + include NFSHelpers def initialize(app,env) @app = app @@ -27,7 +30,12 @@ module Vagrant @env = env extract_folders - export_folders if !folders.empty? + + if !folders.empty? + export_folders + clear_nfs_exports(env) + end + return if env.error? @app.call(env) diff --git a/lib/vagrant/action/vm/nfs_helpers.rb b/lib/vagrant/action/vm/nfs_helpers.rb new file mode 100644 index 000000000..d5ed5332a --- /dev/null +++ b/lib/vagrant/action/vm/nfs_helpers.rb @@ -0,0 +1,11 @@ +module Vagrant + class Action + module VM + module NFSHelpers + def clear_nfs_exports(env) + env["host"].nfs_cleanup if env["host"] + end + end + end + end +end diff --git a/lib/vagrant/hosts/base.rb b/lib/vagrant/hosts/base.rb index ba1d76ec5..378949dba 100644 --- a/lib/vagrant/hosts/base.rb +++ b/lib/vagrant/hosts/base.rb @@ -67,6 +67,10 @@ module Vagrant # @param [Hash] folders Shared folders to sync. def nfs_export(ip, folders) end + + # Cleans up the exports for the current VM. + def nfs_cleanup + end end end end diff --git a/lib/vagrant/hosts/bsd.rb b/lib/vagrant/hosts/bsd.rb index 34122a3ad..21d91f301 100644 --- a/lib/vagrant/hosts/bsd.rb +++ b/lib/vagrant/hosts/bsd.rb @@ -28,6 +28,16 @@ module Vagrant # is not starting system("sudo nfsd restart") end + + def nfs_cleanup + system("cat /etc/exports | grep 'VAGRANT-BEGIN: #{env.vm.uuid}' > /dev/null 2>&1") + + if $?.to_i == 0 + # Use sed to just strip out the block of code which was inserted + # by Vagrant + system("sudo sed -e '/^# VAGRANT-BEGIN: #{env.vm.uuid}/,/^# VAGRANT-END: #{env.vm.uuid}/ d' -i bak /etc/exports") + end + end end end end diff --git a/test/vagrant/action/vm/clear_nfs_exports_test.rb b/test/vagrant/action/vm/clear_nfs_exports_test.rb new file mode 100644 index 000000000..a65fc7523 --- /dev/null +++ b/test/vagrant/action/vm/clear_nfs_exports_test.rb @@ -0,0 +1,22 @@ +require File.join(File.dirname(__FILE__), '..', '..', '..', 'test_helper') + +class ClearNFSExportsActionTest < Test::Unit::TestCase + setup do + @klass = Vagrant::Action::VM::ClearNFSExports + @app, @env = mock_action_data + @env.env.stubs(:host).returns(Vagrant::Hosts::Base.new(@env)) + + @instance = @klass.new(@app, @env) + end + + should "include the NFS helpers module" do + assert @klass.included_modules.include?(Vagrant::Action::VM::NFSHelpers) + end + + should "clear NFS exports then continue chain" do + seq = sequence("seq") + @instance.expects(:clear_nfs_exports).with(@env).in_sequence(seq) + @app.expects(:call).with(@env).in_sequence(seq) + @instance.call(@env) + end +end diff --git a/test/vagrant/action/vm/nfs_helpers_test.rb b/test/vagrant/action/vm/nfs_helpers_test.rb new file mode 100644 index 000000000..ba7a1a066 --- /dev/null +++ b/test/vagrant/action/vm/nfs_helpers_test.rb @@ -0,0 +1,23 @@ +require File.join(File.dirname(__FILE__), '..', '..', '..', 'test_helper') + +class NFSHelpersVMActionTest < Test::Unit::TestCase + setup do + @klass = Class.new + @klass.send(:include, Vagrant::Action::VM::NFSHelpers) + @app, @env = mock_action_data + + @instance = @klass.new + end + + should "clear NFS exports for the environment if the host exists" do + @host = mock("host") + @env.env.stubs(:host).returns(@host) + @host.expects(:nfs_cleanup).once + + @instance.clear_nfs_exports(@env) + end + + should "not do anything if host is nil" do + assert_nothing_raised { @instance.clear_nfs_exports(@env) } + end +end diff --git a/test/vagrant/action/vm/nfs_test.rb b/test/vagrant/action/vm/nfs_test.rb index 633755ec1..674416c1c 100644 --- a/test/vagrant/action/vm/nfs_test.rb +++ b/test/vagrant/action/vm/nfs_test.rb @@ -46,6 +46,7 @@ class NFSVMActionTest < Test::Unit::TestCase seq = sequence('seq') @instance.expects(:extract_folders).in_sequence(seq) @instance.expects(:export_folders).in_sequence(seq) + @instance.expects(:clear_nfs_exports).with(@env).in_sequence(seq) @app.expects(:call).with(@env).in_sequence(seq) @instance.expects(:mount_folders).in_sequence(seq) @instance.call(@env) @@ -57,6 +58,7 @@ class NFSVMActionTest < Test::Unit::TestCase seq = sequence('seq') @instance.expects(:extract_folders).in_sequence(seq) @instance.expects(:export_folders).never + @instance.expects(:clear_nfs_exports).never @app.expects(:call).with(@env).in_sequence(seq) @instance.expects(:mount_folders).never @instance.call(@env) @@ -68,6 +70,7 @@ class NFSVMActionTest < Test::Unit::TestCase seq = sequence('seq') @instance.expects(:extract_folders).in_sequence(seq) @instance.expects(:export_folders).in_sequence(seq) + @instance.expects(:clear_nfs_exports).in_sequence(seq) @app.expects(:call).never @instance.call(@env) end diff --git a/test/vagrant/hosts/bsd_test.rb b/test/vagrant/hosts/bsd_test.rb index df476de0c..b79f93cab 100644 --- a/test/vagrant/hosts/bsd_test.rb +++ b/test/vagrant/hosts/bsd_test.rb @@ -5,6 +5,8 @@ class BSDHostTest < Test::Unit::TestCase @klass = Vagrant::Hosts::BSD @env = mock_environment @env.stubs(:vm).returns(Vagrant::VM.new(:env => @env)) + @env.logger.stubs(:info) + @instance = @klass.new(@env) end @@ -53,4 +55,8 @@ class BSDHostTest < Test::Unit::TestCase @instance.nfs_export(@ip, @folders) end end + + context "nfs cleanup" do + # TODO: How to test all the system calls? + end end