Class: Bard::Copy

Inherits:
Struct
  • Object
show all
Defined in:
lib/bard/copy.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#fromObject

Returns the value of attribute from

Returns:

  • (Object)

    the current value of from



5
6
7
# File 'lib/bard/copy.rb', line 5

def from
  @from
end

#pathObject

Returns the value of attribute path

Returns:

  • (Object)

    the current value of path



5
6
7
# File 'lib/bard/copy.rb', line 5

def path
  @path
end

#toObject

Returns the value of attribute to

Returns:

  • (Object)

    the current value of to



5
6
7
# File 'lib/bard/copy.rb', line 5

def to
  @to
end

#verboseObject

Returns the value of attribute verbose

Returns:

  • (Object)

    the current value of verbose



5
6
7
# File 'lib/bard/copy.rb', line 5

def verbose
  @verbose
end

Class Method Details

.dir(path, from:, to:, verbose: false) ⇒ Object



10
11
12
# File 'lib/bard/copy.rb', line 10

def self.dir path, from:, to:, verbose: false
  new(path, from, to, verbose).rsync
end

.file(path, from:, to:, verbose: false) ⇒ Object



6
7
8
# File 'lib/bard/copy.rb', line 6

def self.file path, from:, to:, verbose: false
  new(path, from, to, verbose).scp
end

Instance Method Details

#rsyncObject



42
43
44
45
46
47
48
49
50
# File 'lib/bard/copy.rb', line 42

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

#rsync_as_mediatorObject

Raises:

  • (NotImplementedError)


66
67
68
69
70
71
72
73
74
# File 'lib/bard/copy.rb', line 66

def rsync_as_mediator
  raise NotImplementedError if from.gateway || to.gateway || from.ssh_key || to.ssh_key

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

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

#rsync_using_local(direction, server) ⇒ Object



52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/bard/copy.rb', line 52

def rsync_using_local direction, server
  gateway = server.gateway ? "-oProxyCommand=\"ssh #{server.ssh_uri(:gateway)} -W %h:%p\"" : ""

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

  from_and_to = ["./#{path}", server.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

#scpObject



14
15
16
17
18
19
20
21
22
# File 'lib/bard/copy.rb', line 14

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

#scp_as_mediatorObject

Raises:

  • (NotImplementedError)


36
37
38
39
40
# File 'lib/bard/copy.rb', line 36

def scp_as_mediator
  raise NotImplementedError if from.gateway || to.gateway || from.ssh_key || to.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, server) ⇒ Object



24
25
26
27
28
29
30
31
32
33
34
# File 'lib/bard/copy.rb', line 24

def scp_using_local direction, server
  gateway = server.gateway ? "-oProxyCommand='ssh #{server.ssh_uri(:gateway)} -W %h:%p'" : ""

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

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

  command = ["scp", gateway, ssh_key, *from_and_to].join(" ")
  Bard::Command.run! command, verbose: verbose
end