Rethinking Unison foundation. Tearing things out.

This commit is contained in:
Mitchell Hashimoto 2010-06-19 11:34:37 -07:00
parent 854f160c9c
commit b0879dea6a
6 changed files with 115 additions and 140 deletions

View File

@ -27,10 +27,11 @@ Vagrant::Config.run do |config|
# other Vagrantfiles, if they wish. # other Vagrantfiles, if they wish.
config.vm.share_folder("v-root", "/vagrant", ".") config.vm.share_folder("v-root", "/vagrant", ".")
# TODO new config class config.unison.folder_suffix = ".sync"
config.vm.sync_opts = "-terse -group -owner -batch -silent" # TODO fix these
config.vm.sync_script = "/tmp/sync" # config.vm.sync_opts = "-terse -group -owner -batch -silent"
config.vm.sync_crontab_entry_file = "/tmp/crontab-entry" # config.vm.sync_script = "/tmp/sync"
# config.vm.sync_crontab_entry_file = "/tmp/crontab-entry"
config.package.name = 'vagrant' config.package.name = 'vagrant'
config.package.extension = '.box' config.package.extension = '.box'

View File

@ -2,10 +2,21 @@ module Vagrant
module Actions module Actions
module VM module VM
class SharedFolders < Base class SharedFolders < Base
# This method returns an actual list of VirtualBox shared
# folders to create and their proper path.
def shared_folders def shared_folders
@runner.env.config.vm.shared_folders.inject([]) do |acc, data| runner.env.config.vm.shared_folders.inject({}) do |acc, data|
name, value = data key, value = data
acc << [name, File.expand_path(value[:hostpath], @runner.env.root_path), value[:guestpath], value[:syncpath]].compact
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
end end
@ -15,21 +26,25 @@ module Vagrant
end end
def after_boot def after_boot
mount_shared_folders
setup_unison
end
def mount_shared_folders
logger.info "Mounting shared folders..." logger.info "Mounting shared folders..."
@runner.ssh.execute do |ssh| @runner.ssh.execute do |ssh|
@runner.system.prepare_sync(ssh) if @runner.env.config.vm.sync_required shared_folders.each do |name, data|
logger.info "-- #{name}: #{data[:guestpath]}"
shared_folders.each do |name, hostpath, guestpath, syncpath| @runner.system.mount_shared_folder(ssh, name, data[:guestpath])
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
end end
end end
end end
def setup_unison
# TODO
end
def clear_shared_folders def clear_shared_folders
if runner.vm.shared_folders.length > 0 if runner.vm.shared_folders.length > 0
logger.info "Clearing previously set shared folders..." logger.info "Clearing previously set shared folders..."
@ -46,10 +61,10 @@ module Vagrant
def create_metadata def create_metadata
logger.info "Creating shared folders 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 = VirtualBox::SharedFolder.new
folder.name = name folder.name = name
folder.host_path = hostpath folder.host_path = File.expand_path(data[:hostpath], runner.env.root_path)
@runner.vm.shared_folders << folder @runner.vm.shared_folders << folder
end end

View File

@ -77,6 +77,18 @@ module Vagrant
end end
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 class VMConfig < Base
include Util::StackedProcRunner include Util::StackedProcRunner
@ -85,10 +97,6 @@ module Vagrant
attr_accessor :box_ovf attr_accessor :box_ovf
attr_accessor :base_mac attr_accessor :base_mac
attr_accessor :boot_mode 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 :forwarded_ports
attr_reader :shared_folders attr_reader :shared_folders
attr_reader :network_options attr_reader :network_options
@ -128,19 +136,11 @@ module Vagrant
forwarded_ports[name] = options forwarded_ports[name] = options
end end
def share_folder(name, guestpath, hostpath = nil, opts = {}) def share_folder(name, guestpath, hostpath, opts=nil)
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
@shared_folders[name] = { @shared_folders[name] = {
:syncpath => opts[:sync],
:guestpath => guestpath, :guestpath => guestpath,
:hostpath => hostpath :hostpath => hostpath
} }.merge(opts || {})
end end
def network(ip, options=nil) def network(ip, options=nil)
@ -185,15 +185,6 @@ module Vagrant
defined_vms[name.to_sym].options.merge!(options) defined_vms[name.to_sym].options.merge!(options)
defined_vms[name.to_sym].push_proc(&block) defined_vms[name.to_sym].push_proc(&block)
end end
def shift(orig, sync)
if sync
@sync_required = true
[orig + '-sync', sync == true ? orig : sync]
else
[orig, sync]
end
end
end end
class PackageConfig < Base class PackageConfig < Base
@ -228,6 +219,7 @@ module Vagrant
# Setup default configures # Setup default configures
configures :package, PackageConfig configures :package, PackageConfig
configures :ssh, SSHConfig configures :ssh, SSHConfig
configures :unison, UnisonConfig
configures :vm, VMConfig configures :vm, VMConfig
configures :vagrant, VagrantConfig configures :vagrant, VagrantConfig

View File

