Class: Chef::Knife::WinrmSession

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

Instance Attribute Summary collapse

Class Method 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)
  Chef::Application.new.configure_proxy_environment_variables
  @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]}")
  
  WinrmSession.load_windows_specific_gems if options[:transport] == :sspinegotiate
  @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

Class Method Details

.load_windows_specific_gemsObject



76
77
78
79
80
# File 'lib/chef/knife/winrm_session.rb', line 76

def self.load_windows_specific_gems
  #checking for windows in case testing on linux
  require 'winrm-s'
  Chef::Log.debug("Applied 'winrm-s' monkey patch and trying WinRM communication with 'sspinegotiate'")
end

Instance Method Details

#get_output(remote_id, command_id) ⇒ Object



60
61
62
63
64
65
# File 'lib/chef/knife/winrm_session.rb', line 60

def get_output(remote_id, command_id)
  @winrm_session.get_command_output(remote_id, command_id) do |out,error|
    print_data(@host, out) if out
    print_data(@host, error, :red) if error
  end
end


67
68
69
70
71
72
73
74
# File 'lib/chef/knife/winrm_session.rb', line 67

def print_data(host, data, color = :cyan)
  if data =~ /\n/
    data.split(/\n/).each { |d| print_data(host, d, color) }
  elsif !data.nil?
    print Chef::Knife::Winrm.ui.color(host, color)
    puts " #{data}"
  end
end

#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