Class: WinRM::Transport::FileTransporter

Inherits:
Object
  • Object
show all
Includes:
Logging
Defined in:
lib/winrm/transport/file_transporter.rb

Overview

Object which can upload one or more files or directories to a remote host over WinRM using PowerShell scripts and CMD commands. Note that this form of file transfer is not ideal and extremely costly on both the local and remote sides. Great pains are made to minimize round trips to the remote host and to minimize the number of PowerShell sessions being invoked which can be 2 orders of magnitude more expensive than vanilla CMD commands.

This object is supported by either a ‘WinRM::WinRMWebService` or `CommandExecutor` instance as it depends on the `#run_cmd` and `#run_powershell_script` API contracts.

An optional logger can be supplied, assuming it can respond to the ‘#debug` and `#debug?` messages.

Author:

Instance Method Summary collapse

Methods included from Logging

#debug, #log_subject

Constructor Details

#initialize(service, logger = nil, opts = {}) ⇒ FileTransporter

Creates a FileTransporter given a service object and optional logger. The service object may be a ‘WinRM::WinRMWebService` or `CommandExecutor` instance.

Parameters:

  • service (WinRM::WinRMWebService, CommandExecutor)

    a winrm web service object

  • logger (#debug, #debug?) (defaults to: nil)

    an optional logger/ui object that responds to ‘#debug` and `#debug?` (default: `nil`)



66
67
68
69
70
# File 'lib/winrm/transport/file_transporter.rb', line 66

def initialize(service, logger = nil, opts = {})
  @service  = service
  @logger   = logger
  @id_generator = opts.fetch(:id_generator) { -> { SecureRandom.uuid } }
end

Instance Method Details

#upload(locals, remote) ⇒ Hash

Uploads a collection of files and/or directories to the remote host.

**TODO Notes:**

  • options could specify zip mode, zip options, etc.

  • maybe option to set tmpfile base dir to override $env:PATH?

  • progress yields block like net-scp progress

  • final API: def upload(locals, remote, _options = {}, &_progress)

Parameters:

  • locals (Array<String>, String)

    one or more local file or directory paths

  • remote (String)

    the base destination path on the remote host

Returns:

  • (Hash)

    report hash, keyed by the local MD5 digest



84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
# File 'lib/winrm/transport/file_transporter.rb', line 84

def upload(locals, remote)
  files = nil

  elapsed = Benchmark.measure do
    files = make_files_hash(Array(locals), remote)

    report = check_files(files)
    merge_with_report!(files, report)

    report = stream_upload_files(files)
    merge_with_report!(files, report)

    report = decode_files(files)
    merge_with_report!(files, report)

    cleanup(files)
  end

  debug {
    "Uploaded #{files.keys.size} items " \
    "in #{duration(elapsed.real)}"
  }

  files
end