Browse Source

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
Pranesh Pandurangan 4 years ago
parent
commit
0be7808705
5 changed files with 39 additions and 25 deletions
  1. 13
    16
      entropy/__main__.py
  2. 2
    0
      entropy/backends/base.py
  3. 18
    0
      entropy/backends/file_backend.py
  4. 6
    4
      entropy/engine.py
  5. 0
    5
      entropy/utils.py

+ 13
- 16
entropy/__main__.py View File

@@ -30,35 +30,32 @@ LOG = logging.getLogger(__name__)
30 30
 engine_cfg = os.path.join(tempfile.gettempdir(), 'engines.cfg')
31 31
 
32 32
 
33
-def get_cfg_file(engine, script_type):
34
-    cfg_key = {'audit': 'audit_cfg', 'repair': 'repair_cfg'}
33
+def _get_backend_from_engine(engine):
35 34
     try:
36 35
         engine_config = dict(utils.load_yaml(engine_cfg))[engine]
37 36
         this_engine_cfg_file = engine_config['cfg']
38 37
         this_engine_cfg = dict(utils.load_yaml(this_engine_cfg_file))
39
-        return this_engine_cfg[engine][cfg_key[script_type]]
38
+        return Engine.get_backend(this_engine_cfg[engine]['backend'],
39
+                                  this_engine_cfg[engine])
40 40
     except KeyError:
41
-        LOG.exception('Could not find engine/react script')
42
-        return None
41
+        LOG.exception("Could not find engine's cfg script")
43 42
 
44 43
 
45
-def add_to_list(engine, script_type, script_name, **script_args):
46
-    cfg_file = get_cfg_file(engine, script_type)
47
-    if cfg_file is None:
48
-        LOG.error('Could not find cfg file')
49
-        return
50
-    if utils.check_duplicate(script_name, cfg_file):
44
+def _add_to_list(engine, script_type, script_name, **script_args):
45
+    backend = _get_backend_from_engine(engine)
46
+    if backend.check_script_exists(script_type, script_name):
51 47
         LOG.error('%s already exists, not registering', script_type)
52
-        return
48
+        return False
53 49
     try:
54 50
         data = {
55 51
             script_name: script_args
56 52
         }
57
-        utils.write_yaml(data, cfg_file)
53
+        backend.add_script(script_type, data)
58 54
         return True
59 55
     except Exception:
60 56
         LOG.exception("Could not register %s script %s", script_type,
61 57
                       script_name)
58
+        return False
62 59
 
63 60
 
64 61
 def register_audit(args):
@@ -71,7 +68,7 @@ def register_audit(args):
71 68
 
72 69
     # Write to audit file
73 70
     audit_cfg_args = {'cfg': os.path.join(os.getcwd(), args.conf)}
74
-    if add_to_list(args.engine, 'audit', args.name, **audit_cfg_args):
71
+    if _add_to_list(args.engine, 'audit', args.name, **audit_cfg_args):
75 72
         LOG.info('Registered audit %s', args.name)
76 73
 
77 74
 
@@ -83,9 +80,9 @@ def register_repair(args):
83 80
         LOG.error('Need path to script, json and engine name')
84 81
         return
85 82
 
86
-    # Write to audit file
83
+    # Write to repair file
87 84
     repair_cfg_args = {'cfg': os.path.join(os.getcwd(), args.conf)}
88
-    if add_to_list(args.engine, 'repair', args.name, **repair_cfg_args):
85
+    if _add_to_list(args.engine, 'repair', args.name, **repair_cfg_args):
89 86
         LOG.info('Registered repair script %s', args.name)
90 87
 
91 88
 

+ 2
- 0
entropy/backends/base.py View File

@@ -28,6 +28,8 @@ class Backend(object):
28 28
             raise TypeError("Configuration dictionary expected not: %s"
29 29
                             % type(conf))
30 30
         self._conf = conf
31
+        self._audit = 'audit'
32
+        self._repair = 'repair'
31 33
 
32 34
     @abc.abstractmethod
33 35
     def open(self):

+ 18
- 0
entropy/backends/file_backend.py View File

@@ -51,3 +51,21 @@ class FileBackend(base.Backend):
51 51
         conf = repairs[name]['cfg']
52 52
         repair_cfg = dict(utils.load_yaml(conf))
53 53
         return repair_cfg
54
+
55
+    def get_script_cfg(self, script_type):
56
+        """Return the audit/repair cfg file."""
57
+        if script_type == self._audit:
58
+            return self._audit_cfg
59
+        elif script_type == self._repair:
60
+            return self._repair_cfg
61
+        raise TypeError('Script type must be one of: ', [self._audit_cfg,
62
+                                                         self._repair_cfg])
63
+
64
+    def check_script_exists(self, script_type, script_name):
65
+        script_metadata = self.get_script_cfg(script_type)
66
+        scripts = utils.load_yaml(script_metadata)
67
+        return scripts and script_name in scripts
68
+
69
+    def add_script(self, script_type, data):
70
+        script_metadata = self.get_script_cfg(script_type)
71
+        utils.write_yaml(data, script_metadata)

+ 6
- 4
entropy/engine.py View File

@@ -55,7 +55,8 @@ class Engine(object):
55 55
         self.engine_timeout = cfg_data['engine_timeout']
56 56
         # TODO(praneshp): Assuming cfg files are in 1 dir. Change later
57 57
         self._backend = cfg_data['backend']
58
-        self._backend_driver = self.get_backend()
58
+        self._backend_driver = self.get_backend(self._backend,
59
+                                                self._engine_cfg_data)
59 60
         self.cfg_dir = os.path.dirname(self.audit_cfg)
60 61
         self.log_file = cfg_data['log_file']
61 62
         self.executor = cf.ThreadPoolExecutor(max_workers=self.max_workers)
@@ -79,12 +80,13 @@ class Engine(object):
79 80
         LOG.addHandler(log_to_file)
80 81
         LOG.propagate = False
81 82
 
82
-    def get_backend(self):
83
+    @staticmethod
84
+    def get_backend(backend, cfg_data):
83 85
         backend = driver.DriverManager(
84 86
             namespace='entropy.backend',
85
-            name=self._backend,
87
+            name=backend,
86 88
             invoke_on_load=True,
87
-            invoke_args=(self._engine_cfg_data,),
89
+            invoke_args=(cfg_data,),
88 90
         )
89 91
         return backend.driver
90 92
 

+ 0
- 5
entropy/utils.py View File

@@ -111,11 +111,6 @@ def watch_dir_for_change(dir_to_watch, event_fn):
111 111
     return observer
112 112
 
113 113
 
114
-def check_duplicate(name, cfg_file):
115
-    scripts = load_yaml(cfg_file)
116
-    return scripts and name in scripts
117
-
118
-
119 114
 def reset_logger(log):
120 115
     if not log:
121 116
         return

Loading…
Cancel
Save