X-Git-Url: http://git.onelab.eu/?a=blobdiff_plain;f=nodegroups.py;h=ecb9c08125bdde6c0422793a093e493952c51816;hb=17447cbba68069b0bf043f25ae75f86a0ccc26f7;hp=7ec1dde2df1a9d6bf01ceb966ca694eafad741b9;hpb=d931e6a9b64a42d0a946c741bf4fbd4b9c9f464b;p=monitor.git diff --git a/nodegroups.py b/nodegroups.py index 7ec1dde..ecb9c08 100755 --- a/nodegroups.py +++ b/nodegroups.py @@ -13,76 +13,122 @@ # Given a nodelist, it could tag each one with a nodegroup name. # * -import plc -import auth -api = plc.PLC(auth.auth, auth.plc) - -from config import config -from optparse import OptionParser - -import soltesz -fb = soltesz.dbLoad("findbad") - -def get_current_state(fbnode): - state = fbnode['state'] - l = state.lower() - if l == "debug": return 'dbg' - return l - -parser = OptionParser() -parser.set_defaults(nodegroup="Alpha", - node=None, - nodelist=None, - list=False, - add=False, - delete=False, - ) -parser.add_option("", "--nodegroup", dest="nodegroup", metavar="NodegroupName", - help="Specify a nodegroup to perform actions on") -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() - -# COLLECT nodegroups, nodes and node lists -ng = api.GetNodeGroups({'name' : config.nodegroup}) -nodelist = api.GetNodes(ng[0]['node_ids']) -hostnames = [ n['hostname'] for n in nodelist ] - -if config.node or config.nodelist: - if config.node: hostnames = [ config.node ] - else: hostnames = config.getListFromFile(config.nodelist) - -# commands: -if config.list: - print " ---- Nodes in the %s Node Group ----" % config.nodegroup - i = 0 - for node in nodelist: - print "%-2d" % i, - if node['hostname'] in fb['nodes']: - node['current'] = get_current_state(fb['nodes'][node['hostname']]['values']) +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 + +api = plc.getAuthAPI() + +from monitor.common import * +from sets import Set + +def main(): + + parser = parsermodule.getParser(['nodesets']) + parser.set_defaults( list=True, + add=False, + nocolor=False, + notng=False, + delete=False,) + + parser.add_option("", "--not", dest="notng", action="store_true", + help="All nodes NOT in nodegroup.") + 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 = 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 = 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.append( plccache.GetNodeByName(h) ) + + 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: - node['current'] = 'none' - print "%(hostname)-38s %(boot_state)5s %(current)5s %(key)s" % node - i += 1 - -elif config.add: - for node in hostnames: - print "Adding %s to %s nodegroup" % (config.node, config.nodegroup) - api.AddNodeToNodeGroup(config.node, config.nodegroup) - -elif config.delete: - for node in hostnames: - print "Deleting %s from %s nodegroup" % (config.node, config.nodegroup) - api.DeleteNodeFromNodeGroup(config.node, config.nodegroup) - -else: - print "no other options supported." + nodelist = [] + + group_str = config.site + + elif config.nodeselect: + hostlist = query.node_select(config.nodeselect) + nodelist = [ plccache.GetNodeByName(h) for h in hostlist ] + + group_str = "selection" + + else: + ng = api.GetNodeGroups({'name' : config.nodegroup}) + nodelist = plccache.GetNodesByIds(ng[0]['node_ids']) + + group_str = config.nodegroup + + if config.notng: + # Get nodegroup nodes + ng_nodes = nodelist + + # Get all nodes + all_nodes = plccache.l_nodes + + # remove ngnodes from all node list + ng_list = [ x['hostname'] for x in ng_nodes ] + all_list = [ x['hostname'] for x in all_nodes ] + not_ng_nodes = Set(all_list) - Set(ng_list) + + # keep each node that *is* in the not_ng_nodes set + nodelist = filter(lambda x : x['hostname'] in not_ng_nodes, all_nodes) + + hostnames = [ n['hostname'] for n in nodelist ] + + # commands: + + if config.add and config.nodegroup: + for node in hostnames: + print "Adding %s to %s nodegroup" % (node, config.nodegroup) + api.AddNodeToNodeGroup(node, config.nodegroup) + + elif config.delete: + for node in hostnames: + 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." + +if __name__ == "__main__": + try: + main() + except IOError: + pass