Class: ChefMetal::Transport::WinRM

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

Defined Under Namespace

Classes: WinRMResult

Constant Summary

Constants inherited from ChefMetal::Transport

DEFAULT_TIMEOUT

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, global_config) ⇒ WinRM

Returns a new instance of WinRM.



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

def initialize(endpoint, type, options, global_config)
  @endpoint = endpoint
  @type = type
  @options = options
  @config = global_config
end

Instance Attribute Details

#configObject (readonly)

Returns the value of attribute config.



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

def config
  @config
end

#endpointObject (readonly)

Returns the value of attribute endpoint.



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

def endpoint
  @endpoint
end

#optionsObject (readonly)

Returns the value of attribute options.



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

def options
  @options
end

#typeObject (readonly)

Returns the value of attribute type.



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

def type
  @type
end

Instance Method Details

#disconnectObject



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

def disconnect
  #
end

#escape(string) ⇒ Object



64
65
66
# File 'lib/chef_metal/transport/winrm.rb', line 64

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

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



20
21
22
23
24
25
26
27
# File 'lib/chef_metal/transport/winrm.rb', line 20

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

#read_file(path) ⇒ Object



29
30
31
32
33
34
35
36
# File 'lib/chef_metal/transport/winrm.rb', line 29

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



38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/chef_metal/transport/winrm.rb', line 38

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