From mboxrd@z Thu Jan 1 00:00:00 1970 From: rmccabe@sourceware.org Date: 8 Aug 2007 21:16:36 -0000 Subject: [Cluster-devel] conga/luci/site/luci/Extensions LuciZopeAsync.py Message-ID: <20070808211636.32681.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 Branch: RHEL5 Changes by: rmccabe at sourceware.org 2007-08-08 21:16:36 Modified files: luci/site/luci/Extensions: LuciZopeAsync.py Log message: Fix 230451, pass 3 - luci backend support for managing fence_xvm keys, pass 2 Patches: http://sourceware.org/cgi-bin/cvsweb.cgi/conga/luci/site/luci/Extensions/LuciZopeAsync.py.diff?cvsroot=cluster&only_with_tag=RHEL5&r1=1.1.2.1&r2=1.1.2.2 --- conga/luci/site/luci/Extensions/LuciZopeAsync.py 2007/08/08 21:14:38 1.1.2.1 +++ conga/luci/site/luci/Extensions/LuciZopeAsync.py 2007/08/08 21:16:35 1.1.2.2 @@ -0,0 +1,182 @@ +# Copyright (C) 2007 Red Hat, Inc. +# +# This program is free software; you can redistribute +# it and/or modify it under the terms of version 2 of the +# GNU General Public License as published by the +# Free Software Foundation. + +from xml.dom import minidom + +from LuciSyslog import get_logger +from LuciZope import GetReqVars +from ricci_communicator import RicciCommunicator +from conga_constants import LUCI_DEBUG_MODE + +luci_log = get_logger() + +def write_xml_resp(request, xml_obj): + request.RESPONSE.setHeader('Content-Type', 'text/xml; charset=UTF-8') + request.RESPONSE.setHeader('Cache-Control', 'no-cache, no-store, private') + request.RESPONSE.write(str(xml_obj.toprettyxml())) + +def result_to_xml(result): + import types + + numeric_types = [ + types.IntType, types.BooleanType, types.LongType, types.FloatType + ] + + root = minidom.Document() + + def pyobj_to_xml(element_name, element, parent_node): + if type(element) is types.DictType: + if len(element) > 0: + xml_elem = root.createElement('dict') + xml_elem.setAttribute('name', str(element_name)) + + for i in element.iterkeys(): + pyobj_to_xml(i, element[i], xml_elem) + else: + xml_elem = None + elif type(element) in [ types.ListType, types.TupleType ]: + if len(element) > 0: + xml_elem = root.createElement('list') + xml_elem.setAttribute('name', str(element_name)) + for i in element: + pyobj_to_xml(element_name, i, xml_elem) + else: + xml_elem = None + else: + cur_tagname = None + try: + if parent_node.tagName == 'list': + cur_tagname = parent_node.getAttribute('name') + except: + cur_tagname = None + + if not cur_tagname: + xml_elem = root.createElement('var') + else: + xml_elem = root.createElement(cur_tagname) + + if type(element) in types.StringTypes: + cur_type = 'str' + elif type(element) in numeric_types: + cur_type = 'num' + else: + cur_type = None + + if cur_type: + try: + if parent_node.tagName == 'dict': + xml_elem.setAttribute('name', str(element_name)) + except: + pass + + xml_elem.setAttribute('type', cur_type) + xml_elem.setAttribute('value', str(element)) + else: + xml_elem = None + + if xml_elem is not None: + parent_node.appendChild(xml_elem) + + pyobj_to_xml('result', result[1], root) + res_elem = root.createElement('result') + res_elem.setAttribute('name', 'success') + res_elem.setAttribute('value', str(result[0]).lower()) + root.firstChild.appendChild(res_elem) + return root + +def write_err_async(request, err_msg): + xml_obj = result_to_xml((False, { 'errors': err_msg })) + write_xml_resp(request, xml_obj) + +def get_cluster_nodes_async(self, request): + from LuciClusterInfo import getClusterConfNodes + from RicciQueries import getClusterConf + + fvars = GetReqVars(request, [ 'QUERY_STRING' ]) + if fvars['QUERY_STRING'] is None: + if LUCI_DEBUG_MODE is True: + luci_log.debug_verbose('GCNA: No query string was given') + write_err_async(request, 'No node names were given') + return None + + try: + nodes = fvars['QUERY_STRING'].split('&') + node_list = map(lambda x: x[1], filter(lambda x: x[0][:4] == 'node', map(lambda x: x.split('='), nodes))) + if not node_list or len(node_list) < 1: + raise Exception, 'No node list' + except Exception, e: + if LUCI_DEBUG_MODE is True: + luci_log.debug_verbose('GCNA: %r %s' % (e, str(e))) + write_err_async(request, 'No node names were given') + return None + + errors = list() + ret = {} + for node_host in node_list: + try: + rc = RicciCommunicator(node_host) + cluster_name = rc.cluster_info()[0] + if not cluster_name: + errors.append('%s is not a member of a cluster' \ + % cluster_name) + continue + except Exception, e: + if LUCI_DEBUG_MODE is True: + luci_log.debug_verbose('GCNA0: ricci: %s: %r %s' \ + % (node_host, e, str(e))) + errors.append('Unable to communicate with the ricci agent on %s' \ + % node_host) + continue + + try: + conf = getClusterConf(rc) + node_names = getClusterConfNodes(conf) + if not node_names or len(node_names) < 1: + raise Exception, 'no nodes' + except Exception, e: + if LUCI_DEBUG_MODE is True: + luci_log.debug_verbose('GCNA1: ricci: %s: %r %s' \ + % (node_host, e, str(e))) + errors.append('Unable to retrieve a list of cluster nodes from %s' \ + % node_host) + continue + ret[cluster_name] = { + 'cluster': cluster_name, + 'num_nodes': len(node_names), + 'clusternode': node_names + } + + ret['errors'] = errors + xml_obj = result_to_xml((len(errors) < len(node_list), ret)) + write_xml_resp(request, xml_obj) + +def get_sysinfo_async(self, request): + from HelperFunctions import get_system_info + + fvars = GetReqVars(request, [ 'QUERY_STRING' ]) + try: + nodes = fvars['QUERY_STRING'].split('&') + node_list = map(lambda x: x[1], filter(lambda x: x[0][:4] == 'node', map(lambda x: x.split('='), nodes))) + if not node_list or len(node_list) < 1: + raise Exception, 'No node list' + except Exception, e: + if LUCI_DEBUG_MODE is True: + luci_log.debug_verbose('GSIA: %r %s' % (e, str(e))) + write_err_async(request, 'No node names were given') + return None + + ret = {} + try: + ret = get_system_info(self, node_list) + except Exception, e: + if LUCI_DEBUG_MODE is True: + luci_log.debug_verbose('GNFPA %r: %r %s' \ + % (request['nodenames'], e, str(e))) + write_err_async(request, 'Error retrieving information') + return None + xml_obj = result_to_xml(True, { 'result': ret }) + write_xml_resp(request, xml_obj)