Massive commit. Just put all local changes into svn.
[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 optparse import OptionParser
21 from sets import Set
22
23 from nodecommon import *
24 import soltesz
25
26 def main():
27         from config import config
28         fb = soltesz.dbLoad("findbad")
29
30         parser = OptionParser()
31         parser.set_defaults(nodegroup="Alpha",
32                                                 node=None,
33                                                 nodelist=None,
34                                                 list=False,
35                                                 add=False,
36                                                 notng=False,
37                                                 delete=False,
38                                                 )
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")
43
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)
55         config.parse_args()
56
57         # COLLECT nodegroups, nodes and node lists
58         if config.node or config.nodelist:
59                 if config.node: 
60                         hostlist = [ config.node ] 
61                 else: 
62                         hostlist = config.getListFromFile(config.nodelist)
63
64                 # NOTE: preserve order given in file.  Otherwise, return values are not in order
65                 # given to GetNodes
66                 nodelist = []
67                 for h in hostlist:
68                         nodelist += api.GetNodes(h)
69
70                 #nodelist = api.GetNodes(hostlist)
71                 group_str = "Given"
72
73         else:
74                 ng = api.GetNodeGroups({'name' : config.nodegroup})
75                 nodelist = api.GetNodes(ng[0]['node_ids'])
76
77                 group_str = config.nodegroup
78
79         if config.notng:
80                 # Get nodegroup nodes
81                 ng_nodes = nodelist
82
83                 # Get all nodes
84                 all_nodes = api.GetNodes({'peer_id': None})
85                 
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)
90
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)
93
94         hostnames = [ n['hostname'] for n in nodelist ]
95
96         # commands:
97         if config.list:
98                 print " ---- Nodes in the %s Node Group ----" % group_str
99                 i = 1
100                 for node in nodelist:
101                         print "%-2d" % i, 
102                         print nodegroup_display(node, fb)
103                         i += 1
104
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)
109
110         elif config.delete:
111                 for node in hostnames:
112                         print "Deleting %s from %s nodegroup" % (node, config.nodegroup)
113                         api.DeleteNodeFromNodeGroup(node, config.nodegroup)
114
115         else:
116                 print "no other options supported."
117
118 if __name__ == "__main__":
119         try:
120                 main()
121         except IOError:
122                 pass