Merge branch 'master' into sqlalchemy
[sfa.git] / sfa / server / sfa-clean-peer-records.py
1 #!/usr/bin/python
2
3 import sys
4 import os
5 import traceback
6 import socket
7
8 from sfa.util.prefixTree import prefixTree
9 from sfa.util.config import Config
10
11 from sfa.trust.certificate import Keypair
12 from sfa.trust.hierarchy import Hierarchy
13 from sfa.server.registry import Registries
14
15 from sfa.storage.alchemy import dbsession
16 from sfa.storage.persistentobjs import RegRecord
17
18 from sfa.client.sfaserverproxy import SfaServerProxy 
19
20 from sfa.generic import Generic
21
22 def main():
23     config = Config()
24     if not config.SFA_REGISTRY_ENABLED:
25         sys.exit(0)
26
27     # Get the path to the sfa server key/cert files from 
28     # the sfa hierarchy object
29     sfa_hierarchy = Hierarchy()
30     auth_info = sfa_hierarchy.get_interface_auth_info()
31     key_file = auth_info.get_privkey_filename()
32     cert_file = auth_info.get_gid_filename()
33     key = Keypair(filename=key_file) 
34
35     # get a connection to our local sfa registry
36     # and a valid credential
37     authority = config.SFA_INTERFACE_HRN
38     url = 'http://%s:%s/' %(config.SFA_REGISTRY_HOST, config.SFA_REGISTRY_PORT)
39     registry = SfaServerProxy(url, key_file, cert_file)
40     sfa_api = Generic.the_flavour()
41     credential = sfa_api.getCredential()
42
43     # get peer registries
44     registries = Registries(sfa_api)
45     tree = prefixTree()
46     tree.load(registries.keys())
47     
48     # get local peer records
49     peer_records=dbsession.query(RegRecord).filter (RegRecord.peer_authority != None).all()
50     found_records = []
51     hrn_dict = {}
52     for record in peer_records:
53         registry_hrn = tree.best_match(record.hrn)
54         if registry_hrn not in hrn_dict:
55             hrn_dict[registry_hrn] = []
56         hrn_dict[registry_hrn].append(record.hrn)
57
58     # attempt to resolve the record at the authoritative interface 
59     for registry_hrn in hrn_dict:
60         if registry_hrn in registries:
61             records = []
62             target_hrns = hrn_dict[registry_hrn]    
63             try:
64                 records = registries[registry_hrn].Resolve(target_hrns, credential)
65                 found_records.extend([record['hrn'] for record in records])
66             except ServerException:
67                 # an exception will be thrown if the record doenst exist
68                 # if so remove the record from the local registry
69                 continue
70             except:
71                 # this deosnt necessarily mean the records dont exist
72                 # lets give them the benefit of the doubt here (for now)
73                 found_records.extend(target_hrns)
74                 traceback.print_exc()
75
76     # remove what wasnt found 
77     for peer_record in peer_records:
78         if peer_record.hrn not in found_records:
79             registries[sfa_api.hrn].Remove(peer_record.hrn, credential, peer_record.type)
80                 
81 if __name__ == '__main__':
82     main()