Skip to content

Commit

Permalink
Rethinking Unison foundation. Tearing things out.
Browse files Browse the repository at this point in the history
  • Loading branch information
mitchellh committed Jun 19, 2010
1 parent 854f160 commit b0879de
Show file tree
Hide file tree
Showing 6 changed files with 117 additions and 142 deletions.
9 changes: 5 additions & 4 deletions config/default.rb
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,11 @@
# other Vagrantfiles, if they wish.
config.vm.share_folder("v-root", "/vagrant", ".")

# TODO new config class
config.vm.sync_opts = "-terse -group -owner -batch -silent"
config.vm.sync_script = "/tmp/sync"
config.vm.sync_crontab_entry_file = "/tmp/crontab-entry"
config.unison.folder_suffix = ".sync"
# TODO fix these
# config.vm.sync_opts = "-terse -group -owner -batch -silent"
# config.vm.sync_script = "/tmp/sync"
# config.vm.sync_crontab_entry_file = "/tmp/crontab-entry"

config.package.name = 'vagrant'
config.package.extension = '.box'
Expand Down
41 changes: 28 additions & 13 deletions lib/vagrant/actions/vm/shared_folders.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,21 @@ module Vagrant
module Actions
module VM
class SharedFolders < Base
# This method returns an actual list of VirtualBox shared
# folders to create and their proper path.
def shared_folders
@runner.env.config.vm.shared_folders.inject([]) do |acc, data|
name, value = data
acc << [name, File.expand_path(value[:hostpath], @runner.env.root_path), value[:guestpath], value[:syncpath]].compact
runner.env.config.vm.shared_folders.inject({}) do |acc, data|
key, value = data

if value[:sync]
# Syncing this folder. Change the guestpath to reflect
# what we're actually mounting.
value[:original] = value.dup
value[:guestpath] = "#{value[:guestpath]}#{runner.env.config.unison.folder_suffix}"
end

acc[key] = value
acc
end
end

Expand All @@ -15,21 +26,25 @@ def before_boot
end

def after_boot
mount_shared_folders
setup_unison
end

def mount_shared_folders
logger.info "Mounting shared folders..."

@runner.ssh.execute do |ssh|
@runner.system.prepare_sync(ssh) if @runner.env.config.vm.sync_required

shared_folders.each do |name, hostpath, guestpath, syncpath|
logger.info "-- #{name}: #{syncpath ? guestpath + " -sync-> " + syncpath : guestpath}"
@runner.system.mount_shared_folder(ssh, name, guestpath)
if syncpath
@runner.system.create_sync(ssh, :syncpath => syncpath, :guestpath => guestpath)
end
shared_folders.each do |name, data|
logger.info "-- #{name}: #{data[:guestpath]}"
@runner.system.mount_shared_folder(ssh, name, data[:guestpath])
end
end
end

def setup_unison
# TODO
end

def clear_shared_folders
if runner.vm.shared_folders.length > 0
logger.info "Clearing previously set shared folders..."
Expand All @@ -46,10 +61,10 @@ def clear_shared_folders
def create_metadata
logger.info "Creating shared folders metadata..."

shared_folders.each do |name, hostpath, guestpath|
shared_folders.each do |name, data|
folder = VirtualBox::SharedFolder.new
folder.name = name
folder.host_path = hostpath
folder.host_path = File.expand_path(data[:hostpath], runner.env.root_path)
@runner.vm.shared_folders << folder
end

Expand Down
38 changes: 15 additions & 23 deletions lib/vagrant/config.rb
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,18 @@ def private_key_path
end
end

class UnisonConfig < Base
attr_accessor :folder_suffix

# TODO figure out what is needed here, the options above were
# added after the fact so they are fine. Below needs to be
# reanalyzed:
attr_accessor :sync_opts
attr_accessor :sync_script
attr_accessor :sync_crontab_entry_file
attr_reader :sync_required
end

class VMConfig < Base
include Util::StackedProcRunner

Expand All @@ -85,10 +97,6 @@ class VMConfig < Base
attr_accessor :box_ovf
attr_accessor :base_mac
attr_accessor :boot_mode
attr_accessor :sync_opts
attr_accessor :sync_script
attr_accessor :sync_crontab_entry_file
attr_reader :sync_required
attr_reader :forwarded_ports
attr_reader :shared_folders
attr_reader :network_options
Expand Down Expand Up @@ -128,19 +136,11 @@ def forward_port(name, guestport, hostport, options=nil)
forwarded_ports[name] = options
end

