From 31413508a9d624631276bd00cb75227e0d03835f Mon Sep 17 00:00:00 2001 From: Joshua Harlow Date: Mon, 16 Dec 2013 20:38:49 -0800 Subject: [PATCH] Small adjustments Move entropy.py -> __main__.py so that it can just be used by running $ python entropy (inside the entropy root folder). Also use yaml loading instead of json loading since yaml allows for comments inside the file (yaml is a superset of json). Removed import json from __main__.py to keep pep8 happy Also changed react.py to use a per module logger instead of root logger Change-Id: I5eb24319dee4f04891878c6e61cc4d7835b14d34 --- entropy/{entropy.py => __main__.py} | 33 ++++++++++++++--------------- entropy/react.py | 9 +++++--- entropy/utils.py | 7 ++++++ requirements.txt | 1 + 4 files changed, 30 insertions(+), 20 deletions(-) rename entropy/{entropy.py => __main__.py} (85%) diff --git a/entropy/entropy.py b/entropy/__main__.py similarity index 85% rename from entropy/entropy.py rename to entropy/__main__.py index 1e456fe..e514672 100644 --- a/entropy/entropy.py +++ b/entropy/__main__.py @@ -17,7 +17,6 @@ import argparse import datetime -import json import logging import os import sys @@ -29,12 +28,12 @@ import croniter sys.path.insert(0, os.path.join(os.path.abspath(os.pardir))) sys.path.insert(0, os.path.abspath(os.getcwd())) -import audit -import utils +from entropy import audit +from entropy import utils GOOD_MOOD = 1 SCRIPT_REPO = os.path.dirname(__file__) -LOG_REPO = os.path.join(os.getcwd(), 'logs') +LOG_REPO = os.path.join(os.path.dirname(__file__), 'logs') LOG = logging.getLogger(__name__) @@ -61,7 +60,7 @@ def start_audit(**kwargs): next_iteration = cron.get_next(datetime.datetime) while True: now = datetime.datetime.now() - logging.warning(str(now) + str(next_iteration)) + LOG.warning(str(now) + str(next_iteration)) if now > next_iteration: do_something(**kwargs['mq_args']) next_iteration = cron.get_next(datetime.datetime) @@ -85,19 +84,19 @@ def register_audit(args): # Now pick out relevant info # TODO(praneshp) eventually this must become a function call - with open(conf_file, 'r') as json_data: - data = json.load(json_data) - # stuff for the message queue - mq_args = {'mq_host': data['mq_host'], - 'mq_port': data['mq_port'], - 'mq_user': data['mq_user'], - 'mq_password': data['mq_password']} + data = utils.load_yaml(conf_file) - # general stuff for the audit module - kwargs = {'sshkey': utils.get_key_path(), - 'name': data['name'], - 'schedule': data['cron-freq'], - 'mq_args': mq_args} + # stuff for the message queue + mq_args = {'mq_host': data['mq_host'], + 'mq_port': data['mq_port'], + 'mq_user': data['mq_user'], + 'mq_password': data['mq_password']} + + # general stuff for the audit module + kwargs = {'sshkey': utils.get_key_path(), + 'name': data['name'], + 'schedule': data['cron-freq'], + 'mq_args': mq_args} #Start a thread to run a cron job for this audit script t = threading.Thread(name=kwargs['name'], target=start_audit, diff --git a/entropy/react.py b/entropy/react.py index 4421b86..e20ff1b 100644 --- a/entropy/react.py +++ b/entropy/react.py @@ -23,6 +23,7 @@ from queues import pass_events SCRIPT_REPO = os.path.dirname(__file__) conf_file = os.path.join(SCRIPT_REPO, 'react.json') +LOG = logging.getLogger(__name__) class SomeConsumer(ConsumerMixin): @@ -34,7 +35,7 @@ class SomeConsumer(ConsumerMixin): return [Consumer(pass_events, callbacks=[self.on_message])] def on_message(self, body, message): - logging.warning("Received message: %r" % body) + LOG.warning("Received message: %r" % body) message.ack() return @@ -46,7 +47,7 @@ def recv_message(**kwargs): try: SomeConsumer(conn).run() except KeyboardInterrupt: - logging.warning('Quitting %s' % __name__) + LOG.warning('Quitting %s' % __name__) def parse_conf(): @@ -61,6 +62,8 @@ def parse_conf(): if __name__ == '__main__': - logging.warning('starting react script %s' % __file__) + #can log to stdout for now + logging.basicConfig() + LOG.warning('starting react script %s' % __file__) mq_args = parse_conf() recv_message(**mq_args) diff --git a/entropy/utils.py b/entropy/utils.py index 5217685..030f1b1 100644 --- a/entropy/utils.py +++ b/entropy/utils.py @@ -15,6 +15,8 @@ # under the License. import os +import yaml + def get_key_path(): home_dir = os.path.expanduser("~") @@ -26,3 +28,8 @@ def get_key_path(): if os.path.isfile(path): return path return None + + +def load_yaml(filename): + with open(filename, "rb") as fh: + return yaml.safe_load(fh.read()) diff --git a/requirements.txt b/requirements.txt index 70a9e3b..29ead11 100644 --- a/requirements.txt +++ b/requirements.txt @@ -3,3 +3,4 @@ Pbr>=0.5.21,<1.0 sphinx>=1.1.2,<1.2 croniter>=0.3.3 kombu==3.0.7 +PyYAML>=3.1.0