Module: ZTK::SSH::Upload

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

Overview

SSH Upload Functionality

Instance Method Summary (collapse)

Instance Method Details

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

Uploads a local file to a remote host.

Examples:

Upload 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(ENV['HOME'], ".ssh", "id_rsa.pub"))
remote = File.expand_path(File.join(ZTK::Locator.root, "tmp", "id_rsa.pub"))
ssh.upload(local, remote)

Parameters:

  • local (String)

    The local file/path you wish to upload from.

  • remote (String)

    The remote file/path you with to upload 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.

  • :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.



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

def upload(local, remote, options={})
  options = {
    :recursive => false,
    :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}" }
  config.ui.logger.info { "upload(#{local.inspect}, #{remote.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_upload(local, remote, options)
    else
      sftp_upload(local, remote, options)
    end
  end

  true
end