Class: Chef::Knife::WinrmSession

Inherits:
Object
  • Object
show all
Defined in:
lib/chef/knife/helpers/winrm_session.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options) ⇒ WinrmSession

Returns a new instance of WinrmSession.



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/chef/knife/helpers/winrm_session.rb', line 29

def initialize(options)
  configure_proxy

  @host = options[:host]
  @port = options[:port]
  @user = options[:user]
  @shell_args = [ options[:shell] ]
  @shell_args << { codepage: options[:codepage] } if options[:shell] == :cmd
  url = "#{options[:host]}:#{options[:port]}/wsman"
  scheme = options[:transport] == :ssl ? "https" : "http"
  @endpoint = "#{scheme}://#{url}"

  opts = {}
  opts = {
    user: @user,
    password: options[:password],
    basic_auth_only: options[:basic_auth_only],
    disable_sspi: options[:disable_sspi],
    no_ssl_peer_verification: options[:no_ssl_peer_verification],
    ssl_peer_fingerprint: options[:ssl_peer_fingerprint],
    endpoint: endpoint,
    transport: options[:transport],
  }
  options[:transport] == :kerberos ? opts.merge!({ service: options[:service], realm: options[:realm] }) : opts.merge!({ ca_trust_path: options[:ca_trust_path] })
  opts[:operation_timeout] = options[:operation_timeout] if options[:operation_timeout]
  Chef::Log.debug("WinRM::WinRMWebService options: #{opts}")
  Chef::Log.debug("Endpoint: #{endpoint}")
  Chef::Log.debug("Transport: #{options[:transport]}")

  @winrm_session = WinRM::Connection.new(opts)
  @winrm_session.logger = Chef::Log

  transport = @winrm_session.send(:transport)
  http_client = transport.instance_variable_get(:@httpcli)
  Chef::HTTP::DefaultSSLPolicy.new(http_client.ssl_config).set_custom_certs
end

Instance Attribute Details

#endpointObject (readonly)

Returns the value of attribute endpoint.



27
28
29
# File 'lib/chef/knife/helpers/winrm_session.rb', line 27

def endpoint
  @endpoint
end

#errorObject (readonly)

Returns the value of attribute error.



27
28
29
# File 'lib/chef/knife/helpers/winrm_session.rb', line 27

def error
  @error
end

#exit_codeObject (readonly)

Returns the value of attribute exit_code.



27
28
29
# File 'lib/chef/knife/helpers/winrm_session.rb', line 27

def exit_code
  @exit_code
end

#hostObject (readonly)

Returns the value of attribute host.



27
28
29
# File 'lib/chef/knife/helpers/winrm_session.rb', line 27

def host
  @host
end

#outputObject (readonly)

Returns the value of attribute output.



27
28
29
# File 'lib/chef/knife/helpers/winrm_session.rb', line 27

def output
  @output
end

#portObject (readonly)

Returns the value of attribute port.



27
28
29
# File 'lib/chef/knife/helpers/winrm_session.rb', line 27

def port
  @port
end

Instance Method Details

#relay_command(command) ⇒ Object



66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/chef/knife/helpers/winrm_session.rb', line 66

def relay_command(command)
  session_result = WinRM::Output.new
  @winrm_session.shell(*@shell_args) do |shell|
    shell.username = @user.split("\\").last if shell.respond_to?(:username)
    session_result = shell.run(command) do |stdout, stderr|
      print_data(@host, stdout) if stdout
      print_data(@host, stderr, :red) if stderr
    end
  end
  @exit_code = session_result.exitcode
  session_result
rescue WinRM::WinRMHTTPTransportError, WinRM::WinRMAuthorizationError => e
  @exit_code = 401
  raise e
end