2013-09-15 01:32:52 +00:00
|
|
|
import collections
|
2013-11-02 17:40:53 +00:00
|
|
|
import confluent.interface.console
|
2013-11-02 17:06:48 +00:00
|
|
|
import confluent.messages as msg
|
2013-09-09 17:29:36 +00:00
|
|
|
import eventlet
|
2013-09-15 14:45:38 +00:00
|
|
|
import eventlet.event
|
2013-10-13 23:59:30 +00:00
|
|
|
import eventlet.greenpool as greenpool
|
|
|
|
import eventlet.queue
|
2013-09-15 01:32:52 +00:00
|
|
|
import os
|
2013-09-12 19:52:34 +00:00
|
|
|
import pyghmi.ipmi.console as console
|
2013-10-12 19:13:34 +00:00
|
|
|
import pyghmi.ipmi.command as ipmicommand
|
2013-09-12 19:52:34 +00:00
|
|
|
console.session.select = eventlet.green.select
|
2013-09-09 17:29:36 +00:00
|
|
|
|
2013-09-15 01:32:52 +00:00
|
|
|
_ipmithread = None
|
|
|
|
#pullchain is a pipe to tug on to induce the ipmi thread process pending data
|
|
|
|
pullchain = None
|
2013-10-10 18:06:02 +00:00
|
|
|
chainpulled = False
|
2013-09-15 01:32:52 +00:00
|
|
|
tmptimeout = None
|
|
|
|
ipmiq = collections.deque([])
|
2013-09-15 14:45:38 +00:00
|
|
|
ipmiwaiters = collections.deque([])
|
2013-10-12 19:13:34 +00:00
|
|
|
|
|
|
|
|
|
|
|
def wait_on_ipmi():
|
|
|
|
waitevt = eventlet.event.Event()
|
|
|
|
ipmiwaiters.append(waitevt)
|
|
|
|
waitevt.wait()
|
2013-09-09 17:29:36 +00:00
|
|
|
|
|
|
|
|
|
|
|
def _ipmi_evtloop():
|
2013-09-15 01:32:52 +00:00
|
|
|
global tmptimeout
|
|
|
|
global pullchain
|
|
|
|
console.session.Session.register_handle_callback(pullchain[0],
|
|
|
|
_process_chgs)
|
2013-09-09 17:29:36 +00:00
|
|
|
while (1):
|
2014-01-28 22:41:01 +00:00
|
|
|
try:
|
|
|
|
if tmptimeout is not None:
|
|
|
|
console.session.Session.wait_for_rsp(timeout=tmptimeout)
|
|
|
|
tmptimeout = None
|
|
|
|
else:
|
|
|
|
console.session.Session.wait_for_rsp(timeout=600)
|
|
|
|
while ipmiwaiters:
|
|
|
|
waiter = ipmiwaiters.popleft()
|
|
|
|
waiter.send()
|
2014-01-29 17:52:57 +00:00
|
|
|
except RuntimeError:
|
2014-01-28 22:48:59 +00:00
|
|
|
raise
|
2014-01-28 22:41:01 +00:00
|
|
|
except:
|
2014-01-30 14:47:06 +00:00
|
|
|
import traceback
|
|
|
|
traceback.print_exc()
|
2013-09-15 01:32:52 +00:00
|
|
|
|
|
|
|
def _process_chgs(intline):
|
2013-10-12 19:13:34 +00:00
|
|
|
#here we receive functions to run in our thread
|
|
|
|
#the tuples on the deque consist of:
|
|
|
|
#function, arg tuple, and optionally a callback
|
|
|
|
#to send the return value back to the requester
|
2013-10-10 18:06:02 +00:00
|
|
|
global chainpulled
|
2013-09-15 01:32:52 +00:00
|
|
|
os.read(intline,1) # answer the bell
|
2013-10-10 18:06:02 +00:00
|
|
|
chainpulled = False
|
2014-01-28 22:41:01 +00:00
|
|
|
try:
|
|
|
|
while ipmiq:
|
|
|
|
cval = ipmiq.popleft()
|
|
|
|
if hasattr(cval[0], '__call__'):
|
|
|
|
if isinstance(cval[1], tuple):
|
|
|
|
rv = cval[0](*cval[1])
|
|
|
|
elif isinstance(cval[1], dict):
|
|
|
|
rv = cval[0](**cval[1])
|
|
|
|
if len(cval) > 2:
|
|
|
|
cval[2](rv)
|
|
|
|
except: # assure the thread does not crash and burn
|
2014-01-30 14:47:06 +00:00
|
|
|
import traceback
|
|
|
|
traceback.print_exc()
|
2014-01-30 03:08:48 +00:00
|
|
|
# If we are inside a loop within pyghmi, this is our only shot
|
|
|
|
# so we have to wake up anything that might be interested in
|
|
|
|
# state changes here as well as the evtloop
|
|
|
|
while ipmiwaiters:
|
|
|
|
waiter = ipmiwaiters.popleft()
|
|
|
|
waiter.send()
|
2013-09-09 17:29:36 +00:00
|
|
|
|
|
|
|
|
2013-09-12 19:52:34 +00:00
|
|
|
def get_conn_params(node, configdata):
|
2013-09-09 17:29:36 +00:00
|
|
|
if 'secret.ipmiuser' in configdata:
|
|
|
|
username = configdata['secret.ipmiuser']['value']
|
|
|
|
elif 'secret.managementuser' in configdata:
|
|
|
|
username = configdata['secret.managementuser']['value']
|
|
|
|
else:
|
|
|
|
username = 'USERID'
|
|
|
|
if 'secret.ipmipassphrase' in configdata:
|
2013-09-12 19:52:34 +00:00
|
|
|
passphrase = configdata['secret.ipmipassphrase']['value']
|
2013-09-09 19:30:24 +00:00
|
|
|
elif 'secret.managementpassphrase' in configdata:
|
2013-09-12 19:52:34 +00:00
|
|
|
passphrase = configdata['secret.managementpassphrase']['value']
|
2013-09-09 17:29:36 +00:00
|
|
|
else:
|
|
|
|
passphrase = 'PASSW0RD' # for lack of a better guess
|
2013-09-12 19:52:34 +00:00
|
|
|
if 'hardwaremanagement.manager' in configdata:
|
|
|
|
bmc = configdata['hardwaremanagement.manager']['value']
|
2013-09-09 17:29:36 +00:00
|
|
|
else:
|
|
|
|
bmc = node
|
|
|
|
if 'secret.ipmikg' in configdata:
|
|
|
|
kg = configdata['secret.ipmikg']['value']
|
|
|
|
else:
|
|
|
|
kg = passphrase
|
|
|
|
#TODO(jbjohnso): check if the end has some number after a : without []
|
|
|
|
#for non default port
|
|
|
|
return {
|
|
|
|
'username': username,
|
|
|
|
'passphrase': passphrase,
|
|
|
|
'kg': kg,
|
|
|
|
'bmc': bmc,
|
|
|
|
'port': 623,
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2013-11-02 17:40:53 +00:00
|
|
|
class IpmiConsole(confluent.interface.console.Console):
|
2013-09-12 19:52:34 +00:00
|
|
|
def __init__(self, node, config):
|
2013-09-09 17:29:36 +00:00
|
|
|
crypt = config.decrypt
|
|
|
|
config.decrypt = True
|
|
|
|
configdata = config.get_node_attributes([node],
|
|
|
|
['secret.ipmiuser', 'secret.ipmipassphrase',
|
|
|
|
'secret.managementuser', 'secret.managementpassphrase',
|
|
|
|
'hardwaremanagement.manager'])
|
2013-09-09 19:42:00 +00:00
|
|
|
connparams = get_conn_params(node, configdata[node])
|
2013-09-09 17:29:36 +00:00
|
|
|
self.username = connparams['username']
|
|
|
|
self.password = connparams['passphrase']
|
|
|
|
self.kg = connparams['kg']
|
|
|
|
self.bmc = connparams['bmc']
|
|
|
|
self.port = connparams['port']
|
|
|
|
# Cannot actually create console until 'connect', when we get callback
|
|
|
|
|
2014-02-01 14:39:57 +00:00
|
|
|
def handle_data(self, data):
|
|
|
|
if type(data) == dict:
|
|
|
|
#TODO: convert dict into a confluent.interface.console.ConsoleEvent
|
|
|
|
else:
|
|
|
|
self.datacallback(data)
|
|
|
|
|
2013-09-09 19:42:00 +00:00
|
|
|
def connect(self,callback):
|
2013-09-15 01:32:52 +00:00
|
|
|
global _ipmithread
|
|
|
|
global pullchain
|
2013-10-12 19:13:34 +00:00
|
|
|
global chainpulled
|
2014-02-01 14:39:57 +00:00
|
|
|
self.datacallback = callback
|
2013-09-15 01:32:52 +00:00
|
|
|
if _ipmithread is None:
|
|
|
|
pullchain = os.pipe()
|
|
|
|
_ipmithread = eventlet.spawn(_ipmi_evtloop)
|
2013-10-12 19:13:34 +00:00
|
|
|
self.solconnection = None
|
|
|
|
ipmiq.append((console.Console,{'bmc': self.bmc,
|
|
|
|
'port': self.port,
|
|
|
|
'userid': self.username,
|
|
|
|
'password': self.password,
|
|
|
|
'kg': self.kg,
|
|
|
|
'force': True,
|
2014-02-01 14:39:57 +00:00
|
|
|
'iohandler': self.handle_data}, self.got_consobject))
|
2013-10-12 19:13:34 +00:00
|
|
|
if not chainpulled:
|
|
|
|
chainpulled = True
|
|
|
|
os.write(pullchain[1],'1')
|
|
|
|
while self.solconnection is None:
|
|
|
|
wait_on_ipmi()
|
|
|
|
|
|
|
|
def got_consobject(self, solconnection):
|
|
|
|
self.solconnection = solconnection
|
2013-09-09 17:29:36 +00:00
|
|
|
|
2013-09-09 19:42:00 +00:00
|
|
|
def write(self, data):
|
2013-10-10 18:06:02 +00:00
|
|
|
global chainpulled
|
2013-09-15 01:32:52 +00:00
|
|
|
ipmiq.append((self.solconnection.send_data, (data,)))
|
2013-10-10 18:06:02 +00:00
|
|
|
if not chainpulled:
|
|
|
|
chainpulled = True
|
|
|
|
os.write(pullchain[1],'1')
|
|
|
|
|
2013-09-15 01:32:52 +00:00
|
|
|
#self.solconnection.send_data(data)
|
2013-09-09 17:29:36 +00:00
|
|
|
|
2013-09-12 20:54:39 +00:00
|
|
|
def wait_for_data(self, timeout=600):
|
|
|
|
"""Wait for some network event.
|
|
|
|
|
|
|
|
This is currently not guaranteed to actually have data when
|
|
|
|
return. This is supposed to be something more appropriate
|
|
|
|
than sleep(0), but only marginally so.
|
|
|
|
"""
|
|
|
|
# reason for this is that we currently nicely pass through the callback
|
|
|
|
# straight to ipmi library. To implement this accurately, easiest path
|
|
|
|
# would be to add a layer through the callback. IMO there isn't enough
|
|
|
|
# value in assuring data coming back to bother with making the stack
|
|
|
|
# taller than it has to be
|
2013-09-15 01:32:52 +00:00
|
|
|
global tmptimeout
|
2013-10-10 18:06:02 +00:00
|
|
|
global chainpulled
|
2013-09-15 01:32:52 +00:00
|
|
|
tmptimeout = timeout
|
2013-10-10 18:06:02 +00:00
|
|
|
if not chainpulled:
|
|
|
|
chainpulled=True
|
|
|
|
os.write(pullchain[1],'1')
|
2013-09-15 14:45:38 +00:00
|
|
|
eventlet.sleep(0.001)
|
2013-10-12 19:13:34 +00:00
|
|
|
wait_on_ipmi()
|
2013-09-15 01:32:52 +00:00
|
|
|
#TODO: a channel for the ipmithread to tug back instead of busy wait
|
2013-09-15 14:45:38 +00:00
|
|
|
#while tmptimeout is not None:
|
|
|
|
# eventlet.sleep(0)
|
2013-09-15 01:32:52 +00:00
|
|
|
#console.session.Session.wait_for_rsp(timeout=timeout)
|
2013-09-12 20:54:39 +00:00
|
|
|
|
2013-09-09 17:29:36 +00:00
|
|
|
|
2013-10-12 19:13:34 +00:00
|
|
|
class IpmiIterator(object):
|
2013-11-02 23:45:10 +00:00
|
|
|
def __init__(self, operator, nodes, element, cfg, inputdata):
|
2013-10-12 22:03:51 +00:00
|
|
|
crypt = cfg.decrypt
|
|
|
|
cfg.decrypt = True
|
2013-10-12 19:13:34 +00:00
|
|
|
configdata = cfg.get_node_attributes(nodes,
|
|
|
|
['secret.ipmiuser', 'secret.ipmipassphrase',
|
|
|
|
'secret.managementuser', 'secret.managementpassphrase',
|
|
|
|
'hardwaremanagement.manager'])
|
2013-10-12 22:03:51 +00:00
|
|
|
cfg.decrypt = crypt
|
2013-10-14 00:51:30 +00:00
|
|
|
self.gpile = greenpool.GreenPile()
|
2013-10-12 19:13:34 +00:00
|
|
|
for node in nodes:
|
2013-11-02 23:45:10 +00:00
|
|
|
self.gpile.spawn(perform_request, operator, node, element, configdata, inputdata)
|
2013-10-12 19:13:34 +00:00
|
|
|
|
|
|
|
def __iter__(self):
|
|
|
|
return self
|
|
|
|
|
|
|
|
def next(self):
|
2013-10-14 00:51:30 +00:00
|
|
|
ndata = self.gpile.next()
|
|
|
|
# need to apply any translations between pyghmi and confluent
|
|
|
|
return ndata
|
2013-10-12 19:13:34 +00:00
|
|
|
|
|
|
|
|
2013-11-02 23:45:10 +00:00
|
|
|
def perform_request(operator, node, element, configdata, inputdata):
|
|
|
|
return IpmiHandler(operator, node, element, configdata, inputdata).handle_request()
|
2013-10-12 19:13:34 +00:00
|
|
|
|
|
|
|
|
|
|
|
class IpmiHandler(object):
|
|
|
|
def __iter__():
|
|
|
|
return self
|
|
|
|
|
2013-11-02 23:45:10 +00:00
|
|
|
def __init__(self, operation, node, element, cfd, inputdata):
|
2013-10-12 22:03:51 +00:00
|
|
|
global chainpulled
|
|
|
|
global _ipmithread
|
|
|
|
global pullchain
|
|
|
|
if _ipmithread is None:
|
|
|
|
pullchain = os.pipe()
|
|
|
|
_ipmithread = eventlet.spawn(_ipmi_evtloop)
|
|
|
|
eventlet.sleep(0)
|
2013-10-12 19:13:34 +00:00
|
|
|
self.cfg = cfd[node]
|
2013-10-12 22:03:51 +00:00
|
|
|
self.loggedin = False
|
2013-10-12 19:13:34 +00:00
|
|
|
self.node = node
|
|
|
|
self.element = element
|
|
|
|
self.op = operation
|
|
|
|
connparams = get_conn_params(node, self.cfg)
|
|
|
|
self.ipmicmd = None
|
2013-11-02 23:45:10 +00:00
|
|
|
self.inputdata = inputdata
|
2013-10-12 19:13:34 +00:00
|
|
|
ipmiq.append((ipmicommand.Command,{'bmc': connparams['bmc'],
|
|
|
|
'userid': connparams['username'],
|
|
|
|
'password': connparams['passphrase'],
|
|
|
|
'kg': connparams['kg'],
|
|
|
|
'port': connparams['port'],
|
2013-10-13 23:59:30 +00:00
|
|
|
'onlogon': self.logged},
|
2013-10-12 22:03:51 +00:00
|
|
|
self.got_ipmicmd))
|
|
|
|
if not chainpulled:
|
|
|
|
chainpulled = True
|
|
|
|
os.write(pullchain[1],'1')
|
2013-10-12 19:13:34 +00:00
|
|
|
while self.ipmicmd == None:
|
|
|
|
wait_on_ipmi()
|
|
|
|
|
|
|
|
def got_ipmicmd(self, ipmicmd):
|
|
|
|
self.ipmicmd = ipmicmd
|
|
|
|
|
2013-10-12 22:03:51 +00:00
|
|
|
def logged(self, response, ipmicmd):
|
|
|
|
if 'error' in response:
|
|
|
|
raise Exception(response['error'])
|
|
|
|
self.loggedin = True
|
|
|
|
|
2013-10-12 19:13:34 +00:00
|
|
|
def call_ipmicmd(self, function, *args):
|
2013-10-12 22:03:51 +00:00
|
|
|
global chainpulled
|
2013-10-12 19:13:34 +00:00
|
|
|
self.lastrsp = None
|
|
|
|
ipmiq.append((function, args, self.got_rsp))
|
2013-10-12 22:03:51 +00:00
|
|
|
if not chainpulled:
|
|
|
|
chainpulled = True
|
|
|
|
os.write(pullchain[1],'1')
|
2013-10-12 19:13:34 +00:00
|
|
|
while self.lastrsp is None:
|
|
|
|
wait_on_ipmi()
|
|
|
|
return self.lastrsp
|
|
|
|
|
|
|
|
def got_rsp(self, response):
|
|
|
|
self.lastrsp = response
|
|
|
|
|
2013-10-12 22:03:51 +00:00
|
|
|
def handle_request(self):
|
|
|
|
while not self.loggedin:
|
|
|
|
wait_on_ipmi()
|
2013-11-22 23:27:15 +00:00
|
|
|
bootdevices = {
|
|
|
|
'optical': 'cd'
|
|
|
|
}
|
2013-11-07 19:39:34 +00:00
|
|
|
if self.element == [ 'power', 'state' ]:
|
2013-10-12 19:13:34 +00:00
|
|
|
if 'read' == self.op:
|
2013-11-02 17:06:48 +00:00
|
|
|
power = self.call_ipmicmd(self.ipmicmd.get_power)
|
|
|
|
return msg.PowerState(node=self.node,
|
|
|
|
state=power['powerstate'])
|
2013-11-02 23:45:10 +00:00
|
|
|
elif 'update' == self.op:
|
2013-11-03 14:52:43 +00:00
|
|
|
powerstate = self.inputdata.powerstate(self.node)
|
|
|
|
#TODO: call with wait argument
|
|
|
|
self.call_ipmicmd(self.ipmicmd.set_power, powerstate)
|
2013-11-02 23:45:10 +00:00
|
|
|
power = self.call_ipmicmd(self.ipmicmd.get_power)
|
2013-11-03 14:05:50 +00:00
|
|
|
return msg.PowerState(node=self.node,
|
|
|
|
state=power['powerstate'])
|
2013-11-07 21:33:48 +00:00
|
|
|
elif self.element == [ 'boot', 'device' ]:
|
|
|
|
if 'read' == self.op:
|
|
|
|
bootdev = self.call_ipmicmd(self.ipmicmd.get_bootdev)
|
2013-11-22 23:27:15 +00:00
|
|
|
if bootdev['bootdev'] in bootdevices:
|
|
|
|
bootdev['bootdev'] = bootdevices[bootdev['bootdev']]
|
2013-11-07 21:33:48 +00:00
|
|
|
return msg.BootDevice(node=self.node,
|
|
|
|
device=bootdev['bootdev'])
|
|
|
|
elif 'update' == self.op:
|
|
|
|
bootdev = self.inputdata.bootdevice(self.node)
|
|
|
|
bootdev = self.call_ipmicmd(self.ipmicmd.set_bootdev, bootdev)
|
2013-11-22 23:27:15 +00:00
|
|
|
if bootdev['bootdev'] in bootdevices:
|
|
|
|
bootdev['bootdev'] = bootdevices[bootdev['bootdev']]
|
2013-11-07 21:33:48 +00:00
|
|
|
return msg.BootDevice(node=self.node,
|
|
|
|
device=bootdev['bootdev'])
|
|
|
|
|
2013-11-02 23:45:10 +00:00
|
|
|
|
2013-10-12 19:13:34 +00:00
|
|
|
|
2013-11-02 23:45:10 +00:00
|
|
|
def create(nodes, element, configmanager, inputdata):
|
2013-11-07 19:39:34 +00:00
|
|
|
if element == [ '_console', 'session' ]:
|
2013-09-09 17:29:36 +00:00
|
|
|
if len(nodes) > 1:
|
|
|
|
raise Exception("_console/session does not support multiple nodes")
|
2013-11-02 17:06:48 +00:00
|
|
|
return IpmiConsole(nodes[0], configmanager)
|
2013-09-09 17:29:36 +00:00
|
|
|
else:
|
2013-11-02 23:45:10 +00:00
|
|
|
return IpmiIterator('update', nodes, element, configmanager, inputdata)
|
|
|
|
|
|
|
|
def update(nodes, element, configmanager, inputdata):
|
2013-11-03 14:05:50 +00:00
|
|
|
return create(nodes, element, configmanager, inputdata)
|
2013-09-09 17:29:36 +00:00
|
|
|
|
|
|
|
|
2013-10-12 19:13:34 +00:00
|
|
|
|
2013-11-02 23:45:10 +00:00
|
|
|
def retrieve(nodes, element, configmanager, inputdata):
|
|
|
|
return IpmiIterator('read', nodes, element, configmanager, inputdata)
|
2013-10-12 22:03:51 +00:00
|
|
|
|