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.



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

def initialize params
    @site   = params["site"]
    @user   = params["user"]

    super params, "sftp"
end

Class Method Details

.optionsObject



13
14
15
# File 'lib/bdsync/sftp.rb', line 13

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

Instance Method Details

#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



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

def get_remote_file_md5 remote_path
    puts "#{Utils.caller_info 1} sftp.session.exec! md5sum #{remote_path}".white
    res = @sftp.session.exec! "md5sum #{remote_path}"
    res.split[0]
end

#remote_dir_foreach(remote_path) ⇒ Object



24
25
26
27
28
# File 'lib/bdsync/sftp.rb', line 24

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

#remote_ensure_dir(path) ⇒ Object



73
74
75
76
77
78
79
80
# File 'lib/bdsync/sftp.rb', line 73

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



82
83
84
# File 'lib/bdsync/sftp.rb', line 82

def remote_ensure_parent path
    remote_ensure_dir File.dirname path
end

#remote_get_object(remote_path) ⇒ Object



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
# File 'lib/bdsync/sftp.rb', line 63

def remote_remove_dir remote_path
    puts "#{Utils.caller_info 1} sftp.session.exec! rm -rf #{remote_path}".white
    @sftp.session.exec! "rm -rf #{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



68
69
70
71
# File 'lib/bdsync/sftp.rb', line 68

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

#start_session(&block) ⇒ Object



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

def start_session &block
    Net::SFTP.start(@site, @user) { |sftp|
        @sftp = sftp
        yield
    }
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