From 97148379d2dee0f6b56835fc1ecb070e07b8f66c Mon Sep 17 00:00:00 2001 From: Mitchell Hashimoto Date: Thu, 21 Nov 2013 17:38:17 -0800 Subject: [PATCH] providers/virtualbox: implement the synced folder plugin --- lib/vagrant.rb | 1 + lib/vagrant/plugin/v2/synced_folder.rb | 4 +- plugins/providers/virtualbox/plugin.rb | 5 ++ plugins/providers/virtualbox/synced_folder.rb | 71 +++++++++++++++++++ .../virtualbox/synced_folder_test.rb | 38 ++++++++++ test/unit/vagrant_test.rb | 17 +++++ 6 files changed, 134 insertions(+), 2 deletions(-) create mode 100644 plugins/providers/virtualbox/synced_folder.rb create mode 100644 test/unit/plugins/providers/virtualbox/synced_folder_test.rb diff --git a/lib/vagrant.rb b/lib/vagrant.rb index 9dfdac559..ee2b61a20 100644 --- a/lib/vagrant.rb +++ b/lib/vagrant.rb @@ -101,6 +101,7 @@ module Vagrant c.register([:"2", :host]) { Plugin::V2::Host } c.register([:"2", :provider]) { Plugin::V2::Provider } c.register([:"2", :provisioner]) { Plugin::V2::Provisioner } + c.register([:"2", :synced_folder]) { Plugin::V2::SyncedFolder } end # This returns a true/false showing whether we're running from the diff --git a/lib/vagrant/plugin/v2/synced_folder.rb b/lib/vagrant/plugin/v2/synced_folder.rb index 1b7a856fd..691e49b45 100644 --- a/lib/vagrant/plugin/v2/synced_folder.rb +++ b/lib/vagrant/plugin/v2/synced_folder.rb @@ -6,10 +6,10 @@ module Vagrant def usable?(machine) end - def prepare(machine) + def prepare(machine, folders) end - def enable(machine) + def enable(machine, folders) end end end diff --git a/plugins/providers/virtualbox/plugin.rb b/plugins/providers/virtualbox/plugin.rb index 8349c5cb1..4b9146f9e 100644 --- a/plugins/providers/virtualbox/plugin.rb +++ b/plugins/providers/virtualbox/plugin.rb @@ -18,6 +18,11 @@ module VagrantPlugins require File.expand_path("../config", __FILE__) Config end + + synced_folder(:virtualbox) do + require File.expand_path("../synced_folder", __FILE__) + SyncedFolder + end end autoload :Action, File.expand_path("../action", __FILE__) diff --git a/plugins/providers/virtualbox/synced_folder.rb b/plugins/providers/virtualbox/synced_folder.rb new file mode 100644 index 000000000..15149c01a --- /dev/null +++ b/plugins/providers/virtualbox/synced_folder.rb @@ -0,0 +1,71 @@ +require "vagrant/util/platform" + +module VagrantPlugins + module ProviderVirtualBox + class SyncedFolder < Vagrant.plugin("2", :synced_folder) + def usable?(machine) + # These synced folders only work if the provider if VirtualBox + machine.provider_name == :virtualbox + end + + def prepare(machine, folders) + defs = [] + folders.each do |id, data| + hostpath = Vagrant::Util::Platform.cygwin_windows_path(data[:hostpath]) + + defs << { + name: id, + hostpath: hostpath, + transient: data[:transient], + } + end + + driver(machine).share_folders(defs) + end + + def enable(machine, folders) + # short guestpaths first, so we don't step on ourselves + folders = folders.sort_by do |id, data| + if data[:guestpath] + data[:guestpath].length + else + # A long enough path to just do this at the end. + 10000 + end + end + + # Go through each folder and mount + folders.each do |id, data| + if data[:guestpath] + # Guest path specified, so mount the folder to specified point + machine.ui.info(I18n.t("vagrant.actions.vm.share_folders.mounting_entry", + :guest_path => data[:guestpath])) + + # Dup the data so we can pass it to the guest API + data = data.dup + + # Calculate the owner and group + ssh_info = machine.ssh_info + data[:owner] ||= ssh_info[:username] + data[:group] ||= ssh_info[:username] + + # Mount the actual folder + machine.guest.capability( + :mount_virtualbox_shared_folder, id, data[:guestpath], data) + else + # If no guest path is specified, then automounting is disabled + machine.ui.info(I18n.t("vagrant.actions.vm.share_folders.nomount_entry", + :host_path => data[:hostpath])) + end + end + end + + protected + + # This is here so that we can stub it for tests + def driver(machine) + machine.provider.driver + end + end + end +end diff --git a/test/unit/plugins/providers/virtualbox/synced_folder_test.rb b/test/unit/plugins/providers/virtualbox/synced_folder_test.rb new file mode 100644 index 000000000..20a3fcfd8 --- /dev/null +++ b/test/unit/plugins/providers/virtualbox/synced_folder_test.rb @@ -0,0 +1,38 @@ +require "vagrant" +require Vagrant.source_root.join("test/unit/base") + +require Vagrant.source_root.join("plugins/providers/virtualbox/synced_folder") + +# TODO(mitchellh): tag with v2 +describe VagrantPlugins::ProviderVirtualBox::SyncedFolder do + let(:machine) do + double("machine").tap do |m| + end + end + + subject { described_class.new } + + describe "usable" do + it "should be with virtualbox provider" do + machine.stub(provider_name: :virtualbox) + subject.should be_usable(machine) + end + + it "should not be with another provider" do + machine.stub(provider_name: :vmware_fusion) + subject.should_not be_usable(machine) + end + end + + describe "prepare" do + let(:driver) { double("driver") } + + before do + machine.stub(driver: driver) + end + + it "should share the folders" do + pending + end + end +end diff --git a/test/unit/vagrant_test.rb b/test/unit/vagrant_test.rb index 84340f840..8cb0799e5 100644 --- a/test/unit/vagrant_test.rb +++ b/test/unit/vagrant_test.rb @@ -24,6 +24,23 @@ describe Vagrant do end end + describe "v2" do + it "returns the proper class for version 2" do + described_class.plugin("2").should == Vagrant::Plugin::V2::Plugin + end + + it "returns the proper components for version 2" do + described_class.plugin("2", :command).should == Vagrant::Plugin::V2::Command + described_class.plugin("2", :communicator).should == Vagrant::Plugin::V2::Communicator + described_class.plugin("2", :config).should == Vagrant::Plugin::V2::Config + described_class.plugin("2", :guest).should == Vagrant::Plugin::V2::Guest + described_class.plugin("2", :host).should == Vagrant::Plugin::V2::Host + described_class.plugin("2", :provider).should == Vagrant::Plugin::V2::Provider + described_class.plugin("2", :provisioner).should == Vagrant::Plugin::V2::Provisioner + described_class.plugin("2", :synced_folder).should == Vagrant::Plugin::V2::SyncedFolder + end + end + it "raises an exception if an unsupported version is given" do expect { described_class.plugin("88") }. to raise_error(ArgumentError)