Skip to content

Commit

Permalink
[IMP] server_environmnet: allow to overwrite odoo config options from…
Browse files Browse the repository at this point in the history
… server_environment_files
  • Loading branch information
petrus-v authored and thienvh332 committed Sep 24, 2024
1 parent e9533df commit b9ec8c3
Show file tree
Hide file tree
Showing 5 changed files with 110 additions and 22 deletions.
24 changes: 15 additions & 9 deletions server_environment/README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,9 @@ on the configured environment: you define the environment in the main
configuration file, and the values for the various possible environments
are stored in the ``server_environment_files`` companion module.

The ``server_environment_files`` module is optional, the values can be
set using an environment variable with a fallback on default values in
the database.
The ``server_environment_files`` module is optional, the values can be set using
an environment variable with a fallback on default values in the database. you
will be able to overwrite some odoo options.

The configuration read from the files are visible under the
Configuration menu. If you are not in the 'dev' environment you will not
Expand Down Expand Up @@ -96,12 +96,18 @@ You can edit the settings you need in the ``server_environment_files``
addon. The ``server_environment_files_sample`` can be used as an
example:

- values common to all / most environments can be stored in the
``default/`` directory using the .ini file syntax;
- each environment you need to define is stored in its own directory
and can override or extend default values;
- you can override or extend values in the main configuration file of
your instance;
* values common to all / most environments can be stored in the
``default/`` directory using the .ini file syntax;
* each environment you need to define is stored in its own directory
and can override or extend default values;
* you can override or extend values in the main configuration
file of your instance;
* In some platforms (like odoo.sh where production config file is copied to staging)
it can be usefull to overwrite options write in the `[options]` section. You must
allow the overwrite by adding `server_environment_allow_overwrite_options_section = True``
to the former `odoo.cfg` config file or through the environment variable:
`export SERVER_ENVIRONMENT_ALLOW_OVERWRITE_OPTIONS_SECTION=True` (if both are set
config file take precedent).

Environment variable
--------------------
Expand Down
14 changes: 14 additions & 0 deletions server_environment/server_env.py
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,19 @@ def _listconf(env_path):
return files


def _update_odoo_config_options(config_p):
allow_overwrite = system_base_config.get(
"server_environment_allow_overwrite_options_section",
os.environ.get("SERVER_ENVIRONMENT_ALLOW_OVERWRITE_OPTIONS_SECTION"),
)
if isinstance(allow_overwrite, str) and allow_overwrite:
allow_overwrite = _boolean_states.get(allow_overwrite.lower(), False)
if allow_overwrite and config_p.has_section("options"):
system_base_config.options.update(
{k: v for k, v in config_p["options"].items()}
)


def _load_config_from_server_env_files(config_p):
default = os.path.join(_dir, "default")
running_env = os.path.join(_dir, system_base_config["running_env"])
Expand All @@ -119,6 +132,7 @@ def _load_config_from_server_env_files(config_p):
config_p.read(conf_files)
except Exception as e:
raise Exception(f'Cannot read config files "{conf_files}": {e}') from e
_update_odoo_config_options(config_p)


def _load_config_from_rcfile(config_p):
Expand Down
33 changes: 20 additions & 13 deletions server_environment/static/description/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -371,17 +371,18 @@ <h1 class="title">server configuration environment files</h1>
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! -->
<p><a class="reference external image-reference" href="https://odoo-community.org/page/development-status"><img alt="Production/Stable" src="https://img.shields.io/badge/maturity-Production%2FStable-green.png" /></a> <a class="reference external image-reference" href="http://www.gnu.org/licenses/lgpl-3.0-standalone.html"><img alt="License: LGPL-3" src="https://img.shields.io/badge/licence-LGPL--3-blue.png" /></a> <a class="reference external image-reference" href="https://github.com/OCA/server-env/tree/17.0/server_environment"><img alt="OCA/server-env" src="https://img.shields.io/badge/github-OCA%2Fserver--env-lightgray.png?logo=github" /></a> <a class="reference external image-reference" href="https://translation.odoo-community.org/projects/server-env-17-0/server-env-17-0-server_environment"><img alt="Translate me on Weblate" src="https://img.shields.io/badge/weblate-Translate%20me-F47D42.png" /></a> <a class="reference external image-reference" href="https://runboat.odoo-community.org/builds?repo=OCA/server-env&amp;target_branch=17.0"><img alt="Try me on Runboat" src="https://img.shields.io/badge/runboat-Try%20me-875A7B.png" /></a></p>
<p>This module provides a way to define an environment in the main Odoo
configuration file and to read some configurations from files depending
on the configured environment: you define the environment in the main
configuration file, and the values for the various possible environments
are stored in the <tt class="docutils literal">server_environment_files</tt> companion module.</p>
<p>The <tt class="docutils literal">server_environment_files</tt> module is optional, the values can be
set using an environment variable with a fallback on default values in
the database.</p>
<p>The configuration read from the files are visible under the
Configuration menu. If you are not in the ‘dev’ environment you will not
be able to see the values contained in the defined secret keys (by
default : ‘<em>passw</em>’, ‘<em>key</em>’, ‘<em>secret</em>’ and ‘<em>token</em>’).</p>
configuration file and to read some configurations from files
depending on the configured environment: you define the environment in
the main configuration file, and the values for the various possible
environments are stored in the <tt class="docutils literal">server_environment_files</tt> companion
module.</p>
<p>The <tt class="docutils literal">server_environment_files</tt> module is optional, the values can be set using
an environment variable with a fallback on default values in the database. you
will be able to overwrite some odoo options.</p>
<p>The configuration read from the files are visible under the Configuration
menu. If you are not in the ‘dev’ environment you will not be able to
see the values contained in the defined secret keys
(by default : ‘<em>passw</em>’, ‘<em>key</em>’, ‘<em>secret</em>’ and ‘<em>token</em>’).</p>
<p><strong>Table of contents</strong></p>
<div class="contents local topic" id="contents">
<ul class="simple">
Expand Down Expand Up @@ -445,8 +446,14 @@ <h2><a class="toc-backref" href="#toc-entry-3">server_environment_files</a></h2>
<tt class="docutils literal">default/</tt> directory using the .ini file syntax;</li>
<li>each environment you need to define is stored in its own directory
and can override or extend default values;</li>
<li>you can override or extend values in the main configuration file of
your instance;</li>
<li>you can override or extend values in the main configuration
file of your instance;</li>
<li>In some platforms (like odoo.sh where production config file is copied to staging)
it can be usefull to overwrite options write in the <cite>[options]</cite> section. You must
allow the overwrite by adding <cite>server_environment_allow_overwrite_options_section = True`</cite>
to the former <cite>odoo.cfg</cite> config file or through the environment variable:
<cite>export SERVER_ENVIRONMENT_ALLOW_OVERWRITE_OPTIONS_SECTION=True</cite> (if both are set
config file take precedent).</li>
</ul>
</div>
<div class="section" id="environment-variable">
Expand Down
58 changes: 58 additions & 0 deletions server_environment/tests/test_server_environment.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,3 +61,61 @@ def test_value_retrival(self):
self.assertEqual(val, "testing")
val = parser.get("external_service.ftp", "host")
self.assertEqual(val, "sftp.example.com")

