From: Tony Mack Date: Fri, 23 Oct 2009 22:40:05 +0000 (+0000) Subject: send output to logfile X-Git-Tag: sfa-0.9-6~139 X-Git-Url: http://git.onelab.eu/?a=commitdiff_plain;h=5ac003be7734b5d9a7e8076cf59452da35ca5078;p=sfa.git send output to logfile --- diff --git a/sfa/plc/sfa-import-plc.py b/sfa/plc/sfa-import-plc.py index e0de5f4b..11a259f8 100755 --- a/sfa/plc/sfa-import-plc.py +++ b/sfa/plc/sfa-import-plc.py @@ -20,12 +20,12 @@ import getopt import sys import tempfile - +import logging.handlers +import logging from sfa.util.record import * from sfa.util.genitable import GeniTable from sfa.util.misc import * from sfa.util.config import Config -from sfa.util.report import trace, error from sfa.trust.certificate import convert_public_key, Keypair from sfa.trust.trustedroot import * from sfa.trust.hierarchy import * @@ -33,8 +33,7 @@ from sfa.plc.api import * from sfa.util.geniclient import * from sfa.trust.gid import create_uuid from sfa.plc.sfaImport import * - - +from sfa.util.report import trace, error def process_options(): global hrn @@ -62,6 +61,15 @@ def save_keys(filename, keys): f.close() def main(): + # setup the logger + LOGFILE='/var/log/sfa_import_plc.log' + logging.basicConfig(level=logging.INFO, + format='%(asctime)s - %(message)s', + filename=LOGFILE) + rotate_handler = logging.handlers.RotatingFileHandler(LOGFILE, maxBytes=1000000, backupCount=5) + logger = logging.getLogger() + logger.addHandler(rotate_handler) + process_options() config = Config() if not config.SFA_REGISTRY_ENABLED: @@ -69,7 +77,7 @@ def main(): root_auth = config.SFA_REGISTRY_ROOT_AUTH level1_auth = config.SFA_REGISTRY_LEVEL1_AUTH keys_filename = config.config_path + os.sep + 'person_keys.py' - sfaImporter = sfaImport() + sfaImporter = sfaImport(logger) shell = sfaImporter.shell plc_auth = sfaImporter.plc_auth AuthHierarchy = sfaImporter.AuthHierarchy @@ -81,7 +89,6 @@ def main(): if not level1_auth or level1_auth in ['']: level1_auth = None - print "Import: creating top level authorities" if not level1_auth: sfaImporter.create_top_level_auth_records(root_auth) import_auth = root_auth @@ -91,7 +98,7 @@ def main(): sfaImporter.create_top_level_auth_records(level1_auth) import_auth = level1_auth - print "Import: adding", import_auth, "to trusted list" + trace("Import: adding" + import_auth + "to trusted list", logger) authority = AuthHierarchy.get_auth_info(import_auth) TrustedRoots.add_gid(authority.get_gid_object()) @@ -259,12 +266,11 @@ def main(): continue if not found: - trace("Import: Removing %s %s" % (type, record_hrn)) record_object = existing_records[(record_hrn, type)] sfaImporter.delete_record(record_hrn, type) # save pub keys - trace('saving current pub keys') + trace('Import: saving current pub keys', logger) save_keys(keys_filename, person_keys) if __name__ == "__main__": diff --git a/sfa/plc/sfaImport.py b/sfa/plc/sfaImport.py index 18b3bd25..8859c008 100644 --- a/sfa/plc/sfaImport.py +++ b/sfa/plc/sfaImport.py @@ -50,7 +50,8 @@ def cleanup_string(str): class sfaImport: - def __init__(self): + def __init__(self, logger=None): + self.logger = logger self.AuthHierarchy = Hierarchy() self.config = Config() self.TrustedRoots = TrustedRootList(Config.get_trustedroots_dir(self.config)) @@ -75,6 +76,7 @@ class sfaImport: # if auth records for this hrn dont exist, create it if not AuthHierarchy.auth_exists(hrn): + trace("Import: creating top level authorites", self.logger) AuthHierarchy.create_auth(hrn) @@ -95,7 +97,7 @@ class sfaImport: if not auth_record: auth_record = GeniRecord(hrn=hrn, gid=auth_info.get_gid_object(), type="authority", pointer=-1) auth_record['authority'] = get_authority(auth_record['hrn']) - trace(" inserting authority record for " + hrn) + trace("Import: inserting authority record for " + hrn, self.logger) table.insert(auth_record) @@ -107,7 +109,7 @@ class sfaImport: if len(hrn) > 64: hrn = hrn[:64] - trace("Import: importing person " + hrn) + trace("Import: importing person " + hrn, self.logger) key_ids = [] if 'key_ids' in person and person['key_ids']: key_ids = person["key_ids"] @@ -120,7 +122,7 @@ class sfaImport: pkey = Keypair(create=True) else: # the user has no keys - trace(" person " + hrn + " does not have a PL public key") + trace(" person " + hrn + " does not have a PL public key", self.logger) # if a key is unavailable, then we still need to put something in the # user's GID. So make one up. pkey = Keypair(create=True) @@ -134,7 +136,7 @@ class sfaImport: if not existing_records: table.insert(person_record) else: - trace("Import: %s exists, updating " % hrn) + trace("Import: %s exists, updating " % hrn, self.logger) existing_record = existing_records[0] person_record['record_id'] = existing_record['record_id'] table.update(person_record) @@ -145,11 +147,11 @@ class sfaImport: slicename = cleanup_string(slicename) if not slicename: - error("Import_Slice: failed to parse slice name " + slice['name']) + error("Import_Slice: failed to parse slice name " + slice['name'], self.logger) return hrn = parent_hrn + "." + slicename - trace("Import: importing slice " + hrn) + trace("Import: importing slice " + hrn, self.logger) pkey = Keypair(create=True) slice_gid = AuthHierarchy.create_gid(hrn, create_uuid(), pkey) @@ -160,7 +162,7 @@ class sfaImport: if not existing_records: table.insert(slice_record) else: - trace("Import: %s exists, updating " % hrn) + trace("Import: %s exists, updating " % hrn, self.logger) existing_record = existing_records[0] slice_record['record_id'] = existing_record['record_id'] table.update(slice_record) @@ -171,11 +173,11 @@ class sfaImport: nodename = cleanup_string(nodename) if not nodename: - error("Import_node: failed to parse node name " + node['hostname']) + error("Import_node: failed to parse node name " + node['hostname'], self.logger) return hrn = parent_hrn + "." + nodename - trace("Import: importing node " + hrn) + trace("Import: importing node " + hrn, self.logger) # ASN.1 will have problems with hrn's longer than 64 characters if len(hrn) > 64: hrn = hrn[:64] @@ -190,7 +192,7 @@ class sfaImport: if not existing_records: table.insert(node_record) else: - trace("Import: %s exists, updating " % hrn) + trace("Import: %s exists, updating " % hrn, self.logger) existing_record = existing_records[0] node_record['record_id'] = existing_record['record_id'] table.update(node_record) @@ -216,7 +218,7 @@ class sfaImport: #sitename = sitename.replace("nlr", "") hrn = ".".join([parent_hrn, "internet2", sitename]) - trace("Import_Site: importing site " + hrn) + trace("Import: importing site " + hrn, self.logger) # create the authority if not AuthHierarchy.auth_exists(hrn): @@ -231,7 +233,7 @@ class sfaImport: if not existing_records: table.insert(auth_record) else: - trace("Import: %s exists, updating " % hrn) + trace("Import: %s exists, updating " % hrn, self.logger) existing_record = existing_records[0] auth_record['record_id'] = existing_record['record_id'] table.update(auth_record) @@ -244,4 +246,5 @@ class sfaImport: table = GeniTable() record_list = table.find({'type': type, 'hrn': hrn}) for record in record_list: + trace("Import: Removing record %s %s" % (type, hrn), self.logger) table.remove(record)