Class: SupplyDrop::Rsync

Inherits:
Object
  • Object
show all
Defined in:
lib/supply_drop/rsync.rb

Class Method Summary collapse

Class Method Details

.command(from, to, options = {}) ⇒ Object



4
5
6
7
8
9
10
11
# File 'lib/supply_drop/rsync.rb', line 4

def command(from, to, options={})
  flags = ['-az']
  flags << '--delete' if options[:delete]
  flags << excludes(options[:excludes]) if options.has_key?(:excludes)
  flags << ssh_options(options[:ssh]) if options.has_key?(:ssh)

  "rsync #{flags.compact.join(' ')} #{from} #{to}"
end

.excludes(patterns) ⇒ Object



18
19
20
# File 'lib/supply_drop/rsync.rb', line 18

def excludes(patterns)
  [patterns].flatten.map { |p| "--exclude=#{p}" }
end

.remote_address(user, host, path) ⇒ Object



13
14
15
16
# File 'lib/supply_drop/rsync.rb', line 13

def remote_address(user, host, path)
  user_with_host = [user, host].compact.join('@')
  [user_with_host, path].join(':')
end

.ssh_options(options) ⇒ Object

Convert Net::SSH options into OpenSSH options.

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:

  • many OpenSSH options not supported

  • some options only make sense for Net::SSH

  • compression: for Net::SSH, this option is supposed to accept true, false, or algorithm. OpenSSH accepts ‘yes’ or ‘no’



37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/supply_drop/rsync.rb', line 37

def ssh_options(options)
  mapped_options = options.map do |key, value|
    next if value.nil?

    case key
    when :auth_methods            then opt_auth_methods(value)
    when :bind_address            then opt('BindAddress', value)
    when :compression             then opt('Compression', value ? 'yes' : 'no')
    when :compression_level       then opt('CompressionLevel', value.to_i)
    when :config                  then value ? "-F '#{value}'" : nil
    when :encryption              then opt('Ciphers', [value].flatten.join(','))
    when :forward_agent           then opt('ForwardAgent', value)
    when :global_known_hosts_file then opt('GlobalKnownHostsFile', value)
    when :hmac                    then opt('MACs', [value].flatten.join(','))
    when :host_key                then opt('HostKeyAlgorithms', [value].flatten.join(','))
    when :host_key_alias          then opt('HostKeyAlias', value)
    when :host_name               then opt('HostName', value)
    when :kex                     then opt('KexAlgorithms', [value].flatten.join(','))
    when :key_data                then nil # not supported
    when :keys                    then [value].flatten.select { |k| File.exist?(k) }.map { |k| "-i '#{k}'" }
    when :keys_only               then opt('IdentitiesOnly', value ? 'yes' : 'no')
    when :languages               then nil # not applicable
    when :logger                  then nil # not applicable
    when :paranoid                then opt('StrictHostKeyChecking', value ? 'yes' : 'no')
    when :passphrase              then nil # not supported
    when :password                then nil # not supported
    when :port                    then "-p #{value.to_i}"
    when :properties              then nil # not applicable
    when :proxy                   then nil # not applicable
    when :rekey_blocks_limit      then nil # not supported
    when :rekey_limit             then opt('RekeyLimit', reverse_interpret_size(value))
    when :rekey_packet_limit      then nil # not supported
    when :timeout                 then opt('ConnectTimeout', value.to_i)
    when :user                    then "-l #{value}"
    when :user_known_hosts_file   then multi_opt('UserKnownHostsFile', value)
    when :verbose                 then opt('LogLevel', interpret_log_level(value))
    end
  end.compact

  %[-e "ssh #{mapped_options.join(' ')}"] unless mapped_options.empty?
end