Class: WinRM::FS::Core::FileTransporter

Inherits:
Object
  • Object
show all
Defined in:
lib/winrm-fs/core/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 ‘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

Constructor Details

#initialize(executor, opts = {}) ⇒ FileTransporter

Creates a FileTransporter given a CommandExecutor object.

Parameters:

  • executor (CommandExecutor)

    a winrm CommandExecutor object



56
57
58
59
60
# File 'lib/winrm-fs/core/file_transporter.rb', line 56

def initialize(executor, opts = {})
  @executor  = executor
  @logger = executor.service.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



74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
# File 'lib/winrm-fs/core/file_transporter.rb', line 74

def upload(locals, remote)
  files = nil
  report = nil

  elapsed1 = Benchmark.measure do
    files = make_files_hash(Array(locals), remote)
    report = check_files(files)
    merge_with_report!(files, report)
  end
  total_size = total_base64_transfer_size(files)

  elapsed2 = Benchmark.measure do
    report = stream_upload_files(files) do |local_path, xfered|
      yield xfered, total_size, local_path, remote if block_given?
    end
    merge_with_report!(files, report)
  end

  elapsed3 = Benchmark.measure do
    report = decode_files(files)
    merge_with_report!(files, report)
    cleanup(files)
  end

  logger.debug(
    "Uploaded #{files.keys.size} items " \
    "dirty_check: #{duration(elapsed1.real)} " \
    "stream_files: #{duration(elapsed2.real)} " \
    "decode: #{duration(elapsed3.real)} " \
  )

  [total_size, files]
end