another step of moving stuff around where it belongs
[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 import sfa.client.xmlrpcprotocol as xmlrpcprotocol 
9 from sfa.util.table import SfaTable
10 from sfa.util.prefixTree import prefixTree
11 from sfa.util.config import Config
12
13 from sfa.generic import Generic
14
15 from sfa.trust.certificate import Keypair
16 from sfa.trust.hierarchy import Hierarchy
17 from sfa.server.registry import Registries
18
19 def main():
20     config = Config()
21     if not config.SFA_REGISTRY_ENABLED:
22         sys.exit(0)
23
24     # Get the path to the sfa server key/cert files from 
25     # the sfa hierarchy object
26     sfa_hierarchy = Hierarchy()
27     sfa_key_path = sfa_hierarchy.basedir
28     key_file = os.path.join(sfa_key_path, "server.key")
29     cert_file = os.path.join(sfa_key_path, "server.cert")
30     key = Keypair(filename=key_file) 
31
32     # get a connection to our local sfa registry
33     # and a valid credential
34     authority = config.SFA_INTERFACE_HRN
35     url = 'http://%s:%s/' %(config.SFA_REGISTRY_HOST, config.SFA_REGISTRY_PORT)
36     registry = xmlrpcprotocol.server_proxy(url, key_file, cert_file)
37     sfa_api = Generic.the_flavour()
38     credential = sfa_api.getCredential()
39
40     # get peer registries
41     registries = Registries(sfa_api)
42     tree = prefixTree()
43     tree.load(registries.keys())
44     
45     # get local peer records
46     table = SfaTable()
47     peer_records = table.find({'~peer_authority': None})
48     found_records = []
49     hrn_dict = {}
50     for record in peer_records:
51         registry_hrn = tree.best_match(record['hrn'])
52         if registry_hrn not in hrn_dict:
53             hrn_dict[registry_hrn] = []
54         hrn_dict[registry_hrn].append(record['hrn'])
55
56     # attempt to resolve the record at the authoritative interface 
57     for registry_hrn in hrn_dict:
58         if registry_hrn in registries:
59             records = []
60             target_hrns = hrn_dict[registry_hrn]    
61             try:
62                 records = registries[registry_hrn].Resolve(target_hrns, credential)
63                 found_records.extend([record['hrn'] for record in records])
64             except ServerException:
65                 # an exception will be thrown if the record doenst exist
66                 # if so remove the record from the local registry
67                 continue
68             except:
69                 # this deosnt necessarily mean the records dont exist
70                 # lets give them the benefit of the doubt here (for now)
71                 found_records.extend(target_hrns)
72                 traceback.print_exc()
73
74     # remove what wasnt found 
75     for peer_record in peer_records:
76         if peer_record['hrn'] not in found_records:
77             registries[sfa_api.hrn].Remove(peer_record['hrn'], credential, peer_record['type'])
78                 
79 if __name__ == '__main__':
80     main()