120eaac52fceb7397ccfde717d7b75806a32f20e
[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, slicename_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 "Person %s OK"%person['email']
42
43
44 def handle_slices (sites,sites_by_id, dry_run,verbose):
45     slices=GetSlices ({'peer_id':None},['slice_id','name','hrn','site_id'])
46     for slice in slices:
47         try:    login_base=sites_by_id[slice['site_id']]['login_base']
48         except: print "Cannot handle slice %s - site not found"%slice['name']; continue
49         hrn=slicename_to_hrn (toplevel, slice['name'])
50         if slice['hrn'] != hrn:
51             print "Slice %s - current hrn %s, should be %s"%(slice['name'], slice['hrn'], hrn)
52             if dry_run: continue
53             SetSliceHrn (slice['slice_id'],hrn)
54         else:
55             if verbose: print "Slice %s OK"%slice['name']
56
57         
58             
59 def main():
60     usage="""Usage: %prog
61   Checks that the hrn tags are correctly set
62 Example:
63   %prog -- -p -nv
64 """
65     parser = OptionParser(usage=usage)
66     parser.add_option("-p", "--person", action = "store_true", default = False, 
67                       dest='persons',help="run on persons")
68     parser.add_option("-n", "--node", action = "store_true", default = False, 
69                       dest='nodes',help="run on nodes")
70     parser.add_option("-S", "--slice", action = "store_true", default = False,
71                       dest='slices',help="run on slices")
72     parser.add_option("-s", "--show", action = "store_true", default = False, 
73                       dest='show', help="dry run, only show discrepencies")
74     parser.add_option("-v", "--verbose", action = "store_true", default = False, 
75                       dest='verbose', help="be verbose")
76     (options, args) = parser.parse_args()
77
78     if args: 
79         parser.print_help()
80         sys.exit(1)
81     # if neither -p nor -n, run both
82     if not options.nodes and not options.persons and not options.slices:
83         options.nodes=True
84         options.persons=True
85         options.slices=True        
86
87     dry_run=options.show
88     verbose=options.verbose
89     # optimizing : we compute the set of sites only once
90     sites = GetSites({'peer_id':None},['site_id','login_base','node_ids','person_ids','name'])
91     # remove external sites created through SFA
92     sites = [site for site in sites if not site['name'].startswith('sfa.')]
93
94     sites_by_id = dict ( [ (site['site_id'], site) for site in sites ] )
95     if options.nodes: handle_nodes(sites,sites_by_id,dry_run,verbose)
96     if options.persons: handle_persons(sites,sites_by_id,dry_run,verbose)
97     if options.slices: handle_slices(sites,sites_by_id,dry_run,verbose)
98
99 if __name__ == "__main__":
100     main()