Class: Eco::API::Common::Session::SFTP
- Includes:
- Language::AuxiliarLogger
- Defined in:
- lib/eco/api/common/session/sftp.rb
Instance Method Summary collapse
-
#download(files, local_folder: nil, &block) ⇒ Array<String>
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.
- #host ⇒ Object
-
#initialize(enviro:) ⇒ SFTP
constructor
A new instance of SFTP.
- #move(fullname_source, fullname_dest, flags = 0x0001, override: true, &callback) ⇒ Object
- #sftp_session ⇒ Object
-
#upload(local_file, remote_folder:, gid: nil) ⇒ String, FalseClass
Upload a file to the specific
remote_folder.
Methods included from Language::AuxiliarLogger
Constructor Details
#initialize(enviro:) ⇒ SFTP
Returns a new instance of SFTP.
5 6 7 8 9 10 |
# File 'lib/eco/api/common/session/sftp.rb', line 5 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, &block) ⇒ Array<String>
Downloads the files specified to a local folder
101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 |
# File 'lib/eco/api/common/session/sftp.rb', line 101 def download(files, local_folder: nil, &block) puts 'Creating local files:' created_files = [] [files].flatten.compact.map do |fullname| basename = windows_basename(fullname) dest_fullname = File.join(local_folder || '.', basename) puts " * #{dest_fullname}" created_files << dest_fullname sftp_session.download(fullname, dest_fullname) end.each(&:wait) # run SSH event loop while dw.active? created_files.tap { created_files.each(&block) } end |
#entries(path) ⇒ Object
33 34 35 |
# File 'lib/eco/api/common/session/sftp.rb', line 33 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.
42 43 44 45 46 47 |
# File 'lib/eco/api/common/session/sftp.rb', line 42 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.
54 55 56 57 58 59 |
# File 'lib/eco/api/common/session/sftp.rb', line 54 def folders(path, pattern: nil) entries = entries(path).select(&:directory?) return entries unless pattern entries.select {|remote_file| remote_file.name =~ pattern} end |
#host ⇒ Object
12 13 14 |
# File 'lib/eco/api/common/session/sftp.rb', line 12 def host @host ||= fetch_host end |
#move(fullname_source, fullname_dest, flags = 0x0001, override: true, &callback) ⇒ Object
62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 |
# File 'lib/eco/api/common/session/sftp.rb', line 62 def move(fullname_source, fullname_dest, flags = 0x0001, override: true, &callback) sftp_session.rename!(fullname_source, fullname_dest, flags, &callback) true rescue Net::SFTP::StatusException => err case err.code when Net::SFTP::Constants::StatusCodes::FX_FILE_ALREADY_EXISTS log(:waning) { msg = "Remote file '#{fullname_dest}' already exists." msg << "Overriding..." if override } if override sftp_session.remove(fullname_dest) # sftp_session.rename!(fullname_source, fullname_dest, flags, &callback) move(fullname_source, fullname_dest, flags, override: false, &callback) end when Net::SFTP::Constants::StatusCodes::FX_PERMISSION_DENIED, Net::SFTP::Constants::StatusCodes::FX_WRITE_PROTECT log(:error) { "Not allowed to rename '#{fullname_source}' to '#{fullname_dest}'" } when Net::SFTP::Constants::StatusCodes::FX_NO_SUCH_FILE log(:error) { "Remote file '#{fullname_source}' does not exist"} when Net::SFTP::Constants::StatusCodes::FX_NO_SUCH_PATH log(:error) { err } else msg = "Error when trying to move file '#{fullname_source}' to '#{fullname_dest}'" log(:error) { "#{msg}\n#{err}" } # raise err, msg, err.backtrace end false end |
#sftp_session ⇒ Object
17 18 19 20 21 22 23 24 25 26 27 28 29 30 |
# File 'lib/eco/api/common/session/sftp.rb', line 17 def sftp_session require "net/sftp" @sftp_session ||= Net::SFTP.start( host, fetch_user, ** ) rescue StandardError => err log(:error) { "Could not open SFTP session. Possible misconfiguration: #{err}" } raise end |
#upload(local_file, remote_folder:, gid: nil) ⇒ String, FalseClass
Upload a file to the specific remote_folder
119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 |
# File 'lib/eco/api/common/session/sftp.rb', line 119 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}' as '#{dest_file}' (#{res})" } dest_file rescue StandardError log(:error) { "Could not upload file '#{dest_file}' (#{res})" } raise end |