Class: Bdsync::Sftp

Inherits:
Core
  • Object
show all
Defined in:
lib/bdsync/sftp.rb

Instance Attribute Summary

Attributes inherited from Core

#data_path

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Core

#do_first_time_sync_from_local, #do_first_time_sync_from_remote, #do_sync_from_local, #do_sync_from_remote, #handle_local_conflict, #handle_local_entry, #handle_remote_conflict, #handle_remote_entry, #is_same_contents, #load_data, #local_ensure_dir, #local_ensure_parent, #local_mkdir, #local_remove_directory, #local_remove_file, #local_rename, #save_data, #synchronize, #traverse_local_path, #traverse_remote_path, #update_directory_data, #update_file_data

Constructor Details

#initialize(params) ⇒ Sftp

Returns a new instance of Sftp.



7
8
9
10
11
12
13
# File 'lib/bdsync/sftp.rb', line 7

def initialize params
    @site   = params["site"]
    @user   = params["user"]
    @sftp   = Net::SFTP.start(@site, @user)

    super params, "sftp"
end

Class Method Details

.optionsObject



15
16
17
# File 'lib/bdsync/sftp.rb', line 15

def self.options
    Core.options + ["site:", "user:"]
end

Instance Method Details

#create_remote_file(remote_path, content) ⇒ Object

for test



119
120
121
122
123
124
125
126
127
128
129
130
131
# File 'lib/bdsync/sftp.rb', line 119

def create_remote_file remote_path, content
    tmpfile = Tempfile.new 'sftp.rb-create_remote_file-'

    begin
        tmpfile.write content
        tmpfile.flush

        @sftp.upload! tmpfile.path, remote_path
    ensure
        tmpfile.close
        tmpfile.unlink  # deletes the temp file
    end
end

#download_file(local_path, remote_path) ⇒ Object



36
37
38
39
40
41
# File 'lib/bdsync/sftp.rb', line 36

def download_file local_path, remote_path
    local_ensure_parent local_path

    puts "#{Utils.caller_info 1} sftp.download! #{remote_path}, #{local_path}".white
    @sftp.download! remote_path, local_path
end

#get_remote_file_md5(remote_path) ⇒ Object



103
104
105
106
107
108
109
110
111
112
113
114
115
116
# File 'lib/bdsync/sftp.rb', line 103

def get_remote_file_md5 remote_path
    puts "#{Utils.caller_info 1} get_remote_file_md5 #{remote_path}".white

    tmpfile = Tempfile.new 'sftp.rb-get_remote_file_md5-'

    begin
        tmpfile.close

        @sftp.download! remote_path, tmpfile.path
        Utils.file_md5 tmpfile.path
    ensure
        tmpfile.unlink  # deletes the temp file
    end
end

#remote_dir_foreach(remote_path) ⇒ Object



19
20
21
22
23
# File 'lib/bdsync/sftp.rb', line 19

def remote_dir_foreach remote_path
    @sftp.dir.foreach(remote_path) { |entry|
        yield entry
    }
end

#remote_ensure_dir(path) ⇒ Object



90
91
92
93
94
95
96
97
# File 'lib/bdsync/sftp.rb', line 90

def remote_ensure_dir path
    begin
        @sftp.lstat! path
    rescue Net::SFTP::StatusException
        remote_ensure_parent path
        @sftp.mkdir! path
    end
end

#remote_ensure_parent(path) ⇒ Object



99
100
101
# File 'lib/bdsync/sftp.rb', line 99

def remote_ensure_parent path
    remote_ensure_dir File.dirname path
end

#remote_get_object(remote_path) ⇒ Object

Return OpenStruct.new(

directory?:
mtime:

)



30
31
32
33
34
# File 'lib/bdsync/sftp.rb', line 30

def remote_get_object remote_path
    @sftp.lstat! remote_path
rescue Net::SFTP::StatusException
    nil
end

#remote_mkdir(remote_path) ⇒ Object



52
53
54
55
56
# File 'lib/bdsync/sftp.rb', line 52

def remote_mkdir remote_path
    puts "#{Utils.caller_info 1} sftp.mkdir! #{remote_path}".white
    @sftp.mkdir! remote_path
    remote_get_object remote_path
end

#remote_remove_dir(remote_path) ⇒ Object



63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/bdsync/sftp.rb', line 63

def remote_remove_dir remote_path
    puts "#{Utils.caller_info 1} remote_remove_dir #{remote_path}".white

    remote = remote_get_object remote_path
    return if !remote || !remote.directory?

    remote_dir_foreach(remote_path) { |entry|
        next if [".", ".."].include? entry.name

        path = "#{remote_path}/#{entry.name}"
        remote = entry.attributes

        if remote.directory?
            remote_remove_dir path
        else
            remote_remove_file path
        end
    }

    @sftp.rmdir! remote_path
end

#remote_remove_file(remote_path) ⇒ Object



58
59
60
61
# File 'lib/bdsync/sftp.rb', line 58

def remote_remove_file remote_path
    puts "#{Utils.caller_info 1} sftp.remove! #{remote_path}".white
    @sftp.remove! remote_path
end

#remote_rename(remote_path, new_remote_path) ⇒ Object



85
86
87
88
# File 'lib/bdsync/sftp.rb', line 85

def remote_rename remote_path, new_remote_path
    puts "#{Utils.caller_info 1} sftp.rename! #{remote_path} #{new_remote_path}".yellow
    @sftp.rename! remote_path, new_remote_path
end

#upload_file(local_path, remote_path) ⇒ Object



43
44
45
46
47
48
49
50
# File 'lib/bdsync/sftp.rb', line 43

def upload_file local_path, remote_path
    remote_ensure_parent remote_path

    puts "#{Utils.caller_info 1} sftp.upload! #{local_path}, #{remote_path}".white
    @sftp.upload! local_path, remote_path

    remote_get_object remote_path
end