Class: Ridley::HostConnector::WinRM::CommandUploader

Inherits:
Object
  • Object
show all
Defined in:
lib/ridley/host_connector/winrm/command_uploader.rb

Overview

Examples:

command_uploader = CommandUploader.new(long_command, winrm)
command_uploader.upload
command_uploader.command

This class is used by WinRM Workers when the worker is told to execute a command that
might be too long for WinRM to handle.

After an instance of this class is created, the upload method will upload the long command
to the node being worked on in chunks. Once on the node, some Powershell code will decode
the long_command into a batch file. The command method will return a String representing the
command the worker will use to execute the uploaded batch script.

Constant Summary collapse

CHUNK_LIMIT =
1024

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(winrm) ⇒ CommandUploader

Returns a new instance of CommandUploader.

Parameters:

  • winrm (WinRM::WinRMWebService)


27
28
29
30
31
# File 'lib/ridley/host_connector/winrm/command_uploader.rb', line 27

def initialize(winrm)
  @winrm             = winrm
  @base64_file_name  = get_file_path("winrm-upload-base64-#{unique_string}")
  @command_file_name = get_file_path("winrm-upload-#{unique_string}.bat")
end

Instance Attribute Details

#base64_file_nameString (readonly)

Returns:

  • (String)


22
23
24
# File 'lib/ridley/host_connector/winrm/command_uploader.rb', line 22

def base64_file_name
  @base64_file_name
end

#command_file_nameString (readonly)

Returns:

  • (String)


24
25
26
# File 'lib/ridley/host_connector/winrm/command_uploader.rb', line 24

def command_file_name
  @command_file_name
end

#winrmWinRM::WinRMWebService (readonly)

Returns:

  • (WinRM::WinRMWebService)


20
21
22
# File 'lib/ridley/host_connector/winrm/command_uploader.rb', line 20

def winrm
  @winrm
end

Instance Method Details

#cleanupObject

Runs a delete command on the files generated by #base64_file_name and #command_file_name



50
51
52
53
# File 'lib/ridley/host_connector/winrm/command_uploader.rb', line 50

def cleanup
  winrm.run_cmd( "del #{base64_file_name} /F /Q" )
  winrm.run_cmd( "del #{command_file_name} /F /Q" )
end

#commandString

Returns the command to execute the uploaded file.

Returns:

  • (String)

    the command to execute the uploaded file



44
45
46
# File 'lib/ridley/host_connector/winrm/command_uploader.rb', line 44

def command
  "cmd.exe /C #{command_file_name}"
end

#upload(command_string) ⇒ Object

Uploads the command encoded as base64 to a file on the host and then uses Powershell to transform the base64 file into the command that was originally passed through.

Parameters:

  • command_string (String)


38
39
40
41
# File 'lib/ridley/host_connector/winrm/command_uploader.rb', line 38

def upload(command_string)
  upload_command(command_string)
  convert_command
end