2013-09-15 01:32:52 +00:00
|
|
|
import collections
|
2014-02-02 00:28:31 +00:00
|
|
|
import confluent.exceptions as exc
|
2014-02-01 23:49:36 +00:00
|
|
|
import confluent.interface.console as conapi
|
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
|
2014-02-06 16:08:55 +00:00
|
|
|
import eventlet.green.threading as threading
|
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
|
2014-02-02 00:41:15 +00:00
|
|
|
import pyghmi.exceptions as pygexc
|
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
|
2014-04-01 20:32:56 +00:00
|
|
|
import socket
|
2013-09-12 19:52:34 +00:00
|
|
|
console.session.select = eventlet.green.select
|
2014-02-03 00:16:59 +00:00
|
|
|
console.session.threading = eventlet.green.threading
|
2013-09-09 17:29:36 +00:00
|
|
|
|
2014-02-03 00:16:59 +00:00
|
|
|
_ipmithread = None
|
2014-04-01 20:53:20 +00:00
|
|
|
_ipmiwaiters = []
|
2013-09-09 17:29:36 +00:00
|
|
|
|
|
|
|
def _ipmi_evtloop():
|
2014-02-06 14:27:38 +00:00
|
|
|
while True:
|
2014-01-28 22:41:01 +00:00
|
|
|
try:
|
2014-02-06 14:27:38 +00:00
|
|
|
console.session.Session.wait_for_rsp(timeout=600)
|
2014-04-01 20:53:20 +00:00
|
|
|
while _ipmiwaiters:
|
|
|
|
waiter = _ipmiwaiters.pop()
|
|
|
|
waiter.send()
|
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
|
|
|
|
2013-09-09 17:29:36 +00:00
|
|
|
|
2013-09-12 19:52:34 +00:00
|
|
|
def get_conn_params(node, configdata):
|
2014-04-04 13:09:00 +00:00
|
|
|
if 'secret.hardwaremanagementuser' in configdata:
|
2013-09-09 17:29:36 +00:00
|
|
|
username = configdata['secret.managementuser']['value']
|
|
|
|
else:
|
|
|
|
username = 'USERID'
|
2014-04-04 13:09:00 +00:00
|
|
|
if 'secret.hardwaremanagementpassphrase' 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,
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2014-02-01 23:49:36 +00:00
|
|
|
class IpmiConsole(conapi.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
|
2014-02-01 23:49:36 +00:00
|
|
|
self.broken = False
|
2013-09-09 17:29:36 +00:00
|
|
|
configdata = config.get_node_attributes([node],
|
2014-04-04 13:09:00 +00:00
|
|
|
['secret.hardwaremanagementuser',
|
|
|
|
'secret.hardwaremanagementpassphrase',
|
|
|
|
'secret.ipmikg', 'hardwaremanagement.manager'])
|
2013-09-09 19:42:00 +00:00
|
|
|
connparams = get_conn_params(node, configdata[node])
|
2014-04-02 12:59:40 +00:00
|
|
|
config.decrypt = crypt
|
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']
|
2014-04-01 20:53:20 +00:00
|
|
|
self.connected = False
|
2013-09-09 17:29:36 +00:00
|
|
|
# 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:
|
2014-02-01 23:49:36 +00:00
|
|
|
disconnect = frozenset(('Session Disconnected', 'timeout'))
|
|
|
|
if 'error' in data and data['error'] in disconnect:
|
|
|
|
self.broken = True
|
2014-04-01 20:53:20 +00:00
|
|
|
self.error = data['error']
|
|
|
|
if self.connected:
|
|
|
|
self.datacallback(conapi.ConsoleEvent.Disconnect)
|
2014-02-01 23:49:36 +00:00
|
|
|
else:
|
|
|
|
raise Exception("Unrecognized pyghmi input %s" % repr(data))
|
2014-02-01 14:39:57 +00:00
|
|
|
else:
|
|
|
|
self.datacallback(data)
|
|
|
|
|
2013-09-09 19:42:00 +00:00
|
|
|
def connect(self,callback):
|
2014-02-01 14:39:57 +00:00
|
|
|
self.datacallback = callback
|
2014-04-01 20:32:56 +00:00
|
|
|
try:
|
|
|
|
self.solconnection = console.Console(bmc=self.bmc, port=self.port,
|
|
|
|
userid=self.username,
|
|
|
|
password=self.password,
|
|
|
|
kg=self.kg, force=True,
|
|
|
|
iohandler=self.handle_data)
|
2014-04-01 20:53:20 +00:00
|
|
|
while not self.solconnection.connected and not self.broken:
|
|
|
|
w = eventlet.event.Event()
|
|
|
|
_ipmiwaiters.append(w)
|
|
|
|
w.wait()
|
|
|
|
if self.broken:
|
|
|
|
raise exc.TargetEndpointUnreachable(self.error)
|
2014-04-01 20:32:56 +00:00
|
|
|
except socket.gaierror as err:
|
|
|
|
raise exc.TargetEndpointUnreachable(str(err))
|
2013-09-09 17:29:36 +00:00
|
|
|
|
2014-04-01 20:53:20 +00:00
|
|
|
|
2013-09-09 19:42:00 +00:00
|
|
|
def write(self, data):
|
2014-02-03 00:16:59 +00:00
|
|
|
self.solconnection.send_data(data)
|
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,
|
2014-04-04 13:09:00 +00:00
|
|
|
['secret.hardwaremanagementuser',
|
|
|
|
'secret.hardwaremanagementpassphrase',
|
|
|
|
'secret.ipmikg', '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):
|
2013-11-02 23:45:10 +00:00
|
|
|
def __init__(self, operation, node, element, cfd, inputdata):
|
2014-02-02 00:28:31 +00:00
|
|
|
self.broken = False
|
2013-10-12 22:03:51 +00:00
|
|
|
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
|
2014-02-06 16:08:55 +00:00
|
|
|
self._logevt = threading.Event()
|
2013-11-02 23:45:10 +00:00
|
|
|
self.inputdata = inputdata
|
2014-02-03 00:16:59 +00:00
|
|
|
self.ipmicmd = ipmicommand.Command(bmc=connparams['bmc'],
|
|
|
|
userid=connparams['username'],
|
|
|
|
password=connparams['passphrase'],
|
|
|
|
kg=connparams['kg'],
|
|
|
|
port=connparams['port'],
|
|
|
|
onlogon=self.logged)
|
2014-02-06 16:08:55 +00:00
|
|
|
|
|
|
|
bootdevices = {
|
|
|
|
'optical': 'cd'
|
|
|
|
}
|
2013-10-12 19:13:34 +00:00
|
|
|
|
2013-10-12 22:03:51 +00:00
|
|
|
def logged(self, response, ipmicmd):
|
|
|
|
if 'error' in response:
|
2014-02-02 00:28:31 +00:00
|
|
|
self.broken = True
|
|
|
|
self.error = response['error']
|
|
|
|
else:
|
|
|
|
self.loggedin = True
|
2014-02-06 16:08:55 +00:00
|
|
|
self._logevt.set()
|
2013-10-12 22:03:51 +00:00
|
|
|
|
|
|
|
def handle_request(self):
|
2014-02-06 16:08:55 +00:00
|
|
|
self._logevt.wait()
|
2014-02-02 00:28:31 +00:00
|
|
|
if self.broken:
|
|
|
|
if self.error == 'timeout':
|
2014-04-01 20:32:56 +00:00
|
|
|
raise exc.TargetEndpointUnreachable('Target timed out')
|
2014-02-02 00:28:31 +00:00
|
|
|
else:
|
|
|
|
raise Exception(self.error)
|
2013-11-07 19:39:34 +00:00
|
|
|
if self.element == [ 'power', 'state' ]:
|
2014-02-06 16:08:55 +00:00
|
|
|
return self.power()
|
2014-02-23 02:14:55 +00:00
|
|
|
elif self.element == [ 'boot', 'nextdevice' ]:
|
2014-02-06 16:08:55 +00:00
|
|
|
return self.bootdevice()
|
|
|
|
|
|
|
|
def bootdevice(self):
|
|
|
|
if 'read' == self.op:
|
|
|
|
bootdev = self.ipmicmd.get_bootdev()
|
|
|
|
if bootdev['bootdev'] in self.bootdevices:
|
|
|
|
bootdev['bootdev'] = self.bootdevices[bootdev['bootdev']]
|
|
|
|
return msg.BootDevice(node=self.node,
|
|
|
|
device=bootdev['bootdev'])
|
|
|
|
elif 'update' == self.op:
|
|
|
|
bootdev = self.inputdata.bootdevice(self.node)
|
|
|
|
bootdev = self.ipmicmd.set_bootdev(bootdev)
|
|
|
|
if bootdev['bootdev'] in self.bootdevices:
|
|
|
|
bootdev['bootdev'] = self.bootdevices[bootdev['bootdev']]
|
|
|
|
return msg.BootDevice(node=self.node,
|
|
|
|
device=bootdev['bootdev'])
|
|
|
|
|
|
|
|
def power(self):
|
|
|
|
if 'read' == self.op:
|
|
|
|
power = self.ipmicmd.get_power()
|
|
|
|
return msg.PowerState(node=self.node,
|
|
|
|
state=power['powerstate'])
|
|
|
|
elif 'update' == self.op:
|
|
|
|
powerstate = self.inputdata.powerstate(self.node)
|
|
|
|
#TODO: call with wait argument
|
|
|
|
self.ipmicmd.set_power(powerstate)
|
|
|
|
power = self.ipmicmd.get_power()
|
|
|
|
return msg.PowerState(node=self.node,
|
|
|
|
state=power['powerstate'])
|
2013-11-07 21:33:48 +00:00
|
|
|
|
2013-11-02 23:45:10 +00:00
|
|
|
|
2013-10-12 19:13:34 +00:00
|
|
|
|
2014-02-06 14:27:38 +00:00
|
|
|
def initthread():
|
|
|
|
global _ipmithread
|
|
|
|
if _ipmithread is None:
|
|
|
|
_ipmithread = eventlet.spawn(_ipmi_evtloop)
|
|
|
|
|
|
|
|
|
2013-11-02 23:45:10 +00:00
|
|
|
def create(nodes, element, configmanager, inputdata):
|
2014-02-06 14:27:38 +00:00
|
|
|
initthread()
|
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):
|
2014-02-06 14:27:38 +00:00
|
|
|
initthread()
|
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):
|
2014-02-06 14:27:38 +00:00
|
|
|
initthread()
|
2013-11-02 23:45:10 +00:00
|
|
|
return IpmiIterator('read', nodes, element, configmanager, inputdata)
|
2013-10-12 22:03:51 +00:00
|
|
|
|