X-Git-Url: http://git.onelab.eu/?a=blobdiff_plain;f=nodegroups.py;h=dee3cec6a9b1b3a7778a56d6e56b024b182b684f;hb=2d1c0c5c0bf2fb15651dcfd813ae68bff4b80535;hp=725d0e0317ef44f6dea97075a87fe05a412ab510;hpb=f38420ff4bc9fb114db5f62b01cc8990dc0a0af2;p=monitor.git diff --git a/nodegroups.py b/nodegroups.py index 725d0e0..dee3cec 100755 --- a/nodegroups.py +++ b/nodegroups.py @@ -13,66 +13,75 @@ # Given a nodelist, it could tag each one with a nodegroup name. # * -import plc -import auth -api = plc.PLC(auth.auth, auth.plc) +from monitor import database +from monitor.database.info.model import FindbadNodeRecord +from monitor import util +from monitor.wrapper import plc +from monitor import parser as parsermodule -from optparse import OptionParser -from sets import Set +api = plc.getAuthAPI() -from nodecommon import * -import soltesz +from monitor.query import verify,query_to_dict,node_select +from monitor.common import * +from sets import Set def main(): - from config import config - fb = soltesz.dbLoad("findbad") - - parser = OptionParser() - parser.set_defaults(nodegroup="Alpha", - node=None, - nodelist=None, - list=False, + + parser = parsermodule.getParser(['nodesets']) + parser.set_defaults( list=True, add=False, + nocolor=False, notng=False, - delete=False, - ) + delete=False,) + parser.add_option("", "--not", dest="notng", action="store_true", help="All nodes NOT in nodegroup.") - parser.add_option("", "--nodegroup", dest="nodegroup", metavar="NodegroupName", - help="Specify a nodegroup to perform actions on") - + parser.add_option("", "--nocolor", dest="nocolor", action="store_true", + help="Enable color") parser.add_option("", "--list", dest="list", action="store_true", help="List all nodes in the given nodegroup") parser.add_option("", "--add", dest="add", action="store_true", help="Add nodes to the given nodegroup") parser.add_option("", "--delete", dest="delete", action="store_true", help="Delete nodes from the given nodegroup") - parser.add_option("", "--node", dest="node", metavar="nodename.edu", - help="A single node name to add to the nodegroup") - parser.add_option("", "--nodelist", dest="nodelist", metavar="list.txt", - help="Use all nodes in the given file for operation.") - config = config(parser) - config.parse_args() + + parser = parsermodule.getParser(['defaults'], parser) + config = parsermodule.parse_args(parser) # COLLECT nodegroups, nodes and node lists if config.node or config.nodelist: if config.node: hostlist = [ config.node ] else: - hostlist = config.getListFromFile(config.nodelist) + hostlist = util.file.getListFromFile(config.nodelist) # NOTE: preserve order given in file. Otherwise, return values are not in order # given to GetNodes nodelist = [] for h in hostlist: - nodelist += api.GetNodes(h) + nodelist.append( plccache.GetNodeByName(h) ) - #nodelist = api.GetNodes(hostlist) group_str = "Given" + elif config.site: + site = plccache.GetSitesByName([config.site]) + if len (site) > 0: + site = site[0] + nodelist = plccache.GetNodesByIds(site['node_ids']) + else: + nodelist = [] + + group_str = config.site + + elif config.nodeselect: + hostlist = node_select(config.nodeselect) + nodelist = [ plccache.GetNodeByName(h) for h in hostlist ] + + group_str = "selection" + else: ng = api.GetNodeGroups({'name' : config.nodegroup}) - nodelist = api.GetNodes(ng[0]['node_ids']) + nodelist = plccache.GetNodesByIds(ng[0]['node_ids']) group_str = config.nodegroup @@ -81,7 +90,7 @@ def main(): ng_nodes = nodelist # Get all nodes - all_nodes = api.GetNodes({'peer_id': None}) + all_nodes = plccache.l_nodes # remove ngnodes from all node list ng_list = [ x['hostname'] for x in ng_nodes ] @@ -94,15 +103,8 @@ def main(): hostnames = [ n['hostname'] for n in nodelist ] # commands: - if config.list: - print " ---- Nodes in the %s Node Group ----" % group_str - i = 1 - for node in nodelist: - print "%-2d" % i, - print nodegroup_display(node, fb) - i += 1 - elif config.add and config.nodegroup: + if config.add and config.nodegroup: for node in hostnames: print "Adding %s to %s nodegroup" % (node, config.nodegroup) api.AddNodeToNodeGroup(node, config.nodegroup) @@ -112,6 +114,17 @@ def main(): print "Deleting %s from %s nodegroup" % (node, config.nodegroup) api.DeleteNodeFromNodeGroup(node, config.nodegroup) + elif config.list: + print " ---- Nodes in the %s Node Group ----" % group_str + print " Hostname plc obs pcu key kernel last_contact, last change, comon uptime" + i = 1 + for node in nodelist: + print "%-2d" % i, + fbrec = FindbadNodeRecord.get_latest_by(hostname=node['hostname']) + fbdata = fbrec.to_dict() + print nodegroup_display(node, fbdata, config) + i += 1 + else: print "no other options supported."