print the exception even if we don't do anything about it (yet)
[sfa.git] / sfa / server / sfa-clean-peer-records.py
1 #!/usr/bin/python
2
3 import sys
4 import os
5 import traceback
6 from sfa.util.misc import *
7 from sfa.util.genitable import GeniTable
8 from sfa.util.geniclient import GeniClient
9 from sfa.plc.api import GeniAPI
10 from sfa.util.config import Config
11 from sfa.trust.hierarchy import Hierarchy
12 from sfa.util.report import trace, error
13 from sfa.server.registry import Registries
14 from sfa.util.xmlrpcprotocol import ServerException
15 import socket
16
17 def main():
18     config = Config()
19     if not config.SFA_REGISTRY_ENABLED:
20         sys.exit(0)
21
22     # Get the path to the sfa server key/cert files from 
23     # the sfa hierarchy object
24     sfa_hierarchy = Hierarchy()
25     sfa_key_path = sfa_hierarchy.basedir
26     key_file = os.path.join(sfa_key_path, "server.key")
27     cert_file = os.path.join(sfa_key_path, "server.cert")
28
29     # get a connection to our local sfa registry
30     # and a valid credential
31     authority = config.SFA_INTERFACE_HRN
32     url = 'http://%s:%s/' %(config.SFA_REGISTRY_HOST, config.SFA_REGISTRY_PORT)
33     registry = GeniClient(url, key_file, cert_file)
34     sfa_api = GeniAPI(key_file = key_file, cert_file = cert_file, interface='registry')
35     credential = sfa_api.getCredential()
36
37     # get peer registries
38     registries = Registries(sfa_api)
39
40
41     # get local peer records
42     table = GeniTable()
43     peer_records = table.find({'~peer_authority': None})
44     for peer_record in peer_records:
45         peer_auth = peer_record['peer_authority']
46         if peer_auth in registries:
47             try:
48                 records = registries[peer_auth].resolve(credential, peer_record['hrn'])
49             except ServerException:
50                 # an exception will be thrown if the record doenst exist
51                 # if so remove the record from the local registry
52                 registries[sfa_api.hrn].remove_peer_object(credential, peer_record)
53             except:
54                 traceback.print_exc()
55 if __name__ == '__main__':
56     main()