Module: ZTK::SSH::Download

Included in:
ZTK::SSH
Defined in:
lib/ztk/ssh/download.rb

Overview

SSH Download Functionality

Instance Method Summary (collapse)

Instance Method Details

- (Boolean) download(remote, local, options = {})

Downloads a remote file to the local host.

Examples:

Download a file:

$logger = ZTK::Logger.new(STDOUT)
ssh = ZTK::SSH.new
ssh.config do |config|
  config.user = ENV["USER"]
  config.host_name = "127.0.0.1"
end
local = File.expand_path(File.join(ZTK::Locator.root, "tmp", "id_rsa.pub"))
remote = File.expand_path(File.join(ENV['HOME'], ".ssh", "id_rsa.pub"))
ssh.download(remote, local)

Parameters:

  • remote (String)

    The remote file/path you with to download from.

  • local (String)

    The local file/path you wish to download to.

  • options (Hash) (defaults to: {})

    An optional hash of options.

Options Hash (options):

  • :recursive (Boolean) — default: false

    Whether or not to recursively download files and directories. By default this looks at the local target and if it is a directory this is set to true otherwise it will default to false.

  • :use_scp (Boolean) — default: false

    If set to true, the file will be transfered using SCP instead of SFTP. The default behaviour is to use SFTP. WARNING: Recursive downloads are handled in differing manners between SCP and SFTP!

Returns:

  • (Boolean)

    True if successful.



32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/ztk/ssh/download.rb', line 32

def download(remote, local, options={})
  options = {
    :recursive => ::File.directory?(local),
    :use_scp => false
  }.merge(options)
  options = OpenStruct.new(config.send(:table).merge(options))

  options.ui.logger.debug { "config=#{config.send(:table).inspect}" }
  options.ui.logger.debug { "options=#{options.send(:table).inspect}" }
  options.ui.logger.info { "download(#{remote.inspect}, #{local.inspect})" }

  ZTK::RescueRetry.try(:ui => config.ui, :tries => ZTK::SSH::RESCUE_RETRY_ATTEMPTS, :on_retry => method(:on_retry)) do
    if (options.use_scp == true)
      scp_download(remote, local, options)
    else
      sftp_download(remote, local, options)
    end
  end

  true
end