3 # This script is used to manipulate the operational state of nodes in
4 # different node groups. These are basically set operations on nodes via the
7 # Take the ng name as an argument....
10 # * Set some or all in the set to rins.
11 # * get a list of nodes in the Alpha nodegroup.
13 # Given a nodelist, it could tag each one with a nodegroup name.
18 api = plc.PLC(auth.auth, auth.plc)
20 from config import config
21 from optparse import OptionParser
24 from nodecommon import *
26 fb = soltesz.dbLoad("findbad")
28 parser = OptionParser()
29 parser.set_defaults(nodegroup="Alpha",
37 parser.add_option("", "--not", dest="notng", action="store_true",
38 help="All nodes NOT in nodegroup.")
39 parser.add_option("", "--nodegroup", dest="nodegroup", metavar="NodegroupName",
40 help="Specify a nodegroup to perform actions on")
42 parser.add_option("", "--list", dest="list", action="store_true",
43 help="List all nodes in the given nodegroup")
44 parser.add_option("", "--add", dest="add", action="store_true",
45 help="Add nodes to the given nodegroup")
46 parser.add_option("", "--delete", dest="delete", action="store_true",
47 help="Delete nodes from the given nodegroup")
48 parser.add_option("", "--node", dest="node", metavar="nodename.edu",
49 help="A single node name to add to the nodegroup")
50 parser.add_option("", "--nodelist", dest="nodelist", metavar="list.txt",
51 help="Use all nodes in the given file for operation.")
52 config = config(parser)
55 # COLLECT nodegroups, nodes and node lists
56 if config.node or config.nodelist:
58 hostlist = [ config.node ]
60 hostlist = config.getListFromFile(config.nodelist)
61 nodelist = api.GetNodes(hostlist)
66 ng = api.GetNodeGroups({'name' : config.nodegroup})
67 nodelist = api.GetNodes(ng[0]['node_ids'])
69 group_str = config.nodegroup
76 all_nodes = api.GetNodes({'peer_id': None})
78 # remove ngnodes from all node list
79 ng_list = [ x['hostname'] for x in ng_nodes ]
80 all_list = [ x['hostname'] for x in all_nodes ]
81 not_ng_nodes = Set(all_list) - Set(ng_list)
83 # keep each node that *is* in the not_ng_nodes set
84 nodelist = filter(lambda x : x['hostname'] in not_ng_nodes, all_nodes)
86 hostnames = [ n['hostname'] for n in nodelist ]
90 print " ---- Nodes in the %s Node Group ----" % group_str
94 print nodegroup_display(node, fb)
97 elif config.add and config.nodegroup:
98 for node in hostnames:
99 print "Adding %s to %s nodegroup" % (node, config.nodegroup)
100 api.AddNodeToNodeGroup(node, config.nodegroup)
103 for node in hostnames:
104 print "Deleting %s from %s nodegroup" % (node, config.nodegroup)
105 api.DeleteNodeFromNodeGroup(node, config.nodegroup)
108 print "no other options supported."