2014-04-07 20:43:39 +00:00
|
|
|
# Copyright 2014 IBM Corporation
|
|
|
|
#
|
|
|
|
# Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
# you may not use this file except in compliance with the License.
|
|
|
|
# You may obtain a copy of the License at
|
|
|
|
#
|
|
|
|
# http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
#
|
|
|
|
# Unless required by applicable law or agreed to in writing, software
|
|
|
|
# distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
# See the License for the specific language governing permissions and
|
|
|
|
# limitations under the License.
|
|
|
|
|
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
|
2014-04-15 18:59:36 +00:00
|
|
|
import pyghmi.constants as pygconstants
|
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
|
2014-04-21 14:48:18 +00:00
|
|
|
|
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
|
|
|
|
2014-04-21 14:48:18 +00:00
|
|
|
|
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-04-21 14:48:18 +00:00
|
|
|
except: # TODO(jbjohnso): log the trace into the log
|
2014-01-30 14:47:06 +00:00
|
|
|
import traceback
|
2014-04-21 14:48:18 +00:00
|
|
|
|
2014-01-30 14:47:06 +00:00
|
|
|
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:
|
2014-04-04 13:15:20 +00:00
|
|
|
username = configdata['secret.hardwaremanagementuser']['value']
|
2013-09-09 17:29:36 +00:00
|
|
|
else:
|
|
|
|
username = 'USERID'
|
2014-04-04 13:09:00 +00:00
|
|
|
if 'secret.hardwaremanagementpassphrase' in configdata:
|
2014-04-04 13:15:20 +00:00
|
|
|
passphrase = configdata['secret.hardwaremanagementpassphrase']['value']
|
2013-09-09 17:29:36 +00:00
|
|
|
else:
|
2014-04-21 14:48:18 +00:00
|
|
|
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-04-21 14:48:18 +00:00
|
|
|
|
2014-04-04 22:40:45 +00:00
|
|
|
_configattributes = ('secret.hardwaremanagementuser',
|
2014-04-21 14:48:18 +00:00
|
|
|
'secret.hardwaremanagementpassphrase',
|
|
|
|
'secret.ipmikg', 'hardwaremanagement.manager')
|
|
|
|
|
2013-09-09 17:29:36 +00:00
|
|
|
|
2014-04-05 00:45:48 +00:00
|
|
|
def _donothing(data):
|
|
|
|
# a dummy function to avoid some awkward exceptions from
|
|
|
|
# zombie pyghmi console objects
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
2014-02-01 23:49:36 +00:00
|
|
|
class IpmiConsole(conapi.Console):
|
2014-04-04 22:40:45 +00:00
|
|
|
configattributes = frozenset(_configattributes)
|
|
|
|
|
2013-09-12 19:52:34 +00:00
|
|
|
def __init__(self, node, config):
|
2014-04-21 14:48:18 +00:00
|
|
|
self.error = None
|
|
|
|
self.datacallback = None
|
2013-09-09 17:29:36 +00:00
|
|
|
crypt = config.decrypt
|
2014-04-21 14:48:18 +00:00
|
|
|
self.solconnection = None
|
2013-09-09 17:29:36 +00:00
|
|
|
config.decrypt = True
|
2014-02-01 23:49:36 +00:00
|
|
|
self.broken = False
|
2014-04-04 22:40:45 +00:00
|
|
|
configdata = config.get_node_attributes([node], _configattributes)
|
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-04-04 22:40:45 +00:00
|
|
|
def __del__(self):
|
|
|
|
self.solconnection = None
|
|
|
|
|
2014-02-01 14:39:57 +00:00
|
|
|
def handle_data(self, data):
|
|
|
|
if type(data) == dict:
|
2014-04-16 14:18:59 +00:00
|
|
|
if 'error' in data:
|
2014-04-04 22:40:45 +00:00
|
|
|
self.solconnection = None
|
2014-02-01 23:49:36 +00:00
|
|
|
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 14:39:57 +00:00
|
|
|
else:
|
|
|
|
self.datacallback(data)
|
|
|
|
|
2014-04-21 14:48:18 +00:00
|
|
|
def connect(self, callback):
|
2014-02-01 14:39:57 +00:00
|
|
|
self.datacallback = callback
|
2014-04-04 22:40:45 +00:00
|
|
|
# we provide a weak reference to pyghmi as otherwise we'd
|
|
|
|
# have a circular reference and reference counting would never get
|
|
|
|
# out...
|
2014-04-01 20:32:56 +00:00
|
|
|
try:
|
|
|
|
self.solconnection = console.Console(bmc=self.bmc, port=self.port,
|
2014-04-21 14:48:18 +00:00
|
|
|
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()
|
2014-04-04 22:40:45 +00:00
|
|
|
if self.broken:
|
|
|
|
break
|
2014-04-01 20:53:20 +00:00
|
|
|
if self.broken:
|
|
|
|
raise exc.TargetEndpointUnreachable(self.error)
|
2014-04-16 14:18:59 +00:00
|
|
|
self.connected = True
|
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-04 22:40:45 +00:00
|
|
|
def close(self):
|
2014-04-21 14:48:18 +00:00
|
|
|
if self.solconnection is not None:
|
2014-04-04 22:40:45 +00:00
|
|
|
# break the circular reference here
|
2014-04-05 00:45:48 +00:00
|
|
|
self.solconnection.out_handler = _donothing
|
2014-04-04 22:40:45 +00:00
|
|
|
self.solconnection = None
|
|
|
|
self.broken = True
|
|
|
|
self.error = "closed"
|
|
|
|
|
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
|
|
|
|
2014-04-09 19:37:35 +00:00
|
|
|
def send_break(self):
|
|
|
|
self.solconnection.send_break()
|
|
|
|
|
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):
|
2014-04-15 18:59:36 +00:00
|
|
|
self.currdata = None
|
2013-10-12 22:03:51 +00:00
|
|
|
crypt = cfg.decrypt
|
|
|
|
cfg.decrypt = True
|
2014-04-04 22:40:45 +00:00
|
|
|
configdata = cfg.get_node_attributes(nodes, _configattributes)
|
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:
|
2014-04-15 19:24:32 +00:00
|
|
|
self.gpile.spawn(perform_request, operator, node, element,
|
|
|
|
configdata, inputdata, cfg)
|
2013-10-12 19:13:34 +00:00
|
|
|
|
|
|
|
def __iter__(self):
|
|
|
|
return self
|
|
|
|
|
|
|
|
def next(self):
|
2014-04-15 18:59:36 +00:00
|
|
|
if self.currdata is None:
|
|
|
|
self.currdata = self.gpile.next()
|
2013-10-14 00:51:30 +00:00
|
|
|
# need to apply any translations between pyghmi and confluent
|
2014-04-15 18:59:36 +00:00
|
|
|
try:
|
|
|
|
retdata = self.currdata.next()
|
|
|
|
except AttributeError:
|
|
|
|
retdata = self.currdata
|
|
|
|
self.currdata = None
|
|
|
|
return retdata
|
2013-10-12 19:13:34 +00:00
|
|
|
|
2014-04-21 14:48:18 +00:00
|
|
|
|
2014-04-15 19:24:32 +00:00
|
|
|
def perform_request(operator, node, element, configdata, inputdata, cfg):
|
|
|
|
return IpmiHandler(operator, node, element, configdata, inputdata, cfg
|
2014-04-21 14:48:18 +00:00
|
|
|
).handle_request()
|
|
|
|
|
2013-10-12 19:13:34 +00:00
|
|
|
|
2014-04-15 19:24:32 +00:00
|
|
|
persistent_ipmicmds = {}
|
2013-10-12 19:13:34 +00:00
|
|
|
|
2014-04-21 14:48:18 +00:00
|
|
|
|
2013-10-12 19:13:34 +00:00
|
|
|
class IpmiHandler(object):
|
2014-04-15 19:24:32 +00:00
|
|
|
def __init__(self, operation, node, element, cfd, inputdata, cfg):
|
2014-02-02 00:28:31 +00:00
|
|
|
self.broken = False
|
2014-04-21 14:48:18 +00:00
|
|
|
self.error = None
|
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
|
2013-11-02 23:45:10 +00:00
|
|
|
self.inputdata = inputdata
|
2014-04-15 19:24:32 +00:00
|
|
|
tenant = cfg.tenant
|
|
|
|
self._logevt = None
|
2014-04-21 14:48:18 +00:00
|
|
|
if (node, tenant) not in persistent_ipmicmds:
|
2014-04-15 19:24:32 +00:00
|
|
|
self._logevt = threading.Event()
|
2014-04-21 14:48:18 +00:00
|
|
|
persistent_ipmicmds[(node, tenant)] = ipmicommand.Command(
|
2014-04-15 19:24:32 +00:00
|
|
|
bmc=connparams['bmc'], userid=connparams['username'],
|
|
|
|
password=connparams['passphrase'], kg=connparams['kg'],
|
|
|
|
port=connparams['port'], onlogon=self.logged)
|
2014-04-21 14:48:18 +00:00
|
|
|
self.ipmicmd = persistent_ipmicmds[(node, tenant)]
|
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-04-15 19:24:32 +00:00
|
|
|
if self._logevt is not None:
|
|
|
|
self._logevt.wait()
|
|
|
|
self._logevt = None
|
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)
|
2014-04-21 14:48:18 +00:00
|
|
|
if self.element == ['power', 'state']:
|
2014-02-06 16:08:55 +00:00
|
|
|
return self.power()
|
2014-04-21 14:48:18 +00:00
|
|
|
elif self.element == ['boot', 'nextdevice']:
|
2014-02-06 16:08:55 +00:00
|
|
|
return self.bootdevice()
|
2014-04-21 14:48:18 +00:00
|
|
|
elif self.element == ['health', 'hardware']:
|
2014-04-15 18:59:36 +00:00
|
|
|
return self.health()
|
|
|
|
|
2014-04-21 14:48:18 +00:00
|
|
|
@staticmethod
|
|
|
|
def _str_health(health):
|
2014-04-15 18:59:36 +00:00
|
|
|
if pygconstants.Health.Failed & health:
|
|
|
|
health = 'failed'
|
|
|
|
elif pygconstants.Health.Critical & health:
|
|
|
|
health = 'critical'
|
|
|
|
elif pygconstants.Health.Warning & health:
|
|
|
|
health = 'warning'
|
|
|
|
else:
|
|
|
|
health = 'ok'
|
|
|
|
return health
|
|
|
|
|
|
|
|
def _dict_sensor(self, pygreading):
|
2014-04-21 14:48:18 +00:00
|
|
|
retdict = {'name': pygreading.name, 'value': pygreading.value,
|
|
|
|
'states': pygreading.states,
|
|
|
|
'health': self._str_health(pygreading.health)}
|
2014-04-15 18:59:36 +00:00
|
|
|
return retdict
|
|
|
|
|
|
|
|
def health(self):
|
|
|
|
if 'read' == self.op:
|
|
|
|
response = self.ipmicmd.get_health()
|
|
|
|
health = response['health']
|
|
|
|
health = self._str_health(health)
|
|
|
|
yield msg.HealthSummary(health, self.node)
|
|
|
|
if 'badreadings' in response:
|
|
|
|
badsensors = []
|
|
|
|
for reading in response['badreadings']:
|
|
|
|
badsensors.append(self._dict_sensor(reading))
|
|
|
|
yield msg.SensorReadings(badsensors, name=self.node)
|
|
|
|
else:
|
|
|
|
raise exc.InvalidArgumentException('health is read-only')
|
2014-02-06 16:08:55 +00:00
|
|
|
|
|
|
|
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,
|
2014-04-21 14:48:18 +00:00
|
|
|
device=bootdev['bootdev'])
|
2014-02-06 16:08:55 +00:00
|
|
|
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,
|
2014-04-21 14:48:18 +00:00
|
|
|
device=bootdev['bootdev'])
|
2014-02-06 16:08:55 +00:00
|
|
|
|
|
|
|
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
|
|
|
|
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()
|
2014-04-21 14:48:18 +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)
|
|
|
|
|
2014-04-21 14:48:18 +00:00
|
|
|
|
2013-11-02 23:45:10 +00:00
|
|
|
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-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)
|