Browse Source

Add some more checks to engine creation

Add a field called enabled in the engine cfg file that keeps track of
files. This can be used to keep track of stopped engines, and
monitored using watchdog/similar. Also added some more checks at
engine creation time.

Partially Closes-bug:1309406
Change-Id: I1c365c2c438e6ed0a44413e1d09c69d3fab7ab7b
pran1990 4 years ago
parent
commit
81a9042ff1
2 changed files with 66 additions and 14 deletions
  1. 26
    11
      entropy/__main__.py
  2. 40
    3
      entropy/utils.py

+ 26
- 11
entropy/__main__.py View File

@@ -90,18 +90,33 @@ def start_engine(args):
90 90
     if not (args.name and args.engine_cfg):
91 91
         LOG.error('Need name and engine cfg')
92 92
         return
93
-    cfg_data = dict(utils.load_yaml(args.engine_cfg))[args.name]
94
-    cfg = {
95
-        args.name: {
96
-            'cfg': os.path.join(os.getcwd(), args.engine_cfg),
97
-            'pid': os.getpid(),
98
-            'backend': cfg_data['backend']
93
+    utils.create_files([engine_cfg])
94
+    if args.purge:
95
+        utils.purge_disabled(engine_cfg)
96
+    if utils.check_exists_and_enabled(args.name, engine_cfg):
97
+        LOG.error("An engine of the same name %s is already "
98
+                  "registered and running", args.name)
99
+        return
100
+    if utils.check_exists_and_disabled(args.name, engine_cfg):
101
+        LOG.error("And engine of the same name %s is already "
102
+                  "registered, but disabled. Run with purge?", args.name)
103
+        return
104
+    try:
105
+        cfg_data = dict(utils.load_yaml(args.engine_cfg))[args.name]
106
+        cfg = {
107
+            args.name: {
108
+                'cfg': os.path.join(os.getcwd(), args.engine_cfg),
109
+                'pid': os.getpid(),
110
+                'backend': cfg_data['backend']
111
+            }
99 112
         }
100
-    }
101
-    utils.write_yaml(cfg, engine_cfg)
102
-    LOG.info('Added %s to engine cfg', args.name)
103
-    entropy_engine = Engine(args.name, **cfg_data)
104
-    entropy_engine.run()
113
+        utils.write_yaml(cfg, engine_cfg)
114
+        LOG.info('Added %s to engine cfg', args.name)
115
+        entropy_engine = Engine(args.name, **cfg_data)
116
+        entropy_engine.run()
117
+    except Exception:
118
+        LOG.exception("Could not start engine %s", args.name)
119
+        return
105 120
 
106 121
 
107 122
 def parse():

+ 40
- 3
entropy/utils.py View File

@@ -106,11 +106,47 @@ class WatchdogHandler(FileSystemEventHandler):
106 106
 def watch_dir_for_change(dir_to_watch, event_fn):
107 107
     event_handler = WatchdogHandler(event_fn)
108 108
     observer = Observer()
109
-    observer.schedule(event_handler, path=dir_to_watch, recursive=True)
109
+    observer.schedule(event_handler, path=dir_to_watch)
110 110
     observer.start()
111 111
     return observer
112 112
 
113 113
 
114
+def check_exists_and_enabled(name, cfg_file):
115
+    engines = load_yaml(cfg_file)
116
+    return engines and name in engines and engines[name]['enabled']
117
+
118
+
119
+def check_exists_and_disabled(name, cfg_file):
120
+    engines = load_yaml(cfg_file)
121
+    return engines and name in engines and not engines[name]['enabled']
122
+
123
+
124
+def purge_disabled(cfg_file):
125
+    engines = load_yaml(cfg_file)
126
+    final_engines = {}
127
+    if not engines:
128
+        return
129
+    for engine in engines:
130
+        if engines[engine]['enabled']:
131
+            final_engines[engine] = engines[engine]
132
+    if final_engines:
133
+        write_yaml(final_engines, cfg_file, append=False)
134
+    else:
135
+        with open(cfg_file, 'w'):
136
+            pass
137
+
138
+
139
+def disable_engine(name, cfg_file):
140
+    engines = load_yaml(cfg_file)
141
+    if not engines:
142
+        raise Exception("No engines at all!")
143
+    if name not in engines:
144
+        raise Exception("No such engine!")
145
+    engines[name]['enabled'] = False
146
+    write_yaml(engines, cfg_file, append=False)
147
+    return engines[name]['pid']
148
+
149
+
114 150
 def reset_logger(log):
115 151
     if not log:
116 152
         return
@@ -123,8 +159,9 @@ def reset_logger(log):
123 159
     log.addHandler(logging.NullHandler())
124 160
 
125 161
 
126
-def write_yaml(data, filename):
127
-    with open(filename, "a") as cfg_file:
162
+def write_yaml(data, filename, append=True):
163
+    mode = "a" if append else "w"
164
+    with open(filename, mode) as cfg_file:
128 165
         cfg_file.write(yaml.safe_dump(data,
129 166
                                       default_flow_style=False,
130 167
                                       canonical=False))

Loading…
Cancel
Save