changes for 3.0
[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 api = plc.getAuthAPI()
18
19 import parser as parsermodule
20 from sets import Set
21 from nodequery import verify,query_to_dict,node_select
22
23 from nodecommon import *
24 import database
25 import util.file
26
27 def main():
28         fb = database.dbLoad("findbad")
29
30         parser = parsermodule.getParser(['nodesets'])
31         parser.set_defaults( list=True,
32                                                 add=False,
33                         nocolor=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("", "--nocolor", dest="nocolor", action="store_true", 
40                                                 help="Enable color")
41         parser.add_option("", "--list", dest="list", action="store_true", 
42                                                 help="List all nodes in the given nodegroup")
43         parser.add_option("", "--add", dest="add", action="store_true", 
44                                                 help="Add nodes to the given nodegroup")
45         parser.add_option("", "--delete", dest="delete", action="store_true", 
46                                                 help="Delete nodes from the given nodegroup")
47
48         parser = parsermodule.getParser(['defaults'], parser)
49         config = parsermodule.parse_args(parser)
50
51         # COLLECT nodegroups, nodes and node lists
52         if config.node or config.nodelist:
53                 if config.node: 
54                         hostlist = [ config.node ] 
55                 else: 
56                         hostlist = util.file.getListFromFile(config.nodelist)
57
58                 # NOTE: preserve order given in file.  Otherwise, return values are not in order
59                 # given to GetNodes
60                 nodelist = []
61                 for h in hostlist:
62                         nodelist += api.GetNodes(h)
63
64                 #nodelist = api.GetNodes(hostlist)
65                 group_str = "Given"
66
67         elif config.site:
68                 site = api.GetSites(config.site)
69                 if len (site) > 0:
70                         site = site[0]
71                         nodelist = api.GetNodes(site['node_ids'])
72                 else:
73                         nodelist = []
74
75                 group_str = config.site
76
77         elif config.nodeselect:
78                 hostlist = node_select(config.nodeselect)
79                 nodelist = api.GetNodes(hostlist)
80
81                 group_str = "selection"
82                 
83         else:
84                 ng = api.GetNodeGroups({'grouname' : config.nodegroup})
85                 nodelist = api.GetNodes(ng[0]['node_ids'])
86
87                 group_str = config.nodegroup
88
89         if config.notng:
90                 # Get nodegroup nodes
91                 ng_nodes = nodelist
92
93                 # Get all nodes
94                 all_nodes = api.GetNodes({'peer_id': None})
95                 
96                 # remove ngnodes from all node list
97                 ng_list = [ x['hostname'] for x in ng_nodes ]
98                 all_list = [ x['hostname'] for x in all_nodes ]
99                 not_ng_nodes = Set(all_list) - Set(ng_list)
100
101                 # keep each node that *is* in the not_ng_nodes set
102                 nodelist = filter(lambda x : x['hostname'] in not_ng_nodes, all_nodes)
103
104         hostnames = [ n['hostname'] for n in nodelist ]
105
106         # commands:
107
108         if config.add and config.nodegroup:
109                 for node in hostnames:
110                         print "Adding %s to %s nodegroup" % (node, config.nodegroup)
111                         api.AddNodeToNodeGroup(node, config.nodegroup)
112
113         elif config.delete:
114                 for node in hostnames:
115                         print "Deleting %s from %s nodegroup" % (node, config.nodegroup)
116                         api.DeleteNodeFromNodeGroup(node, config.nodegroup)
117
118         elif config.list:
119                 print " ---- Nodes in the %s Node Group ----" % group_str
120                 print "   Hostname                                   plc  obs     pcu     key         kernel                        last_contact, last change, comon uptime"
121                 i = 1
122                 for node in nodelist:
123                         print "%-2d" % i, 
124                         print nodegroup_display(node, fb, config)
125                         i += 1
126
127         else:
128                 print "no other options supported."
129
130 if __name__ == "__main__":
131         try:
132                 main()
133         except IOError:
134                 pass