Class: Chef::Provisioning::Transport::WinRM

Inherits:
Chef::Provisioning::Transport show all
Defined in:
lib/chef/provisioning/transport/winrm.rb

Overview

Transport to handle the WinRM connection protocol.

Defined Under Namespace

Classes: WinRMResult

Constant Summary

Constants inherited from Chef::Provisioning::Transport

DEFAULT_TIMEOUT

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from Chef::Provisioning::Transport

#download_file, #upload_file

Constructor Details

#initialize(endpoint, type, options, global_config) ⇒ WinRM

Create a new WinRM transport.

Arguments

  • endpoint: the WinRM endpoint, e.g. 145.14.51.45:5985/wsman.

  • type: the connection type, e.g. :plaintext.

  • options: options hash, including both WinRM options and transport options. For transport options, see the Transport.options definition. WinRM options include :user, :pass, :disable_sspi => true, among others.

  • global_config: an options hash that looks suspiciously similar to Chef::Config, containing at least the key :log_level.

The actual connection is made as ::WinRM::WinRMWebService.new(endpoint, type, options)


24
25
26
27
28
29
# File 'lib/chef/provisioning/transport/winrm.rb', line 24

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


34
35
36
# File 'lib/chef/provisioning/transport/winrm.rb', line 34

def config
  @config
end

#endpointObject (readonly)

Returns the value of attribute endpoint


31
32
33
# File 'lib/chef/provisioning/transport/winrm.rb', line 31

def endpoint
  @endpoint
end

#optionsObject (readonly)

Returns the value of attribute options


33
34
35
# File 'lib/chef/provisioning/transport/winrm.rb', line 33

def options
  @options
end

#typeObject (readonly)

Returns the value of attribute type


32
33
34
# File 'lib/chef/provisioning/transport/winrm.rb', line 32

def type
  @type
end

Instance Method Details

#available?Boolean


85
86
87
88
89
90
91
92
93
94
95
96
97
# File 'lib/chef/provisioning/transport/winrm.rb', line 85

def available?
  # If you can't pwd within 10 seconds, you can't pwd
  execute('pwd', :timeout => 10)
  true
rescue Timeout::Error, Errno::EHOSTUNREACH, Errno::ETIMEDOUT, Errno::ECONNREFUSED, Errno::ECONNRESET, ::WinRM::WinRMHTTPTransportError, ::WinRM::WinRMWebServiceError, ::WinRM::WinRMWSManFault
  Chef::Log.debug("unavailable: network connection failed or broke: #{$!.inspect}")
  disconnect
  false
rescue ::WinRM::WinRMAuthorizationError
  Chef::Log.debug("unavailable: winrm authentication error: #{$!.inspect} ")
  disconnect
  false
end

#disconnectObject


77
78
79
# File 'lib/chef/provisioning/transport/winrm.rb', line 77

def disconnect
  #
end

#escape(string) ⇒ Object


81
82
83
# File 'lib/chef/provisioning/transport/winrm.rb', line 81

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

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


36
37
38
39
40
41
42
43
44
# File 'lib/chef/provisioning/transport/winrm.rb', line 36

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

#make_url_available_to_remote(local_url) ⇒ Object


99
100
101
102
103
104
105
106
# File 'lib/chef/provisioning/transport/winrm.rb', line 99

def make_url_available_to_remote(local_url)
  uri = URI(local_url)
  host = Socket.getaddrinfo(uri.host, uri.scheme, nil, :STREAM)[0][3]
  if host == '127.0.0.1' || host == '::1'
    raise 'Unable to converge locally via winrm. Local converge is currently only supported with SSH. You may only converge with winrm against a chef-server.'
  end
  local_url
end

#read_file(path) ⇒ Object


46
47
48
49
50
51
52
53
# File 'lib/chef/provisioning/transport/winrm.rb', line 46

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


55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/chef/provisioning/transport/winrm.rb', line 55

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