sfaprotocol is renamed into sfaserverproxy, with class SfaServerProxy
[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.table import SfaTable
16
17 from sfa.client.sfaserverproxy import SfaServerProxy 
18
19 from sfa.generic import Generic
20
21 def main():
22     config = Config()
23     if not config.SFA_REGISTRY_ENABLED:
24         sys.exit(0)
25
26     # Get the path to the sfa server key/cert files from 
27     # the sfa hierarchy object
28     sfa_hierarchy = Hierarchy()
29     sfa_key_path = sfa_hierarchy.basedir
30     key_file = os.path.join(sfa_key_path, "server.key")
31     cert_file = os.path.join(sfa_key_path, "server.cert")
32     key = Keypair(filename=key_file) 
33
34     # get a connection to our local sfa registry
35     # and a valid credential
36     authority = config.SFA_INTERFACE_HRN
37     url = 'http://%s:%s/' %(config.SFA_REGISTRY_HOST, config.SFA_REGISTRY_PORT)
38     registry = SfaServerProxy(url, key_file, cert_file)
39     sfa_api = Generic.the_flavour()
40     credential = sfa_api.getCredential()
41
42     # get peer registries
43     registries = Registries(sfa_api)
44     tree = prefixTree()
45     tree.load(registries.keys())
46     
47     # get local peer records
48     table = SfaTable()
49     peer_records = table.find({'~peer_authority': None})
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()