@ -23,6 +23,7 @@ class Test::Unit::TestCase
Vagrant::Config.reset!(environment) Vagrant::Config.reset!(environment)
Vagrant::Config.run do |config| Vagrant::Config.run do |config|
config.vagrant.home = '~/.home'
config.vagrant.dotfile_name = ".vagrant" config.vagrant.dotfile_name = ".vagrant"
config.vagrant.log_output = nil config.vagrant.log_output = nil
@ -42,13 +43,14 @@ class Test::Unit::TestCase
config.vm.shared_folder_uid = nil config.vm.shared_folder_uid = nil
config.vm.shared_folder_gid = nil config.vm.shared_folder_gid = nil
config.vm.system = :linux 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.vm.share_folder("v-root", "/vagrant", ".")
config.package.name = 'vagrant' config.package.name = 'vagrant'
config.package.extension = '.box' config.package.extension = '.box'
# Unison
config.unison.folder_suffix = ".sync"
# Chef # Chef
config.chef.chef_server_url = "http://localhost:4000" config.chef.chef_server_url = "http://localhost:4000"
config.chef.validation_key_path = "validation.pem" config.chef.validation_key_path = "validation.pem"
@ -60,8 +62,6 @@ class Test::Unit::TestCase
config.chef.json = { config.chef.json = {
:recipes => ["vagrant_main"] :recipes => ["vagrant_main"]
} }
config.vagrant.home = '~/.home'
end end
if block_given? if block_given?

View File

@ -26,39 +26,45 @@ class SharedFoldersActionTest < Test::Unit::TestCase
File.stubs(:expand_path).returns("baz") File.stubs(:expand_path).returns("baz")
end end
should "convert the vagrant config values into an array" do should "return a hash of the shared folders" do
mock_env_shared_folders data = {
"foo" => %W[bar baz],
"bar" => %W[foo baz]
}
result = [["foo", "baz", "bar"]] mock_env do |config|
assert_equal result, @action.shared_folders data.each do |name, value|
end config.vm.share_folder(name, *value)
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")
end end
@runner.expects(:env).returns(env) result = @action.shared_folders
assert_equal data.length, result.length
result = [["foo", "expanded_baz", "bar"]] data.each do |name, value|
assert_equal result, @action.shared_folders guest, host = value
end assert_equal guest, result[name][:guestpath]
assert_equal host, result[name][:hostpath]
context "with sync" do
should "append the sync value to the other config values" do
mock_env_shared_folders(:sync => true)
assert_equal [["foo", "baz", "bar-sync", "bar"]], @action.shared_folders
end end
end end
def mock_env_shared_folders(opts={}) should "append sync suffix if sync enabled to a folder" do
name = "foo"
guest = "bar"
host = "baz"
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
env = mock_environment do |config| env = mock_environment do |config|
config.vm.shared_folders.clear config.vm.shared_folders.clear
config.vm.share_folder("foo", "bar", "baz", opts) yield config
end end
@runner.expects(:env).returns(env) @runner.expects(:env).returns(env)
@ -94,49 +100,43 @@ class SharedFoldersActionTest < Test::Unit::TestCase
@folders = stub_shared_folders @folders = stub_shared_folders
end end
should "add all shared folders to the VM" do # should "add all shared folders to the VM" do
share_seq = sequence("share_seq") # share_seq = sequence("share_seq")
shared_folders = mock("shared_folders") # 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 == "foo" && sf.host_path == "from" }
shared_folders.expects(:<<).in_sequence(share_seq).with() { |sf| sf.name == "bar" && sf.host_path == "bfrom" } # shared_folders.expects(:<<).in_sequence(share_seq).with() { |sf| sf.name == "bar" && sf.host_path == "bfrom" }
@vm.stubs(:shared_folders).returns(shared_folders) # @vm.stubs(:shared_folders).returns(shared_folders)
@vm.expects(:save).once # @vm.expects(:save).once
@action.create_metadata # @action.create_metadata
end # end
end end
context "mounting the shared folders" do # context "mounting the shared folders" do
setup do # setup do
@folders = stub_shared_folders # @folders = stub_shared_folders
@ssh = mock("ssh") # @ssh = mock("ssh")
@runner.ssh.stubs(:execute).yields(@ssh) # @runner.ssh.stubs(:execute).yields(@ssh)
@runner.system.stubs(:mount_shared_folder) # @runner.system.stubs(:mount_shared_folder)
end # end
should "mount all shared folders to the VM" do # should "mount all shared folders to the VM" do
mount_seq = sequence("mount_seq") # mount_seq = sequence("mount_seq")
@folders.each do |name, hostpath, guestpath| # @folders.each do |name, hostpath, guestpath|
@runner.system.expects(:mount_shared_folder).with(@ssh, name, guestpath).in_sequence(mount_seq) # @runner.system.expects(:mount_shared_folder).with(@ssh, name, guestpath).in_sequence(mount_seq)
end # end
@action.after_boot # @action.after_boot
end # end
should "execute the necessary rysnc commands for each sync folder" do # should "execute the necessary rysnc commands for each sync folder" do
@folders.map { |f| f << 'sync' } # @folders.map { |f| f << 'sync' }
@folders.each do |name, hostpath, guestpath, syncd| # @folders.each do |name, hostpath, guestpath, syncd|
@runner.system.expects(:create_sync).with(@ssh, :syncpath => syncd, :guestpath => guestpath) # @runner.system.expects(:create_sync).with(@ssh, :syncpath => syncd, :guestpath => guestpath)
end # end
@runner.ssh.expects(:execute).yields(@ssh) # @runner.ssh.expects(:execute).yields(@ssh)
@action.after_boot # @action.after_boot
end # end
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
end end

View File

@ -262,39 +262,6 @@ class ConfigTest < Test::Unit::TestCase
end end
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 context "uid/gid" do
should "return the shared folder UID if set" do should "return the shared folder UID if set" do
@config.shared_folder_uid = "foo" @config.shared_folder_uid = "foo"