Commit 1192cc3da85826fd876748607a330c6bc96dbd78
1 parent
d02a5e4b
Exists in
dev
Nouveau système de configuration de "base" (ROOT_DIR, ...)
Inspiré de https://whalesalad.com/blog/doing-python-configuration-right et https://12factor.net/config
Showing
6 changed files
with
95 additions
and
0 deletions
Show diff stats
config/__init__.py
... | ... | @@ -0,0 +1,21 @@ |
1 | +# Inspired from https://whalesalad.com/blog/doing-python-configuration-right | |
2 | + | |
3 | +import os | |
4 | +import importlib | |
5 | + | |
6 | +# Determine the environment we want to load, default is development.py | |
7 | +PYROS_ENV = os.environ.get("PYROS_ENV", "config_dev") | |
8 | + | |
9 | +module = importlib.import_module( | |
10 | + #f".environments.{ENV}", package="service.config" | |
11 | + f".{PYROS_ENV}", package="config" | |
12 | +) | |
13 | + | |
14 | +# update globals of this module (i.e. settings) with imported. | |
15 | +globals().update(vars(module)) | |
16 | + | |
17 | + | |
18 | +def is_dev_env(): return PYROS_ENV == "config_dev" | |
19 | +def is_test_env(): return PYROS_ENV == "config_test" | |
20 | +def is_prod_env(): return PYROS_ENV == "config_prod" | |
21 | + | ... | ... |
... | ... | @@ -0,0 +1,27 @@ |
1 | +# Inspired from https://whalesalad.com/blog/doing-python-configuration-right | |
2 | + | |
3 | +import os | |
4 | + | |
5 | +APP_NAME = "PYROS" | |
6 | + | |
7 | +# Conveniences to always have a quick reference to the | |
8 | +# top-level application directory. | |
9 | +ROOT_DIR = os.path.join( | |
10 | + os.path.dirname(os.path.realpath(__file__)), | |
11 | + os.pardir, # '..' | |
12 | +) | |
13 | +ROOT_DIR = os.path.realpath(ROOT_DIR) | |
14 | + | |
15 | +# root directories | |
16 | +DOC_DIR = os.path.join(ROOT_DIR, "doc") | |
17 | +INSTALL_DIR = os.path.join(ROOT_DIR, "install") | |
18 | +LOG_DIR = os.path.join(ROOT_DIR, "config", "logs") | |
19 | +SOURCE_DIR = os.path.join(ROOT_DIR, "src") | |
20 | + | |
21 | +# source sub-directories | |
22 | +CELME_DIR = os.path.join(SOURCE_DIR, "core", "celme") | |
23 | +PYROS_DJANGO_DIR = os.path.join(SOURCE_DIR, "core", "pyros_django") | |
24 | +DEVICES_DIR = os.path.join(SOURCE_DIR, "device_controller") | |
25 | + | |
26 | + | |
27 | +SYSTEM_REBOOT_COMMAND = "sudo systemctl restart foo.service" | |
0 | 28 | \ No newline at end of file | ... | ... |
src/core/pyros_django/agent/Agent.py
... | ... | @@ -110,6 +110,11 @@ from django import db |
110 | 110 | #from django.conf import settings as djangosettings |
111 | 111 | |
112 | 112 | # --- SPECIFIC IMPORT --- |
113 | +# Many ways to import configuration settings: | |
114 | +import config | |
115 | +#from config import PYROS_ENV, ROOT_DIR, DOC_DIR | |
116 | +#from config import * | |
117 | + | |
113 | 118 | from common.models import AgentSurvey, Command, AgentLogs |
114 | 119 | from config.configpyros import ConfigPyros |
115 | 120 | #from dashboard.views import get_sunelev |
... | ... | @@ -402,6 +407,18 @@ class Agent: |
402 | 407 | #def __init__(self, name:str="Agent", config_filename:str=None, RUN_IN_THREAD=True): |
403 | 408 | #def __init__(self, config_filename:str=None, RUN_IN_THREAD=True, DEBUG_MODE=False): |
404 | 409 | def __init__(self, config_filename:str=None, RUN_IN_THREAD=True): |
410 | + | |
411 | + ''' | |
412 | + print('PYROS_ENV', PYROS_ENV) | |
413 | + print('ROOT_DIR', ROOT_DIR) | |
414 | + print('DOC_DIR', DOC_DIR) | |
415 | + ''' | |
416 | + print('PYROS_ENV', config.PYROS_ENV) | |
417 | + print('ROOT_DIR', config.ROOT_DIR) | |
418 | + print('DOC_DIR', config.DOC_DIR) | |
419 | + if config.is_dev_env(): print("DEV ENV") | |
420 | + if config.is_prod_env(): print("PROD ENV") | |
421 | + | |
405 | 422 | #self.name = name |
406 | 423 | self.name = self.__class__.__name__ |
407 | 424 | printd("*** ENVIRONMENT VARIABLE PYROS_DEBUG is:", os.environ.get('PYROS_DEBUG'), '***') | ... | ... |