Skip to content

Commit ba9d961

Browse files
committed
Using environment variables to set odoo config parameters along with .conf template files
1 parent 5a5d94d commit ba9d961

File tree

2 files changed

+33
-4
lines changed

2 files changed

+33
-4
lines changed

README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -402,6 +402,9 @@ It will be full of symlinks to the addons you selected in [`addons.yaml`][].
402402

403403
#### `/opt/odoo/auto/odoo.conf`
404404

405+
> :warning: **Deprecated**: Configuration file is now generated via environment
406+
variables set as such `$ODOO_CFG_{ODOO_CFG_PARAMETER}`
407+
405408
It will have the result of merging all configurations under
406409
`/opt/odoo/{common,custom}/conf.d/`, in that order.
407410

bin/config-generate

Lines changed: 30 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,23 +3,31 @@
33
"""Generate Odoo server configuration from templates"""
44

55
import os
6+
import sys
67
from contextlib import closing
78
from string import Template
89

9-
from doodbalib import logger
10+
from doodbalib import logger, ODOO_DIR
1011

1112
try:
1213
# Python 2, where io.StringIO fails because it is unicode-only
1314
from StringIO import StringIO
1415
except 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+
1725
try:
1826
from configparser import RawConfigParser
1927

2028
parser = RawConfigParser(strict=False)
2129
except ImportError:
22-
# Python 2, where strict=True doesn't exist
30+
# Python 2, where strict=True doesn"t exist
2331
from ConfigParser import RawConfigParser
2432

2533
parser = RawConfigParser()
@@ -29,9 +37,8 @@ TARGET_FILE = os.environ.get("OPENERP_SERVER", "/opt/odoo/auto/odoo.conf")
2937
if ODOO_VERSION not in {"8.0", "9.0"}:
3038
TARGET_FILE = os.environ.get("ODOO_RC", TARGET_FILE)
3139
CONFIG_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
3542
logger.info("Merging found configuration files in %s", TARGET_FILE)
3643
for dir_ in CONFIG_DIRS:
3744
try:
@@ -40,6 +47,25 @@ 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+
logger.info("Updating configuration file with set environment variables")
53+
odoo_cfg = configmanager()
54+
cfg_env_prefix = "ODOO_CFG_"
55+
56+
for k, val in os.environ.items():
57+
if not val or not k.startswith(cfg_env_prefix):
58+
continue
59+
60+
parameter = k.split(cfg_env_prefix)[1].lower()
61+
if parameter in odoo_cfg.options:
62+
parser.set("options", parameter, val)
63+
64+
# Warn user about all invalid parameters
65+
invalid_params = set(parser.options) - set(odoo_cfg.options)
66+
for param in invalid_params:
67+
logger.warning("'%s' is not a valid Odoo configuration parameter")
68+
4369
# Write it to a memory string object
4470
with closing(StringIO()) as resultfp:
4571
parser.write(resultfp)

0 commit comments

Comments
 (0)