Class: PgExport::Gateways::Ssh

Inherits:
Object
  • Object
show all
Defined in:
lib/pg_export/gateways/ssh.rb

Instance Method Summary collapse

Constructor Details

#initialize(host:, user:, password:) ⇒ Ssh

Returns a new instance of Ssh.



10
11
12
# File 'lib/pg_export/gateways/ssh.rb', line 10

def initialize(host:, user:, password:)
  @host, @user, @password, @logger = host, user, password
end

Instance Method Details

#closeObject



27
28
29
# File 'lib/pg_export/gateways/ssh.rb', line 27

def close
  @ssh&.close
end

#delete(name) ⇒ Object



47
48
49
# File 'lib/pg_export/gateways/ssh.rb', line 47

def delete(name)
  ssh.exec!("rm #{name}")
end

#get(file, name) ⇒ Object



55
56
57
# File 'lib/pg_export/gateways/ssh.rb', line 55

def get(file, name)
  ssh.scp.download(name, file.path).wait
end

#list(name) ⇒ Object



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/pg_export/gateways/ssh.rb', line 31

def list(name)
  grep =
    if name.nil? || name.empty?
      ''
    else
      " | grep #{name}"
    end

  ssh
    .exec!("ls -l#{grep}")
    .split("\n").map { |row| extract_meaningful_attributes(row) }
    .reject { |item| item[:name].nil? }
    .sort_by { |item| item[:name] }
    .reverse
end

#openObject



14
15
16
17
18
19
20
21
# File 'lib/pg_export/gateways/ssh.rb', line 14

def open
  @ssh =
    if password.nil?
      Net::SSH.start(host, user)
    else
      Net::SSH.start(host, user, password)
    end
end

#persist(file, name) ⇒ Object



51
52
53
# File 'lib/pg_export/gateways/ssh.rb', line 51

def persist(file, name)
  ssh.scp.upload(file.path, name).wait
end

#sshObject



63
64
65
# File 'lib/pg_export/gateways/ssh.rb', line 63

def ssh
  @ssh ||= open
end

#to_sObject



59
60
61
# File 'lib/pg_export/gateways/ssh.rb', line 59

def to_s
  host
end

#welcomeObject



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

def welcome
  open.exec!('hostname; whoami')
end