class RsyncCommand

Converts capistrano-style ssh configuration (which uses Net::SSH) into a OpenSSH command line flags suitable for rsync.

For a list of the options normally support by Net::SSH (and thus Capistrano), see net-ssh.github.com/net-ssh/classes/Net/SSH.html#method-c-start

Also, to see how Net::SSH does the opposite of the conversion we are doing here, check out: github.com/net-ssh/net-ssh/blob/master/lib/net/ssh/config.rb

API mismatch:

Constants

VERSION

Attributes

failures[RW]
logger[RW]

Public Class Methods

new(options={}) click to toggle source
# File vendor/rsync_command/lib/rsync_command.rb, line 48
def initialize(options={})
  @options = options.dup
  @logger = @options.delete(:logger)
  @flags = @options.delete(:flags)
  @failures = []
  @failures.extend(MonitorMixin)
end

Public Instance Methods

asynchronously(array, &block) click to toggle source

takes an Enumerable and iterates each item in the list in parallel.

# File vendor/rsync_command/lib/rsync_command.rb, line 59
def asynchronously(array, &block)
  pool = ThreadPool.new
  array.each do |item|
    pool.schedule(RsyncRunner.new(self), item, &block)
  end
  pool.shutdown
end
command(src, dest, options={}) click to toggle source

build rsync command

# File vendor/rsync_command/lib/rsync_command.rb, line 98
def command(src, dest, options={})
  src = remote_address(src)
  dest = remote_address(dest)
  options = @options.merge(options)
  flags = []
  flags << @flags if @flags
  flags << options[:flags] if options.has_key?(:flags)
  flags << '--delete' if options[:delete]
  flags << includes(options[:includes]) if options.has_key?(:includes)
  flags << excludes(options[:excludes]) if options.has_key?(:excludes)
  flags << SshOptions.new(options[:ssh]).to_flags if options.has_key?(:ssh)
  "rsync #{flags.compact.join(' ')} #{src} #{dest}"
end
excludes(patterns) click to toggle source
# File vendor/rsync_command/lib/rsync_command.rb, line 124
def excludes(patterns)
  [patterns].flatten.compact.map { |p| "--exclude='#{p}'" }
end
exec_rsync(src, dest, options={}) click to toggle source

runs rsync, recording failures

# File vendor/rsync_command/lib/rsync_command.rb, line 77
def exec_rsync(src, dest, options={})
  logger = options[:logger] || @logger
  @failures.synchronize do
    @failures.clear
  end
  rsync_cmd = command(src, dest, options)
  if options[:chdir]
    rsync_cmd = "cd '#{options[:chdir]}'; #{rsync_cmd}"
  end
  logger.debug rsync_cmd if logger
  ok = system(rsync_cmd)
  unless ok
    @failures.synchronize do
      @failures << {:source => src, :dest => dest, :options => options.dup}
    end
  end
end
failed?() click to toggle source

returns true if last exec returned a failure

# File vendor/rsync_command/lib/rsync_command.rb, line 70
def failed?
  @failures && @failures.any?
end
includes(patterns) click to toggle source
# File vendor/rsync_command/lib/rsync_command.rb, line 128
def includes(patterns)
  [patterns].flatten.compact.map { |p| "--include='#{p}'" }
end
remote_address(address) click to toggle source

Creates an rsync location if the address is a hash with keys :user, :host, and :path (each component is optional). If address is a string, we just pass it through.

# File vendor/rsync_command/lib/rsync_command.rb, line 116
def remote_address(address)
  if address.is_a? String
    address # assume it is already formatted.
  elsif address.is_a? Hash
    [[address[:user], address[:host]].compact.join('@'), address[:path]].compact.join(':')
  end
end