Make entropy suitable for pypi distribution, part 1

Set logging handlers in each file, instead of a global one
Move CLI output to stdout
Remove one hardcoded value

Change-Id: I0d1bfcbd642bdc43547bf177bed53c32eaf956b9
This commit is contained in:
pran1990
2014-04-14 00:53:50 -07:00
parent e58f59c858
commit 92b73b563b
4 changed files with 39 additions and 12 deletions

View File

@@ -33,15 +33,17 @@ LOG = logging.getLogger(__name__)
# TODO(praneshp): Only hardcoded stuff in the project. Find a way to move # TODO(praneshp): Only hardcoded stuff in the project. Find a way to move
engine_cfg = os.path.join(os.getcwd(), 'entropy', 'examples', engine_cfg = os.path.join(os.getcwd(), 'entropy', 'examples',
'cfg', 'engines.cfg') 'cfg', 'engines.cfg')
log_file = os.path.join(os.getcwd(), 'entropy', 'examples',
'logs', 'entropy.log')
def get_cfg_file(engine, script_type): def get_cfg_file(engine, script_type):
cfg_key = {'audit': 'audit_cfg', 'repair': 'repair_cfg'} cfg_key = {'audit': 'audit_cfg', 'repair': 'repair_cfg'}
try:
engine_config = dict(utils.load_yaml(engine_cfg).next())[engine] engine_config = dict(utils.load_yaml(engine_cfg).next())[engine]
this_engine_cfg = dict(utils.load_yaml(engine_config).next())[engine] this_engine_cfg = dict(utils.load_yaml(engine_config).next())[engine]
return this_engine_cfg[cfg_key[script_type]] return this_engine_cfg[cfg_key[script_type]]
except KeyError:
LOG.exception('Could not find engine/react script')
return None
def add_to_list(engine, script_type, **kwargs): def add_to_list(engine, script_type, **kwargs):
@@ -149,9 +151,11 @@ def parse():
if __name__ == '__main__': if __name__ == '__main__':
#TODO(praneshp): AMQP, json->yaml, reaction scripts(after amqp) FORMAT = '%(lineno)s %(message)s'
FORMAT = '%(filename)s %(lineno)s %(message)s' console = logging.StreamHandler()
logging.basicConfig(filename=log_file, console.setLevel(logging.DEBUG)
level=logging.DEBUG, console.setFormatter(FORMAT)
format=FORMAT) LOG.addHandler(console)
print LOG.handlers
# logging.basicConfig(level=logging.DEBUG)
parse() parse()

View File

@@ -31,6 +31,7 @@ LOG = logging.getLogger(__name__)
class Engine(object): class Engine(object):
def __init__(self, name, **cfg_data): def __init__(self, name, **cfg_data):
Engine.set_logger(**cfg_data)
# constants # constants
# TODO(praneshp): Hardcode for now, could/should be cmdline input # TODO(praneshp): Hardcode for now, could/should be cmdline input
self.max_workers = 8 self.max_workers = 8
@@ -50,6 +51,16 @@ class Engine(object):
self.futures = [] self.futures = []
LOG.info('Created engine obj %s', self.name) LOG.info('Created engine obj %s', self.name)
@staticmethod
def set_logger(**cfg_data):
# Set the logger
LOG.handlers = []
log_to_file = logging.FileHandler(cfg_data['log_file'])
log_to_file.setLevel(logging.DEBUG)
log_format = logging.Formatter(cfg_data['log_format'])
log_to_file.setFormatter(log_format)
LOG.addHandler(log_to_file)
def run(self): def run(self):
LOG.info('Starting Scheduler for %s', self.name) LOG.info('Starting Scheduler for %s', self.name)
self.start_scheduler() self.start_scheduler()

View File

@@ -23,12 +23,12 @@ from kombu.pools import producers
from novaclient.client import Client from novaclient.client import Client
import paramiko import paramiko
from entropy.audit import base from entropy.audit.base import AuditBase
LOG = logging.getLogger(__name__) LOG = logging.getLogger(__name__)
class Audit(base.AuditBase): class Audit(AuditBase):
# TODO(praneshp): this can be done with plumbum instead. # TODO(praneshp): this can be done with plumbum instead.
@staticmethod @staticmethod
def remote_call(cmd, **kwargs): def remote_call(cmd, **kwargs):
@@ -120,6 +120,12 @@ class Audit(base.AuditBase):
'boot': Audit.remote_call(boot_command, **kwargs)} 'boot': Audit.remote_call(boot_command, **kwargs)}
def send_message(self, **kwargs): def send_message(self, **kwargs):
LOG.handlers = []
log_to_file = logging.FileHandler(kwargs['log_file'])
log_to_file.setLevel(logging.DEBUG)
log_format = logging.Formatter(kwargs['log_format'])
log_to_file.setFormatter(log_format)
LOG.addHandler(log_to_file)
connection = BrokerConnection('amqp://%(mq_user)s:%(mq_password)s@' connection = BrokerConnection('amqp://%(mq_user)s:%(mq_password)s@'
'%(mq_host)s:%(mq_port)s//' '%(mq_host)s:%(mq_port)s//'
% kwargs['mq_args']) % kwargs['mq_args'])

View File

@@ -84,6 +84,12 @@ def parse_conf(conf):
def main(**kwargs): def main(**kwargs):
LOG.handlers = []
log_to_file = logging.FileHandler(kwargs['log_file'])
log_to_file.setLevel(logging.DEBUG)
log_format = logging.Formatter(kwargs['log_format'])
log_to_file.setFormatter(log_format)
LOG.addHandler(log_to_file)
LOG.info('starting react script %s' % kwargs['name']) LOG.info('starting react script %s' % kwargs['name'])
args = parse_conf(kwargs['conf']) args = parse_conf(kwargs['conf'])
recv_message(**args) recv_message(**args)