class Typingpool::Project::Remote::SFTP
Subclass for storing remote files on an SFTP
server. Only public/private key authentication has been tested. There is not yet any provision for password-based authentication, though adding it should be trivial.
Attributes
Returns the remote host (server) name. This is set from Config#sftp#host.
Returns the remote path (directory). This is set from Config#sftp#path.
Returns the base URL, which is prepended to the remote files. This is set from Config#sftp#url.
Returns the name of the user used to log in to the SFTP
server. This is et from Config#sftp#user.
Public Class Methods
Takes a Config#sftp, extracts the needed params, and returns a Project::Remote::SFTP
instance. Raises an exception of type Error::File::Remote::SFTP
if any required params (user, host, url) are missing from the config.
# File lib/typingpool/project/remote/sftp.rb, line 16 def self.from_config(config_sftp) user = config_sftp.user or raise Error::File::Remote::SFTP, "No SFTP user specified in config" host = config_sftp.host or raise Error::File::Remote::SFTP, "No SFTP host specified in config" url = config_sftp.url or raise Error::File::Remote::SFTP, "No SFTP url specified in config" path = config_sftp.path new(user, host, url, path) end
Constructor. Takes the project name, SFTP
user, SFTP
host, URL prefix to append to file names, and an optional SFTP
path (for SFTP
uploading, not appended to URL).
# File lib/typingpool/project/remote/sftp.rb, line 43 def initialize(user, host, url, path=nil) @user = user @host = host @url = url @path = path || '' end
Public Instance Methods
See docs for Project::Remote::S3#put
.
# File lib/typingpool/project/remote/sftp.rb, line 51 def put(io_streams, as=io_streams.map{|file| File.basename(file)}) begin i = 0 batch(io_streams) do |stream, connection| dest = as[i] i += 1 yield(stream, dest) if block_given? connection.upload(stream, join_with_path(dest)) file_to_url(dest) end rescue Net::SFTP::StatusException => e raise Error::File::Remote::SFTP, "SFTP upload failed: #{e.description}" end end
See docs for Project::Remote::S3#remove
.
# File lib/typingpool/project/remote/sftp.rb, line 67 def remove(files) requests = batch(files) do |file, connection| yield(file) if block_given? connection.remove(join_with_path(file)) end failures = requests.reject{|request| request.response.ok?} if not(failures.empty?) summary = failures.map{|request| request.response.to_s}.join('; ') raise Error::File::Remote::SFTP, "SFTP removal failed: #{summary}" end end
Protected Instance Methods
# File lib/typingpool/project/remote/sftp.rb, line 92 def batch(files) results = [] connection do |connection| files.each do |file| results.push(yield(file, connection)) end end return results end
# File lib/typingpool/project/remote/sftp.rb, line 81 def connection begin Net::SFTP.start(@host, @user) do |connection| yield(connection) connection.loop end rescue Net::SSH::AuthenticationFailed raise Error::File::Remote::SFTP, "SFTP authentication failed: #{$?}" end end
# File lib/typingpool/project/remote/sftp.rb, line 102 def join_with_path(file) if @path [@path, file].join('/') else file end end