mass commit
[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 from sets import Set
23
24 from nodecommon import *
25 import soltesz
26 fb = soltesz.dbLoad("findbad")
27
28 parser = OptionParser()
29 parser.set_defaults(nodegroup="Alpha",
30                                         node=None,
31                                         nodelist=None,
32                                         list=False,
33                                         add=False,
34                                         notng=False,
35                                         delete=False,
36                                         )
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")
41
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)
53 config.parse_args()
54
55 # COLLECT nodegroups, nodes and node lists
56 if config.node or config.nodelist:
57         if config.node: 
58                 hostlist = [ config.node ] 
59         else: 
60                 hostlist = config.getListFromFile(config.nodelist)
61         nodelist = api.GetNodes(hostlist)
62
63         group_str = "Given"
64
65 else:
66         ng = api.GetNodeGroups({'name' : config.nodegroup})
67         nodelist = api.GetNodes(ng[0]['node_ids'])
68
69         group_str = config.nodegroup
70
71 if config.notng:
72         # Get nodegroup nodes
73         ng_nodes = nodelist
74
75         # Get all nodes
76         all_nodes = api.GetNodes({'peer_id': None})
77         
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)
82
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)
85
86 hostnames = [ n['hostname'] for n in nodelist ]
87
88 # commands:
89 if config.list:
90         print " ---- Nodes in the %s Node Group ----" % group_str
91         i = 1
92         for node in nodelist:
93                 print "%-2d" % i, 
94                 print nodegroup_display(node, fb)
95                 i += 1
96
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)
101
102 elif config.delete:
103         for node in hostnames:
104                 print "Deleting %s from %s nodegroup" % (node, config.nodegroup)
105                 api.DeleteNodeFromNodeGroup(node, config.nodegroup)
106
107 else:
108         print "no other options supported."