def share_folder(name, guestpath, hostpath = nil, opts = {})
guestpath, opts[:sync] = shift(guestpath, opts[:sync])

# TODO if both are nil the exception information will be unusable
if opts[:sync] == guestpath
raise Exception.new("The sync directory #{opts[:sync]} is identical to the shifted shared folder mount point #{guestpath}")
end

def share_folder(name, guestpath, hostpath, opts=nil)
@shared_folders[name] = {
:syncpath => opts[:sync],
:guestpath => guestpath,
:hostpath => hostpath
}
}.merge(opts || {})
end

def network(ip, options=nil)
Expand Down Expand Up @@ -185,15 +185,6 @@ def define(name, options=nil, &block)
defined_vms[name.to_sym].options.merge!(options)
defined_vms[name.to_sym].push_proc(&block)
end

def shift(orig, sync)
if sync
@sync_required = true
[orig + '-sync', sync == true ? orig : sync]
else
[orig, sync]
end
end
end

class PackageConfig < Base
Expand Down Expand Up @@ -228,6 +219,7 @@ def configures(key, klass)
# Setup default configures
configures :package, PackageConfig
configures :ssh, SSHConfig
configures :unison, UnisonConfig
configures :vm, VMConfig
configures :vagrant, VagrantConfig

Expand Down
8 changes: 4 additions & 4 deletions test/test_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ def mock_environment
Vagrant::Config.reset!(environment)

Vagrant::Config.run do |config|
config.vagrant.home = '~/.home'
config.vagrant.dotfile_name = ".vagrant"
config.vagrant.log_output = nil

Expand All @@ -42,13 +43,14 @@ def mock_environment
config.vm.shared_folder_uid = nil
config.vm.shared_folder_gid = nil
config.vm.system = :linux
config.vm.sync_script = "/foo"
config.vm.sync_crontab_entry_file = "/tmp/foo"
config.vm.share_folder("v-root", "/vagrant", ".")

config.package.name = 'vagrant'
config.package.extension = '.box'

# Unison
config.unison.folder_suffix = ".sync"

# Chef
config.chef.chef_server_url = "http://localhost:4000"
config.chef.validation_key_path = "validation.pem"
Expand All @@ -60,8 +62,6 @@ def mock_environment
config.chef.json = {
:recipes => ["vagrant_main"]
}

config.vagrant.home = '~/.home'
end

if block_given?
Expand Down
130 changes: 65 additions & 65 deletions test/vagrant/actions/vm/shared_folders_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -26,39 +26,45 @@ def stub_shared_folders
File.stubs(:expand_path).returns("baz")
end

should "convert the vagrant config values into an array" do
mock_env_shared_folders

result = [["foo", "baz", "bar"]]
assert_equal result, @action.shared_folders
end

should "expand the path of the host folder" do
File.expects(:expand_path).with("baz", @runner.env.root_path).once.returns("expanded_baz")

env = mock_environment do |config|
config.vm.shared_folders.clear
config.vm.share_folder("foo", "bar", "baz")
should "return a hash of the shared folders" do
data = {
"foo" => %W[bar baz],
"bar" => %W[foo baz]
}

mock_env do |config|
data.each do |name, value|
config.vm.share_folder(name, *value)
end
end

@runner.expects(:env).returns(env)

result = [["foo", "expanded_baz", "bar"]]
assert_equal result, @action.shared_folders
result = @action.shared_folders
assert_equal data.length, result.length
data.each do |name, value|
guest, host = value
assert_equal guest, result[name][:guestpath]
assert_equal host, result[name][:hostpath]
end
end

context "with sync" do
should "append the sync value to the other config values" do
mock_env_shared_folders(:sync => true)
should "append sync suffix if sync enabled to a folder" do
name = "foo"
guest = "bar"
host = "baz"

assert_equal [["foo", "baz", "bar-sync", "bar"]], @action.shared_folders
mock_env do |config|
config.vm.share_folder(name, guest, host, :sync => true)
end

result = @action.shared_folders
assert_equal "#{guest}#{@runner.env.config.unison.folder_suffix}", result[name][:guestpath]
assert_equal guest, result[name][:original][:guestpath]
end

def mock_env_shared_folders(opts={})
def mock_env
env = mock_environment do |config|
config.vm.shared_folders.clear
config.vm.share_folder("foo", "bar", "baz", opts)
yield config
end

