From aa723aee27cf6f4f8eb84f6925c1f1337aa719ec Mon Sep 17 00:00:00 2001 From: Paul Date: Sun, 1 Mar 2020 21:12:29 +0100 Subject: [PATCH] Using environment variables to set odoo config parameters along with .conf template files --- README.md | 4 ++++ bin/config-generate | 31 ++++++++++++++++++++++++++++--- 2 files changed, 32 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index d963daa4..694be0a3 100644 --- a/README.md +++ b/README.md @@ -402,9 +402,13 @@ It will be full of symlinks to the addons you selected in [`addons.yaml`][]. #### `/opt/odoo/auto/odoo.conf` +> :warning: **Deprecated**: Configuration file is now generated via environment +variables set as such `$ODOO_CFG_{ODOO_CFG_PARAMETER}` + It will have the result of merging all configurations under `/opt/odoo/{common,custom}/conf.d/`, in that order. + ## The `Dockerfile` I will document all build arguments and environment variables some day, but for diff --git a/bin/config-generate b/bin/config-generate index 88e33d71..4c911f6a 100755 --- a/bin/config-generate +++ b/bin/config-generate @@ -3,10 +3,11 @@ """Generate Odoo server configuration from templates""" import os +import sys from contextlib import closing from string import Template -from doodbalib import logger +from doodbalib import logger, ODOO_DIR try: # Python 2, where io.StringIO fails because it is unicode-only @@ -14,6 +15,13 @@ try: except ImportError: from io import StringIO +try: + from odoo.tools.config import configmanager +except ImportError: + # For non-pip installations + sys.path.append(ODOO_DIR) + from odoo.tools.config import configmanager + try: from configparser import RawConfigParser @@ -29,9 +37,8 @@ TARGET_FILE = os.environ.get("OPENERP_SERVER", "/opt/odoo/auto/odoo.conf") if ODOO_VERSION not in {"8.0", "9.0"}: TARGET_FILE = os.environ.get("ODOO_RC", TARGET_FILE) CONFIG_DIRS = ("/opt/odoo/common/conf.d", "/opt/odoo/custom/conf.d") -CONFIG_FILES = [] -# Read all configuraiton files found in those folders +# Read all configuration files found in those folders logger.info("Merging found configuration files in %s", TARGET_FILE) for dir_ in CONFIG_DIRS: try: @@ -40,6 +47,24 @@ for dir_ in CONFIG_DIRS: except OSError: # TODO Use FileNotFoundError when we drop python 2 continue + +# Add configuration parameters sent via env vars +odoo_cfg = configmanager() +cfg_env_prefix = 'ODOO_CFG_' + +for k, val in os.environ.items(): + if not val or not k.startswith(cfg_env_prefix): + continue + + parameter = k.split(cfg_env_prefix)[1].lower() + if parameter in odoo_cfg.options: + parser.set('options', parameter, val) + +# Warn user about all invalid parameters +invalid_params = set(parser.options) - set(odoo_cfg.options) +for param in invalid_params: + logger.warning("'%s' is not a valid Odoo configuration parameter") + # Write it to a memory string object with closing(StringIO()) as resultfp: parser.write(resultfp)