X-Git-Url: http://git.onelab.eu/?a=blobdiff_plain;f=sfa%2Fimporter%2F__init__.py;h=e1c3a289dd704d0132ca973de577d3a0769eda45;hb=fe5b40a3e021b9c87caf2d1dd9740227759cc05c;hp=969cc5baa2960d6064d44e2eb2b32b1a75a9fd37;hpb=f0216fbadfed478863200fa7eae158e23f41ccf9;p=sfa.git diff --git a/sfa/importer/__init__.py b/sfa/importer/__init__.py index 969cc5ba..e1c3a289 100644 --- a/sfa/importer/__init__.py +++ b/sfa/importer/__init__.py @@ -1,31 +1,43 @@ #!/usr/bin/python import sys +from datetime import datetime from sfa.util.xrn import get_authority, hrn_to_urn -from sfa.util.plxrn import email_to_hrn from sfa.generic import Generic from sfa.util.config import Config from sfa.util.sfalogging import _SfaLogger from sfa.trust.hierarchy import Hierarchy -from sfa.trust.trustedroots import TrustedRoots +#from sfa.trust.trustedroots import TrustedRoots from sfa.trust.gid import create_uuid -from sfa.storage.alchemy import dbsession +# using global alchemy.session() here is fine +# as importer is on standalone one-shot process +from sfa.storage.alchemy import global_dbsession from sfa.storage.model import RegRecord, RegAuthority, RegUser from sfa.trust.certificate import convert_public_key, Keypair class Importer: - def __init__(self): + + def __init__(self,auth_hierarchy=None,logger=None): self.config = Config() - self.logger = _SfaLogger(logfile='/var/log/sfa_import.log', loggername='importlog') - self.logger.setLevelFromOptVerbose(config.SFA_API_LOGLEVEL) - self.auth_hierarchy = Hierarchy () - self.TrustedRoots = TrustedRoots(self.config.get_trustedroots_dir()) + if auth_hierarchy is not None: + self.auth_hierarchy=auth_hierarchy + else: + self.auth_hierarchy = Hierarchy () + if logger is not None: + self.logger=logger + else: + self.logger = _SfaLogger(logfile='/var/log/sfa_import.log', loggername='importlog') + self.logger.setLevelFromOptVerbose(self.config.SFA_API_LOGLEVEL) +# ugly side effect so that other modules get it right + import sfa.util.sfalogging + sfa.util.sfalogging.logger=logger +# self.TrustedRoots = TrustedRoots(self.config.get_trustedroots_dir()) # check before creating a RegRecord entry as we run this over and over def record_exists (self, type, hrn): - return dbsession.query(RegRecord).filter_by(hrn=hrn,type=type).count()!=0 + return global_dbsession.query(RegRecord).filter_by(hrn=hrn,type=type).count()!=0 def create_top_level_auth_records(self, hrn): """ @@ -46,8 +58,8 @@ class Importer: auth_record = RegAuthority(hrn=hrn, gid=auth_info.get_gid_object(), authority=get_authority(hrn)) auth_record.just_created() - dbsession.add (auth_record) - dbsession.commit() + global_dbsession.add (auth_record) + global_dbsession.commit() self.logger.info("SfaImporter: imported authority (parent) %s " % auth_record) @@ -66,8 +78,8 @@ class Importer: user_record = RegUser(hrn=hrn, gid=auth_info.get_gid_object(), authority=get_authority(hrn)) user_record.just_created() - dbsession.add (user_record) - dbsession.commit() + global_dbsession.add (user_record) + global_dbsession.commit() self.logger.info("SfaImporter: importing user (slicemanager) %s " % user_record) @@ -77,9 +89,9 @@ class Importer: """ # just create certs for all sfa interfaces even if they # aren't enabled - auth_info = self.auth_hierarchy.get_auth_info(self.interface_hrn) + auth_info = self.auth_hierarchy.get_auth_info(self.config.SFA_INTERFACE_HRN) pkey = auth_info.get_pkey_object() - hrn=self.interface_hrn + hrn=self.config.SFA_INTERFACE_HRN for type in [ 'authority+sa', 'authority+am', 'authority+sm', ]: urn = hrn_to_urn(hrn, type) gid = self.auth_hierarchy.create_gid(urn, create_uuid(), pkey) @@ -88,8 +100,8 @@ class Importer: interface_record = RegAuthority(type=type, hrn=hrn, gid=gid, authority=get_authority(hrn)) interface_record.just_created() - dbsession.add (interface_record) - dbsession.commit() + global_dbsession.add (interface_record) + global_dbsession.commit() self.logger.info("SfaImporter: imported authority (%s) %s " % (type,interface_record)) def run(self, options=None): @@ -99,17 +111,22 @@ class Importer: # testbed-neutral : create local certificates and the like auth_hierarchy = Hierarchy () self.create_top_level_auth_records(self.config.SFA_INTERFACE_HRN) - + self.create_interface_records() + # testbed-specific testbed_importer = None generic=Generic.the_flavour() importer_class = generic.importer_class() if importer_class: - logger.info ("Using flavour %s for importing (class %s)"%\ - (generic.flavour,importer_class.__name__)) - testbed_importer = importer_class (auth_hierarchy, logger) + begin_time=datetime.utcnow() + self.logger.info (30*'=') + self.logger.info ("Starting import on %s, using class %s from flavour %s"%\ + (begin_time,importer_class.__name__,generic.flavour)) + testbed_importer = importer_class (auth_hierarchy, self.logger) if testbed_importer: testbed_importer.add_options(options) - testbed_importer.run (parser) - - + testbed_importer.run (options) + end_time=datetime.utcnow() + duration=end_time-begin_time + self.logger.info("Import took %s"%duration) + self.logger.info (30*'=')