Class: Typingpool::Project::Remote::SFTP

Inherits:
Typingpool::Project::Remote show all
Defined in:
lib/typingpool/project/remote/sftp.rb

Overview

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.

Instance Attribute Summary collapse

Attributes inherited from Typingpool::Project::Remote

#name

Instance Method Summary collapse

Methods inherited from Typingpool::Project::Remote

#file_to_url, from_config, #remove_urls, #url_basename

Constructor Details

#initialize(name, sftp_config) ⇒ SFTP

Constructor. Takes the project name and a Config#sftp.



29
30
31
32
33
34
35
36
# File 'lib/typingpool/project/remote/sftp.rb', line 29

def initialize(name, sftp_config)
  @name = name
  @config = sftp_config   
  @user = @config.user or raise Error::File::Remote::SFTP, "No SFTP user specified in config"
  @host = @config.host or raise Error::File::Remote::SFTP, "No SFTP host specified in config"
  @url = @config.url or raise Error::File::Remote::SFTP, "No SFTP url specified in config"
  @path = @config.path || ''
end

Instance Attribute Details

#hostObject (readonly)

Returns the remote host (server) name. This is set from Config#sftp#host.



14
15
16
# File 'lib/typingpool/project/remote/sftp.rb', line 14

def host
  @host
end

#pathObject (readonly)

Returns the remote path (directory). This is set from Config#sftp#path.



18
19
20
# File 'lib/typingpool/project/remote/sftp.rb', line 18

def path
  @path
end

#urlObject (readonly)

Returns the base URL, which is prepended to the remote files. This is set from Config#sftp#url.



26
27
28
# File 'lib/typingpool/project/remote/sftp.rb', line 26

def url
  @url
end

#userObject (readonly)

Returns the name of the user used to log in to the SFTP server. This is et from Config#sftp#user.



22
23
24
# File 'lib/typingpool/project/remote/sftp.rb', line 22

def user
  @user
end

Instance Method Details

#put(io_streams, as = io_streams.map{|file| File.basename(file)}) ⇒ Object

See docs for Project::Remote::S3#put.



39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/typingpool/project/remote/sftp.rb', line 39

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

#remove(files) ⇒ Object

See docs for Project::Remote::S3#remove.



55
56
57
58
59
60
61
62
63
64
65
# File 'lib/typingpool/project/remote/sftp.rb', line 55

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