Class: Eco::API::Common::Session::SFTP

Inherits:
Object
  • Object
show all
Defined in:
lib/eco/api/common/session/sftp.rb

Instance Method Summary collapse

Constructor Details

#initialize(enviro:) ⇒ SFTP

Returns a new instance of SFTP.



9
10
11
12
# File 'lib/eco/api/common/session/sftp.rb', line 9

def initialize (enviro:)
  raise "Required Environment object (enviro:). Given: #{enviro}" if enviro && !enviro.is_a?(Eco::API::Common::Session::Environment)
  @enviro = enviro
end

Instance Method Details

#download(files, local_folder: nil) ⇒ Object

Downloads the files specified to a local folder

Parameters:

  • files (String, Array<String>)

    full path to remote file(s) to be downloaded

  • local_folder (String) (defaults to: nil)

    local destination folder ("." if not specified)

See Also:

  • Net::SFTP::Operations::Download


68
69
70
71
72
73
74
75
76
# File 'lib/eco/api/common/session/sftp.rb', line 68

def download(files, local_folder: nil)
  [files].flatten.compact.map do |fullname|
    dest_fullname = File.join(local_folder || ".", File.basename(fullname))
    sftp_session.download(fullname, dest_fullname)
  end.each do |dw|
    # run SSH event loop while dw.active?
    dw.wait
  end
end

#entries(path) ⇒ Object

See Also:

  • Net::SFTP::Operations::Dir#entries


33
34
35
# File 'lib/eco/api/common/session/sftp.rb', line 33

def entries(path)
  sftp_session.dir.entries(path).sort_by {|rf| rf.name}
end

#files(path, pattern: nil) ⇒ Array<Net::SFTP::Protocol::V01::Name>

Files of the remote directory.

Parameters:

  • path (String)

    remote directory path

  • pattern (Regexp) (defaults to: nil)

    if given, filters by using this pattern

Returns:

  • (Array<Net::SFTP::Protocol::V01::Name>)

See Also:

  • Net::SFTP::Operations::Dir#entries


42
43
44
45
46
# File 'lib/eco/api/common/session/sftp.rb', line 42

def files(path, pattern: nil)
  entries = entries(path).select {|remote_file| remote_file.file?}
  return entries unless pattern
  entries.select {|remote_file| remote_file.name =~ pattern}
end

#folders(path, pattern: nil) ⇒ Array<Net::SFTP::Protocol::V01::Name>

Folders of the remote directory.

Parameters:

  • path (String)

    remote directory path

  • pattern (Regexp) (defaults to: nil)

    if given, filters by using this pattern

Returns:

  • (Array<Net::SFTP::Protocol::V01::Name>)

See Also:

  • Net::SFTP::Operations::Dir#entries


53
54
55
56
57
# File 'lib/eco/api/common/session/sftp.rb', line 53

def folders(path, pattern: nil)
  entries = entries(path).select {|remote_file| remote_file.directory?}
  return entries unless pattern
  entries.select {|remote_file| remote_file.name =~ pattern}
end

#move(fullname_source, fullname_dest, flags = 0x0001, &callback) ⇒ Object

See Also:

  • Net::SFTP::Session#rename


60
61
62
# File 'lib/eco/api/common/session/sftp.rb', line 60

def move(fullname_source, fullname_dest, flags=0x0001, &callback)
  sftp_session.rename!(fullname_source, fullname_dest, flags, &callback)
end

#sftp_sessionObject

See Also:

  • Net::SFTP::Session


15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/eco/api/common/session/sftp.rb', line 15

def sftp_session
  begin
    @sftp_session ||= Net::SFTP.start(
      fetch_host,
      fetch_user,
      keys:            fetch_key_files,
      keys_only:       true,
      non_interactive: true
    )
  rescue Exception => e
    msg = "Could not open SFTP session. Possible misconfiguration: #{e}"
    logger.error(msg)
    raise msg
  end
  @sftp_session
end