Class: Eco::API::Common::Session::SFTP
- Inherits:
-
Object
- Object
- Eco::API::Common::Session::SFTP
- Defined in:
- lib/eco/api/common/session/sftp.rb
Instance Method Summary collapse
-
#download(files, local_folder: nil) ⇒ Object
Downloads the files specified to a local folder.
- #entries(path) ⇒ Object
-
#files(path, pattern: nil) ⇒ Array<Net::SFTP::Protocol::V01::Name>
Files of the remote directory.
-
#folders(path, pattern: nil) ⇒ Array<Net::SFTP::Protocol::V01::Name>
Folders of the remote directory.
-
#initialize(enviro:) ⇒ SFTP
constructor
A new instance of SFTP.
- #move(fullname_source, fullname_dest, flags = 0x0001, &callback) ⇒ Object
- #sftp_session ⇒ Object
Constructor Details
#initialize(enviro:) ⇒ SFTP
Returns a new instance of SFTP.
10 11 12 13 |
# File 'lib/eco/api/common/session/sftp.rb', line 10 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
69 70 71 72 73 74 75 76 77 |
# File 'lib/eco/api/common/session/sftp.rb', line 69 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
34 35 36 |
# File 'lib/eco/api/common/session/sftp.rb', line 34 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.
43 44 45 46 47 |
# File 'lib/eco/api/common/session/sftp.rb', line 43 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.
54 55 56 57 58 |
# File 'lib/eco/api/common/session/sftp.rb', line 54 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
61 62 63 |
# File 'lib/eco/api/common/session/sftp.rb', line 61 def move(fullname_source, fullname_dest, flags=0x0001, &callback) sftp_session.rename!(fullname_source, fullname_dest, flags, &callback) end |
#sftp_session ⇒ Object
16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 |
# File 'lib/eco/api/common/session/sftp.rb', line 16 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 |