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.



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

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.



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

def gateway_options
  @gateway_options
end

#rsync_optionsObject (readonly)

Returns the value of attribute rsync_options.



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

def rsync_options
  @rsync_options
end

Instance Method Details

#delete(remote_path) ⇒ Object



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

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

#exec!(cmd) ⇒ Object



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

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



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

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

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



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

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

#optionsObject



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

def options
  @options.clone
end

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



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

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

#put_file(file_path, remote_path) ⇒ Object



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

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