From 3f2691c5d4f518effac458319a4a96774b71e941 Mon Sep 17 00:00:00 2001 From: kevgliss Date: Wed, 16 Nov 2016 13:23:35 -0800 Subject: [PATCH] Minor fixes. (#502) --- lemur/manage.py | 30 ++++++++++++++++++++---------- lemur/sources/service.py | 9 ++++++--- 2 files changed, 26 insertions(+), 13 deletions(-) diff --git a/lemur/manage.py b/lemur/manage.py index 2177a6a6..a5024ecb 100755 --- a/lemur/manage.py +++ b/lemur/manage.py @@ -858,12 +858,13 @@ class Sources(Command): Defines a set of actions to take against Lemur's sources. """ option_list = ( - Option('-s', '--sources', dest='sources', action='append', help='Sources to operate on.'), + Option('-s', '--sources', dest='source_strings', action='append', help='Sources to operate on.'), Option('-a', '--action', choices=['sync', 'clean'], dest='action', help='Action to take on source.') ) - def run(self, sources, action): - if not sources: + def run(self, source_strings, action): + sources = [] + if not source_strings: table = [] for source in source_service.get_all(): table.append([source.label, source.active, source.description]) @@ -871,12 +872,19 @@ class Sources(Command): sys.stdout.write(tabulate(table, headers=['Label', 'Active', 'Description'])) sys.exit(1) - for label in sources: - source = source_service.get_by_label(label) + elif 'all' in source_strings: + sources = source_service.get_all() - if not source: - sys.stderr.write("Unable to find specified source with label: {0}".format(label)) + else: + for source_str in source_strings: + source = source_service.get_by_label(source_str) + if not source: + sys.stderr.write("Unable to find specified source with label: {0}".format(source_str)) + + sources.append(source) + + for source in sources: if action == 'sync': self.sync(source) @@ -888,8 +896,10 @@ class Sources(Command): start_time = time.time() sys.stdout.write("[+] Staring to sync source: {label}!\n".format(label=source.label)) + user = user_service.get_by_username('lemur') + try: - source_service.sync(source) + source_service.sync(source, user) sys.stdout.write( "[+] Finished syncing source: {label}. Run Time: {time}\n".format( label=source.label, @@ -900,10 +910,10 @@ class Sources(Command): current_app.logger.exception(e) sys.stdout.write( - "[X] Failed syncing source {label}!\n".format(labe=source.label) + "[X] Failed syncing source {label}!\n".format(label=source.label) ) - metrics.send('{0}_sync_failed'.format(source.label), 'counter', 1) + metrics.send('sync_failed', 'counter', 1, metric_tags={'source': source.label}) @staticmethod def clean(source): diff --git a/lemur/sources/service.py b/lemur/sources/service.py index 711c6452..663261a4 100644 --- a/lemur/sources/service.py +++ b/lemur/sources/service.py @@ -151,7 +151,7 @@ def sync_endpoints(source): _disassociate_endpoints_from_source(endpoints, source) -def sync_certificates(source): +def sync_certificates(source, user): new, updated = 0, 0 current_app.logger.debug("Retrieving certificates from {0}".format(source.label)) @@ -161,6 +161,9 @@ def sync_certificates(source): for certificate in certificates: exists = cert_service.find_duplicates(certificate) + certificate['owner'] = user.email + certificate['creator'] = user + if not exists: certificate_create(certificate, source) new += 1 @@ -180,8 +183,8 @@ def sync_certificates(source): _disassociate_certs_from_source(certificates, source) -def sync(source): - sync_certificates(source) +def sync(source, user): + sync_certificates(source, user) sync_endpoints(source) source.last_run = datetime.datetime.utcnow()