Browse Source

Added config file foreignkey ref in config

Change-Id: Ia37b538ad7fa8a9b377e796dbf4e005370e723cd
Kanagaraj Manickam 3 years ago
parent
commit
59a3041e70

+ 69
- 30
namos/conductor/manager.py View File

@@ -167,6 +167,29 @@ class ServiceProcessor(object):
167 167
     def __init__(self, registration_info):
168 168
         self.registration_info = registration_info
169 169
 
170
+    def file_to_configs(self, file_content):
171
+        tmp_file_path = '/tmp/sample-namos-config.conf'
172
+        with open(tmp_file_path, 'w') as file:
173
+            file.write(file_content)
174
+
175
+        with open(tmp_file_path, 'r') as file:
176
+            section = ''
177
+            conf_dict = dict()
178
+
179
+            for line in file:
180
+                if line.strip().startswith('['):
181
+                    section = line.replace('[', '').replace(']', '').strip()
182
+                    continue
183
+                if section:
184
+                    kv = line.strip().split('=')
185
+                    conf_dict[
186
+                        '%s::%s' % (section, kv[0].strip())] = None
187
+
188
+        import os
189
+        os.remove(tmp_file_path)
190
+
191
+        return conf_dict
192
+
170 193
     def process_service(self, context):
171 194
         # Service Node
172 195
         try:
@@ -225,34 +248,6 @@ class ServiceProcessor(object):
225 248
                          service_component)
226 249
             # TODO(mrkanag) what to do when service_components size is > 1
227 250
 
228
-        # config file
229
-        for cfg_f in self.registration_info['config_file_list']:
230
-            try:
231
-                config_file = db_api.config_file_create(
232
-                    context,
233
-                    dict(name=cfg_f,
234
-                         file=self.registration_info[
235
-                             'config_file_dict'][cfg_f],
236
-                         service_component_id=service_component.id,
237
-                         service_node_id=node.id))
238
-                LOG.info('Oslo config file %s is created' % config_file)
239
-            except exception.AlreadyExist:
240
-                config_files = \
241
-                    db_api.config_file_get_by_name_for_service_node(
242
-                        context,
243
-                        service_node_id=node.id,
244
-                        name=cfg_f
245
-                    )
246
-                if len(config_files) == 1:
247
-                    config_file = \
248
-                        db_api.config_file_update(
249
-                            context,
250
-                            config_files[0].id,
251
-                            dict(file=self.registration_info[
252
-                                'config_file_dict'][cfg_f]))
253
-                    LOG.info('Oslo config file %s is existing and is updated'
254
-                             % config_file)
255
-
256 251
         # Service Worker
257 252
         try:
258 253
             service_worker = db_api.service_worker_create(
@@ -293,6 +288,41 @@ class ServiceProcessor(object):
293 288
 
294 289
             # TODO(mrkanag) what to do when service_workers size is > 1
295 290
 
291
+        # config file
292
+        conf_files = dict()
293
+        for cfg_f in self.registration_info['config_file_list']:
294
+            try:
295
+                config_file = db_api.config_file_create(
296
+                    context,
297
+                    dict(name=cfg_f,
298
+                         file=self.registration_info[
299
+                             'config_file_dict'][cfg_f],
300
+                         service_component_id=service_component.id,
301
+                         service_node_id=node.id))
302
+                LOG.info('Oslo config file %s is created' % config_file)
303
+            except exception.AlreadyExist:
304
+                config_files = \
305
+                    db_api.config_file_get_by_name_for_service_node(
306
+                        context,
307
+                        service_node_id=node.id,
308
+                        name=cfg_f
309
+                    )
310
+                if len(config_files) == 1:
311
+                    config_file = \
312
+                        db_api.config_file_update(
313
+                            context,
314
+                            config_files[0].id,
315
+                            dict(file=self.registration_info[
316
+                                'config_file_dict'][cfg_f]))
317
+                    LOG.info('Oslo config file %s is existing and is updated'
318
+                             % config_file)
319
+
320
+            config_dict = self.file_to_configs(
321
+                self.registration_info['config_file_dict'][cfg_f]
322
+            )
323
+
324
+            conf_files[config_file.id] = config_dict
325
+
296 326
         # Config
297 327
         # TODO(mrkanag) Optimize the config like per service_component
298 328
         # or per service_worker,
@@ -338,11 +368,20 @@ class ServiceProcessor(object):
338 368
                     LOG.debug("Config Schema %s is existing and is updated" %
339 369
                               cfg_sche)
340 370
 
371
+            # is it part of config file
372
+            cfg_name = "%s::%s" % (cfg_obj['group'], cfg_name)
373
+            file_id = None
374
+            for f_id, conf_keys in conf_files.items():
375
+                if cfg_name in conf_keys.keys():
376
+                    file_id = f_id
377
+                    break
378
+
341 379
             cfg_obj_ = dict(
342 380
                 service_worker_id=service_worker.id,
343
-                name="%s.%s" % (cfg_obj['group'], cfg_name),
381
+                name=cfg_name,
344 382
                 value=cfg_obj['value'],
345
-                oslo_config_schema_id=cfg_sche.id
383
+                oslo_config_schema_id=cfg_sche.id,
384
+                oslo_config_file_id=file_id
346 385
             )
347 386
 
348 387
             try:

+ 1
- 1
namos/db/sqlalchemy/alembic/versions/48ebec3cd6f6_initial_version.py View File

@@ -177,8 +177,8 @@ def upgrade():
177 177
 
178 178
 
179 179
 def downgrade():
180
-    op.drop_table('oslo_config_file')
181 180
     op.drop_table('oslo_config')
181
+    op.drop_table('oslo_config_file')
182 182
     op.drop_table('oslo_config_schema')
183 183
     op.drop_table('device_driver')
184 184
     op.drop_table('service_worker')

+ 3
- 2
namos/db/sqlalchemy/models.py View File

@@ -357,8 +357,9 @@ class OsloConfig(BASE,
357 357
     value = sqlalchemy.Column(
358 358
         sqlalchemy.Text
359 359
     )
360
-    file = sqlalchemy.Column(
361
-        sqlalchemy.String(512)
360
+    oslo_config_file_id = sqlalchemy.Column(
361
+        Uuid,
362
+        sqlalchemy.ForeignKey('oslo_config_file.id')
362 363
     )
363 364
     oslo_config_schema_id = sqlalchemy.Column(
364 365
         Uuid,

Loading…
Cancel
Save