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
This commit is contained in:
Pranesh Pandurangan 2014-06-05 11:23:55 -07:00
parent e4a6dc64de
commit 0be7808705
5 changed files with 39 additions and 25 deletions

View File

@ -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)

View File

@ -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):

View File

@ -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)

View File

@ -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

View File

@ -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