Class: Photocopier::SSH

Inherits:
Adapter show all
Defined in:
lib/photocopier/ssh.rb

Instance Attribute Summary collapse

Attributes inherited from Adapter

#logger

Instance Method Summary collapse

Methods inherited from Adapter

#put

Constructor Details

#initialize(options = {}) ⇒ SSH

Returns a new instance of SSH.



6
7
8
9
10
# File 'lib/photocopier/ssh.rb', line 6

def initialize(options = {})
  @options = options
  @gateway_options = options.delete(:gateway)
  @rsync_options = options.delete(:rsync_options)
end

Instance Attribute Details

#gateway_optionsObject (readonly)

Returns the value of attribute gateway_options.



4
5
6
# File 'lib/photocopier/ssh.rb', line 4

def gateway_options
  @gateway_options
end

#rsync_optionsObject (readonly)

Returns the value of attribute rsync_options.



4
5
6
# File 'lib/photocopier/ssh.rb', line 4

def rsync_options
  @rsync_options
end

Instance Method Details

#delete(remote_path) ⇒ Object



24
25
26
# File 'lib/photocopier/ssh.rb', line 24

def delete(remote_path)
  exec!("rm -rf #{Shellwords.escape(remote_path)}")
end

#exec!(cmd) ⇒ Object



39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/photocopier/ssh.rb', line 39

def exec!(cmd)
  stdout = ""
  stderr = ""
  exit_code = nil
  session.open_channel do |channel|
    channel.exec(cmd) do |ch, success|
      channel.on_data do |ch, data|
        stdout << data
      end
      channel.on_extended_data do |ch, type, data|
        stderr << data
      end
      channel.on_request("exit-status") do |ch, data|
        exit_code = data.read_long
      end
    end
  end
  session.loop
  [ stdout, stderr, exit_code ]
end

#get(remote_path, file_path = nil) ⇒ Object



16
17
18
# File 'lib/photocopier/ssh.rb', line 16

def get(remote_path, file_path = nil)
  session.scp.download! remote_path, file_path
end

#get_directory(remote_path, local_path, exclude = []) ⇒ Object



28
29
30
31
32
# File 'lib/photocopier/ssh.rb', line 28

def get_directory(remote_path, local_path, exclude = [])
  FileUtils.mkdir_p(local_path)
  remote_path << "/" unless remote_path.end_with?("/")
  rsync ":#{remote_path}", local_path, exclude
end

#optionsObject



12
13
14
# File 'lib/photocopier/ssh.rb', line 12

def options
  @options.clone
end

#put_directory(local_path, remote_path, exclude = []) ⇒ Object



34
35
36
37
# File 'lib/photocopier/ssh.rb', line 34

def put_directory(local_path, remote_path, exclude = [])
  local_path << "/" unless local_path.end_with?("/")
  rsync "#{local_path}", ":#{remote_path}", exclude
end

#put_file(file_path, remote_path) ⇒ Object



20
21
22
# File 'lib/photocopier/ssh.rb', line 20

def put_file(file_path, remote_path)
  session.scp.upload! file_path, remote_path
end