Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Smarter, less strict, config generator #227

Merged
merged 2 commits into from
Jul 17, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion 11.0.Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ RUN python -m venv --system-site-packages /qa/venv \
flake8 \
pylint-odoo \
six \
&& npm install --loglevel error --prefix /qa eslint \
&& npm install --loglevel error --prefix /qa 'eslint@<6' \
&& deactivate \
&& mkdir -p /qa/artifacts \
&& git clone --depth 1 $MQT /qa/mqt
Expand Down
2 changes: 1 addition & 1 deletion 8.0.Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ RUN virtualenv --system-site-packages /qa/venv \
flake8 \
pylint-odoo \
six \
&& npm install --loglevel error --prefix /qa eslint \
&& npm install --loglevel error --prefix /qa 'eslint@<6' \
&& deactivate \
&& mkdir -p /qa/artifacts \
&& git clone --depth 1 $MQT /qa/mqt
Expand Down
60 changes: 51 additions & 9 deletions bin/config-generate
Original file line number Diff line number Diff line change
@@ -1,9 +1,51 @@
#!/bin/bash
# Generate Odoo server configuration from templates
set -e
src="/opt/odoo/common/conf.d/* /opt/odoo/custom/conf.d/*"
log INFO Merging $(ls $src | wc -l) configuration files in $OPENERP_SERVER
conf=$(cat $src | envsubst)
log DEBUG "Resulting configuration:
$conf"
echo "$conf" > $OPENERP_SERVER
#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""Generate Odoo server configuration from templates"""

import os
from contextlib import closing
from doodbalib import logger
from string import Template

try:
# Python 2, where io.StringIO fails because it is unicode-only
from StringIO import StringIO
except ImportError:
from io import StringIO

try:
from configparser import RawConfigParser
parser = RawConfigParser(strict=False)
except ImportError:
# Python 2, where strict=True doesn't exist
from ConfigParser import RawConfigParser
parser = RawConfigParser()

ODOO_VERSION = os.environ.get("ODOO_VERSION")
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
logger.info("Merging found configuration files in %s", TARGET_FILE)
for dir_ in CONFIG_DIRS:
try:
for file_ in os.listdir(dir_):
parser.read(os.path.join(dir_, file_))
except OSError: # TODO Use FileNotFoundError when we drop python 2
continue

# Write it to a memory string object
with closing(StringIO()) as resultfp:
parser.write(resultfp)
resultfp.seek(0)
# Obtain the config string
result = resultfp.read()
# Expand environment variables found within
result = Template(result).substitute(os.environ)
logger.debug("Resulting configuration:\n%s", result)
# Write it to destination
with open(TARGET_FILE, "w") as targetfp:
targetfp.write(result)
1 change: 1 addition & 0 deletions conf.d/10-addons.conf
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
[options]
; Addons in priority order: private, then other repos, then base Odoo
addons_path = /opt/odoo/auto/addons
1 change: 1 addition & 0 deletions conf.d/20-database.conf
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
[options]
; Database settings, matching defaults when you execute `psql`
db_user = $PGUSER
db_password = $PGPASSWORD
Expand Down
1 change: 1 addition & 0 deletions conf.d/30-proxy-mode.conf
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
[options]
; Normally Odoo should run behind a proxy
proxy_mode = $PROXY_MODE
1 change: 1 addition & 0 deletions conf.d/40-smtp.conf
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
[options]
; Prepared to link with https://hub.docker.com/r/tecnativa/postfix-relay
smtp_server = $SMTP_SERVER
smtp_port = $SMTP_PORT
Expand Down
1 change: 1 addition & 0 deletions conf.d/50-demo-data.conf
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
[options]
; Useful for testing, useless for production, disabled by default
without_demo = $WITHOUT_DEMO
3 changes: 3 additions & 0 deletions tests/scaffoldings/settings/custom/conf.d/00-defaults.conf
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
[options]
# Simulate a default value of 1000 MiB
limit_memory_soft = 1048576000
3 changes: 3 additions & 0 deletions tests/scaffoldings/settings/custom/conf.d/10-custom.conf
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
[options]
# Customize the default to 2000 MiB
limit_memory_soft = 2097152000
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@


assert config.get("email_from") == "test@example.com"
assert config.get("limit_memory_soft") == 2097152000
assert config.get("smtp_password") is False
assert config.get("smtp_port") == 1025
assert config.get("smtp_server") == "mailhog"
Expand Down