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.



10
11
12
13
14
15
16
17
# File 'lib/etna/remote.rb', line 10

def initialize(host:, username:, password: nil, port: 22, root:, **args)
  @username = username
  @password = password
  @host = host
  @port = port
  @root = root
  raise "RemoteSSH must have host, username and password" unless @host && @username && @password
end

Instance Method Details

#file_upload(remote_path, content) ⇒ Object



41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/etna/remote.rb', line 41

def file_upload(remote_path, content)
  begin
    Tempfile.create do |temp_file|
      temp_file.binmode
      temp_file.write(content)
      temp_file.flush
      ssh.scp.upload!(temp_file.path, remote_path)
    end
  rescue StandardError => e
    raise RemoteSSHError.new("File upload failed: #{e.message}")
  end
end

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

Yields:

  • (remote_filename)

Raises:



29
30
31
32
33
34
35
36
37
38
39
# File 'lib/etna/remote.rb', line 29

def lftp_get(username:, password:, host:, remote_filename:, local_filename:, &block)
  full_local_path = local_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:



23
24
25
26
27
# File 'lib/etna/remote.rb', line 23

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

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

#sshObject



19
20
21
# File 'lib/etna/remote.rb', line 19

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