Additional shared folders can now be configured through the `config.vm.share_folder` method
This commit is contained in:
parent
6cfddb6c38
commit
a71815df4e
|
@ -2,6 +2,10 @@ module Vagrant
|
||||||
module Actions
|
module Actions
|
||||||
module VM
|
module VM
|
||||||
class Boot < Base
|
class Boot < Base
|
||||||
|
def prepare
|
||||||
|
Vagrant.config.vm.share_folder("vagrant-root", Vagrant.config.vm.project_directory, Env.root_path)
|
||||||
|
end
|
||||||
|
|
||||||
def execute!
|
def execute!
|
||||||
@runner.invoke_around_callback(:boot) do
|
@runner.invoke_around_callback(:boot) do
|
||||||
# Startup the VM
|
# Startup the VM
|
||||||
|
@ -17,11 +21,6 @@ error
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
def collect_shared_folders
|
|
||||||
# The root shared folder for the project
|
|
||||||
["vagrant-root", Env.root_path, Vagrant.config.vm.project_directory]
|
|
||||||
end
|
|
||||||
|
|
||||||
def boot
|
def boot
|
||||||
logger.info "Booting VM..."
|
logger.info "Booting VM..."
|
||||||
@runner.vm.start(:headless, true)
|
@runner.vm.start(:headless, true)
|
||||||
|
|
|
@ -2,6 +2,10 @@ module Vagrant
|
||||||
module Actions
|
module Actions
|
||||||
module VM
|
module VM
|
||||||
class Provision < Base
|
class Provision < Base
|
||||||
|
def prepare
|
||||||
|
Vagrant.config.vm.share_folder("vagrant-provisioning", cookbooks_path, File.expand_path(Vagrant.config.chef.cookbooks_path, Env.root_path))
|
||||||
|
end
|
||||||
|
|
||||||
def execute!
|
def execute!
|
||||||
chown_provisioning_folder
|
chown_provisioning_folder
|
||||||
setup_json
|
setup_json
|
||||||
|
@ -61,10 +65,6 @@ solo
|
||||||
def cookbooks_path
|
def cookbooks_path
|
||||||
File.join(Vagrant.config.chef.provisioning_path, "cookbooks")
|
File.join(Vagrant.config.chef.provisioning_path, "cookbooks")
|
||||||
end
|
end
|
||||||
|
|
||||||
def collect_shared_folders
|
|
||||||
["vagrant-provisioning", File.expand_path(Vagrant.config.chef.cookbooks_path, Env.root_path), cookbooks_path]
|
|
||||||
end
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
|
@ -3,32 +3,15 @@ module Vagrant
|
||||||
module VM
|
module VM
|
||||||
class SharedFolders < Base
|
class SharedFolders < Base
|
||||||
def shared_folders
|
def shared_folders
|
||||||
shared_folders = @runner.invoke_callback(:collect_shared_folders)
|
Vagrant.config.vm.shared_folders.inject([]) do |acc, data|
|
||||||
|
name, value = data
|
||||||
# Basic filtering of shared folders. Basically only verifies that
|
acc << [name, File.expand_path(value[:hostpath]), value[:guestpath]]
|
||||||
# the result is an array of 3 elements. In the future this should
|
|
||||||
# also verify that the host path exists, the name is valid,
|
|
||||||
# and that the guest path is valid.
|
|
||||||
shared_folders.collect do |folder|
|
|
||||||
if folder.is_a?(Array) && folder.length == 3
|
|
||||||
folder
|
|
||||||
else
|
|
||||||
nil
|
|
||||||
end
|
end
|
||||||
end.compact
|
|
||||||
end
|
end
|
||||||
|
|
||||||
def before_boot
|
def before_boot
|
||||||
logger.info "Creating shared folders metadata..."
|
clear_shared_folders
|
||||||
|
create_metadata
|
||||||
shared_folders.each do |name, hostpath, guestpath|
|
|
||||||
folder = VirtualBox::SharedFolder.new
|
|
||||||
folder.name = name
|
|
||||||
folder.hostpath = hostpath
|
|
||||||
@runner.vm.shared_folders << folder
|
|
||||||
end
|
|
||||||
|
|
||||||
@runner.vm.save(true)
|
|
||||||
end
|
end
|
||||||
|
|
||||||
def after_boot
|
def after_boot
|
||||||
|
@ -44,6 +27,29 @@ module Vagrant
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def clear_shared_folders
|
||||||
|
logger.info "Clearing previously set shared folders..."
|
||||||
|
|
||||||
|
@runner.vm.shared_folders.each do |shared_folder|
|
||||||
|
shared_folder.destroy
|
||||||
|
end
|
||||||
|
|
||||||
|
@runner.reload!
|
||||||
|
end
|
||||||
|
|
||||||
|
def create_metadata
|
||||||
|
logger.info "Creating shared folders metadata..."
|
||||||
|
|
||||||
|
shared_folders.each do |name, hostpath, guestpath|
|
||||||
|
folder = VirtualBox::SharedFolder.new
|
||||||
|
folder.name = name
|
||||||
|
folder.hostpath = hostpath
|
||||||
|
@runner.vm.shared_folders << folder
|
||||||
|
end
|
||||||
|
|
||||||
|
@runner.vm.save(true)
|
||||||
|
end
|
||||||
|
|
||||||
def mount_folder(ssh, name, guestpath, sleeptime=5)
|
def mount_folder(ssh, name, guestpath, sleeptime=5)
|
||||||
# Note: This method seems pretty OS-specific and could also use
|
# Note: This method seems pretty OS-specific and could also use
|
||||||
# some configuration options. For now its duct tape and "works"
|
# some configuration options. For now its duct tape and "works"
|
||||||
|
|
|
@ -72,12 +72,14 @@ module Vagrant
|
||||||
attr_accessor :base_mac
|
attr_accessor :base_mac
|
||||||
attr_accessor :project_directory
|
attr_accessor :project_directory
|
||||||
attr_reader :forwarded_ports
|
attr_reader :forwarded_ports
|
||||||
|
attr_reader :shared_folders
|
||||||
attr_accessor :hd_location
|
attr_accessor :hd_location
|
||||||
attr_accessor :disk_image_format
|
attr_accessor :disk_image_format
|
||||||
|
|
||||||
|
|
||||||
def initialize
|
def initialize
|
||||||
@forwarded_ports = {}
|
@forwarded_ports = {}
|
||||||
|
@shared_folders = {}
|
||||||
end
|
end
|
||||||
|
|
||||||
def forward_port(name, guestport, hostport, protocol="TCP")
|
def forward_port(name, guestport, hostport, protocol="TCP")
|
||||||
|
@ -88,6 +90,13 @@ module Vagrant
|
||||||
}
|
}
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def share_folder(name, guestpath, hostpath)
|
||||||
|
@shared_folders[name] = {
|
||||||
|
:guestpath => guestpath,
|
||||||
|
:hostpath => hostpath
|
||||||
|
}
|
||||||
|
end
|
||||||
|
|
||||||
def hd_location=(val)
|
def hd_location=(val)
|
||||||
raise Exception.new "disk_storage must be set to a directory" unless File.directory?(val)
|
raise Exception.new "disk_storage must be set to a directory" unless File.directory?(val)
|
||||||
@hd_location=val
|
@hd_location=val
|
||||||
|
|
|
@ -19,6 +19,10 @@ module Vagrant
|
||||||
@vm = vm
|
@vm = vm
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def reload!
|
||||||
|
@vm = VirtualBox::VM.find(@vm.uuid)
|
||||||
|
end
|
||||||
|
|
||||||
def package(out_path, include_files=[])
|
def package(out_path, include_files=[])
|
||||||
add_action(Actions::VM::Export)
|
add_action(Actions::VM::Export)
|
||||||
add_action(Actions::VM::Package, out_path, include_files)
|
add_action(Actions::VM::Package, out_path, include_files)
|
||||||
|
|
|
@ -7,6 +7,13 @@ class BootActionTest < Test::Unit::TestCase
|
||||||
mock_config
|
mock_config
|
||||||
end
|
end
|
||||||
|
|
||||||
|
context "preparing" do
|
||||||
|
should "add the root shared folder" do
|
||||||
|
Vagrant.config.vm.expects(:share_folder).with("vagrant-root", Vagrant.config.vm.project_directory, Vagrant::Env.root_path).once
|
||||||
|
@action.prepare
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
context "execution" do
|
context "execution" do
|
||||||
should "invoke the 'boot' around callback" do
|
should "invoke the 'boot' around callback" do
|
||||||
boot_seq = sequence("boot_seq")
|
boot_seq = sequence("boot_seq")
|
||||||
|
@ -45,11 +52,4 @@ class BootActionTest < Test::Unit::TestCase
|
||||||
assert !@action.wait_for_boot(0)
|
assert !@action.wait_for_boot(0)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
context "callbacks" do
|
|
||||||
should "setup the root directory shared folder" do
|
|
||||||
expected = ["vagrant-root", Vagrant::Env.root_path, Vagrant.config.vm.project_directory]
|
|
||||||
assert_equal expected, @action.collect_shared_folders
|
|
||||||
end
|
|
||||||
end
|
|
||||||
end
|
end
|
||||||
|
|
|
@ -14,7 +14,8 @@ class ProvisionActionTest < Test::Unit::TestCase
|
||||||
should "setup shared folder on VM for the cookbooks" do
|
should "setup shared folder on VM for the cookbooks" do
|
||||||
File.expects(:expand_path).with(Vagrant.config.chef.cookbooks_path, Vagrant::Env.root_path).returns("foo")
|
File.expects(:expand_path).with(Vagrant.config.chef.cookbooks_path, Vagrant::Env.root_path).returns("foo")
|
||||||
@action.expects(:cookbooks_path).returns("bar")
|
@action.expects(:cookbooks_path).returns("bar")
|
||||||
assert_equal ["vagrant-provisioning", "foo", "bar"], @action.collect_shared_folders
|
Vagrant.config.vm.expects(:share_folder).with("vagrant-provisioning", "bar", "foo").once
|
||||||
|
@action.prepare
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
|
@ -12,17 +12,58 @@ class SharedFoldersActionTest < Test::Unit::TestCase
|
||||||
folders
|
folders
|
||||||
end
|
end
|
||||||
|
|
||||||
|
context "before boot" do
|
||||||
|
should "clear folders and create metadata, in order" do
|
||||||
|
before_seq = sequence("before")
|
||||||
|
@action.expects(:clear_shared_folders).once.in_sequence(before_seq)
|
||||||
|
@action.expects(:create_metadata).once.in_sequence(before_seq)
|
||||||
|
@action.before_boot
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
context "collecting shared folders" do
|
context "collecting shared folders" do
|
||||||
should "return the arrays that the callback returns" do
|
setup do
|
||||||
result = [[1,2,3],[4,5,6]]
|
File.stubs(:expand_path).returns("baz")
|
||||||
@mock_vm.expects(:invoke_callback).with(:collect_shared_folders).once.returns(result)
|
end
|
||||||
|
|
||||||
|
should "convert the vagrant config values into an array" do
|
||||||
|
mock_config do |config|
|
||||||
|
config.vm.shared_folders.clear
|
||||||
|
config.vm.share_folder("foo", "bar", "baz")
|
||||||
|
end
|
||||||
|
|
||||||
|
result = [["foo", "baz", "bar"]]
|
||||||
assert_equal result, @action.shared_folders
|
assert_equal result, @action.shared_folders
|
||||||
end
|
end
|
||||||
|
|
||||||
should "filter out invalid results" do
|
should "expand the path of the host folder" do
|
||||||
result = [[1,2,3],[4,5]]
|
File.expects(:expand_path).with("baz").once.returns("expanded_baz")
|
||||||
@mock_vm.expects(:invoke_callback).with(:collect_shared_folders).once.returns(result)
|
|
||||||
assert_equal [[1,2,3]], @action.shared_folders
|
mock_config do |config|
|
||||||
|
config.vm.shared_folders.clear
|
||||||
|
config.vm.share_folder("foo", "bar", "baz")
|
||||||
|
end
|
||||||
|
|
||||||
|
result = [["foo", "expanded_baz", "bar"]]
|
||||||
|
assert_equal result, @action.shared_folders
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
context "clearing shared folders" do
|
||||||
|
setup do
|
||||||
|
@shared_folder = mock("shared_folder")
|
||||||
|
@shared_folders = [@shared_folder]
|
||||||
|
@vm.stubs(:shared_folders).returns(@shared_folders)
|
||||||
|
end
|
||||||
|
|
||||||
|
should "call destroy on each shared folder then reload" do
|
||||||
|
destroy_seq = sequence("destroy")
|
||||||
|
@shared_folders.each do |sf|
|
||||||
|
sf.expects(:destroy).once.in_sequence(destroy_seq)
|
||||||
|
end
|
||||||
|
|
||||||
|
@mock_vm.expects(:reload!).once.in_sequence(destroy_seq)
|
||||||
|
@action.clear_shared_folders
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
@ -39,7 +80,7 @@ class SharedFoldersActionTest < Test::Unit::TestCase
|
||||||
@vm.stubs(:shared_folders).returns(shared_folders)
|
@vm.stubs(:shared_folders).returns(shared_folders)
|
||||||
@vm.expects(:save).with(true).once
|
@vm.expects(:save).with(true).once
|
||||||
|
|
||||||
@action.before_boot
|
@action.create_metadata
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
|
@ -35,6 +35,16 @@ class VMTest < Test::Unit::TestCase
|
||||||
context "vagrant VM instance" do
|
context "vagrant VM instance" do
|
||||||
setup do
|
setup do
|
||||||
@vm = Vagrant::VM.new(@mock_vm)
|
@vm = Vagrant::VM.new(@mock_vm)
|
||||||
|
@mock_vm.stubs(:uuid).returns("foo")
|
||||||
|
end
|
||||||
|
|
||||||
|
context "reloading" do
|
||||||
|
should "load the same VM and set it" do
|
||||||
|
new_vm = mock("vm")
|
||||||
|
VirtualBox::VM.expects(:find).with(@mock_vm.uuid).returns(new_vm)
|
||||||
|
@vm.reload!
|
||||||
|
assert_equal new_vm, @vm.vm
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
context "packaging" do
|
context "packaging" do
|
||||||
|
|
Loading…
Reference in New Issue