@patch.dict(os.environ, {"SERVER_ENVIRONMENT_ALLOW_OVERWRITE_OPTIONS_SECTION": "0"})
@patch.dict(
odoo_config.options,
{
"running_env": "testing",
"server_environment_allow_overwrite_options_section": True,
"odoo_test_option": "fake odoo config",
},
)
def test_server_environment_allow_overwrite_options_section(self):
with self.set_config_dir("testfiles"):
server_env._load_config()
self.assertEqual(
odoo_config["odoo_test_option"], "Set in config file for testing env"
)

@patch.dict(os.environ, {"SERVER_ENVIRONMENT_ALLOW_OVERWRITE_OPTIONS_SECTION": "1"})
@patch.dict(
odoo_config.options,
{
"running_env": "testing",
"server_environment_allow_overwrite_options_section": False,
"odoo_test_option": "fake odoo config",
},
)
def test_server_environment_disabled_overwrite_options_section(self):
with self.set_config_dir("testfiles"):
server_env._load_config()
self.assertEqual(odoo_config["odoo_test_option"], "fake odoo config")

@patch.dict(os.environ, {"SERVER_ENVIRONMENT_ALLOW_OVERWRITE_OPTIONS_SECTION": "1"})
@patch.dict(
odoo_config.options,
{
"running_env": "testing",
"odoo_test_option": "fake odoo config",
},
)
def test_server_environment_allow_overwrite_options_section_by_env(self):
with self.set_config_dir("testfiles"):
server_env._load_config()
self.assertEqual(
odoo_config["odoo_test_option"], "Set in config file for testing env"
)

@patch.dict(os.environ, {"SERVER_ENVIRONMENT_ALLOW_OVERWRITE_OPTIONS_SECTION": "0"})
@patch.dict(
odoo_config.options,
{
"running_env": "testing",
"odoo_test_option": "fake odoo config",
},
)
def test_server_environment_disabled_overwrite_options_section_by_env(self):
with self.set_config_dir("testfiles"):
server_env._load_config()
self.assertEqual(odoo_config["odoo_test_option"], "fake odoo config")
3 changes: 3 additions & 0 deletions server_environment/tests/testfiles/testing/base.conf
Original file line number Diff line number Diff line change
@@ -1,2 +1,5 @@
[options]
odoo_test_option = Set in config file for testing env

[external_service.ftp]
user = testing

0 comments on commit b9ec8c3

Please sign in to comment.