Class: Moob::Idrac8

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

Constant Summary collapse

INFO_FIELDS =
%w[
  biosVer svcTag expSvcCode hostName
  osName osVersion sysDesc sysRev datetime initCountdown presentCountdown
  fwVersion fwUpdated LCCfwVersion
  firstBootDevice vmBootOnce
  racName hwVersionmacAddr recoveryAction
  NicEtherMac1  NicEtherMac2  NicEtherMac3  NicEtherMac4
  NiciSCSIMac1  NiciSCSIMac2  NiciSCSIMac3  NiciSCSIMac4
  NicEtherVMac1 NicEtherVMac2 NicEtherVMac3 NicEtherVMac4
  v4Enabled v4IPAddr v4Gateway v4NetMask
  v6Enabled v6Addr   v6Gateway v6Prefix v6LinkLocal
  v4DHCPEnabled v4DHCPServers v4DNS1 v4DNS2
  v6DHCPEnabled v6DHCPServers v6DNS1 v6DNS2
  v6SiteLocal v6SiteLocal3 v6SiteLocal4 v6SiteLocal5 v6SiteLocal6 v6SiteLocal7 v6SiteLocal8
  v6SiteLocal9 v6SiteLocal10 v6SiteLocal11 v6SiteLocal12 v6SiteLocal13 v6SiteLocal14 v6SiteLocal15
  ipmiLAN ipmiMinPriv ipmiKey hostname
]

Instance Attribute Summary

Attributes inherited from BaseLom

#hostname, #username

Instance Method Summary collapse

Methods inherited from BaseLom

action, actions, name

Constructor Details

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

Returns a new instance of Idrac8.


28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/moob/idrac8.rb', line 28

def initialize hostname, options = {}
  super hostname, options
  @username ||= 'root'
  @password ||= 'calvin'
  @index = nil

  @skiplogout = false

  @session.headers['Accept'] = 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8'
  @session.headers['Accept-Language'] = 'en-US,en;q=0.8,sv;q=0.6'
  # idrac8 responds with a 404 if the request is sent for html is sent without encoding headers
  @session.headers['Accept-Encoding'] = 'gzip,deflate,sdch'
end

Instance Method Details

#authenticateObject


42
43
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
81
82
83
84
85
86
87
88
89
# File 'lib/moob/idrac8.rb', line 42

def authenticate
  @session.handle_cookies nil

   = @session.get 'login.html'

  raise ResponseError.new  unless .status == 200

  auth = @session.post 'data/login', "user=#{@username}&password=#{@password}"

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

  auth.body =~ /<authResult>([^<]+)<\/authResult>/

  raise 'Cannot find auth result' unless $&
  raise "Auth failed with: \"#{auth.body}\"" unless $1 == "0"

  auth.body =~ /<forwardUrl>([^<]+)<\/forwardUrl>/

  raise 'Cannot find the authenticated index url after auth' unless $&

  @indexurl = $1

  if @indexurl =~ /defaultCred/
    @indexurl.gsub!(/defaultCred/,'index')
    Moob.warn "iDRAC recommends you should change the default credentials!"
  end

  Moob.inform "Requesting indexurl of #{@indexurl}"

  # someone decided it was a good idea to include a ST2 token in every XHR
  # request. We need it for a lot of our features.
  @authhash = @indexurl.split('?')[1]
  @authhash =~ /ST2=([0-9a-f]+)/
  @st2 = $1

  if @st2.nil?
      Moob.inform 'Trying to parse ST2 token from HTML page'

      @index = @session.get @indexurl
      @index.body =~ /var TOKEN_VALUE = "([0-9a-f]+)";/
      raise ResponseError.new @index unless @index.status == 200
      @st2 = $1
  end

  @session.headers['ST2'] = @st2

  return self
end

#boot_on(level) ⇒ Object


142
143
144
145
146
# File 'lib/moob/idrac8.rb', line 142

def boot_on level
  req = @session.post "data?set=vmBootOnce:1,firstBootDevice:#{level}", {}
  raise ResponseError.new req unless req.status == 200
  return nil
end

#decompress_response(response) ⇒ Object


101
102
103
104
# File 'lib/moob/idrac8.rb', line 101

def decompress_response response
  gz_reader = Zlib::GzipReader.new(StringIO.new(response))
  gz_reader.read
end

#detectObject


