From ba9d961cbf8ff1b274ac51c600a6e976a1fbf6a8 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 | 3 +++ bin/config-generate | 34 ++++++++++++++++++++++++++++++---- 2 files changed, 33 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index d963daa4..99c375f9 100644 --- a/README.md +++ b/README.md @@ -402,6 +402,9 @@ 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. diff --git a/bin/config-generate b/bin/config-generate index 88e33d71..6a5c791a 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,12 +15,19 @@ 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 parser = RawConfigParser(strict=False) except ImportError: - # Python 2, where strict=True doesn't exist + # Python 2, where strict=True doesn"t exist from ConfigParser import RawConfigParser parser = 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,25 @@ for dir_ in CONFIG_DIRS: except OSError: # TODO Use FileNotFoundError when we drop python 2 continue + +# Add configuration parameters sent via env vars +logger.info("Updating configuration file with set environment variables") +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)