add simple command line tools for manipulating node groups, and for querying
[monitor.git] / nodegroups.py
1 #!/usr/bin/python
2
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
5 # PLC api.
6
7 # Take the ng name as an argument....
8 # optionally, 
9 #  * restart them all.
10 #  * Set some or all in the set to rins.
11 #  * get a list of nodes in the Alpha nodegroup.
12
13 # Given a nodelist, it could tag each one with a nodegroup name.
14 #  * 
15
16 import plc
17 import auth
18 api = plc.PLC(auth.auth, auth.plc)
19
20 from config import config
21 from optparse import OptionParser
22
23 parser = OptionParser()
24 parser.set_defaults(nodegroup="Alpha",
25                                         node=None,
26                                         nodelist=None,
27                                         list=False,
28                                         add=False,
29                                         delete=False,
30                                         )
31 parser.add_option("", "--nodegroup", dest="nodegroup", metavar="NodegroupName",
32                                         help="Specify a nodegroup to perform actions on")
33 parser.add_option("", "--list", dest="list", action="store_true", 
34                                         help="List all nodes in the given nodegroup")
35 parser.add_option("", "--add", dest="add", action="store_true", 
36                                         help="Add nodes to the given nodegroup")
37 parser.add_option("", "--delete", dest="delete", action="store_true", 
38                                         help="Delete nodes from the given nodegroup")
39 parser.add_option("", "--node", dest="node", metavar="nodename.edu", 
40                                         help="A single node name to add to the nodegroup")
41 parser.add_option("", "--nodelist", dest="nodelist", metavar="list.txt", 
42                                         help="Use all nodes in the given file for operation.")
43 config = config(parser)
44 config.parse_args()
45
46 # COLLECT nodegroups, nodes and node lists
47 ng = api.GetNodeGroups({'name' : config.nodegroup})
48 nodelist = api.GetNodes(ng[0]['node_ids'])
49 hostnames = [ n['hostname'] for n in nodelist ]
50
51 if config.node or config.nodelist:
52         if config.node: hostnames = [ config.node ] 
53         else: hostnames = config.getListFromFile(config.nodelist)
54
55 # commands:
56 if config.list:
57         print " ---- Nodes in the %s Node Group ----" % config.nodegroup
58         i = 0
59         for node in nodelist:
60                 print "%-2d" % i, 
61                 print "%(hostname)-38s %(boot_state)5s %(key)s" % node
62                 i += 1
63
64 elif config.add:
65         for node in hostnames:
66                 print "Adding %s to %s nodegroup" % (config.node, config.nodegroup)
67                 api.AddNodeToNodeGroup(config.node, config.nodegroup)
68
69 elif config.delete:
70         for node in hostnames:
71                 print "Deleting %s from %s nodegroup" % (config.node, config.nodegroup)
72                 api.DeleteNodeFromNodeGroup(config.node, config.nodegroup)
73
74 else:
75         print "no other options supported."