Change format of audit and react cfg files

Try to follow the format
name1:
    arg1:
    arg2:
name2:
    arg1:
    arg2:

Changes in several places to reflect the new format.
Change-Id: I182bbb701ac0e1885078f9ec3789fcff799acf5a
This commit is contained in:
pran1990 2014-05-02 14:30:41 -07:00
parent ef2b443dac
commit 38520d41d8
3 changed files with 39 additions and 42 deletions

View File

@ -20,8 +20,6 @@ import logging
import os
import tempfile
import yaml
from engine import Engine
from entropy import utils
@ -35,32 +33,35 @@ engine_cfg = os.path.join(tempfile.gettempdir(), 'engines.cfg')
def get_cfg_file(engine, script_type):
cfg_key = {'audit': 'audit_cfg', 'repair': 'repair_cfg'}
try:
engine_config = dict(utils.load_yaml(engine_cfg).next())
engine_config = dict(utils.load_yaml(engine_cfg))[engine]
this_engine_cfg_file = engine_config['cfg']
this_engine_cfg = dict(utils.load_yaml(this_engine_cfg_file).next())
this_engine_cfg = dict(utils.load_yaml(this_engine_cfg_file))
return this_engine_cfg[engine][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, script_name, **script_args):
cfg_file = get_cfg_file(engine, script_type)
if cfg_file is None:
LOG.error('Could not find cfg file')
return
if utils.check_duplicate(kwargs['name'], cfg_file):
if utils.check_duplicate(script_name, cfg_file):
LOG.error('%s already exists, not registering', script_type)
return
with open(cfg_file, "a") as cfg:
cfg.write(yaml.dump(kwargs, canonical=False,
default_flow_style=False,
explicit_start=True))
try:
data = {
script_name: script_args
}
utils.write_yaml(data, cfg_file)
return True
except Exception:
LOG.exception("Could not register %s script %s", script_type,
script_name)
def register_audit(args):
# TODO(praneshp) check for sanity (file exists, imp parameters exist, etc)
LOG.info('Registering audit script %s', args.name)
# First check if you have all inputs
@ -69,14 +70,12 @@ def register_audit(args):
return
# Write to audit file
audit_cfg_args = {'name': args.name,
'conf': os.path.join(os.getcwd(), args.conf)}
if add_to_list(args.engine, 'audit', **audit_cfg_args):
audit_cfg_args = {'cfg': os.path.join(os.getcwd(), args.conf)}
if add_to_list(args.engine, 'audit', args.name, **audit_cfg_args):
LOG.info('Registered audit %s', args.name)
def register_repair(args):
# TODO(praneshp) check for sanity (file exists, imp parameters exist, etc)
LOG.info('Registering repair script %s', args.name)
# First check if you have all inputs
@ -85,9 +84,8 @@ def register_repair(args):
return
# Write to audit file
repair_cfg_args = {'name': args.name,
'conf': os.path.join(os.getcwd(), args.conf)}
if add_to_list(args.engine, 'repair', **repair_cfg_args):
repair_cfg_args = {'cfg': os.path.join(os.getcwd(), args.conf)}
if add_to_list(args.engine, 'repair', args.name, **repair_cfg_args):
LOG.info('Registered repair script %s', args.name)

View File

@ -113,19 +113,19 @@ class Engine(object):
futures = []
if scripts:
for script in scripts:
if script['name'] not in running_scripts:
future = setup_func(script)
if script not in running_scripts:
future = setup_func(script, **scripts[script])
if future is not None:
futures.append(future)
LOG.info('Running %s scripts %s', script_type,
', '.join(running_scripts))
return futures
def setup_react(self, script):
LOG.info('Setting up reactor %s', script['name'])
def setup_react(self, script, **script_args):
LOG.info('Setting up reactor %s', script)
# Pick out relevant info
data = dict(utils.load_yaml(script['conf']).next())
data = dict(utils.load_yaml(script_args['cfg']))
react_script = data['script']
search_path, reactor = utils.get_filename_and_path(react_script)
available_modules = imp.find_module(reactor, [search_path])
@ -139,29 +139,29 @@ class Engine(object):
if message_queue not in self.known_queues:
self.known_queues.append(message_queue)
kwargs = data
kwargs['conf'] = script['conf']
kwargs['conf'] = script_args['cfg']
kwargs['exchange'] = self.entropy_exchange
kwargs['message_queue'] = message_queue
# add this job to list of running repairs
self.running_repairs.append(script['name'])
self.running_repairs.append(script)
imported_module = imp.load_module(react_script, *available_modules)
future = self.executor.submit(imported_module.main, **kwargs)
return future
except Exception:
LOG.exception("Could not setup %s", script['name'])
LOG.exception("Could not setup %s", script)
return None
def setup_audit(self, script):
LOG.info('Setting up audit script %s', script['name'])
def setup_audit(self, script, **script_args):
LOG.info('Setting up audit script %s', script)
# add this job to list of running audits
self.running_audits.append(script['name'])
self.running_audits.append(script)
# start a process for this audit script
future = self.executor.submit(self.start_audit, script)
future = self.executor.submit(self.start_audit, script, **script_args)
return future
def start_audit(self, script):
LOG.info("Starting audit for %s", script['name'])
data = dict(utils.load_yaml(script['conf']).next())
def start_audit(self, script, **script_args):
LOG.info("Starting audit for %s", script)
data = dict(utils.load_yaml(script_args['cfg']))
schedule = data['schedule']
now = datetime.datetime.now()
cron = croniter.croniter(schedule, now)
@ -169,13 +169,16 @@ class Engine(object):
while True:
LOG.info('It is %s, Next call at %s', now, next_iteration)
pause.until(next_iteration)
self.run_audit(script)
try:
self.run_audit(script, **script_args)
except Exception:
LOG.exception('Could not run %s at %s', script, next_iteration)
now = datetime.datetime.now()
next_iteration = cron.get_next(datetime.datetime)
def run_audit(self, script):
def run_audit(self, script, **script_args):
# Read the conf file
data = dict(utils.load_yaml(script['conf']).next())
data = dict(utils.load_yaml(script_args['cfg']))
# general stuff for the audit module
# TODO(praneshp): later, fix to send only one copy of mq_args
mq_args = {'mq_host': data['mq_host'],
@ -196,4 +199,4 @@ class Engine(object):
audit_obj = imported_module.Audit(**kwargs)
audit_obj.send_message(**kwargs)
except Exception:
LOG.exception('Could not run audit %s', script['name'])
LOG.exception('Could not run audit %s', script)

View File

@ -110,13 +110,9 @@ def watch_dir_for_change(dir_to_watch, event_fn):
return observer
# TODO(praneshp) move this to utils
def check_duplicate(name, cfg_file):
scripts = load_yaml(cfg_file)
names = [script['name'] for script in scripts]
if name in names:
return True
return False
return scripts and name in scripts.keys()
def reset_logger(log):