providers/virtualbox: implement the synced folder plugin

This commit is contained in:
Mitchell Hashimoto 2013-11-21 17:38:17 -08:00
parent ee0086ddee
commit 97148379d2
6 changed files with 134 additions and 2 deletions

View File

@ -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

View File

@ -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

View File

@ -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__)

View File

@ -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

View File

@ -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

View File

@ -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)