class WinRM::FS::Core::UploadOrchestrator

Orchestrates the upload of a file or directory

Public Class Methods

new(service) click to toggle source
# File lib/winrm-fs/core/upload_orchestrator.rb, line 26
def initialize(service)
  @service = service
  @logger = Logging.logger[self]
end

Public Instance Methods

upload_directory(local_path, remote_path, &block) click to toggle source
# File lib/winrm-fs/core/upload_orchestrator.rb, line 44
def upload_directory(local_path, remote_path, &block)
  with_local_zip(local_path) do |local_zip|
    temp_path = temp_file_path(local_zip.path)
    with_command_executor do |cmd_executor|
      return 0 unless out_of_date?(cmd_executor, local_zip.path, temp_path)
      do_file_upload(cmd_executor, local_zip.path, temp_path, remote_path, &block)
    end
  end
end
upload_file(local_path, remote_path, &block) click to toggle source
# File lib/winrm-fs/core/upload_orchestrator.rb, line 31
def upload_file(local_path, remote_path, &block)
  # If the src has a file extension and the destination does not
  # we can assume the caller specified the dest as a directory
  if File.extname(local_path) != '' && File.extname(remote_path) == ''
    remote_path = File.join(remote_path, File.basename(local_path))
  end
  temp_path = temp_file_path(local_path)
  with_command_executor do |cmd_executor|
    return 0 unless out_of_date?(cmd_executor, local_path, remote_path)
    do_file_upload(cmd_executor, local_path, temp_path, remote_path, &block)
  end
end

Private Instance Methods

create_temp_zip_file(local_path) click to toggle source
# File lib/winrm-fs/core/upload_orchestrator.rb, line 109
def create_temp_zip_file(local_path)
  zip = WinRM::FS::Core::TempZipFile.new(local_path, recurse_paths: true)
  zip.add(local_path)
  zip.build
  zip
end
do_file_upload(cmd_executor, local_path, temp_path, remote_path) { |bytes_copied, total_bytes, local_path, remote_path| ... } click to toggle source
# File lib/winrm-fs/core/upload_orchestrator.rb, line 56
def do_file_upload(cmd_executor, local_path, temp_path, remote_path)
  file_uploader = WinRM::FS::Core::FileUploader.new(cmd_executor)
  bytes = file_uploader.upload(local_path, temp_path) do |bytes_copied, total_bytes|
    yield bytes_copied, total_bytes, local_path, remote_path if block_given?
  end

  cmd_executor.run_powershell(
    WinRM::FS::Scripts.render('decode_file', src: temp_path, dest: remote_path))

  bytes
end
local_checksum(local_path) click to toggle source
# File lib/winrm-fs/core/upload_orchestrator.rb, line 99
def local_checksum(local_path)
  Digest::MD5.file(local_path).hexdigest
end
out_of_date?(cmd_executor, local_path, remote_path) click to toggle source
# File lib/winrm-fs/core/upload_orchestrator.rb, line 83
def out_of_date?(cmd_executor, local_path, remote_path)
  local_checksum = local_checksum(local_path)
  remote_checksum = remote_checksum(cmd_executor, remote_path)

  if remote_checksum == local_checksum
    @logger.debug("#{remote_path} is up to date")
    return false
  end
  true
end
remote_checksum(cmd_executor, remote_path) click to toggle source
# File lib/winrm-fs/core/upload_orchestrator.rb, line 94
def remote_checksum(cmd_executor, remote_path)
  script = WinRM::FS::Scripts.render('checksum', path: remote_path)
  cmd_executor.run_powershell(script).chomp
end
temp_file_path(local_path) click to toggle source
# File lib/winrm-fs/core/upload_orchestrator.rb, line 103
def temp_file_path(local_path)
  ext = '.tmp'
  ext = '.zip' if File.extname(local_path) == '.zip'
  "$env:TEMP/winrm-upload/#{local_checksum(local_path)}#{ext}"
end
with_command_executor() { |cmd_executor| ... } click to toggle source
# File lib/winrm-fs/core/upload_orchestrator.rb, line 68
def with_command_executor
  cmd_executor = WinRM::FS::Core::CommandExecutor.new(@service)
  cmd_executor.open
  yield cmd_executor
ensure
  cmd_executor.close
end
with_local_zip(local_path) { |local_zip| ... } click to toggle source
# File lib/winrm-fs/core/upload_orchestrator.rb, line 76
def with_local_zip(local_path)
  local_zip = create_temp_zip_file(local_path)
  yield local_zip
ensure
  local_zip.delete if local_zip
end