Skip to content

Commit

Permalink
Using environment variables to set odoo config parameters along with …
Browse files Browse the repository at this point in the history
….conf template files
  • Loading branch information
PCatinean committed Mar 1, 2020
1 parent 5a5d94d commit ae209eb
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 4 deletions.
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.

Expand Down
34 changes: 30 additions & 4 deletions bin/config-generate
Original file line number Diff line number Diff line change
Expand Up @@ -3,23 +3,31 @@
"""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
from StringIO import StringIO
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()
Expand All @@ -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:
Expand All @@ -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)
Expand Down

0 comments on commit ae209eb

Please sign in to comment.