lib/vagrant-unison/command.rb in vagrant-unison-0.0.7 vs lib/vagrant-unison/command.rb in vagrant-unison-0.0.8
- old
+ new
@@ -13,86 +13,66 @@
with_target_vms do |machine|
hostpath, guestpath = init_paths machine
ssh_info = machine.ssh_info
- # Create the guest path
+ # Create empty guestpath
+ machine.communicate.sudo("rm -rf '#{guestpath}'")
machine.communicate.sudo("mkdir -p '#{guestpath}'")
machine.communicate.sudo("chown #{ssh_info[:username]} '#{guestpath}'")
- #copy up everything at the beginning
Net::SCP.start(ssh_info[:host], ssh_info[:username],
{ :port => ssh_info[:port],
- :keys => [ ssh_info[:private_key_path],
- :paranoid => false ] }) do |scp|
- scp.upload! hostpath, guestpath, :recursive => true
- end
+ :keys => [ ssh_info[:private_key_path] ],
+ :paranoid => false }) do |scp|
- @env.ui.info "Watching #{hostpath} for changes..."
+ #copy up everything at the beginning
+ @env.ui.info "Uploading {host}::#{hostpath} to {guest}::#{guestpath}"
+ Dir.glob("#{hostpath}**/*", File::FNM_DOTMATCH).each do |file|
+ remote_file = file.gsub(hostpath, guestpath)
+ if File.stat(file).file?
+ scp.upload!( file, remote_file ) do |ch, name, sent, total|
+ @env.ui.info "\r#{name}: #{(sent.to_f * 100 / total.to_f).to_i}%"
+ end
+ end
+ if File.directory?(file)
+ machine.communicate.sudo("mkdir -p '#{remote_file}'")
+ machine.communicate.sudo("chown #{ssh_info[:username]} '#{remote_file}'")
+ end
+ end
- Listen.to(hostpath) do |modified, added, removed|
- Net::SCP.start(ssh_info[:host], ssh_info[:username],
- { :port => ssh_info[:port],
- :keys => [ ssh_info[:private_key_path],
- :paranoid => false ] }) do |scp|
- (modified_list << added_list).flatten.each do |file|
+ @env.ui.info "Watching {host}::#{hostpath} for changes..."
+
+ Listen.to(hostpath) do |modified, added, removed|
+ (modified << added).flatten.each do |file|
remote_file = file.gsub(hostpath, guestpath)
- @env.ui.info "Uploading #{file} to #{remote_file}"
- scp.upload! file, remote_file
+ @env.ui.info "Uploading {host}::#{file} to {guest VM}::#{remote_file}"
+ scp.upload!( file, remote_file ) do |ch, name, sent, total|
+ @env.ui.info "\r#{name}: #{(sent.to_f * 100 / total.to_f).to_i}%"
+ end
end
removed.each do |file|
remote_file = file.gsub(hostpath, guestpath)
- @env.ui.info "Deleting #{remote_file}"
+ @env.ui.info "Deleting {guest VM}::#{remote_file}"
machine.communicate.sudo("rm #{remote_file}")
end
- end
+ end # Listen
+ end # Net::SCP.start
- end
-
end
0 #all is well
end
def init_paths(machine)
hostpath = File.expand_path(machine.config.sync.host_folder, @env.root_path)
guestpath = machine.config.sync.guest_folder
- # Make sure there is a trailing slash on the host path to
- # avoid creating an additional directory with rsync
+ # Make sure there is a trailing slash both paths
hostpath = "#{hostpath}/" if hostpath !~ /\/$/
+ guestpath = "#{guestpath}/" if guestpath !~ /\/$/
[hostpath, guestpath]
- end
-
- def trigger_unison_sync(machine)
- hostpath, guestpath = init_paths machine
-
- @env.ui.info "Unisoning changes from {host}::#{hostpath} --> {guest VM}::#{guestpath}"
-
- ssh_info = machine.ssh_info
-
- # Create the guest path
- machine.communicate.sudo("mkdir -p '#{guestpath}'")
- machine.communicate.sudo("chown #{ssh_info[:username]} '#{guestpath}'")
-
- # Unison over to the guest path using the SSH info
- command = [
- "unison", "-batch",
- "-ignore=Name {.git*,.vagrant/,*.DS_Store}",
- "-sshargs", "-p #{ssh_info[:port]} -o StrictHostKeyChecking=no -i #{ssh_info[:private_key_path]}",
- hostpath,
- "ssh://#{ssh_info[:username]}@#{ssh_info[:host]}/#{guestpath}"
- ]
-
- r = Vagrant::Util::Subprocess.execute(*command)
- if r.exit_code != 0
- raise Vagrant::Errors::UnisonError,
- :command => command.inspect,
- :guestpath => guestpath,
- :hostpath => hostpath,
- :stderr => r.stderr
- end
end
end
end
end
\ No newline at end of file