core: Allow options to be passed down into SF cleanup

This commit is contained in:
Mitchell Hashimoto 2013-12-06 16:40:24 -08:00
parent deb6ba07b2
commit 2c65d247da
9 changed files with 44 additions and 36 deletions

View File

@ -26,6 +26,20 @@ module Vagrant
return nil return nil
end end
# This finds the options in the env that are set for a given
# synced folder type.
def impl_opts(name, env)
{}.tap do |result|
env.each do |k, v|
if k.to_s.start_with?("#{name}_")
k = k.dup if !k.is_a?(Symbol)
v = v.dup if !v.is_a?(Symbol)
result[k] = v
end
end
end
end
# This returns the available synced folder implementations. This # This returns the available synced folder implementations. This
# is a separate method so that it can be easily stubbed by tests. # is a separate method so that it can be easily stubbed by tests.
def plugins def plugins

View File

@ -21,7 +21,8 @@ module Vagrant
# Go through each folder and do cleanup # Go through each folder and do cleanup
folders.each_key do |impl_name| folders.each_key do |impl_name|
@logger.info("Invoking synced folder cleanup for: #{impl_name}") @logger.info("Invoking synced folder cleanup for: #{impl_name}")
plugins[impl_name.to_sym][0].new.cleanup(env[:machine]) plugins[impl_name.to_sym][0].new.cleanup(
env[:machine], impl_opts(impl_name, env))
end end
@app.call(env) @app.call(env)

View File

@ -73,20 +73,6 @@ module Vagrant
plugins[impl_name.to_sym][0].new.enable(env[:machine], fs, impl_opts(impl_name, env)) plugins[impl_name.to_sym][0].new.enable(env[:machine], fs, impl_opts(impl_name, env))
end end
end end
# This finds the options in the env that are set for a given
# synced folder type.
def impl_opts(name, env)
{}.tap do |result|
env.each do |k, v|
if k.to_s.start_with?("#{name}_")
k = k.dup if !k.is_a?(Symbol)
v = v.dup if !v.is_a?(Symbol)
result[k] = v
end
end
end
end
end end
end end
end end

View File

@ -27,10 +27,14 @@ module Vagrant
end end
# This is called after destroying the machine during a # This is called after destroying the machine during a
# `vagrant destroy`. # `vagrant destroy` and also prior to syncing folders during
# a `vagrant up`.
# #
# No return value. # No return value.
def cleanup(machine) #
# @param [Machine] machine
# @param [Hash] opts
def cleanup(machine, opts)
end end
end end
end end

View File

@ -61,7 +61,7 @@ module VagrantPlugins
end end
end end
def cleanup(machine) def cleanup(machine, opts)
driver(machine).clear_shared_folders if machine.id && machine.id != "" driver(machine).clear_shared_folders if machine.id && machine.id != ""
end end

View File

@ -51,7 +51,7 @@ module VagrantPlugins
:mount_nfs_folder, nfsopts[:nfs_host_ip], mount_folders) :mount_nfs_folder, nfsopts[:nfs_host_ip], mount_folders)
end end
def cleanup(machine) def cleanup(machine, opts)
# Get the ID of all active machines. # Get the ID of all active machines.
ids = machine.env.active_machines.map do |name, provider| ids = machine.env.active_machines.map do |name, provider|
machine.env.machine(name, provider).id machine.env.machine(name, provider).id

View File

@ -38,6 +38,21 @@ describe Vagrant::Action::Builtin::MixinSyncedFolders do
end end
end end
describe "impl_opts" do
it "should return only relevant keys" do
env = {
:foo_bar => "baz",
:bar_bar => "nope",
:foo_baz => "bar",
}
result = subject.impl_opts("foo", env)
result.length.should == 2
result[:foo_bar].should == "baz"
result[:foo_baz].should == "bar"
end
end
describe "synced_folders" do describe "synced_folders" do
let(:folders) { {} } let(:folders) { {} }
let(:plugins) { {} } let(:plugins) { {} }

View File

@ -38,7 +38,7 @@ describe Vagrant::Action::Builtin::SyncedFolderCleanup do
class_variable_get(:@@clean) class_variable_get(:@@clean)
end end
def cleanup(machine) def cleanup(machine, opts)
self.class.class_variable_set(:@@clean, true) self.class.class_variable_set(:@@clean, true)
end end
end end
@ -75,7 +75,10 @@ describe Vagrant::Action::Builtin::SyncedFolderCleanup do
} }
expect_any_instance_of(tracker).to receive(:cleanup). expect_any_instance_of(tracker).to receive(:cleanup).
with(env[:machine]) with(env[:machine], { tracker_foo: :bar })
# Test that the impl-specific opts are passed through
env[:tracker_foo] = :bar
subject.call(env) subject.call(env)
end end

View File

@ -113,19 +113,4 @@ describe Vagrant::Action::Builtin::SyncedFolders do
actual["root"][:foo].should == "bar" actual["root"][:foo].should == "bar"
end end
end end
describe "impl_opts" do
it "should return only relevant keys" do
env = {
:foo_bar => "baz",
:bar_bar => "nope",
:foo_baz => "bar",
}
result = subject.impl_opts("foo", env)
result.length.should == 2
result[:foo_bar].should == "baz"
result[:foo_baz].should == "bar"
end
end
end end