Commit 5fd149781ecba4b4ba69cae0d59d28a849635db1

Authored by Etienne Pallier
1 parent 1192cc3d
Exists in dev

Agent lit config de base depuis objet config

config/config_base.py
... ... @@ -2,6 +2,10 @@
2 2  
3 3 import os
4 4  
  5 +from logpyros import LogPyros
  6 +
  7 +
  8 +
5 9 APP_NAME = "PYROS"
6 10  
7 11 # Conveniences to always have a quick reference to the
... ... @@ -10,9 +14,11 @@ ROOT_DIR = os.path.join(
10 14 os.path.dirname(os.path.realpath(__file__)),
11 15 os.pardir, # '..'
12 16 )
13   -ROOT_DIR = os.path.realpath(ROOT_DIR)
  17 +ROOT_DIR = os.path.normpath(ROOT_DIR)
  18 +#ROOT_DIR = os.path.realpath(ROOT_DIR)
14 19  
15 20 # root directories
  21 +CONFIG_DIR = os.path.join(ROOT_DIR, "config")
16 22 DOC_DIR = os.path.join(ROOT_DIR, "doc")
17 23 INSTALL_DIR = os.path.join(ROOT_DIR, "install")
18 24 LOG_DIR = os.path.join(ROOT_DIR, "config", "logs")
... ... @@ -20,8 +26,14 @@ SOURCE_DIR = os.path.join(ROOT_DIR, "src")
20 26  
21 27 # source sub-directories
22 28 CELME_DIR = os.path.join(SOURCE_DIR, "core", "celme")
  29 +#PYROS_DJANGO_BASE_DIR = Path("src/core/pyros_django") # pathlib
23 30 PYROS_DJANGO_DIR = os.path.join(SOURCE_DIR, "core", "pyros_django")
24 31 DEVICES_DIR = os.path.join(SOURCE_DIR, "device_controller")
25 32  
  33 +DEFAULT_CONFIG_FILE_NAME = "config_unit_simulunit1.xml"
  34 +#CONFIG_DIR_NAME = "config"
  35 +
  36 +# Default LOG level is INFO
  37 +PYROS_DEFAULT_GLOBAL_LOG_LEVEL = LogPyros.LOG_LEVEL_INFO # INFO
26 38  
27 39 SYSTEM_REBOOT_COMMAND = "sudo systemctl restart foo.service"
28 40 \ No newline at end of file
... ...
src/core/pyros_django/agent/Agent.py
... ... @@ -238,7 +238,7 @@ class Agent:
238 238 TEST_MODE = False
239 239  
240 240 # Default LOG level is INFO
241   - PYROS_DEFAULT_GLOBAL_LOG_LEVEL = LogPyros.LOG_LEVEL_INFO # INFO
  241 + #PYROS_DEFAULT_GLOBAL_LOG_LEVEL = LogPyros.LOG_LEVEL_INFO # INFO
