Browse Source

Add an audit unregister option

Allow users to remove audit scripts through a simple CLI call. The
unregister-audit call will remove an audit script from audit.cfg,
thus stopping the execution of all but the currently scheduled audit
scripts.

partially implements blueprint simple api
Change-Id: I1d5328d87b607c2f5cfdaebb7448a11673f38d48
Pranesh Pandurangan 4 years ago
parent
commit
473e2febc0
3 changed files with 47 additions and 0 deletions
  1. 25
    0
      entropy/__main__.py
  2. 4
    0
      entropy/backends/base.py
  3. 18
    0
      entropy/backends/file_backend.py

+ 25
- 0
entropy/__main__.py View File

@@ -60,6 +60,15 @@ def _add_to_list(engine, script_type, script_name, **script_args):
60 60
         return False
61 61
 
62 62
 
63
+def _remove_from_list(engine, script_type, script_name):
64
+    backend = _get_backend_from_engine(engine)
65
+    try:
66
+        backend.remove_script(script_type, script_name)
67
+    except Exception:
68
+        LOG.exception("Could not remove %s script %s",
69
+                      script_type, script_name)
70
+
71
+
63 72
 def register_audit(args):
64 73
     LOG.info('Registering audit script %s', args.name)
65 74
 
@@ -74,6 +83,14 @@ def register_audit(args):
74 83
         LOG.info('Registered audit %s', args.name)
75 84
 
76 85
 
86
+def unregister_audit(args):
87
+    LOG.info('Unregistering audit script %s', args.name)
88
+    if not args.name and args.engine:
89
+        LOG.error('Need a audit name and engine to unregister')
90
+        return
91
+    _remove_from_list(args.engine, 'audit', args.name)
92
+
93
+
77 94
 def register_repair(args):
78 95
     LOG.info('Registering repair script %s', args.name)
79 96
 
@@ -151,6 +168,14 @@ def parse():
151 168
                                        help='Engine')
152 169
     register_audit_parser.set_defaults(func=register_audit)
153 170
 
171
+    unregister_audit_parser =\
172
+        subparsers.add_parser('unregister-audit',
173
+                              help='Unregister a repair script')
174
+    unregister_audit_parser.add_argument('-n', dest='name', action='store',
175
+                                         help='Repair script name')
176
+    unregister_audit_parser.add_argument('-e', dest='engine', action='store',
177
+                                         help='Engine')
178
+    unregister_audit_parser.set_defaults(func=unregister_audit)
154 179
     register_repair_parser =\
155 180
         subparsers.add_parser('register-repair',
156 181
                               help='Register a repair script')

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

@@ -67,3 +67,7 @@ class Backend(object):
67 67
     @abc.abstractmethod
68 68
     def add_script(self, script_type, data):
69 69
         pass
70
+
71
+    @abc.abstractmethod
72
+    def remove_script(self, script_type, script_name):
73
+        pass

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

@@ -69,3 +69,21 @@ class FileBackend(base.Backend):
69 69
     def add_script(self, script_type, data):
70 70
         script_metadata = self.get_script_cfg(script_type)
71 71
         utils.write_yaml(data, script_metadata)
72
+
73
+    @staticmethod
74
+    def remove_script_from_cfg(metadata, name):
75
+        scripts = utils.load_yaml(metadata)
76
+        try:
77
+            scripts.pop(name)
78
+        except KeyError:
79
+            raise
80
+        return scripts
81
+
82
+    def remove_script(self, script_type, script_name):
83
+        try:
84
+            script_metadata = self.get_script_cfg(script_type)
85
+            final_data = FileBackend.remove_script_from_cfg(script_metadata,
86
+                                                            script_name)
87
+            utils.write_yaml(final_data, script_metadata, append=False)
88
+        except Exception:
89
+            raise

Loading…
Cancel
Save