class Eco::API::Common::Session::SFTP

Public Class Methods

new(enviro:) click to toggle source
# 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

Public Instance Methods

download(files, local_folder: nil) click to toggle source

Downloads the files specified to a local folder @see Net::SFTP::Operations::Download @param files [String, Array<String>] full path to remote file(s) to be downloaded @param local_folder [String] local destination folder (`“.”` if not specified)

# 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) click to toggle source

@see Net::SFTP::Operations::Dir#entries

# 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) click to toggle source

Files of the remote directory. @see Net::SFTP::Operations::Dir#entries @param path [String] remote directory `path` @param pattern [Regexp] if given, filters by using this pattern @return [Array<Net::SFTP::Protocol::V01::Name>]

# 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) click to toggle source

Folders of the remote directory. @see Net::SFTP::Operations::Dir#entries @param path [String] remote directory `path` @param pattern [Regexp] if given, filters by using this pattern @return [Array<Net::SFTP::Protocol::V01::Name>]

# 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) click to toggle source

@see Net::SFTP::Session#rename

# 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_session() click to toggle source

@see Net::SFTP::Session

# 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

Private Instance Methods

config() click to toggle source
# File lib/eco/api/common/session/sftp.rb, line 84
def config
  @enviro.config || {}
end
fetch_base_path() click to toggle source
# File lib/eco/api/common/session/sftp.rb, line 100
def fetch_base_path
  config.sftp.base_path || ENV['SFTP_BASE_PATH']
end
fetch_host() click to toggle source
# File lib/eco/api/common/session/sftp.rb, line 88
def fetch_host
  config.sftp.host || ENV['SFTP_HOST']
end
fetch_key_files() click to toggle source
# File lib/eco/api/common/session/sftp.rb, line 96
def fetch_key_files
  [config.sftp.key_file || ENV['SFTP_KEY_FILE']]
end
fetch_user() click to toggle source
# File lib/eco/api/common/session/sftp.rb, line 92
def fetch_user
  config.sftp.user || ENV['SFTP_USERNAME']
end
logger() click to toggle source
# File lib/eco/api/common/session/sftp.rb, line 80
def logger
  @enviro&.logger || ::Logger.new(IO::NULL)
end