Module: Dopi::Connector::Winrm

Instance Method Summary collapse

Methods included from Dopi::CommandParser::Credentials

#credentials, #validate_credentials

Instance Method Details

#basic_auth_onlyObject



102
103
104
# File 'lib/dopi/connector/winrm.rb', line 102

def basic_auth_only
  basic_auth_only_valid? ? hash[:basic_auth_only] : nil
end

#ca_trust_pathObject



94
95
96
# File 'lib/dopi/connector/winrm.rb', line 94

def ca_trust_path
  ca_trust_path_valid? ? hash[:ca_trust_path] : nil
end

#disable_sspiObject



98
99
100
# File 'lib/dopi/connector/winrm.rb', line 98

def disable_sspi
  disable_sspi_valid? ? hash[:disable_sspi] : nil
end

#endpointObject



82
83
84
# File 'lib/dopi/connector/winrm.rb', line 82

def endpoint
  "http://#{@node.address(port)}:#{port}/wsman"
end

#operation_timeoutObject



106
107
108
# File 'lib/dopi/connector/winrm.rb', line 106

def operation_timeout
  operation_timeout_valid? ? hash[:operation_timeout] : ( plugin_timeout - 5 )
end

#portObject



86
87
88
# File 'lib/dopi/connector/winrm.rb', line 86

def port
  port_valid? ? hash[:port] : 5985
end

#sslObject



90
91
92
# File 'lib/dopi/connector/winrm.rb', line 90

def ssl
  ssl_valid? ? hash[:ssl] : true
end

#supported_credential_typesObject



110
111
112
# File 'lib/dopi/connector/winrm.rb', line 110

def supported_credential_types
  [:username_password, :kerberos]
end

#validate_winrmObject



12
13
14
15
16
17
18
19
# File 'lib/dopi/connector/winrm.rb', line 12

def validate_winrm
  log_validation_method(:port_valid?)
  log_validation_method(:ssl_valid?)
  log_validation_method(:ca_trust_path_valid?)
  log_validation_method(:disable_sspi_valid?)
  log_validation_method(:basic_auth_only_valid?)
  validate_credentials
end

#winrmObject



44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/dopi/connector/winrm.rb', line 44

def winrm
  working_winrm_connection = nil
  credentials.each do |credential|
    begin
      winrm_connection = WinRM::Connection.new(
        :endpoint          => endpoint,
        :transport         => auth_method(credential),
        :realm             => credential.realm,
        :service           => credential.service,
        :keytab            => credential.keytab,
        :user              => credential.username,
        :password          => credential.password,
        :disable_sspi      => disable_sspi,
        :basic_auth_only   => basic_auth_only,
        :ca_trust_path     => ca_trust_path,
        :operation_timeout => operation_timeout,
      )
      winrm_connection.shell(:cmd) do |shell|
        output = shell.run('exit') # test connection
        if output.exitcode == 0
          working_winrm_connection = winrm_connection
        end
      end
    rescue WinRM::WinRMAuthorizationError, GSSAPI::GssApiError => e
      log(:warn, "Unable to login with credential #{credential.name} : #{e.message}")
    rescue SocketError => e
      raise CommandConnectionError,
        "A problem occurred while trying to connect to node #{@node.name} : #{e.message}"
    rescue WinRM::WinRMWMIError => e
      raise CommandExecutionError,
        "A problem occurred while trying to connect to node #{@node.name} : #{e.message}"
    end
  end
  working_winrm_connection or
    raise CommandExecutionError,
      "Unable to login with any of the given credentials: #{credentials.map{|c| c.name}.join(', ')}"
end

#winrm_command(command_string, use_shell = :cmd) ⇒ Object



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/dopi/connector/winrm.rb', line 21

def winrm_command(command_string, use_shell = :cmd)
  cmd_stdout = ""
  cmd_stderr = ""
  log(:debug, "Executing '#{command_string}' for command #{name}")
  winrm.shell(use_shell) do |shell|
    result = shell.run(command_string) do |stdout, stderr|
      unless stdout.nil? or stdout.empty?
        cmd_stdout << stdout
        log(:debug, stdout)
      end
      unless stderr.nil? or stderr.empty?
        cmd_stderr << stderr
        log(:error, stderr)
      end
    end
    return [cmd_stdout, cmd_stderr, result.exitcode]
  end
end

#winrm_powershell_command(command_string) ⇒ Object



40
41
42
# File 'lib/dopi/connector/winrm.rb', line 40

def winrm_powershell_command(command_string)
  winrm_command(command_string, :powershell)
end