From 0be78087056d593005b7f50f1022b01de3d15d05 Mon Sep 17 00:00:00 2001 From: Pranesh Pandurangan Date: Thu, 5 Jun 2014 11:23:55 -0700 Subject: [PATCH] Move register-audit/repair code to backend Abstract out cfg file operations in the register workflow. Changed the get_driver code in engine to be static, so we can call it from main too. Added a new function in file_backend to return the config file given the script type. Eg. return audit_cfg for audit. Added a new function in file_backend to replace check_duplicate, that returns True if a script already is already registered. Added a couple of string variables in base.py The function get_cfg_file, when using a db, will actually return a table. So this belongs in the backend, the code refactor here ensures this function is not called in the main() code. Raise errors instead of returning None in the some backend functions Completes blueprint backend-abstraction Change-Id: I20d6bd46caf56c750e4b1193a6f5d00ce4e930f6 --- entropy/__main__.py | 29 +++++++++++++---------------- entropy/backends/base.py | 2 ++ entropy/backends/file_backend.py | 18 ++++++++++++++++++ entropy/engine.py | 10 ++++++---- entropy/utils.py | 5 ----- 5 files changed, 39 insertions(+), 25 deletions(-) diff --git a/entropy/__main__.py b/entropy/__main__.py index 293ac8c..65a2839 100644 --- a/entropy/__main__.py +++ b/entropy/__main__.py @@ -30,35 +30,32 @@ LOG = logging.getLogger(__name__) engine_cfg = os.path.join(tempfile.gettempdir(), 'engines.cfg') -def get_cfg_file(engine, script_type): - cfg_key = {'audit': 'audit_cfg', 'repair': 'repair_cfg'} +def _get_backend_from_engine(engine): try: 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)) - return this_engine_cfg[engine][cfg_key[script_type]] + return Engine.get_backend(this_engine_cfg[engine]['backend'], + this_engine_cfg[engine]) except KeyError: - LOG.exception('Could not find engine/react script') - return None + LOG.exception("Could not find engine's cfg script") -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(script_name, cfg_file): +def _add_to_list(engine, script_type, script_name, **script_args): + backend = _get_backend_from_engine(engine) + if backend.check_script_exists(script_type, script_name): LOG.error('%s already exists, not registering', script_type) - return + return False try: data = { script_name: script_args } - utils.write_yaml(data, cfg_file) + backend.add_script(script_type, data) return True except Exception: LOG.exception("Could not register %s script %s", script_type, script_name) + return False def register_audit(args): @@ -71,7 +68,7 @@ def register_audit(args): # Write to audit file audit_cfg_args = {'cfg': os.path.join(os.getcwd(), args.conf)} - if add_to_list(args.engine, 'audit', args.name, **audit_cfg_args): + if _add_to_list(args.engine, 'audit', args.name, **audit_cfg_args): LOG.info('Registered audit %s', args.name) @@ -83,9 +80,9 @@ def register_repair(args): LOG.error('Need path to script, json and engine name') return - # Write to audit file + # Write to repair file repair_cfg_args = {'cfg': os.path.join(os.getcwd(), args.conf)} - if add_to_list(args.engine, 'repair', args.name, **repair_cfg_args): + if _add_to_list(args.engine, 'repair', args.name, **repair_cfg_args): LOG.info('Registered repair script %s', args.name) diff --git a/entropy/backends/base.py b/entropy/backends/base.py index 6e6b22e..f3b3b1f 100644 --- a/entropy/backends/base.py +++ b/entropy/backends/base.py @@ -28,6 +28,8 @@ class Backend(object): raise TypeError("Configuration dictionary expected not: %s" % type(conf)) self._conf = conf + self._audit = 'audit' + self._repair = 'repair' @abc.abstractmethod def open(self): diff --git a/entropy/backends/file_backend.py b/entropy/backends/file_backend.py index 8b726d7..5645c26 100644 --- a/entropy/backends/file_backend.py +++ b/entropy/backends/file_backend.py @@ -51,3 +51,21 @@ class FileBackend(base.Backend): conf = repairs[name]['cfg'] repair_cfg = dict(utils.load_yaml(conf)) return repair_cfg + + def get_script_cfg(self, script_type): + """Return the audit/repair cfg file.""" + if script_type == self._audit: + return self._audit_cfg + elif script_type == self._repair: + return self._repair_cfg + raise TypeError('Script type must be one of: ', [self._audit_cfg, + self._repair_cfg]) + + def check_script_exists(self, script_type, script_name): + script_metadata = self.get_script_cfg(script_type) + scripts = utils.load_yaml(script_metadata) + return scripts and script_name in scripts + + def add_script(self, script_type, data): + script_metadata = self.get_script_cfg(script_type) + utils.write_yaml(data, script_metadata) diff --git a/entropy/engine.py b/entropy/engine.py index ad7fded..c1e6b23 100644 --- a/entropy/engine.py +++ b/entropy/engine.py @@ -55,7 +55,8 @@ class Engine(object): self.engine_timeout = cfg_data['engine_timeout'] # TODO(praneshp): Assuming cfg files are in 1 dir. Change later self._backend = cfg_data['backend'] - self._backend_driver = self.get_backend() + self._backend_driver = self.get_backend(self._backend, + self._engine_cfg_data) self.cfg_dir = os.path.dirname(self.audit_cfg) self.log_file = cfg_data['log_file'] self.executor = cf.ThreadPoolExecutor(max_workers=self.max_workers) @@ -79,12 +80,13 @@ class Engine(object): LOG.addHandler(log_to_file) LOG.propagate = False - def get_backend(self): + @staticmethod + def get_backend(backend, cfg_data): backend = driver.DriverManager( namespace='entropy.backend', - name=self._backend, + name=backend, invoke_on_load=True, - invoke_args=(self._engine_cfg_data,), + invoke_args=(cfg_data,), ) return backend.driver diff --git a/entropy/utils.py b/entropy/utils.py index 55f7cf0..d355d39 100644 --- a/entropy/utils.py +++ b/entropy/utils.py @@ -111,11 +111,6 @@ def watch_dir_for_change(dir_to_watch, event_fn): return observer -def check_duplicate(name, cfg_file): - scripts = load_yaml(cfg_file) - return scripts and name in scripts - - def reset_logger(log): if not log: return