3 from optparse import OptionParser
5 from PLC.Namespace import hostname_to_hrn, email_to_hrn
8 toplevel=api.config.PLC_HRN_ROOT
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 ] )
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)
22 SetNodeHrn (node['node_id'],hrn)
24 if verbose: print "host %s OK"%node['hostname']
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'])
31 if verbose: print "person %s not in 1 site (%s instead) -- ignored"%(person['email'],how_many)
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)
39 SetPersonHrn (person['person_id'],hrn)
41 if verbose: print "host %s OK"%person['email']
45 parser = OptionParser()
46 parser.add_option("-p", "--person", action = "store_true", default = False,
47 dest='persons',help="run on persons")
48 parser.add_option("-n", "--node", action = "store_true", default = False,
49 dest='nodes',help="run on nodes")
50 parser.add_option("-s", "--show", action = "store_true", default = False,
51 dest='show', help="dry run, only show discrepencies")
52 parser.add_option("-v", "--verbose", action = "store_true", default = False,
53 dest='verbose', help="be verbose")
54 (options, args) = parser.parse_args()
59 # if neither -p nor -n, run both
60 if not options.nodes and not options.persons:
65 verbose=options.verbose
66 # optimizing : we compute the set of sites only once
67 sites = GetSites({'peer_id':None},['site_id','login_base','node_ids','person_ids'])
68 sites_by_id = dict ( [ (site['site_id'], site) for site in sites ] )
69 if options.nodes: handle_nodes(sites,sites_by_id,dry_run,verbose)
70 if options.persons: handle_persons(sites,sites_by_id,dry_run,verbose)
72 if __name__ == "__main__":