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
This commit is contained in:
parent
f3843f61f9
commit
31413508a9
|
@ -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,
|
|
@ -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)
|
||||
|
|
|
@ -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())
|
||||
|
|
|
@ -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
|
||||
|
|
Loading…
Reference in New Issue