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.
17 api = plc.getAuthAPI()
19 from optparse import OptionParser
21 from nodequery import verify,query_to_dict,node_select
23 from nodecommon import *
27 from config import config
28 fb = database.dbLoad("findbad")
30 parser = OptionParser()
31 parser.set_defaults(nodegroup="Alpha",
41 parser.add_option("", "--not", dest="notng", action="store_true",
42 help="All nodes NOT in nodegroup.")
43 parser.add_option("", "--nodegroup", dest="nodegroup", metavar="NodegroupName",
44 help="Specify a nodegroup to perform actions on")
45 parser.add_option("", "--nodeselect", dest="nodeselect", metavar="querystring",
46 help="Specify a query to perform on findbad db")
47 parser.add_option("", "--site", dest="site", metavar="site name",
48 help="Specify a site to view node status")
50 parser.add_option("", "--nocolor", dest="nocolor", action="store_true",
52 parser.add_option("", "--list", dest="list", action="store_true",
53 help="List all nodes in the given nodegroup")
54 parser.add_option("", "--add", dest="add", action="store_true",
55 help="Add nodes to the given nodegroup")
56 parser.add_option("", "--delete", dest="delete", action="store_true",
57 help="Delete nodes from the given nodegroup")
58 parser.add_option("", "--node", dest="node", metavar="nodename.edu",
59 help="A single node name to add to the nodegroup")
60 parser.add_option("", "--nodelist", dest="nodelist", metavar="list.txt",
61 help="Use all nodes in the given file for operation.")
62 config = config(parser)
65 # COLLECT nodegroups, nodes and node lists
66 if config.node or config.nodelist:
68 hostlist = [ config.node ]
70 hostlist = config.getListFromFile(config.nodelist)
72 # NOTE: preserve order given in file. Otherwise, return values are not in order
76 nodelist += api.GetNodes(h)
78 #nodelist = api.GetNodes(hostlist)
82 site = api.GetSites(config.site)
85 nodelist = api.GetNodes(site['node_ids'])
89 group_str = config.site
91 elif config.nodeselect:
92 hostlist = node_select(config.nodeselect)
93 nodelist = api.GetNodes(hostlist)
95 group_str = "selection"
98 ng = api.GetNodeGroups({'name' : config.nodegroup})
99 nodelist = api.GetNodes(ng[0]['node_ids'])
101 group_str = config.nodegroup
104 # Get nodegroup nodes
108 all_nodes = api.GetNodes({'peer_id': None})
110 # remove ngnodes from all node list
111 ng_list = [ x['hostname'] for x in ng_nodes ]
112 all_list = [ x['hostname'] for x in all_nodes ]
113 not_ng_nodes = Set(all_list) - Set(ng_list)
115 # keep each node that *is* in the not_ng_nodes set
116 nodelist = filter(lambda x : x['hostname'] in not_ng_nodes, all_nodes)
118 hostnames = [ n['hostname'] for n in nodelist ]
122 if config.add and config.nodegroup:
123 for node in hostnames:
124 print "Adding %s to %s nodegroup" % (node, config.nodegroup)
125 api.AddNodeToNodeGroup(node, config.nodegroup)
128 for node in hostnames:
129 print "Deleting %s from %s nodegroup" % (node, config.nodegroup)
130 api.DeleteNodeFromNodeGroup(node, config.nodegroup)
133 print " ---- Nodes in the %s Node Group ----" % group_str
135 for node in nodelist:
137 print nodegroup_display(node, fb, config)
141 print "no other options supported."
143 if __name__ == "__main__":