Change format of engine configuration file

Store pid along with other details. Change some code to take care of the new
format, including the utils function that reads yaml.

Henceforth, try to keep yaml files in the project in the format:
name1:
    arg1:
    arg2:
name2:
    arg1:
    arg2:

Also wrote a function to write yaml in utils.py

Change-Id: I838ec927a439ac1aeea88ba0b1d71fc782777204
This commit is contained in:
pran1990 2014-04-30 11:05:06 -07:00
parent 479662ad48
commit ef2b443dac
3 changed files with 27 additions and 18 deletions

View File

@ -35,9 +35,10 @@ 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]
this_engine_cfg = dict(utils.load_yaml(engine_config).next())[engine]
return this_engine_cfg[cfg_key[script_type]]
engine_config = dict(utils.load_yaml(engine_cfg).next())
this_engine_cfg_file = engine_config['cfg']
this_engine_cfg = dict(utils.load_yaml(this_engine_cfg_file).next())
return this_engine_cfg[engine][cfg_key[script_type]]
except KeyError:
LOG.exception('Could not find engine/react script')
return None
@ -91,17 +92,17 @@ def register_repair(args):
def start_engine(args):
# TODO(praneshp): for now, always look in entropy/cfg for config files.
if not (args.name and args.engine_cfg):
LOG.error('Need name and engine cfg')
return
cfg_data = dict(utils.load_yaml(args.engine_cfg).next())[args.name]
cfg = {args.name: os.path.join(os.getcwd(), args.engine_cfg)}
with open(engine_cfg, "w") as cfg_file:
cfg_file.write(yaml.dump(cfg, canonical=False,
default_flow_style=False,
explicit_start=True))
cfg_data = dict(utils.load_yaml(args.engine_cfg))[args.name]
cfg = {
args.name: {
'cfg': os.path.join(os.getcwd(), args.engine_cfg),
'pid': os.getpid()
}
}
utils.write_yaml(cfg, engine_cfg)
LOG.info('Added %s to engine cfg', args.name)
entropy_engine = Engine(args.name, **cfg_data)
entropy_engine.run()

View File

@ -111,12 +111,12 @@ class Engine(object):
scripts = utils.load_yaml(cfg)
futures = []
for script in scripts:
if script['name'] not in running_scripts:
future = setup_func(script)
if future is not None:
futures.append(future)
if scripts:
for script in scripts:
if script['name'] not in running_scripts:
future = setup_func(script)
if future is not None:
futures.append(future)
LOG.info('Running %s scripts %s', script_type,
', '.join(running_scripts))
return futures
@ -170,6 +170,7 @@ class Engine(object):
LOG.info('It is %s, Next call at %s', now, next_iteration)
pause.until(next_iteration)
self.run_audit(script)
now = datetime.datetime.now()
next_iteration = cron.get_next(datetime.datetime)
def run_audit(self, script):

View File

@ -43,7 +43,7 @@ def get_key_path():
def load_yaml(filename):
with open(filename, "rb") as fh:
return yaml.safe_load_all(fh.read())
return yaml.safe_load(fh.read())
# importer functions.
@ -129,3 +129,10 @@ def reset_logger(log):
log.removeHandler(h)
log.setLevel(logging.NOTSET)
log.addHandler(logging.NullHandler())
def write_yaml(data, filename):
with open(filename, "a") as cfg_file:
cfg_file.write(yaml.safe_dump(data,
default_flow_style=False,
canonical=False))