rsync mostly complete, few tests left

This commit is contained in:
John Bender 2010-05-18 01:24:59 -07:00
parent 7ac7af15d3
commit ce6d95c131
9 changed files with 114 additions and 16 deletions

View File

@ -24,6 +24,11 @@ Vagrant::Config.run do |config|
config.vm.boot_mode = "vrdp" config.vm.boot_mode = "vrdp"
config.vm.system = :linux config.vm.system = :linux
# TODO new config class
config.vm.rsync_opts = "-ur --delete"
config.vm.rsync_script = "/tmp/rsync"
config.vm.rsync_crontab_entry_file = "/tmp/crontab-entry"
config.package.name = 'vagrant' config.package.name = 'vagrant'
config.package.extension = '.box' config.package.extension = '.box'
end end

View File

@ -5,7 +5,7 @@ module Vagrant
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 name, value = data
acc << [name, File.expand_path(value[:hostpath]), value[:guestpath]] acc << [name, File.expand_path(value[:hostpath]), value[:guestpath], value[:rsyncpath]].compact
end end
end end
@ -18,9 +18,14 @@ module Vagrant
logger.info "Mounting shared folders..." logger.info "Mounting shared folders..."
@runner.ssh.execute do |ssh| @runner.ssh.execute do |ssh|
shared_folders.each do |name, hostpath, guestpath| @runner.system.prepare_rsync(ssh) if @runner.env.config.vm.rsync_required
logger.info "-- #{name}: #{guestpath}"
shared_folders.each do |name, hostpath, guestpath, rsyncpath|
logger.info "-- #{name}: #{rsyncpath ? guestpath + " -rsync-> " + rsyncpath : guestpath}"
@runner.system.mount_shared_folder(ssh, name, guestpath) @runner.system.mount_shared_folder(ssh, name, guestpath)
if rsyncpath
@runner.system.create_rsync(ssh, :rsyncpath => rsyncpath, :guestpath => guestpath)
end
end end
end end
end end

View File

@ -82,6 +82,10 @@ module Vagrant
attr_accessor :boot_mode attr_accessor :boot_mode
attr_accessor :project_directory attr_accessor :project_directory
attr_accessor :rsync_project_directory attr_accessor :rsync_project_directory
attr_accessor :rsync_opts
attr_accessor :rsync_script
attr_accessor :rsync_crontab_entry_file
attr_reader :rsync_required
attr_reader :forwarded_ports attr_reader :forwarded_ports
attr_reader :shared_folders attr_reader :shared_folders
attr_accessor :hd_location attr_accessor :hd_location
@ -151,6 +155,7 @@ module Vagrant
def shift(orig, rsync) def shift(orig, rsync)
if rsync if rsync
@rsync_required = true
[orig + '-rsync', rsync == true ? orig : rsync] [orig + '-rsync', rsync == true ? orig : rsync]
else else
[orig, rsync] [orig, rsync]

View File

@ -49,7 +49,24 @@ module Vagrant
def mount_shared_folder(ssh, name, guestpath) def mount_shared_folder(ssh, name, guestpath)
ssh.exec!("sudo mkdir -p #{guestpath}") ssh.exec!("sudo mkdir -p #{guestpath}")
mount_folder(ssh, name, guestpath) mount_folder(ssh, name, guestpath)
ssh.exec!("sudo chown #{vm.env.config.ssh.username} #{guestpath}") chown(ssh, guestpath)
end
def create_rsync(ssh, opts)
crontab_entry = render_crontab_entry(opts.merge(:rsyncopts => config.vm.rsync_opts,
:scriptname => config.vm.rsync_script))
ssh.exec!("sudo mkdir -p #{opts[:rsyncpath]}")
ssh.exec!("sudo chmod +x #{config.vm.rsync_script}")
ssh.exec!("sudo echo \"#{crontab_entry}\" >> #{config.vm.rsync_crontab_entry_file}")
ssh.exec!("crontab #{config.vm.rsync_crontab_entry_file}")
chown(ssh, opts[:rsyncpath])
end
def prepare_rsync(ssh)
logger.info "Preparing system for rsync..."
vm.env.ssh.upload!(StringIO.new(render_rsync), config.vm.rsync_script)
ssh.exec!('sudo rm #{config.vm.rsync_crontab_entry_file}')
end end
#------------------------------------------------------------------- #-------------------------------------------------------------------
@ -76,6 +93,22 @@ module Vagrant
sleep sleeptime sleep sleeptime
end end
end end
def chown(ssh, dir)
ssh.exec!("sudo chown #{config.ssh.username} #{dir}")
end
def config
vm.env.config
end
def render_rsync
TemplateRenderer.render('rsync')
end
def render_crontab_entry(opts)
TemplateRenderer.render('crontab-entry', opts)
end
end end
end end
end end