242 242  
243 243 # To be overriden by subclasses (empty by default, no agent specific command)
244 244 AGENT_SPECIFIC_COMMANDS = [
... ... @@ -370,9 +370,11 @@ class Agent:
370 370 MODE_ACTIVE = "ACTIVE"
371 371 MODE_IDLE = "IDLE"
372 372  
  373 + ''' Moved to more central file : config.config_base
373 374 PYROS_DJANGO_BASE_DIR = Path("src/core/pyros_django") # pathlib
374 375 DEFAULT_CONFIG_FILE_NAME = "config_unit_simulunit1.xml"
375 376 CONFIG_DIR_NAME = "config"
  377 + '''
376 378  
377 379 # Parameters from config file
378 380 # for /src/
... ... @@ -431,35 +433,21 @@ class Agent:
431 433 log_level = LogPyros.LOG_LEVEL_INFO # INFO
432 434 self.log.set_global_log_level(LogPyros.LOG_LEVEL_DEBUG) if self.DEBUG_MODE else self.log.set_global_log_level(log_level)
433 435 '''
434   - global_log_level = LogPyros.LOG_LEVEL_DEBUG if self.DEBUG_MODE else self.PYROS_DEFAULT_GLOBAL_LOG_LEVEL
  436 + #global_log_level = LogPyros.LOG_LEVEL_DEBUG if self.DEBUG_MODE else self.PYROS_DEFAULT_GLOBAL_LOG_LEVEL
  437 + global_log_level = LogPyros.LOG_LEVEL_DEBUG if self.DEBUG_MODE else config.PYROS_DEFAULT_GLOBAL_LOG_LEVEL
435 438 self.log.set_global_log_level(global_log_level)
436 439 ##self.printd("LOG LEVEL IS:", self.log.debug_level)
437 440 self.print("LOG LEVEL IS:", self.log.get_global_log_level())
438 441  
  442 + # Est-ce bien utile ???
439 443 # New way with PathLib
440 444 my_parent_abs_dir = Path(__file__).resolve().parent
441 445 #TODO: on doit pouvoir faire mieux avec pathlib (sans utiliser str())
442   - self._path_data = str( Path( str(my_parent_abs_dir).split(str(self.PYROS_DJANGO_BASE_DIR))[0] ) / self.CONFIG_DIR_NAME )
  446 + ##self._path_data = str( Path( str(my_parent_abs_dir).split(str(self.PYROS_DJANGO_BASE_DIR))[0] ) / self.CONFIG_DIR_NAME )
  447 + self._path_data = config.CONFIG_DIR
  448 +
443 449 #self._set_mode(self.MODE_IDLE)
444 450 config_filename = self.get_config_filename(config_filename)
445   - '''
446   - if not config_filename:
447   - config_filename = self.DEFAULT_CONFIG_FILE_NAME
448   -
449   - # If config file name is RELATIVE (i.e. without path, just the file name)
450   - # => give it an absolute path (and remove "src/agent/" from it)
451   - if config_filename == os.path.basename(config_filename):
452   - # Build abs path including current agent dir
453   - config_filename = os.path.abspath(self.CONFIG_DIR_NAME + os.sep + config_filename)
454   - # Remove "src/agent_name/" from abs dir :
455   - # (1) Remove "src/core/pyros_django/"
456   - config_filename = config_filename.replace(str(self.PYROS_DJANGO_BASE_DIR)+os.sep, os.sep)
457   - # (2) Remove "agent_name/"
458   - #TODO: bidouille, faire plus propre
459   - config_filename = config_filename.replace(os.sep+"agent"+os.sep, os.sep)
460   - config_filename = config_filename.replace(os.sep+"monitoring"+os.sep, os.sep)
461   - config_filename = os.path.normpath(config_filename)
462   - '''
463 451 self.printd(f"*** Config file used is={config_filename}")
464 452 self.config = ConfigPyros(config_filename)
465 453 if self.config.get_last_errno() != self.config.NO_ERROR:
... ... @@ -501,10 +489,13 @@ class Agent:
501 489  
502 490 def get_config_filename(self, config_filename: str):
503 491 if not config_filename:
504   - config_filename = self.DEFAULT_CONFIG_FILE_NAME
  492 + #config_filename = self.DEFAULT_CONFIG_FILE_NAME
  493 + config_filename = config.DEFAULT_CONFIG_FILE_NAME
505 494 # If config file name is RELATIVE (i.e. without path, just the file name)
506 495 # => give it an absolute path (and remove "src/agent/" from it)
507 496 if config_filename == os.path.basename(config_filename):
  497 + config_filename = os.path.join(config.CONFIG_DIR, config_filename)
  498 + '''
508 499 # Build abs path including current agent dir
509 500 config_filename = os.path.abspath(self.CONFIG_DIR_NAME + os.sep + config_filename)
510 501 # Remove "src/agent_name/" from abs dir :
... ... @@ -514,6 +505,7 @@ class Agent:
514 505 #TODO: bidouille, faire plus propre
515 506 config_filename = config_filename.replace(os.sep+"agent"+os.sep, os.sep)
516 507 config_filename = config_filename.replace(os.sep+"monitoring"+os.sep, os.sep)
  508 + '''
517 509 return os.path.normpath(config_filename)
518 510  
519 511 def __repr__(self):
... ...