@runner.expects(:env).returns(env)
Expand Down Expand Up @@ -94,49 +100,43 @@ def mock_env_shared_folders(opts={})
@folders = stub_shared_folders
end

should "add all shared folders to the VM" do
share_seq = sequence("share_seq")
shared_folders = mock("shared_folders")
shared_folders.expects(:<<).in_sequence(share_seq).with() { |sf| sf.name == "foo" && sf.host_path == "from" }
shared_folders.expects(:<<).in_sequence(share_seq).with() { |sf| sf.name == "bar" && sf.host_path == "bfrom" }
@vm.stubs(:shared_folders).returns(shared_folders)
@vm.expects(:save).once

@action.create_metadata
end
end

context "mounting the shared folders" do
setup do
@folders = stub_shared_folders
@ssh = mock("ssh")
@runner.ssh.stubs(:execute).yields(@ssh)
@runner.system.stubs(:mount_shared_folder)
end

should "mount all shared folders to the VM" do
mount_seq = sequence("mount_seq")
@folders.each do |name, hostpath, guestpath|
@runner.system.expects(:mount_shared_folder).with(@ssh, name, guestpath).in_sequence(mount_seq)
end
# should "add all shared folders to the VM" do
# share_seq = sequence("share_seq")
# shared_folders = mock("shared_folders")
# shared_folders.expects(:<<).in_sequence(share_seq).with() { |sf| sf.name == "foo" && sf.host_path == "from" }
# shared_folders.expects(:<<).in_sequence(share_seq).with() { |sf| sf.name == "bar" && sf.host_path == "bfrom" }
# @vm.stubs(:shared_folders).returns(shared_folders)
# @vm.expects(:save).once

@action.after_boot
end

should "execute the necessary rysnc commands for each sync folder" do
@folders.map { |f| f << 'sync' }
@folders.each do |name, hostpath, guestpath, syncd|
@runner.system.expects(:create_sync).with(@ssh, :syncpath => syncd, :guestpath => guestpath)
end
@runner.ssh.expects(:execute).yields(@ssh)

@action.after_boot
end
# @action.create_metadata
# end
end

context "with syncd folders" do
# TODO prevented by odd configuration swapping when stubbing ssh.execute
should "prepare the system for sync if necessary" do
end
end
# context "mounting the shared folders" do
# setup do
# @folders = stub_shared_folders
# @ssh = mock("ssh")
# @runner.ssh.stubs(:execute).yields(@ssh)
# @runner.system.stubs(:mount_shared_folder)
# end

# should "mount all shared folders to the VM" do
# mount_seq = sequence("mount_seq")
# @folders.each do |name, hostpath, guestpath|
# @runner.system.expects(:mount_shared_folder).with(@ssh, name, guestpath).in_sequence(mount_seq)
# end

# @action.after_boot
# end

# should "execute the necessary rysnc commands for each sync folder" do
# @folders.map { |f| f << 'sync' }
# @folders.each do |name, hostpath, guestpath, syncd|
# @runner.system.expects(:create_sync).with(@ssh, :syncpath => syncd, :guestpath => guestpath)
# end
# @runner.ssh.expects(:execute).yields(@ssh)

# @action.after_boot
# end
# end
end
33 changes: 0 additions & 33 deletions test/vagrant/config_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -262,39 +262,6 @@ class ConfigTest < Test::Unit::TestCase
end
end

context "syncd folders" do
should "set the syncpath to nil by default" do
share_with_opts
assert !@config.shared_folders['foo'][:syncpath]
end

should "append sync to directory name when boolean" do
share_with_opts(:sync => true)
assert_equal @config.shared_folders['foo'][:syncpath], 'foo-dir'
assert_equal @config.shared_folders['foo'][:guestpath], 'foo-dir-sync'
end

should "use the specified sync directory" do
share_with_opts(:sync => 'bar-baz')
assert_equal @config.shared_folders['foo'][:syncpath], 'bar-baz'
end

should "raise an exception an exception if the guestpath and syncpath are the same" do
assert_raise Exception do
share_with_opts(:sync => 'foo-dir-sync')
end
end

should "set the sync required flag to true" do
share_with_opts(:sync => true)
assert @config.sync_required
end

def share_with_opts(opts={})
@config.share_folder('foo', 'foo-dir', '', opts)
end
end

context "uid/gid" do
should "return the shared folder UID if set" do
@config.shared_folder_uid = "foo"
Expand Down

0 comments on commit b0879de

Please sign in to comment.