Class: Chef::Knife::WinrmSession

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options) ⇒ WinrmSession

Returns a new instance of WinrmSession.



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/chef/knife/winrm_session.rb', line 27

def initialize(options)
  configure_proxy
  
  @host = options[:host]
  @port = options[:port]
  url = "#{options[:host]}:#{options[:port]}/wsman"
  scheme = options[:transport] == :ssl ? 'https' : 'http'
  @endpoint = "#{scheme}://#{url}"
  
  opts = Hash.new
  opts = {:user => options[:user], :pass => options[:password], :basic_auth_only => options[:basic_auth_only], :disable_sspi => options[:disable_sspi], :no_ssl_peer_verification => options[:no_ssl_peer_verification]}
  options[:transport] == :kerberos ? opts.merge!({:service => options[:service], :realm => options[:realm], :keytab => options[:keytab]}) : opts.merge!({:ca_trust_path => options[:ca_trust_path]})

  Chef::Log.debug("WinRM::WinRMWebService options: #{opts}")
  Chef::Log.debug("Endpoint: #{endpoint}")
  Chef::Log.debug("Transport: #{options[:transport]}")
  
  @winrm_session = WinRM::WinRMWebService.new(@endpoint, options[:transport], opts)
  @winrm_session.set_timeout(options[:operation_timeout]) if options[:operation_timeout]
end

Instance Attribute Details

#endpointObject (readonly)

Returns the value of attribute endpoint.



25
26
27
# File 'lib/chef/knife/winrm_session.rb', line 25

def endpoint
  @endpoint
end

#errorObject (readonly)

Returns the value of attribute error.



25
26
27
# File 'lib/chef/knife/winrm_session.rb', line 25

def error
  @error
end

#exit_codeObject (readonly)

Returns the value of attribute exit_code.



25
26
27
# File 'lib/chef/knife/winrm_session.rb', line 25

def exit_code
  @exit_code
end

#hostObject (readonly)

Returns the value of attribute host.



25
26
27
# File 'lib/chef/knife/winrm_session.rb', line 25

def host
  @host
end

#outputObject (readonly)

Returns the value of attribute output.



25
26
27
# File 'lib/chef/knife/winrm_session.rb', line 25

def output
  @output
end

#portObject (readonly)

Returns the value of attribute port.



25
26
27
# File 'lib/chef/knife/winrm_session.rb', line 25

def port
  @port
end

Instance Method Details

#relay_command(command) ⇒ Object



48
49
50
51
52
53
54
55
56
57
58
# File 'lib/chef/knife/winrm_session.rb', line 48

def relay_command(command)
  remote_id = @winrm_session.open_shell
  command_id = @winrm_session.run_command(remote_id, command)
  Chef::Log.debug("#{@host}[#{remote_id}] => :run_command[#{command}]")
  session_result = get_output(remote_id, command_id)
  @winrm_session.cleanup_command(remote_id, command_id)
  Chef::Log.debug("#{@host}[#{remote_id}] => :command_cleanup[#{command}]")
  @exit_code = session_result[:exitcode]
  @winrm_session.close_shell(remote_id)
  Chef::Log.debug("#{@host}[#{remote_id}] => :shell_close")
end