Class: Dandelion::Backend::SFTP

Inherits:
Base
  • Object
show all
Defined in:
lib/dandelion/backend/sftp.rb

Instance Method Summary collapse

Methods inherited from Base

create, gem_list, gems, scheme

Constructor Details

#initialize(config) ⇒ SFTP

Returns a new instance of SFTP.



10
11
12
13
14
15
16
17
18
# File 'lib/dandelion/backend/sftp.rb', line 10

def initialize(config)
  require 'net/sftp'
  @config = { 'preserve_permissions' => true }.merge(config)
  options = {
    :password => @config['password'],
    :port => @config['port'] || Net::SSH::Transport::Session::DEFAULT_PORT,
  }
  @sftp = Net::SFTP.start(@config['host'], @config['username'], options)
end

Instance Method Details

#delete(file) ⇒ Object



47
48
49
50
51
52
53
54
# File 'lib/dandelion/backend/sftp.rb', line 47

def delete(file)
  begin
    @sftp.remove!(path(file))
    cleanup(File.dirname(path(file)))
  rescue Net::SFTP::StatusException => e
    raise unless e.code == 2
  end
end

#read(file) ⇒ Object



20
21
22
23
24
25
26
27
28
29
# File 'lib/dandelion/backend/sftp.rb', line 20

def read(file)
  begin
    @sftp.file.open(path(file), 'r') do |f|
      f.gets
    end
  rescue Net::SFTP::StatusException => e
    raise unless e.code == 2
    raise MissingFileError
  end
end

#to_sObject



56
57
58
# File 'lib/dandelion/backend/sftp.rb', line 56

def to_s
  "sftp://#{@config['username']}@#{@config['host']}/#{@config['path']}"
end

#write(file, data) ⇒ Object



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/dandelion/backend/sftp.rb', line 31

def write(file, data)
  temp(file, data) do |temp|
    begin
      @sftp.upload!(temp, path(file))
    rescue Net::SFTP::StatusException => e
      raise unless e.code == 2
      mkdir_p(File.dirname(path(file)))
      @sftp.upload!(temp, path(file))
    end
  end
  if @config['preserve_permissions']
    mode = get_mode(file)
    @sftp.setstat!(path(file), :permissions => mode) if mode
  end
end