From 3d1e398977a6b4c8dad66e9a36736d766b6e7ac5 Mon Sep 17 00:00:00 2001 From: Jarrod Johnson Date: Sun, 13 Oct 2013 20:51:30 -0400 Subject: [PATCH] Convert wsgi function to be generator Since wsgi function returns iterable, it turns out to be easy to do it this way. For now, hardcode json dumps, two things: 1: call out to module supporting HTML and JSON interpretation 2: in both cases, a header and footer must sandwich the responses in http In socketapi, responses will come back without a container, but http does not afford us such luxury. Create a containing json structure. On first iteration, do not prepend with ,. prepend with , all other times. --- confluent/httpapi.py | 20 ++++++++++++-------- plugins/ipmi.py | 13 +++++++------ 2 files changed, 19 insertions(+), 14 deletions(-) diff --git a/confluent/httpapi.py b/confluent/httpapi.py index 22e54e05..06fc3bfb 100644 --- a/confluent/httpapi.py +++ b/confluent/httpapi.py @@ -138,12 +138,14 @@ def resourcehandler(env, start_response): start_response('401 Authentication Required', [('Content-type', 'text/plain'), ('WWW-Authenticate', 'Basic realm="confluent"')]) - return 'authentication required' + yield 'authentication required' + return if authorized['code'] == 403: start_response('403 Forbidden', [('Content-type', 'text/plain'), ('WWW-Authenticate', 'Basic realm="confluent"')]) - return 'authorization failed' + yield 'authorization failed' + return if authorized['code'] != 200: raise Exception("Unrecognized code from auth engine") headers = [('Content-Type', 'application/json; charset=utf-8')] @@ -164,7 +166,8 @@ def resourcehandler(env, start_response): return sessid = _assign_consessionid(consession) start_response('200 OK', headers) - return ['{"session":"%s","data":""}' % sessid] + yield '{"session":"%s","data":""}' % sessid + return elif 'keys' in querydict.keys(): # client wishes to push some keys into the remote console input = "" @@ -174,7 +177,7 @@ def resourcehandler(env, start_response): consolesessions[sessid]['expiry'] = time.time() + 90 consolesessions[sessid]['session'].write(input) start_response('200 OK', headers) - return [] # client has requests to send or receive, not both... + return # client has requests to send or receive, not both... else: #no keys, but a session, means it's hooking to receive data sessid = querydict['session'] consolesessions[sessid]['expiry'] = time.time() + 90 @@ -186,11 +189,12 @@ def resourcehandler(env, start_response): except UnicodeDecodeError: rsp = json.dumps({'session': querydict['session'], 'data': 'DECODEERROR'}) start_response('200 OK', headers) - return [rsp] + yield rsp + return else: - pluginapi.handle_path(env['PATH_INFO'], 'retrieve', cfgmgr) - start_response('404 Not Found', headers) - return ["404 Unrecognized resource"] + start_response('200 OK', headers) + for rsp in pluginapi.handle_path(env['PATH_INFO'], 'retrieve', cfgmgr): + yield json.dumps(rsp, separators=(',', ':')) def serve(): diff --git a/plugins/ipmi.py b/plugins/ipmi.py index 290b4b1f..bfddccab 100644 --- a/plugins/ipmi.py +++ b/plugins/ipmi.py @@ -174,20 +174,21 @@ class IpmiIterator(object): 'secret.managementuser', 'secret.managementpassphrase', 'hardwaremanagement.manager']) cfg.decrypt = crypt + self.gpile = greenpool.GreenPile() for node in nodes: - print IpmiHandler(operator, node, element, configdata).handle_request() - #eventlet.spawn(perform_request, - # (operator, node, element, configdata, self.resultqueue)) + self.gpile.spawn(perform_request, operator, node, element, configdata) def __iter__(self): return self def next(self): - pass + ndata = self.gpile.next() + # need to apply any translations between pyghmi and confluent + return ndata -def perform_request(operator, node, element, configdata, queue): - IpmiHandler(operator, node, element, configdata).handle_request() +def perform_request(operator, node, element, configdata): + return IpmiHandler(operator, node, element, configdata).handle_request() class IpmiHandler(object):