class Bdsync::Sftp

Public Class Methods

new(params) click to toggle source
Calls superclass method Bdsync::Core::new
# 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
options() click to toggle source
# File lib/bdsync/sftp.rb, line 15
def self.options
    Core.options + ["site:", "user:"]
end

Public Instance Methods

create_remote_file(remote_path, content) click to toggle source

for test

# 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) click to toggle source
# 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) click to toggle source
# 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) { |entry| ... } click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# File lib/bdsync/sftp.rb, line 99
def remote_ensure_parent path
    remote_ensure_dir File.dirname path
end
remote_get_object(remote_path) click to toggle source

Return OpenStruct.new(

directory?:
mtime:

)

# 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) click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# 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