Class: Bard::SSH::Copy

Inherits:
Copy
  • Object
show all
Defined in:
lib/bard/plugins/ssh/copy.rb

Instance Attribute Summary

Attributes inherited from Copy

#from, #path, #to, #verbose

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Copy

dir, file, inherited, #initialize

Constructor Details

This class inherits a constructor from Bard::Copy

Class Method Details

.can_handle?(from, to) ⇒ Boolean

Returns:

  • (Boolean)


8
9
10
# File 'lib/bard/plugins/ssh/copy.rb', line 8

def self.can_handle?(from, to)
  from.has_capability?(:ssh) || to.has_capability?(:ssh)
end

Instance Method Details

#dirObject



50
51
52
53
54
55
56
57
58
# File 'lib/bard/plugins/ssh/copy.rb', line 50

def dir
  if from.key == :local
    rsync_using_local :to, to
  elsif to.key == :local
    rsync_using_local :from, from
  else
    rsync_as_mediator
  end
end

#fileObject



12
13
14
15
16
17
18
19
20
# File 'lib/bard/plugins/ssh/copy.rb', line 12

def file
  if from.key == :local
    scp_using_local :to, to
  elsif to.key == :local
    scp_using_local :from, from
  else
    scp_as_mediator
  end
end

#rsync_as_mediatorObject

Raises:

  • (NotImplementedError)


78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
# File 'lib/bard/plugins/ssh/copy.rb', line 78

def rsync_as_mediator
  from_server = from.server
  to_server = to.server

  raise NotImplementedError if from_server.gateway || to_server.gateway || from_server.ssh_key || to_server.ssh_key

  from_uri = from_server.ssh_uri
  to_uri = to_server.ssh_uri

  from_str = "-p#{from_uri.port || 22} #{from_uri.user}@#{from_uri.host}"
  to_str = to.rsync_uri(path).sub(%r(/[^/]+$), '/')

  command = %(ssh -A #{from_str} 'rsync -e \"ssh -A -p#{to_uri.port || 22} -o StrictHostKeyChecking=no -o LogLevel=ERROR\" --delete --info=progress2 -az #{from.path}/#{path} #{to_str}')
  Bard::Command.run! command, verbose: verbose
end

#rsync_using_local(direction, target) ⇒ Object



60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/bard/plugins/ssh/copy.rb', line 60

def rsync_using_local direction, target
  ssh_server = target.server

  ssh_uri = ssh_server.ssh_uri

  gateway = ssh_server.gateway ? "-oProxyCommand=\"ssh #{ssh_server.gateway} -W %h:%p\"" : ""

  ssh_key = ssh_server.ssh_key ? "-i #{ssh_server.ssh_key}" : ""
  ssh = "-e'ssh #{gateway} -p#{ssh_uri.port || 22}'"

  from_and_to = ["./#{path}", target.rsync_uri(path)]
  from_and_to.reverse! if direction == :from
  from_and_to[-1].sub! %r(/[^/]+$), '/'

  command = "rsync #{ssh} --delete --info=progress2 -az #{from_and_to.join(" ")}"
  Bard::Command.run! command, verbose: verbose
end

#scp_as_mediatorObject

Raises:

  • (NotImplementedError)


41
42
43
44
45
46
47
48
# File 'lib/bard/plugins/ssh/copy.rb', line 41

def scp_as_mediator
  from_server = from.server
  to_server = to.server

  raise NotImplementedError if from_server.gateway || to_server.gateway || from_server.ssh_key || to_server.ssh_key
  command = "scp -o ForwardAgent=yes #{from.scp_uri(path)} #{to.scp_uri(path)}"
  Bard::Command.run! command, verbose: verbose
end

#scp_using_local(direction, target) ⇒ Object



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/bard/plugins/ssh/copy.rb', line 22

def scp_using_local direction, target
  ssh_server = target.server

  gateway = ssh_server.gateway ? "-oProxyCommand='ssh #{ssh_server.gateway} -W %h:%p'" : ""

  ssh_key = ssh_server.ssh_key ? "-i #{ssh_server.ssh_key}" : ""

  ssh_opts = "-o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null -o LogLevel=ERROR"

  port = ssh_server.port
  port_opt = port && port.to_s != "22" ? "-P #{port}" : ""

  from_and_to = [path, target.scp_uri(path).to_s]
  from_and_to.reverse! if direction == :from

  command = ["scp", ssh_opts, gateway, ssh_key, port_opt, *from_and_to].reject(&:empty?).join(" ")
  Bard::Command.run! command, verbose: verbose
end