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.



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

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


71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/eco/api/common/session/sftp.rb', line 71

def download(files, local_folder: nil)
  puts "Creating local files:"
  [files].flatten.compact.map do |fullname|
    basename = windows_basename(fullname)
    dest_fullname = File.join(local_folder || ".", basename)
    puts "#{dest_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


30
31
32
# File 'lib/eco/api/common/session/sftp.rb', line 30

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


39
40
41
42
43
# File 'lib/eco/api/common/session/sftp.rb', line 39

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


50
51
52
53
54
# File 'lib/eco/api/common/session/sftp.rb', line 50

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, override: true, &callback) ⇒ Object

See Also:

  • Net::SFTP::Session#rename


57
58
59
60
61
62
63
64
65
# File 'lib/eco/api/common/session/sftp.rb', line 57

def move(fullname_source, fullname_dest, flags=0x0001, override: true, &callback)
  begin
    sftp_session.rename!(fullname_source, fullname_dest, flags, &callback)
  rescue Net::SFTP::StatusException => e
    raise unless override
    sftp_session.remove(fullname_dest)
    sftp_session.rename!(fullname_source, fullname_dest, flags, &callback)
  end
end

#sftp_sessionObject

See Also:

  • Net::SFTP::Session


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

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