Class: Moob::IdracXml

Inherits:
BaseLom show all
Defined in:
lib/moob/idracxml.rb

Instance Attribute Summary

Attributes inherited from BaseLom

#hostname, #username

Instance Method Summary collapse

Methods inherited from BaseLom

action, actions, #detect, name

Constructor Details

#initialize(hostname, options = {}) ⇒ IdracXml

Returns a new instance of IdracXml.



7
8
9
10
11
12
13
14
# File 'lib/moob/idracxml.rb', line 7

def initialize hostname, options = {}
  super hostname, options
  @username ||= 'root'
  @password ||= 'calvin'
  @arg = options[:arg]

  discover
end

Instance Method Details

#authenticateObject



44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/moob/idracxml.rb', line 44

def authenticate
  @session.handle_cookies nil
  resp = xml_request 'post', 'cgi-bin/login',
    "<?xml version='1.0'?><LOGIN><REQ><USERNAME>#{@username}</USERNAME><PASSWORD>#{@password}</PASSWORD></REQ></LOGIN>"

  raise "Auth failed with: \"#{resp[:body]}\"" unless resp[:rc] == 0
  raise "Session ID missing from response" unless resp[:parsed_xml].include?('SID')

  @sid = resp[:parsed_xml]['SID']
  @session.headers['Cookie'] = "sid=#{@sid}"
  return self
end

#discoverObject



57
58
59
60
61
62
63
64
# File 'lib/moob/idracxml.rb', line 57

def discover
  resp = xml_request 'get', 'cgi-bin/discover', {}

  raise "Unsupported iDRAC" unless resp[:parsed_xml]['ENDPOINTTYPE'] =~ /^iDRAC[7-8]?$/
  raise "Unsupported iDRAC subversion" unless resp[:parsed_xml]['ENDPOINTVER'] == '1.00'
  raise "Unsupported protocol type" unless resp[:parsed_xml]['PROTOCOLTYPE'] == 'HTTPS'
  raise "Unsupported protocol version" unless resp[:parsed_xml]['PROTOCOLVER'] == '2.0'
end

#execObject



72
73
74
75
76
77
78
# File 'lib/moob/idracxml.rb', line 72

def exec
  out = xml_request 'post', '/cgi-bin/exec',
    "<?xml version='1.0'?><EXEC><REQ><CMDINPUT>#{@arg}</CMDINPUT><MAXOUTPUTLEN>0x0fff</MAXOUTPUTLEN></REQ></EXEC>"

  puts out[:parsed_xml]['CMDOUTPUT']
  return nil
end

#logoutObject



66
67
68
69
# File 'lib/moob/idracxml.rb', line 66

def logout
  out = xml_request 'get', 'cgi-bin/logout', {}
  return self
end

#xml_request(method, uri, data) ⇒ Object



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/moob/idracxml.rb', line 16

def xml_request method, uri, data
  out = @session.send(method, uri, data)

  raise ResponseError.new out unless out.status == 200

  out_xml = Nokogiri::XML(out.body)
  raise "Cannot parse XML response for request to #{uri}" unless out_xml

  resp_xml = out_xml.xpath("//RESP")
  raise "Cannot find response XML node in response to #{uri}" unless resp_xml

  resp = {}
  resp[:body] = out.body
  resp[:parsed_xml] = {}

  resp_xml.children.each do |n|
    resp[:parsed_xml][n.name] = n.content
  end
  if resp[:parsed_xml].include?('RC')
    resp[:rc] = Integer(resp[:parsed_xml]['RC'])
    if resp[:rc] != 0
      Moob.warn "Return code from #{uri} is #{resp[:rc]}"
    end
  end

  return resp
end