Class: Etna::RemoteSSH

Inherits:
Object
  • Object
show all
Defined in:
lib/etna/remote.rb

Defined Under Namespace

Classes: RemoteSSHError

Instance Method Summary collapse

Constructor Details

#initialize(host:, username:, password: nil, port: 22, root:, **args) ⇒ RemoteSSH

Returns a new instance of RemoteSSH.



8
9
10
11
12
13
14
# File 'lib/etna/remote.rb', line 8

def initialize(host:, username:, password: nil, port: 22, root:, **args)
  @username = username
  @password = password
  @host = host
  @port = port
  @root = root
end

Instance Method Details

#lftp_get(username:, password:, host:, remote_filename:) {|remote_filename| ... } ⇒ Object

Yields:

  • (remote_filename)

Raises:



26
27
28
29
30
31
32
33
34
35
36
# File 'lib/etna/remote.rb', line 26

def lftp_get(username:, password:, host:, remote_filename:, &block)
  full_local_path = ::File.join(@root, host, remote_filename)
  full_local_dir = ::File.dirname(full_local_path)
  mkdir_p(full_local_dir)

  cmd = "lftp sftp://#{username}:#{password}@#{host}  -e \"get #{remote_filename} -o #{full_local_path}; bye\""

  output = ssh.exec!(cmd)
  raise RemoteSSHError.new("LFTP get failure: #{output}") unless 0 == output.exitstatus
  yield remote_filename if block_given?
end

#mkdir_p(dir) ⇒ Object

Raises:



20
21
22
23
24
# File 'lib/etna/remote.rb', line 20

def mkdir_p(dir)
  output = ssh.exec!("mkdir -p #{dir}")

  raise RemoteSSHError.new("Unable to mkdir -p, #{output}") unless 0 == output.exitstatus
end

#sshObject



16
17
18
# File 'lib/etna/remote.rb', line 16

def ssh
  @ssh ||= Net::SSH.start(@host, @username, password: @password)
end