View File

@ -0,0 +1 @@
* * * * * <%= scriptname %> '<%= rsyncopts %>' '<%= guestpath %>/*' '<%= rsyncpath %>/'

14
templates/rsync.erb Normal file
View File

@ -0,0 +1,14 @@
#!/bin/sh
TIMESTART=`date +%s`
while [ 1 ]
do
echo 'Syncing...'
rsync $1 $2 $3
TIME=$((`date +%s`-$TIMESTART))
echo $TIME
if [ $TIME -ge 50 ]
then
break
fi
sleep 1
done

View File

@ -27,12 +27,7 @@ class SharedFoldersActionTest < Test::Unit::TestCase
end end
should "convert the vagrant config values into an array" do should "convert the vagrant config values into an array" do
env = mock_environment do |config| mock_env_shared_folders
config.vm.shared_folders.clear
config.vm.share_folder("foo", "bar", "baz")
end
@runner.expects(:env).returns(env)
result = [["foo", "baz", "bar"]] result = [["foo", "baz", "bar"]]
assert_equal result, @action.shared_folders assert_equal result, @action.shared_folders
@ -51,6 +46,23 @@ class SharedFoldersActionTest < Test::Unit::TestCase
result = [["foo", "expanded_baz", "bar"]] result = [["foo", "expanded_baz", "bar"]]
assert_equal result, @action.shared_folders assert_equal result, @action.shared_folders
end end
context "with rsync" do
should "append the rsync value to the other config values" do
mock_env_shared_folders(:rsync => true)
assert_equal [["foo", "baz", "bar-rsync", "bar"]], @action.shared_folders
end
end
def mock_env_shared_folders(opts={})
env = mock_environment do |config|
config.vm.shared_folders.clear
config.vm.share_folder("foo", "bar", "baz", opts)
end
@runner.expects(:env).returns(env)
end
end end
context "clearing shared folders" do context "clearing shared folders" do
@ -91,17 +103,34 @@ class SharedFoldersActionTest < Test::Unit::TestCase
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")
@runner.env.ssh.stubs(:execute).yields(@ssh)
@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")
ssh = mock("ssh")
@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
@action.after_boot
end
should "execute the necessary rysnc commands for each rsync folder" do
@folders.map { |f| f << 'rsync' }
@folders.each do |name, hostpath, guestpath, rsyncd|
@runner.system.expects(:create_rsync).with(@ssh, :rsyncpath => rsyncd, :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 rsyncd folders" do
# TODO
should "prepare the system for rsync if necessary" do
end
end
end end

View File

@ -257,7 +257,7 @@ class ConfigTest < Test::Unit::TestCase
end end
end end
context "shared folders" do context "rsyncd folders" do
should "set the rsyncpath to nil by default" do should "set the rsyncpath to nil by default" do
share_with_opts share_with_opts
assert !@config.shared_folders['foo'][:rsyncpath] assert !@config.shared_folders['foo'][:rsyncpath]
@ -280,6 +280,11 @@ class ConfigTest < Test::Unit::TestCase
end end
end end
should "set the rsync required flag to true" do
share_with_opts(:rsync => true)
assert @config.rsync_required
end
def share_with_opts(opts={}) def share_with_opts(opts={})
@config.share_folder('foo', 'foo-dir', '', opts) @config.share_folder('foo', 'foo-dir', '', opts)
end end

View File

@ -101,7 +101,9 @@ Gem::Specification.new do |s|
"templates/Vagrantfile.erb", "templates/Vagrantfile.erb",
"templates/chef_server_client.erb", "templates/chef_server_client.erb",
"templates/chef_solo_solo.erb", "templates/chef_solo_solo.erb",
"templates/crontab-entry.erb",
"templates/package_Vagrantfile.erb", "templates/package_Vagrantfile.erb",
"templates/rsync.erb",
"templates/ssh_config.erb", "templates/ssh_config.erb",
"templates/strings.yml", "templates/strings.yml",
"test/test_helper.rb", "test/test_helper.rb",
@ -229,7 +231,6 @@ Gem::Specification.new do |s|
"test/vagrant/commands/resume_test.rb", "test/vagrant/commands/resume_test.rb",
"test/vagrant/commands/ssh_test.rb", "test/vagrant/commands/ssh_test.rb",
"test/vagrant/downloaders/base_test.rb", "test/vagrant/downloaders/base_test.rb",
"test/vagrant/downloaders/file_test.rb",
"test/vagrant/downloaders/http_test.rb", "test/vagrant/downloaders/http_test.rb",
"test/vagrant/util/stacked_proc_runner_test.rb", "test/vagrant/util/stacked_proc_runner_test.rb",
"test/vagrant/util/progress_meter_test.rb", "test/vagrant/util/progress_meter_test.rb",