check-hrns.py: Ciro fix
[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.get('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                 if verbose:
21                     print "Node %s - current hrn %s, should be %s"%(node['hostname'], node['hrn'], hrn)
22                 if dry_run: continue
23                 SetNodeHrn (node['node_id'],hrn)
24             else:
25                 if verbose: print "Node %s OK"%node['hostname']
26
27 def handle_persons (sites,sites_by_id, dry_run,verbose): 
28     persons=GetPersons ({'peer_id':None},['person_id','email','hrn','site_ids'])
29     persons_by_id = dict ( [ (person['person_id'],person) for person in persons ] )
30     for site in sites:
31         login_base=site['login_base']
32         for person_id in site.get('person_ids', []):
33             try:    person=persons_by_id[person_id]
34             except: print 'cannot find person %s'%person_id; continue
35             how_many=len(person['site_ids'])
36             if how_many !=1:
37                 if verbose: print "Checking persons in exactly one site -- person %s in %s site(s) -- ignored"%(person['email'],how_many)
38                 continue
39
40             hrn=email_to_hrn ("%s.%s"%(toplevel,login_base),person['email'])
41             if person['hrn'] != hrn:
42                 if verbose:
43                     print "Person %s - current hrn %s, should be %s"%(person['email'], person['hrn'], hrn)
44                 if dry_run: continue
45                 SetPersonHrn (person['person_id'],hrn)
46             else:
47                 if verbose: print "Person %s OK"%person['email']
48
49
50 def handle_slices (sites,sites_by_id, dry_run,verbose):
51     slices=GetSlices ({'peer_id':None},['slice_id','name','hrn','site_id'])
52     slices_by_id = dict ( [ (slice['slice_id'],slice) for slice in slices ] )
53     for site in sites:
54         login_base=site['login_base']
55         for slice_id in site.get('slice_ids', []):
56             try:    slice=slices_by_id[slice_id]
57             except: print 'cannot find slice %s'%slice_id; continue
58             hrn=slicename_to_hrn (toplevel, slice['name'])
59             if slice['hrn'] != hrn:
60                 if verbose:  
61                     print "Slice %s - current hrn %s, should be %s"%(slice['name'], slice['hrn'], hrn)
62                 if dry_run: continue
63                 SetSliceHrn (slice['slice_id'],hrn)
64             else:
65                 if verbose: print "Slice %s OK"%slice['name']
66
67
68 def handle_sites (sites,sites_by_id, dry_run,verbose):
69     for site in sites:
70         hrn='.'.join([toplevel, site['login_base']])
71         if site['hrn'] != hrn:
72             if verbose:
73                 print "Site %s - current hrn %s, should be %s"%(site['name'].encode('ascii', 'ignore'), site['hrn'], hrn)
74             if dry_run: continue
75             SetSiteHrn (site['site_id'],hrn)
76         else:
77             if verbose: print "Site %s OK"%site['name']
78         
79
80             
81 def main():
82     usage="""Usage: %prog
83   Checks that the hrn tags are correctly set
84 Example:
85   %prog -- -p -nv
86 """
87     parser = OptionParser(usage=usage)
88     parser.add_option("-p", "--person", action = "store_true", default = False, 
89                       dest='persons',help="run on persons")
90     parser.add_option("-n", "--node", action = "store_true", default = False, 
91                       dest='nodes',help="run on nodes")
92     parser.add_option("-S", "--slice", action = "store_true", default = False,
93                       dest='slices',help="run on slices")
94     parser.add_option("-t", "--site", action = "store_true", default = False,
95                       dest='sites',help="run on sites")
96     parser.add_option("-s", "--show", action = "store_true", default = False, 
97                       dest='show', help="dry run, only show discrepencies")
98     parser.add_option("-v", "--verbose", action = "store_true", default = False, 
99                       dest='verbose', help="be verbose")
100     (options, args) = parser.parse_args()
101
102     if args: 
103         parser.print_help()
104         sys.exit(1)
105     # if neither -p nor -n, run both
106     if not options.nodes and not options.persons and not options.slices and not options.sites:
107         options.nodes=True
108         options.persons=True
109         options.slices=True        
110         options.sites=True
111
112     dry_run=options.show
113     verbose=options.verbose
114     # optimizing : we compute the set of sites only once
115     sites = GetSites({'peer_id':None},['site_id','login_base','node_ids','person_ids','name','hrn','slice_ids', 'sfa_created'])
116     # remove external sites created through SFA
117     sites = [site for site in sites if site['sfa_created'] != 'True']
118     sites_by_id = dict ( [ (site['site_id'], site) for site in sites ] )
119
120     if options.nodes: handle_nodes(sites,sites_by_id,dry_run,verbose)
121     if options.persons: handle_persons(sites,sites_by_id,dry_run,verbose)
122     if options.slices: handle_slices(sites,sites_by_id,dry_run,verbose)
123     if options.sites: handle_sites(sites,sites_by_id,dry_run,verbose)
124
125 if __name__ == "__main__":
126     main()