33"""Generate Odoo server configuration from templates"""
44
55import os
6+ import sys
67from contextlib import closing
78from string import Template
89
9- from doodbalib import logger
10+ from doodbalib import logger , ODOO_DIR
1011
1112try :
1213 # Python 2, where io.StringIO fails because it is unicode-only
1314 from StringIO import StringIO
1415except ImportError :
1516 from io import StringIO
1617
18+ try :
19+ from odoo .tools .config import configmanager
20+ except ImportError :
21+ # For non-pip installations
22+ sys .path .append (ODOO_DIR )
23+ from odoo .tools .config import configmanager
24+
1725try :
1826 from configparser import RawConfigParser
1927
@@ -29,9 +37,8 @@ TARGET_FILE = os.environ.get("OPENERP_SERVER", "/opt/odoo/auto/odoo.conf")
2937if ODOO_VERSION not in {"8.0" , "9.0" }:
3038 TARGET_FILE = os .environ .get ("ODOO_RC" , TARGET_FILE )
3139CONFIG_DIRS = ("/opt/odoo/common/conf.d" , "/opt/odoo/custom/conf.d" )
32- CONFIG_FILES = []
3340
34- # Read all configuraiton files found in those folders
41+ # Read all configuration files found in those folders
3542logger .info ("Merging found configuration files in %s" , TARGET_FILE )
3643for dir_ in CONFIG_DIRS :
3744 try :
@@ -40,6 +47,24 @@ for dir_ in CONFIG_DIRS:
4047 except OSError : # TODO Use FileNotFoundError when we drop python 2
4148 continue
4249
50+
51+ # Add configuration parameters sent via env vars
52+ odoo_cfg = configmanager ()
53+ cfg_env_prefix = 'ODOO_CFG_'
54+
55+ for k , val in os .environ .items ():
56+ if not val or not k .startswith (cfg_env_prefix ):
57+ continue
58+
59+ parameter = k .split (cfg_env_prefix )[1 ].lower ()
60+ if parameter in odoo_cfg .options :
61+ parser .set ('options' , parameter , val )
62+
63+ # Warn user about all invalid parameters
64+ invalid_params = set (parser .options ) - set (odoo_cfg .options )
65+ for param in invalid_params :
66+ logger .warning ("'%s' is not a valid Odoo configuration parameter" )
67+
4368# Write it to a memory string object
4469with closing (StringIO ()) as resultfp :
4570 parser .write (resultfp )
0 commit comments