Class: ChefMetal::Transport::WinRM

Inherits:
ChefMetal::Transport show all
Defined in:
lib/chef_metal/transport/winrm.rb

Defined Under Namespace

Classes: WinRMResult

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from ChefMetal::Transport

#available?, #download_file, #make_url_available_to_remote, #upload_file

Constructor Details

#initialize(endpoint, type, options = {}) ⇒ WinRM

Returns a new instance of WinRM.



7
8
9
10
11
# File 'lib/chef_metal/transport/winrm.rb', line 7

def initialize(endpoint, type, options = {})
  @endpoint = endpoint
  @type = type
  @options = options
end

Instance Attribute Details

#endpointObject (readonly)

Returns the value of attribute endpoint.



13
14
15
# File 'lib/chef_metal/transport/winrm.rb', line 13

def endpoint
  @endpoint
end

#optionsObject (readonly)

Returns the value of attribute options.



15
16
17
# File 'lib/chef_metal/transport/winrm.rb', line 15

def options
  @options
end

#typeObject (readonly)

Returns the value of attribute type.



14
15
16
# File 'lib/chef_metal/transport/winrm.rb', line 14

def type
  @type
end

Instance Method Details

#disconnectObject



55
56
57
# File 'lib/chef_metal/transport/winrm.rb', line 55

def disconnect
  #
end

#escape(string) ⇒ Object



59
60
61
# File 'lib/chef_metal/transport/winrm.rb', line 59

def escape(string)
  "'#{string.gsub("'", "''")}'"
end

#execute(command, execute_options = {}) ⇒ Object



17
18
19
20
21
22
# File 'lib/chef_metal/transport/winrm.rb', line 17

def execute(command, execute_options = {})
  output = session.run_powershell_script(command) do |stdout, stderr|
    stream_chunk(execute_options, stdout, stderr)
  end
  WinRMResult.new(command, execute_options, output)
end

#read_file(path) ⇒ Object



24
25
26
27
28
29
30
31
# File 'lib/chef_metal/transport/winrm.rb', line 24

def read_file(path)
  result = execute("[Convert]::ToBase64String((Get-Content #{escape(path)} -Encoding byte -ReadCount 0))")
  if result.exitstatus == 0
    Base64.decode64(result.stdout)
  else
    nil
  end
end

#write_file(path, content) ⇒ Object



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/chef_metal/transport/winrm.rb', line 33

def write_file(path, content)
  chunk_size = options[:chunk_size] || 1024
  # TODO if we could marshal this data directly, we wouldn't have to base64 or do this godawful slow stuff :(
  index = 0
  execute("
$ByteArray = [System.Convert]::FromBase64String(#{escape(Base64.encode64(content[index..index+chunk_size-1]))})
$file = [System.IO.File]::Open(#{escape(path)}, 'Create', 'Write')
$file.Write($ByteArray, 0, $ByteArray.Length)
$file.Close
").error!
  index += chunk_size
  while index < content.length
    execute("
$ByteArray = [System.Convert]::FromBase64String(#{escape(Base64.encode64(content[index..index+chunk_size-1]))})
$file = [System.IO.File]::Open(#{escape(path)}, 'Append', 'Write')
$file.Write($ByteArray, 0, $ByteArray.Length)
$file.Close
").error!
    index += chunk_size
  end
end