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 optparse import OptionParser
23 from nodecommon import *
27 from config import config
28 fb = soltesz.dbLoad("findbad")
30 parser = OptionParser()
31 parser.set_defaults(nodegroup="Alpha",
39 parser.add_option("", "--not", dest="notng", action="store_true",
40 help="All nodes NOT in nodegroup.")
41 parser.add_option("", "--nodegroup", dest="nodegroup", metavar="NodegroupName",
42 help="Specify a nodegroup to perform actions on")
44 parser.add_option("", "--list", dest="list", action="store_true",
45 help="List all nodes in the given nodegroup")
46 parser.add_option("", "--add", dest="add", action="store_true",
47 help="Add nodes to the given nodegroup")
48 parser.add_option("", "--delete", dest="delete", action="store_true",
49 help="Delete nodes from the given nodegroup")
50 parser.add_option("", "--node", dest="node", metavar="nodename.edu",
51 help="A single node name to add to the nodegroup")
52 parser.add_option("", "--nodelist", dest="nodelist", metavar="list.txt",
53 help="Use all nodes in the given file for operation.")
54 config = config(parser)
57 # COLLECT nodegroups, nodes and node lists
58 if config.node or config.nodelist:
60 hostlist = [ config.node ]
62 hostlist = config.getListFromFile(config.nodelist)
64 # NOTE: preserve order given in file. Otherwise, return values are not in order
68 nodelist += api.GetNodes(h)
70 #nodelist = api.GetNodes(hostlist)
74 ng = api.GetNodeGroups({'name' : config.nodegroup})
75 nodelist = api.GetNodes(ng[0]['node_ids'])
77 group_str = config.nodegroup
84 all_nodes = api.GetNodes({'peer_id': None})
86 # remove ngnodes from all node list
87 ng_list = [ x['hostname'] for x in ng_nodes ]
88 all_list = [ x['hostname'] for x in all_nodes ]
89 not_ng_nodes = Set(all_list) - Set(ng_list)
91 # keep each node that *is* in the not_ng_nodes set
92 nodelist = filter(lambda x : x['hostname'] in not_ng_nodes, all_nodes)
94 hostnames = [ n['hostname'] for n in nodelist ]
98 print " ---- Nodes in the %s Node Group ----" % group_str
100 for node in nodelist:
102 print nodegroup_display(node, fb)
105 elif config.add and config.nodegroup:
106 for node in hostnames:
107 print "Adding %s to %s nodegroup" % (node, config.nodegroup)
108 api.AddNodeToNodeGroup(node, config.nodegroup)
111 for node in hostnames:
112 print "Deleting %s from %s nodegroup" % (node, config.nodegroup)
113 api.DeleteNodeFromNodeGroup(node, config.nodegroup)
116 print "no other options supported."
118 if __name__ == "__main__":