diff --git a/entropy/__main__.py b/entropy/__main__.py index 451133d..46be9e1 100644 --- a/entropy/__main__.py +++ b/entropy/__main__.py @@ -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() diff --git a/entropy/engine.py b/entropy/engine.py index 6dd6e18..8a5ab6f 100644 --- a/entropy/engine.py +++ b/entropy/engine.py @@ -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): diff --git a/entropy/utils.py b/entropy/utils.py index 09e0d31..3fd4dfa 100644 --- a/entropy/utils.py +++ b/entropy/utils.py @@ -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))