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