Class: Dabcup::Storage::Driver::SFTP

Inherits:
Base
  • Object
show all
Defined in:
lib/dabcup/storage/driver/sftp.rb

Instance Attribute Summary

Attributes inherited from Base

#uri

Instance Method Summary collapse

Methods inherited from Base

#initialize

Constructor Details

This class inherits a constructor from Dabcup::Storage::Driver::Base

Instance Method Details

#delete(file_name) ⇒ Object



35
36
37
38
# File 'lib/dabcup/storage/driver/sftp.rb', line 35

def delete(file_name)
  file_path = File.join(uri.path, file_name)
  sftp.remove!(file_path)
end

#disconnectObject



40
41
42
# File 'lib/dabcup/storage/driver/sftp.rb', line 40

def disconnect
  @sftp.close(nil) if @sftp
end

#get(remote_name, local_path) ⇒ Object



16
17
18
19
# File 'lib/dabcup/storage/driver/sftp.rb', line 16

def get(remote_name, local_path)
  remote_path = File.join(uri.path, remote_name)
  sftp.download!(remote_path, local_path)
end

#listObject



21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/dabcup/storage/driver/sftp.rb', line 21

def list
  dumps = []
  handle = sftp.opendir!(uri.path)
  while 1
    request = sftp.readdir(handle).wait
    break if request.response.eof?
    raise Dabcup::Error.new("Failed to list files from #{@login}@#{@host}:#{uri.path}") unless request.response.ok?
    request.response.data[:names].each do |file|
      dumps << Dump.new(:name => file.name, :size => file.attributes.size)
    end
  end
  dumps
end

#local?Boolean

Returns:

  • (Boolean)


44
45
46
# File 'lib/dabcup/storage/driver/sftp.rb', line 44

def local?
  false
end

#mkdirsObject

Create directories if necessary



49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/dabcup/storage/driver/sftp.rb', line 49

def mkdirs
  dirs = []
  path = uri.path
  first_exception = nil
  # TODO: find an exists? method
  begin
    sftp.dir.entries(path)
  rescue Net::SFTP::StatusException => ex
    dirs << path
    path = File.dirname(path)
    first_exception ||= ex
    if path == '.'
      raise first_exception
    else
      retry
    end
  end
  dirs.reverse.each { |dir| sftp.mkdir!(dir) }
end

#protocolObject



7
8
9
# File 'lib/dabcup/storage/driver/sftp.rb', line 7

def protocol
  'sftp'
end

#put(local_path, remote_name) ⇒ Object



11
12
13
14
# File 'lib/dabcup/storage/driver/sftp.rb', line 11

def put(local_path, remote_name)
  remote_path = File.join(uri.path, remote_name)
  sftp.upload!(local_path, remote_path)
end

#sftpObject



69
70
71
72
73
74
75
76
# File 'lib/dabcup/storage/driver/sftp.rb', line 69

def sftp
  unless @sftp
    @sftp = Net::SFTP.start(uri.host, uri.user, :password => uri.password)
    @sftp.connect
    mkdirs
  end
  @sftp
end