From mboxrd@z Thu Jan 1 00:00:00 1970 From: kupcevic@sourceware.org Date: 10 Oct 2006 21:07:19 -0000 Subject: [Cluster-devel] conga/luci/site/luci/Extensions ricci_communic ... Message-ID: <20061010210719.29480.qmail@sourceware.org> List-Id: To: cluster-devel.redhat.com MIME-Version: 1.0 Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: 7bit CVSROOT: /cvs/cluster Module name: conga Changes by: kupcevic at sourceware.org 2006-10-10 21:07:18 Modified files: luci/site/luci/Extensions: ricci_communicator.py Log message: luci: helper functions to process ricci's response Patches: http://sourceware.org/cgi-bin/cvsweb.cgi/conga/luci/site/luci/Extensions/ricci_communicator.py.diff?cvsroot=cluster&r1=1.6&r2=1.7 --- conga/luci/site/luci/Extensions/ricci_communicator.py 2006/09/29 21:41:43 1.6 +++ conga/luci/site/luci/Extensions/ricci_communicator.py 2006/10/10 21:07:18 1.7 @@ -162,6 +162,9 @@ return batch_node + + + def __send(self, xml_doc): buff = xml_doc.toxml() + '\n' #print buff @@ -228,3 +231,113 @@ return ricci.auth(password) def ricci_unauthenticate(self, ricci): return ricci.unauth() + + + + + + + + +########## helpers to process batch as returned by ricci ############# + + + + + +# check the status of batch +# returns (int num, int total) +# * total: +# total number of modules in batch +# * num: +# if num == total: +# all modules in the batch completed successfuly +# if num > 0: +# last seq. number of module that successfuly finished +# if num < 0: +# module (-num) failed (next module won't be processed) +def batch_status(batch_xml): + if batch_xml.nodeName != 'batch': + raise 'not a batch' + total = 0 + last = 0 + for node in batch_xml.childNodes: + if node.nodeType == xml.dom.Node.ELEMENT_NODE: + if node.nodeName == 'module': + total = total + 1 + status = node.getAttribute('status') + if status == '0': + # success + last = last + 1 + elif status == '3' or status == '4': + # failure + last = last + 1 + last = last - 2 * last + return (last, total) + + + +# extract error_code from module's response +# * module_num: +# 1-based seq. number of module to process +# +# returns (int error_code, string error_msg) +# * error_code: each module defines own error codes, which are >0 +# -101 - in progress +# -102 - scheduled +# -103 - removed from schedule +# -104 - failed to execute module +# +# >-3 - module executed. Following codes are defined: +# -2 - API error +# -1 - undefined error occured (msg not necesarily very informative) +# 0 - no error (msg is empty string) +# >0 - predefined error has occured +# (check respective API, msg will be fully descriptive) +# * error_msg: error message +def extract_module_status(batch_xml, module_num=1): + if batch_xml.nodeName != 'batch': + raise 'not a batch' + c = 0 + for node in batch_xml.childNodes: + if node.nodeType == xml.dom.Node.ELEMENT_NODE: + if node.nodeName == 'module': + module_xml = node + c = c + 1 + if c == module_num: + status = module_xml.getAttribute('status') + if status == '0' or status == '4': + # module executed, dig deeper into request + for node_i in module_xml.childNodes: + if node_i.nodeType == xml.dom.Node.ELEMENT_NODE: + if node_i.nodeName == 'API_error': + return -2, 'API error' + elif node_i.nodeName == 'response': + for node_j in node_i.childNodes: + if node_j.nodeType == xml.dom.Node.ELEMENT_NODE: + if node_j.nodeName == 'function_response': + code = -11111111 + msg = 'BUG' + for var in node_j.childNodes: + if var.nodeType == xml.dom.Node.ELEMENT_NODE: + if var.nodeName == 'var': + if var.getAttribute('name') == 'success' and var.getAttribute('value') == 'true': + return 0, '' + elif var.getAttribute('name') == 'error_code': + code = int(var.getAttribute('value')) + elif var.getAttribute('name') == 'error_description': + msg = var.getAttribute('value') + return code, msg + + pass + elif status == '1': + return -102, 'module scheduled for execution' + elif status == '2': + return -101, 'module is being executed' + elif status == '3': + return -104, 'failed to locate/execute module' + elif status == '5': + return -103, 'module removed from schedule' + + raise 'no ' + str(module_num) + 'th module in the batch, or malformed response' +