X-Git-Url: http://git.onelab.eu/?a=blobdiff_plain;f=sfa%2Fserver%2Fsfa-clean-peer-records.py;h=0e04ecd9456c691fb283a5f3f499b83be96c6dbb;hb=6f0a757c5adf47b4d222cec09514dcd688b93457;hp=fbf3b684f33a1de6b2adcaac89ca63f8240b420f;hpb=712c7e66e7853ee367c15123f387c9dd2e14e220;p=sfa.git diff --git a/sfa/server/sfa-clean-peer-records.py b/sfa/server/sfa-clean-peer-records.py index fbf3b684..0e04ecd9 100644 --- a/sfa/server/sfa-clean-peer-records.py +++ b/sfa/server/sfa-clean-peer-records.py @@ -2,16 +2,22 @@ import sys import os -from sfa.util.misc import * -from sfa.util.genitable import GeniTable -from sfa.util.geniclient import GeniClient -from sfa.plc.api import GeniAPI +import traceback +import socket + +from sfa.util.prefixTree import prefixTree from sfa.util.config import Config + +from sfa.trust.certificate import Keypair from sfa.trust.hierarchy import Hierarchy -from sfa.util.report import trace, error from sfa.server.registry import Registries -from sfa.util.xmlrpcprotocol import ServerException -import socket + +from sfa.storage.alchemy import dbsession +from sfa.storage.persistentobjs import RegRecord + +from sfa.client.sfaserverproxy import SfaServerProxy + +from sfa.generic import Generic def main(): config = Config() @@ -21,35 +27,56 @@ def main(): # Get the path to the sfa server key/cert files from # the sfa hierarchy object sfa_hierarchy = Hierarchy() - sfa_key_path = sfa_hierarchy.basedir - key_file = os.path.join(sfa_key_path, "server.key") - cert_file = os.path.join(sfa_key_path, "server.cert") + auth_info = sfa_hierarchy.get_interface_auth_info() + key_file = auth_info.get_privkey_filename() + cert_file = auth_info.get_gid_filename() + key = Keypair(filename=key_file) # get a connection to our local sfa registry # and a valid credential authority = config.SFA_INTERFACE_HRN url = 'http://%s:%s/' %(config.SFA_REGISTRY_HOST, config.SFA_REGISTRY_PORT) - registry = GeniClient(url, key_file, cert_file) - sfa_api = GeniAPI(key_file = key_file, cert_file = cert_file, interface='registry') + registry = SfaServerProxy(url, key_file, cert_file) + sfa_api = Generic.the_flavour() credential = sfa_api.getCredential() # get peer registries registries = Registries(sfa_api) - - + tree = prefixTree() + tree.load(registries.keys()) + # get local peer records - table = GeniTable() - peer_records = table.find({'~peer_authority': None}) - for peer_record in peer_records: - peer_auth = peer_record['peer_authority'] - if peer_auth in registries: + peer_records=dbsession.query(RegRecord).filter (RegRecord.peer_authority != None).all() + found_records = [] + hrn_dict = {} + for record in peer_records: + registry_hrn = tree.best_match(record.hrn) + if registry_hrn not in hrn_dict: + hrn_dict[registry_hrn] = [] + hrn_dict[registry_hrn].append(record.hrn) + + # attempt to resolve the record at the authoritative interface + for registry_hrn in hrn_dict: + if registry_hrn in registries: + records = [] + target_hrns = hrn_dict[registry_hrn] try: - records = registries[peer_auth].resolve(credential, peer_record['hrn']) - except ServerException: + records = registries[registry_hrn].Resolve(target_hrns, credential) + found_records.extend([record['hrn'] for record in records]) + except ServerException: # an exception will be thrown if the record doenst exist # if so remove the record from the local registry - registries[sfa_api.hrn].remove_peer_object(credential, peer_record) - except: - pass + continue + except: + # this deosnt necessarily mean the records dont exist + # lets give them the benefit of the doubt here (for now) + found_records.extend(target_hrns) + traceback.print_exc() + + # remove what wasnt found + for peer_record in peer_records: + if peer_record.hrn not in found_records: + registries[sfa_api.hrn].Remove(peer_record.hrn, credential, peer_record.type) + if __name__ == '__main__': main()