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.
18 api = plc.PLC(auth.auth, auth.plc)
20 from optparse import OptionParser
22 from nodequery import verify,query_to_dict,node_select
24 from nodecommon import *
28 from config import config
29 fb = database.dbLoad("findbad")
31 parser = OptionParser()
32 parser.set_defaults(nodegroup="Alpha",
42 parser.add_option("", "--not", dest="notng", action="store_true",
43 help="All nodes NOT in nodegroup.")
44 parser.add_option("", "--nodegroup", dest="nodegroup", metavar="NodegroupName",
45 help="Specify a nodegroup to perform actions on")
46 parser.add_option("", "--nodeselect", dest="nodeselect", metavar="querystring",
47 help="Specify a query to perform on findbad db")
48 parser.add_option("", "--site", dest="site", metavar="site name",
49 help="Specify a site to view node status")
51 parser.add_option("", "--nocolor", dest="nocolor", action="store_true",
53 parser.add_option("", "--list", dest="list", action="store_true",
54 help="List all nodes in the given nodegroup")
55 parser.add_option("", "--add", dest="add", action="store_true",
56 help="Add nodes to the given nodegroup")
57 parser.add_option("", "--delete", dest="delete", action="store_true",
58 help="Delete nodes from the given nodegroup")
59 parser.add_option("", "--node", dest="node", metavar="nodename.edu",
60 help="A single node name to add to the nodegroup")
61 parser.add_option("", "--nodelist", dest="nodelist", metavar="list.txt",
62 help="Use all nodes in the given file for operation.")
63 config = config(parser)
66 # COLLECT nodegroups, nodes and node lists
67 if config.node or config.nodelist:
69 hostlist = [ config.node ]
71 hostlist = config.getListFromFile(config.nodelist)
73 # NOTE: preserve order given in file. Otherwise, return values are not in order
77 nodelist += api.GetNodes(h)
79 #nodelist = api.GetNodes(hostlist)
83 site = api.GetSites(config.site)
86 nodelist = api.GetNodes(site['node_ids'])
90 group_str = config.site
92 elif config.nodeselect:
93 hostlist = node_select(config.nodeselect)
94 nodelist = api.GetNodes(hostlist)
96 group_str = "selection"
99 ng = api.GetNodeGroups({'name' : config.nodegroup})
100 nodelist = api.GetNodes(ng[0]['node_ids'])
102 group_str = config.nodegroup
105 # Get nodegroup nodes
109 all_nodes = api.GetNodes({'peer_id': None})
111 # remove ngnodes from all node list
112 ng_list = [ x['hostname'] for x in ng_nodes ]
113 all_list = [ x['hostname'] for x in all_nodes ]
114 not_ng_nodes = Set(all_list) - Set(ng_list)
116 # keep each node that *is* in the not_ng_nodes set
117 nodelist = filter(lambda x : x['hostname'] in not_ng_nodes, all_nodes)
119 hostnames = [ n['hostname'] for n in nodelist ]
123 if config.add and config.nodegroup:
124 for node in hostnames:
125 print "Adding %s to %s nodegroup" % (node, config.nodegroup)
126 api.AddNodeToNodeGroup(node, config.nodegroup)
129 for node in hostnames:
130 print "Deleting %s from %s nodegroup" % (node, config.nodegroup)
131 api.DeleteNodeFromNodeGroup(node, config.nodegroup)
134 print " ---- Nodes in the %s Node Group ----" % group_str
136 for node in nodelist:
138 print nodegroup_display(node, fb, config)
142 print "no other options supported."
144 if __name__ == "__main__":