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

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

Instance Method Summary collapse

Methods included from Language::AuxiliarLogger

#log

Constructor Details

#initialize(enviro:) ⇒ SFTP

Returns a new instance of SFTP.



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

def initialize(enviro:)
  invalid_env = enviro && !enviro.is_a?(Eco::API::Common::Session::Environment)
  raise "Required Environment object (enviro:). Given: #{enviro}" if invalid_env
  @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


77
78
79
80
81
82
83
84
85
86
# File 'lib/eco/api/common/session/sftp.rb', line 77

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(&:wait)
  # run SSH event loop while dw.active?
end

#entries(path) ⇒ Object

See Also:

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


35
36
37
# File 'lib/eco/api/common/session/sftp.rb', line 35

def entries(path)
  sftp_session.dir.entries(path).sort_by(&: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


44
45
46
47
48
49
# File 'lib/eco/api/common/session/sftp.rb', line 44

def files(path, pattern: nil)
  entries = entries(path).select(&: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


56
57
58
59
60
61
# File 'lib/eco/api/common/session/sftp.rb', line 56

def folders(path, pattern: nil)
  entries = entries(path).select(&:directory?)
  return entries unless pattern

  entries.select {|remote_file| remote_file.name =~ pattern}
end

#hostObject



14
15
16
# File 'lib/eco/api/common/session/sftp.rb', line 14

def host
  @host ||= fetch_host
end

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

See Also:

  • Net::SFTP::Session#rename


64
65
66
67
68
69
70
71
# File 'lib/eco/api/common/session/sftp.rb', line 64

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

  sftp_session.remove(fullname_dest)
  sftp_session.rename!(fullname_source, fullname_dest, flags, &callback)
end

#sftp_sessionObject

See Also:

  • Net::SFTP::Session


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

def sftp_session
  require "net/sftp"

  @sftp_session ||= Net::SFTP.start(
    host,
    fetch_user,
    **session_options
  )
rescue StandardError => err
  log(:error) {
    "Could not open SFTP session. Possible misconfiguration: #{err}"
  }
  raise
end

#upload(local_file, remote_folder:, gid: nil) ⇒ Object

Upload a file to the specific remote_folder



89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
# File 'lib/eco/api/common/session/sftp.rb', line 89

def upload(local_file, remote_folder:, gid: nil)
  return false unless local_file && File.exist?(local_file)

  dest_file = "#{remote_folder}/#{File.basename(local_file)}"
  res       = sftp_session.upload!(local_file, dest_file)

  unless gid.nil?
    attrs = sftp_session.stat!(dest_file)
    unless gid == attrs.gid
      flags     = {permissions: 0o660, uid: attrs.uid, gid: gid}
      _stat_res = sftp_session.setstat!(dest_file, flags)
    end
  end

  log(:info) {
    "Uploaded '#{local_file}' (#{res})"
  }
  true
end