+def verify(constraints, data):
+ """
+ constraints is a list of key, value pairs.
+ # [ {... : ...}==AND , ... , ... , ] == OR
+ """
+ con_or_true = False
+ for con in constraints:
+ #print "con: %s" % con
+ if len(con.keys()) == 0:
+ con_and_true = False
+ else:
+ con_and_true = True
+
+ for key in con.keys():
+ #print "looking at key: %s" % key
+ if key in data:
+ value_re = re.compile(con[key])
+ con_and_true = con_and_true & (value_re.search(data[key]) is not None)
+ elif key not in data:
+ print "missing key %s" % key
+ con_and_true = False
+
+ con_or_true = con_or_true | con_and_true
+
+ return con_or_true
+
+def query_to_dict(query):
+
+ ad = []
+
+ or_queries = query.split('||')
+ for or_query in or_queries:
+ and_queries = or_query.split('&&')
+
+ d = {}
+
+ for and_query in and_queries:
+ (key, value) = and_query.split('=')
+ d[key] = value
+
+ ad.append(d)
+
+ return ad
+
+def _pcu_in(fbdata):
+ if 'plcnode' in fbdata:
+ if 'pcu_ids' in fbdata['plcnode']:
+ if len(fbdata['plcnode']['pcu_ids']) > 0:
+ return True
+ return False
+
+def pcu_select(str_query):
+ pcunames = []
+ if str_query is None: return pcunames
+
+ #print str_query
+ dict_query = query_to_dict(str_query)
+ #print dict_query
+
+ for node in fb['nodes'].keys():
+
+ fb_nodeinfo = fb['nodes'][node]['values']
+ if _pcu_in(fb_nodeinfo):
+ pcuinfo = fbpcu['nodes']['id_%s' % fb_nodeinfo['plcnode']['pcu_ids'][0]]['values']
+ if verify(dict_query, pcuinfo):
+ pcunames.append(node)
+
+ return pcunames
+
+def node_select(str_query):
+ hostnames = []
+ if str_query is None: return hostnames
+
+ #print str_query
+ dict_query = query_to_dict(str_query)
+ #print dict_query
+
+ for node in fb['nodes'].keys():
+
+ fb_nodeinfo = fb['nodes'][node]['values']
+
+ if verify(dict_query, fb_nodeinfo):
+ #print node #fb_nodeinfo
+ hostnames.append(node)
+ else:
+ #print "NO MATCH", node
+ pass
+
+ return hostnames
+
+
+def main():
+ from config import config
+ from optparse import OptionParser
+ parser = OptionParser()
+ parser.set_defaults(node=None, select=None, pcuselect=None, nodelist=None, daysdown=None)
+ parser.add_option("", "--daysdown", dest="daysdown", action="store_true",
+ help="List the node state and days down...")
+ parser.add_option("", "--select", dest="select", metavar="key=value",
+ help="List all nodes with the given key=value pattern")
+ parser.add_option("", "--pcuselect", dest="pcuselect", metavar="key=value",
+ help="List all nodes with the given key=value pattern")
+ parser.add_option("", "--nodelist", dest="nodelist", metavar="nodelist.txt",
+ help="A list of nodes to bring out of debug mode.")
+ config = config(parser)
+ config.parse_args()
+
+ if config.nodelist:
+ nodelist = config.getListFromFile(config.nodelist)
+ elif config.select is not None:
+ nodelist = node_select(config.select)
+ elif config.pcuselect is not None:
+ nodelist = pcu_select(config.pcuselect)
+ else:
+ nodelist = fb['nodes'].keys()