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