Class: Cloudsync::Backend::Sftp

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

Constant Summary

Constants inherited from Base

Base::BUCKET_LIMIT, Base::CONTAINER_LIMIT, Base::OBJECT_LIMIT

Instance Attribute Summary collapse

Attributes inherited from Base

#name, #store, #sync_manager, #upload_prefix

Instance Method Summary collapse

Methods inherited from Base

#copy, #needs_update?, #to_s

Constructor Details

#initialize(options = {}) ⇒ Sftp

Returns a new instance of Sftp.



9
10
11
12
13
14
15
# File 'lib/cloudsync/backend/sftp.rb', line 9

def initialize(options = {})
  @host                 = options[:host]
  @username             = options[:username]
  @password             = options[:password]
  
  super
end

Instance Attribute Details

#hostObject

Returns the value of attribute host.



7
8
9
# File 'lib/cloudsync/backend/sftp.rb', line 7

def host
  @host
end

#passwordObject

Returns the value of attribute password.



7
8
9
# File 'lib/cloudsync/backend/sftp.rb', line 7

def password
  @password
end

#usernameObject

Returns the value of attribute username.



7
8
9
# File 'lib/cloudsync/backend/sftp.rb', line 7

def username
  @username
end

Instance Method Details

#delete(file, delete_bucket_if_empty = true) ⇒ Object

delete



55
56
57
58
59
60
61
62
63
64
# File 'lib/cloudsync/backend/sftp.rb', line 55

def delete(file, delete_bucket_if_empty=true)
  $LOGGER.info("Deleting #{file}")
  return if dry_run?
  
  Net::SSH.start(@host, @username, :password => @password) do |ssh|
    ssh.sftp.connect do |sftp|
      sftp.remove!(file.download_path)
    end
  end
end

#download(file) ⇒ Object

download



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/cloudsync/backend/sftp.rb', line 18

def download(file)
  $LOGGER.info("Downloading #{file} from #{file.download_path}")
  tempfile = file.tempfile
  
  if !dry_run?
    Net::SSH.start(@host, @username, :password => @password) do |ssh|
      ssh.sftp.connect do |sftp|
        begin
          sftp.download!(file.download_path, tempfile)
        rescue RuntimeError => e
          if e.message =~ /permission denied/
            tempfile.close
            return tempfile
          else
            raise
          end
        end
      end
    end
  end
  tempfile.close
  tempfile
end

#files_to_sync(upload_prefix = "") ⇒ Object



66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
# File 'lib/cloudsync/backend/sftp.rb', line 66

def files_to_sync(upload_prefix="")
  $LOGGER.info("Getting files to sync [#{self}]")
  files = []
  Net::SSH.start(@host, @username, :password => @password) do |ssh|
    ssh.sftp.connect do |sftp|
      filepaths = sftp.dir.glob(@download_prefix, "**/**").collect {|entry| entry.name}
    
      filepaths.each do |filepath|
        attrs = sftp.stat!(local_filepath_from_filepath(filepath))
        next unless attrs.file?

        e_tag = ssh.exec!(md5sum_cmd(filepath)).split(" ").first
        file = Cloudsync::File.new \
          :path            => filepath,
          :upload_prefix   => @upload_prefix,
          :download_prefix => @download_prefix,
          :size            => attrs.size,
          :last_modified   => attrs.mtime,
          :e_tag           => e_tag,
          :backend         => self.to_s,
          :backend_type    => Cloudsync::Backend::Sftp
        
        if block_given?
          yield file
        else
          files << file
        end
      end
    end
  end
  files
end

#get_file_from_store(file) ⇒ Object

get_file_from_store



100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
# File 'lib/cloudsync/backend/sftp.rb', line 100

def get_file_from_store(file)
  $LOGGER.debug("Looking for local filepath: #{local_filepath_from_filepath(file.full_download_path)}")

  sftp_file = nil
  Net::SSH.start(@host, @username, :password => @password) do |ssh|
    ssh.sftp.connect do |sftp|
      begin
        attrs = sftp.stat!(local_filepath_from_filepath(file.full_download_path))
      rescue Net::SFTP::StatusException => e
        break if e.message =~ /no such file/
        raise
      end
      break unless attrs.file?
      
      sftp_file = Cloudsync::File.new \
        :path            => file.download_path,
        :upload_prefix   => @upload_prefix,
        :download_prefix => @download_prefix,
        :size            => attrs.size,
        :last_modified   => attrs.mtime,
        :e_tag           => ssh.exec!(md5sum_cmd(file.download_path)).split(" ").first,
        :backend         => self.to_s,
        :backend_type    => Cloudsync::Backend::Sftp
    end
  end
  sftp_file
end

#put(file, local_filepath) ⇒ Object

put



43
44
45
46
47
48
49
50
51
52
# File 'lib/cloudsync/backend/sftp.rb', line 43

def put(file, local_filepath)
  $LOGGER.info("Putting #{file} to #{self} (#{file.upload_path})")
  return if dry_run?
  
  Net::SSH.start(@host, @username, :password => @password) do |ssh|
    ssh.sftp.connect do |sftp|
      sftp.upload!(local_filepath, file.upload_path)
    end
  end
end