106
107
108
109
110
111
112
113
114
115
116
117
118
119
# File 'lib/moob/idrac8.rb', line 106

def detect
  begin
    home = @session.get 'login.html'
    # patron doesn't (always?) decompress the body automatically
    begin
      response = decompress_response home.body
    rescue Zlib::GzipFile::Error
      response = home.body
    end
    response =~ /(Integrated Dell Remote Access Controller 8)|(iDRAC8)/
  rescue Exception => e
    false
  end
end

#drac_set_params(params) ⇒ Object


224
225
226
227
228
229
230
# File 'lib/moob/idrac8.rb', line 224

def drac_set_params params
  params.each do |p,v|
    req = @session.post "data?set=#{p}:#{v}", {}
    raise ResponseError.new req unless req.status == 200
  end
  return nil
end

#enable_ipmiObject


220
221
222
# File 'lib/moob/idrac8.rb', line 220

def enable_ipmi
  drac_set_params({ 'ipmiLAN' => 1 })
end

#fetch_console_previewObject


235
236
237
238
239
240
241
242
243
244
245
246
247
248
# File 'lib/moob/idrac8.rb', line 235

def fetch_console_preview
  imgfile = Tempfile.new('console_preview')

  refreshreq = @session.get "data?get=consolepreview[auto%20#{Time.now.utc.to_i}]", {}

  raise ResponseError.new req unless refreshreq.status == 200

  req = @session.get_file "capconsole/scapture0.png?#{Time.now.utc.to_i}", imgfile.path

  raise ResponseError.new req unless req.status == 200
  raise UnexpectedContentError.new req unless req.headers['Content-type'] =~ /image\//

  return imgfile, req.headers
end

#get_infos(keys) ⇒ Object


195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
# File 'lib/moob/idrac8.rb', line 195

def get_infos keys
  Moob.inform "Requesting data?get=#{keys.join(',')}"
  infos = @session.post "data?get=#{keys.join(',')}", {}

  raise ResponseError.new infos unless infos.status == 200
  raise "The status isn't OK" unless infos.body =~ /<status>ok<\/status>/

  return Hash[keys.collect do |k|
    if infos.body =~ /<#{k}>(.*?)<\/#{k}>/
      [k, $1]
    else
      [k, nil]
    end
  end]
end

#infosObject


191
192
193
# File 'lib/moob/idrac8.rb', line 191

def infos
  return JSON.pretty_generate get_infos INFO_FIELDS
end

#jnlpObject


123
124
125
126
127
128
129
130
131
132
133
134
# File 'lib/moob/idrac8.rb', line 123

def jnlp
  # Request system name and hostname from data end-point
  req = get_infos ['sysDesc' ,'hostname']

  # eg escaped "idrac-A1BCD2E, PowerEdge R610, User:root"
  title = CGI::escape "#{req['hostname']}, #{req['sysDesc']}, User:#{@username}"

  viewer = @session.get "viewer.jnlp(#{@hostname}@[email protected]#{title}@#{Time.now.to_i * 1000}@#{@authhash})"
  raise ResponseError.new viewer unless viewer.status == 200

  return viewer.body
end

#logoutObject


91
92
93
94
95
96
97
98
99
# File 'lib/moob/idrac8.rb', line 91

def logout
  if @skiplogout
    Moob.inform 'Skipping logout...'
  else
    out = @session.get 'data/logout'
    raise ResponseError.new out unless out.status == 200
  end
  return self
end

#lomresetObject


251
252
253
254
# File 'lib/moob/idrac8.rb', line 251

def lomreset
  @skiplogout = true
  drac_set_params({ 'iDracReset' => 1 })
end

#power_control(action) ⇒ Object


136
137
138
139
140
# File 'lib/moob/idrac8.rb', line 136

def power_control action
  req = @session.post "data?set=pwState:#{action}", {}
  raise ResponseError.new req unless req.status == 200
  return nil
end

#pstatusObject


179
180
181
182
183
184
185
186
187
188
# File 'lib/moob/idrac8.rb', line 179

def pstatus
  case get_infos(['pwState'])['pwState']
  when '0'
    return :off
  when '1'
    return :on
  else
    return nil
  end
end

#set_paramsObject


212
213
214
215
216
217
# File 'lib/moob/idrac8.rb', line 212

def set_params
  unless @params
    raise "Params are not set!"
  end
  drac_set_params @params
end