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.
16 from monitor import database
17 from monitor.database.info.model import FindbadNodeRecord
18 from monitor import util
19 from monitor.wrapper import plc
20 from monitor import parser as parsermodule
22 api = plc.getAuthAPI()
24 from nodequery import verify,query_to_dict,node_select
25 from monitor.common import *
30 parser = parsermodule.getParser(['nodesets'])
31 parser.set_defaults( list=True,
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",
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")
48 parser = parsermodule.getParser(['defaults'], parser)
49 config = parsermodule.parse_args(parser)
51 # COLLECT nodegroups, nodes and node lists
52 if config.node or config.nodelist:
54 hostlist = [ config.node ]
56 hostlist = util.file.getListFromFile(config.nodelist)
58 # NOTE: preserve order given in file. Otherwise, return values are not in order
62 nodelist.append( plccache.GetNodeByName(h) )
67 site = plccache.GetSitesByName([config.site])
70 nodelist = plccache.GetNodesByIds(site['node_ids'])
74 group_str = config.site
76 elif config.nodeselect:
77 hostlist = node_select(config.nodeselect)
78 nodelist = [ plccache.GetNodeByName(h) for h in hostlist ]
80 group_str = "selection"
83 ng = api.GetNodeGroups({'name' : config.nodegroup})
84 nodelist = plccache.GetNodesByIds(ng[0]['node_ids'])
86 group_str = config.nodegroup
93 all_nodes = plccache.l_nodes
95 # remove ngnodes from all node list
96 ng_list = [ x['hostname'] for x in ng_nodes ]
97 all_list = [ x['hostname'] for x in all_nodes ]
98 not_ng_nodes = Set(all_list) - Set(ng_list)
100 # keep each node that *is* in the not_ng_nodes set
101 nodelist = filter(lambda x : x['hostname'] in not_ng_nodes, all_nodes)
103 hostnames = [ n['hostname'] for n in nodelist ]
107 if config.add and config.nodegroup:
108 for node in hostnames:
109 print "Adding %s to %s nodegroup" % (node, config.nodegroup)
110 api.AddNodeToNodeGroup(node, config.nodegroup)
113 for node in hostnames:
114 print "Deleting %s from %s nodegroup" % (node, config.nodegroup)
115 api.DeleteNodeFromNodeGroup(node, config.nodegroup)
118 print " ---- Nodes in the %s Node Group ----" % group_str
119 print " Hostname plc obs pcu key kernel last_contact, last change, comon uptime"
121 for node in nodelist:
123 fbrec = FindbadNodeRecord.get_latest_by(hostname=node['hostname'])
124 fbdata = fbrec.to_dict()
125 print nodegroup_display(node, fbdata, config)
129 print "no other options supported."
131 if __name__ == "__main__":