cosmetic
[myplc.git] / bin / check-hrns.py
1 #!/usr/bin/env plcsh
2 import sys
3 from optparse import OptionParser
4
5 from PLC.Namespace import hostname_to_hrn, email_to_hrn
6 # (auth_hrn, email):
7
8 toplevel=api.config.PLC_HRN_ROOT
9
10 def handle_nodes (sites,sites_by_id, dry_run, verbose):
11     nodes=GetNodes ({'peer_id':None},['node_id','hostname','hrn'])
12     nodes_by_id = dict ( [ (node['node_id'],node) for node in nodes ] )
13     for site in sites:
14         login_base=site['login_base']
15         for node_id in site['node_ids']:
16             try:    node=nodes_by_id[node_id]
17             except: print 'cannot find node %s'%node_id; continue
18             hrn=hostname_to_hrn (toplevel, login_base, node['hostname'])
19             if node['hrn'] != hrn:
20                 print "Node %s - current hrn %s, should be %s"%(node['hostname'], node['hrn'], hrn)
21                 if dry_run: continue
22                 SetNodeHrn (node['node_id'],hrn)
23             else:
24                 if verbose: print "Node %s OK"%node['hostname']
25
26 def handle_persons (sites,sites_by_id, dry_run,verbose): 
27     persons=GetPersons ({'peer_id':None},['person_id','email','hrn','site_ids'])
28     for person in persons:
29         how_many=len(person['site_ids'])
30         if how_many !=1:
31             if verbose: print "Checking persons in exactly one site -- person %s in %s site(s) -- ignored"%(person['email'],how_many)
32             continue
33         try:    login_base=sites_by_id[person['site_ids'][0]]['login_base']
34         except: print "Cannot handle person %s - site not found"%person['email']; continue
35         hrn=email_to_hrn ("%s.%s"%(toplevel,login_base),person['email'])
36         if person['hrn'] != hrn:
37             print "Person %s - current hrn %s, should be %s"%(person['email'], person['hrn'], hrn)
38             if dry_run: continue
39             SetPersonHrn (person['person_id'],hrn)
40         else:
41             if verbose: print "host %s OK"%person['email']
42         
43             
44 def main():
45     usage="""Usage: %prog
46   Checks that the hrn tags are correctly set
47 Example:
48   %prog -- -p -nv
49 """
50     parser = OptionParser(usage=usage)
51     parser.add_option("-p", "--person", action = "store_true", default = False, 
52                       dest='persons',help="run on persons")
53     parser.add_option("-n", "--node", action = "store_true", default = False, 
54                       dest='nodes',help="run on nodes")
55     parser.add_option("-s", "--show", action = "store_true", default = False, 
56                       dest='show', help="dry run, only show discrepencies")
57     parser.add_option("-v", "--verbose", action = "store_true", default = False, 
58                       dest='verbose', help="be verbose")
59     (options, args) = parser.parse_args()
60
61     if args: 
62         parser.print_help()
63         sys.exit(1)
64     # if neither -p nor -n, run both
65     if not options.nodes and not options.persons:
66         options.nodes=True
67         options.persons=True
68     
69     dry_run=options.show
70     verbose=options.verbose
71     # optimizing : we compute the set of sites only once
72     sites = GetSites({'peer_id':None},['site_id','login_base','node_ids','person_ids'])
73     sites_by_id = dict ( [ (site['site_id'], site) for site in sites ] )
74     if options.nodes: handle_nodes(sites,sites_by_id,dry_run,verbose)
75     if options.persons: handle_persons(sites,sites_by_id,dry_run,verbose)
76
77 if __name__ == "__main__":
78     main()