use registry.remove_peer_record() to remove the record
[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
14 def main():
15     config = Config()
16     if not config.SFA_REGISTRY_ENABLED:
17         sys.exit(0)
18
19     # Get the path to the sfa server key/cert files from 
20     # the sfa hierarchy object
21     sfa_hierarchy = Hierarchy()
22     sfa_key_path = sfa_hierarchy.basedir
23     key_file = os.path.join(sfa_key_path, "server.key")
24     cert_file = os.path.join(sfa_key_path, "server.cert")
25
26     # get a connection to our local sfa registry
27     # and a valid credential
28     authority = config.SFA_INTERFACE_HRN
29     url = 'http://%s:%s/' %(config.SFA_REGISTRY_HOST, config.SFA_REGISTRY_PORT)
30     registry = GeniClient(url, key_file, cert_file)
31     sfa_api = GeniAPI(key_file = key_file, cert_file = cert_file, interface='registry')
32     credential = sfa_api.getCredential()
33
34     # get peer registries
35     registries = Registries(sfa_api)
36
37
38     # get local peer records
39     table = GeniTable()
40     peer_records = table.find({'~peer_authority': None})
41     for peer_record in peer_records:
42         peer_auth = peer_record['peer_authority']
43         if peer_auth in registries:
44             try:
45                 records = registries[peer_auth].resolve(credential, peer_record['hrn'])
46             except:
47                 # an exception will be thrown if the record doenst exist
48                 # if so remove the record from the local registry
49                 registries[sfa_api.hrn].remove_peer_object(credential, peer_record)
50
51 if __name__ == '__main__':
52     main()