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

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Typingpool::Project::Remote

#file_to_url, #remove_urls, #url_basename

Constructor Details

#initialize(user, host, url, path = nil) ⇒ SFTP

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).



43
44
45
46
47
48
# 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

Instance Attribute Details

#hostObject (readonly)

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



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

def host
  @host
end

#pathObject (readonly)

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



30
31
32
# File 'lib/typingpool/project/remote/sftp.rb', line 30

def path
  @path
end

#urlObject (readonly)

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



38
39
40
# File 'lib/typingpool/project/remote/sftp.rb', line 38

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.



34
35
36
# File 'lib/typingpool/project/remote/sftp.rb', line 34

def user
  @user
end

Class Method Details

.from_config(config_sftp) ⇒ Object

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.



16
17
18
19
20
21
22
# 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

Instance Method Details

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

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



51
52
53
54
55
56
57
58
59
60
61
62
63
64
# 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

#remove(files) ⇒ Object

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



67
68
69
70
71
72
73
74
75
76
77
# 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