Skip to content

Commit 8305b4d

Browse files
petrus-vmetaminux
authored andcommitted
[IMP] server_environmnet: allow to overwrite odoo config options from server_environment_files
1 parent 4a3b946 commit 8305b4d

8 files changed

Lines changed: 126 additions & 34 deletions

File tree

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
{
2+
"pull_requests": {
3+
"OCA/server-env#176": "(auto) Nothing to port from PR #176",
4+
"OCA/server-env#190": "(auto) Nothing to port from PR #190"
5+
}
6+
}

server_environment/README.rst

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,3 @@
1-
.. image:: https://odoo-community.org/readme-banner-image
2-
:target: https://odoo-community.org/get-involved?utm_source=readme
3-
:alt: Odoo Community Association
4-
51
======================================
62
server configuration environment files
73
======================================
@@ -11,13 +7,13 @@ server configuration environment files
117
!! This file is generated by oca-gen-addon-readme !!
128
!! changes will be overwritten. !!
139
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
14-
!! source digest: sha256:39cd61c3a9621d0b7ee7a346adf45f4a2c90e80e6a61e18f182386c6fc90fd19
10+
!! source digest: sha256:dcc784d558683190d604281faea315ff44a3c8ffe998f31f749b06e4def888e1
1511
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
1612
1713
.. |badge1| image:: https://img.shields.io/badge/maturity-Production%2FStable-green.png
1814
:target: https://odoo-community.org/page/development-status
1915
:alt: Production/Stable
20-
.. |badge2| image:: https://img.shields.io/badge/license-LGPL--3-blue.png
16+
.. |badge2| image:: https://img.shields.io/badge/licence-LGPL--3-blue.png
2117
:target: http://www.gnu.org/licenses/lgpl-3.0-standalone.html
2218
:alt: License: LGPL-3
2319
.. |badge3| image:: https://img.shields.io/badge/github-OCA%2Fserver--env-lightgray.png?logo=github
@@ -40,7 +36,7 @@ are stored in the ``server_environment_files`` companion module.
4036

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

4541
The configuration read from the files are visible under the
4642
Configuration menu. If you are not in the 'dev' environment you will not
@@ -106,6 +102,13 @@ example:
106102
can override or extend default values;
107103
- you can override or extend values in the main configuration file of
108104
your instance;
105+
- In some platforms (like odoo.sh where production config file is copied
106+
to staging) it can be useful to overwrite options written in the
107+
``[options]`` section. You must allow the override by adding
108+
``server_environment_allow_overwrite_options_section = True`` to the
109+
former ``odoo.cfg`` config file or through the environment variable:
110+
``export SERVER_ENVIRONMENT_ALLOW_OVERWRITE_OPTIONS_SECTION=True`` (if
111+
both are set config file takes precedence).
109112

110113
Environment variable
111114
--------------------

server_environment/readme/CONFIGURE.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,13 @@ addon. The `server_environment_files_sample` can be used as an example:
2929
can override or extend default values;
3030
- you can override or extend values in the main configuration file of
3131
your instance;
32+
- In some platforms (like odoo.sh where production config file is copied to staging)
33+
it can be useful to overwrite options written in the `[options]` section. You must
34+
allow the override by adding `server_environment_allow_overwrite_options_section = True`
35+
to the former `odoo.cfg` config file or through the environment variable:
36+
`export SERVER_ENVIRONMENT_ALLOW_OVERWRITE_OPTIONS_SECTION=True` (if both are set
37+
config file takes precedence).
38+
3239

3340
## Environment variable
3441

server_environment/readme/DESCRIPTION.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@ configuration file, and the values for the various possible environments
55
are stored in the `server_environment_files` companion module.
66

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

1111
The configuration read from the files are visible under the
1212
Configuration menu. If you are not in the 'dev' environment you will not

server_environment/server_env.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,19 @@ def _listconf(env_path):
107107
return files
108108

109109

110+
def _update_odoo_config_options(config_p):
111+
allow_overwrite = system_base_config.get(
112+
"server_environment_allow_overwrite_options_section",
113+
os.environ.get("SERVER_ENVIRONMENT_ALLOW_OVERWRITE_OPTIONS_SECTION"),
114+
)
115+
if isinstance(allow_overwrite, str) and allow_overwrite:
116+
allow_overwrite = _boolean_states.get(allow_overwrite.lower(), False)
117+
if allow_overwrite and config_p.has_section("options"):
118+
system_base_config.options.update(
119+
{k: v for k, v in config_p["options"].items()}
120+
)
121+
122+
110123
def _load_config_from_server_env_files(config_p):
111124
default = os.path.join(_dir, "default")
112125
running_env = os.path.join(_dir, system_base_config["running_env"])
@@ -119,6 +132,7 @@ def _load_config_from_server_env_files(config_p):
119132
config_p.read(conf_files)
120133
except Exception as e:
121134
raise Exception(f'Cannot read config files "{conf_files}": {e}') from e
135+
_update_odoo_config_options(config_p)
122136

123137

124138
def _load_config_from_rcfile(config_p):

server_environment/static/description/index.html

Lines changed: 26 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
<head>
44
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
55
<meta name="generator" content="Docutils: https://docutils.sourceforge.io/" />
6-
<title>README.rst</title>
6+
<title>server configuration environment files</title>
77
<style type="text/css">
88

99
/*
@@ -360,29 +360,24 @@
360360
</style>
361361
</head>
362362
<body>
363-
<div class="document">
363+
<div class="document" id="server-configuration-environment-files">
364+
<h1 class="title">server configuration environment files</h1>
364365

365-
366-
<a class="reference external image-reference" href="https://odoo-community.org/get-involved?utm_source=readme">
367-
<img alt="Odoo Community Association" src="https://odoo-community.org/readme-banner-image" />
368-
</a>
369-
<div class="section" id="server-configuration-environment-files">
370-
<h1>server configuration environment files</h1>
371366
<!-- !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
372367
!! This file is generated by oca-gen-addon-readme !!
373368
!! changes will be overwritten. !!
374369
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
375-
!! source digest: sha256:39cd61c3a9621d0b7ee7a346adf45f4a2c90e80e6a61e18f182386c6fc90fd19
370+
!! source digest: sha256:dcc784d558683190d604281faea315ff44a3c8ffe998f31f749b06e4def888e1
376371
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! -->
377-
<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/license-LGPL--3-blue.png" /></a> <a class="reference external image-reference" href="https://github.com/OCA/server-env/tree/18.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-18-0/server-env-18-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=18.0"><img alt="Try me on Runboat" src="https://img.shields.io/badge/runboat-Try%20me-875A7B.png" /></a></p>
372+
<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/18.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-18-0/server-env-18-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=18.0"><img alt="Try me on Runboat" src="https://img.shields.io/badge/runboat-Try%20me-875A7B.png" /></a></p>
378373
<p>This module provides a way to define an environment in the main Odoo
379374
configuration file and to read some configurations from files depending
380375
on the configured environment: you define the environment in the main
381376
configuration file, and the values for the various possible environments
382377
are stored in the <tt class="docutils literal">server_environment_files</tt> companion module.</p>
383378
<p>The <tt class="docutils literal">server_environment_files</tt> module is optional, the values can be
384379
set using an environment variable with a fallback on default values in
385-
the database.</p>
380+
the database. you will be able to overwrite some odoo options.</p>
386381
<p>The configuration read from the files are visible under the
387382
Configuration menu. If you are not in the ‘dev’ environment you will not
388383
be able to see the values contained in the defined secret keys (by
@@ -410,7 +405,7 @@ <h1>server configuration environment files</h1>
410405
</ul>
411406
</div>
412407
<div class="section" id="installation">
413-
<h2><a class="toc-backref" href="#toc-entry-1">Installation</a></h2>
408+
<h1><a class="toc-backref" href="#toc-entry-1">Installation</a></h1>
414409
<p>By itself, this module does little. See for instance the
415410
<tt class="docutils literal">mail_environment</tt> addon which depends on this one to allow
416411
configuring the incoming and outgoing mail servers depending on the
@@ -422,7 +417,7 @@ <h2><a class="toc-backref" href="#toc-entry-1">Installation</a></h2>
422417
<tt class="docutils literal">SERVER_ENV_CONFIG</tt> and <tt class="docutils literal">SERVER_ENV_CONFIG_SECRET</tt>.</p>
423418
</div>
424419
<div class="section" id="configuration">
425-
<h2><a class="toc-backref" href="#toc-entry-2">Configuration</a></h2>
420+
<h1><a class="toc-backref" href="#toc-entry-2">Configuration</a></h1>
426421
<p>To configure this module, you need to edit the main configuration file
427422
of your instance, and add a directive called <tt class="docutils literal">running_env</tt>. Commonly
428423
used values are ‘dev’, ‘test’, ‘production’:</p>
@@ -441,7 +436,7 @@ <h2><a class="toc-backref" href="#toc-entry-2">Configuration</a></h2>
441436
<p>If you don’t provide any value, test is used as a safe default.</p>
442437
<p>You have several possibilities to set configuration values:</p>
443438
<div class="section" id="server-environment-files">
444-
<h3><a class="toc-backref" href="#toc-entry-3">server_environment_files</a></h3>
439+
<h2><a class="toc-backref" href="#toc-entry-3">server_environment_files</a></h2>
445440
<p>You can edit the settings you need in the <tt class="docutils literal">server_environment_files</tt>
446441
addon. The <tt class="docutils literal">server_environment_files_sample</tt> can be used as an
447442
example:</p>
@@ -452,10 +447,17 @@ <h3><a class="toc-backref" href="#toc-entry-3">server_environment_files</a></h3>
452447
can override or extend default values;</li>
453448
<li>you can override or extend values in the main configuration file of
454449
your instance;</li>
450+
<li>In some platforms (like odoo.sh where production config file is copied
451+
to staging) it can be useful to overwrite options written in the
452+
<tt class="docutils literal">[options]</tt> section. You must allow the override by adding
453+
<tt class="docutils literal">server_environment_allow_overwrite_options_section = True</tt> to the
454+
former <tt class="docutils literal">odoo.cfg</tt> config file or through the environment variable:
455+
<tt class="docutils literal">export SERVER_ENVIRONMENT_ALLOW_OVERWRITE_OPTIONS_SECTION=True</tt> (if
456+
both are set config file takes precedence).</li>
455457
</ul>
456458
</div>
457459
<div class="section" id="environment-variable">
458-
<h3><a class="toc-backref" href="#toc-entry-4">Environment variable</a></h3>
460+
<h2><a class="toc-backref" href="#toc-entry-4">Environment variable</a></h2>
459461
<p>You can define configuration in the environment variable
460462
<tt class="docutils literal">SERVER_ENV_CONFIG</tt> and/or <tt class="docutils literal">SERVER_ENV_CONFIG_SECRET</tt>. The 2
461463
variables are handled the exact same way, this is only a convenience for
@@ -505,7 +507,7 @@ <h3><a class="toc-backref" href="#toc-entry-4">Environment variable</a></h3>
505507
reference records. See “USAGE”.</blockquote>
506508
</div>
507509
<div class="section" id="default-values">
508-
<h3><a class="toc-backref" href="#toc-entry-5">Default values</a></h3>
510+
<h2><a class="toc-backref" href="#toc-entry-5">Default values</a></h2>
509511
<p>When using the <tt class="docutils literal">server.env.mixin</tt> mixin, for each env-computed field,
510512
a companion field <tt class="docutils literal">&lt;field&gt;_env_default</tt> is created. This field is not
511513
environment-dependent. It’s a fallback value used when no key is set in
@@ -514,15 +516,15 @@ <h3><a class="toc-backref" href="#toc-entry-5">Default values</a></h3>
514516
<p>Note: empty environment keys always take precedence over default fields</p>
515517
</div>
516518
<div class="section" id="server-environment-integration">
517-
<h3><a class="toc-backref" href="#toc-entry-6">Server environment integration</a></h3>
519+
<h2><a class="toc-backref" href="#toc-entry-6">Server environment integration</a></h2>
518520
<p>Read the documentation of the class
519521
<a class="reference external" href="models/server_env_mixin.py">models/server_env_mixin.py</a> and
520522
[models/server_env_tech_name_mixin.py]
521523
(models/server_env_tech_name_mixin.py)</p>
522524
</div>
523525
</div>
524526
<div class="section" id="usage">
525-
<h2><a class="toc-backref" href="#toc-entry-7">Usage</a></h2>
527+
<h1><a class="toc-backref" href="#toc-entry-7">Usage</a></h1>
526528
<p>You can include a mixin in your model and configure the env-computed
527529
fields by an override of <tt class="docutils literal">_server_env_fields</tt>.</p>
528530
<pre class="literal-block">
@@ -546,7 +548,7 @@ <h2><a class="toc-backref" href="#toc-entry-7">Usage</a></h2>
546548
</pre>
547549
</div>
548550
<div class="section" id="known-issues-roadmap">
549-
<h2><a class="toc-backref" href="#toc-entry-8">Known issues / Roadmap</a></h2>
551+
<h1><a class="toc-backref" href="#toc-entry-8">Known issues / Roadmap</a></h1>
550552
<ul class="simple">
551553
<li>it is not possible to set the environment from the command line. A
552554
configuration file must be used.</li>
@@ -558,23 +560,23 @@ <h2><a class="toc-backref" href="#toc-entry-8">Known issues / Roadmap</a></h2>
558560
</ul>
559561
</div>
560562
<div class="section" id="bug-tracker">
561-
<h2><a class="toc-backref" href="#toc-entry-9">Bug Tracker</a></h2>
563+
<h1><a class="toc-backref" href="#toc-entry-9">Bug Tracker</a></h1>
562564
<p>Bugs are tracked on <a class="reference external" href="https://github.com/OCA/server-env/issues">GitHub Issues</a>.
563565
In case of trouble, please check there if your issue has already been reported.
564566
If you spotted it first, help us to smash it by providing a detailed and welcomed
565567
<a class="reference external" href="https://github.com/OCA/server-env/issues/new?body=module:%20server_environment%0Aversion:%2018.0%0A%0A**Steps%20to%20reproduce**%0A-%20...%0A%0A**Current%20behavior**%0A%0A**Expected%20behavior**">feedback</a>.</p>
566568
<p>Do not contact contributors directly about support or help with technical issues.</p>
567569
</div>
568570
<div class="section" id="credits">
569-
<h2><a class="toc-backref" href="#toc-entry-10">Credits</a></h2>
571+
<h1><a class="toc-backref" href="#toc-entry-10">Credits</a></h1>
570572
<div class="section" id="authors">
571-
<h3><a class="toc-backref" href="#toc-entry-11">Authors</a></h3>
573+
<h2><a class="toc-backref" href="#toc-entry-11">Authors</a></h2>
572574
<ul class="simple">
573575
<li>Camptocamp</li>
574576
</ul>
575577
</div>
576578
<div class="section" id="contributors">
577-
<h3><a class="toc-backref" href="#toc-entry-12">Contributors</a></h3>
579+
<h2><a class="toc-backref" href="#toc-entry-12">Contributors</a></h2>
578580
<ul class="simple">
579581
<li>Florent Xicluna (Wingo) &lt;<a class="reference external" href="mailto:florent.xicluna&#64;gmail.com">florent.xicluna&#64;gmail.com</a>&gt;</li>
580582
<li>Nicolas Bessi &lt;<a class="reference external" href="mailto:nicolas.bessi&#64;camptocamp.com">nicolas.bessi&#64;camptocamp.com</a>&gt;</li>
@@ -591,7 +593,7 @@ <h3><a class="toc-backref" href="#toc-entry-12">Contributors</a></h3>
591593
</ul>
592594
</div>
593595
<div class="section" id="maintainers">
594-
<h3><a class="toc-backref" href="#toc-entry-13">Maintainers</a></h3>
596+
<h2><a class="toc-backref" href="#toc-entry-13">Maintainers</a></h2>
595597
<p>This module is maintained by the OCA.</p>
596598
<a class="reference external image-reference" href="https://odoo-community.org">
597599
<img alt="Odoo Community Association" src="https://odoo-community.org/logo.png" />
@@ -604,6 +606,5 @@ <h3><a class="toc-backref" href="#toc-entry-13">Maintainers</a></h3>
604606
</div>
605607
</div>
606608
</div>
607-
</div>
608609
</body>
609610
</html>

server_environment/tests/test_server_environment.py

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,3 +88,61 @@ def test_default_hidden_password(self):
8888
self.assertIn("odoo_I_db_password", defaults)
8989
self.assertIn("odoo_I_smtp_password", defaults)
9090
self.assertIn("outgoing_mail_provider_promail_I_smtp_pass", defaults)
91+
92+
@patch.dict(os.environ, {"SERVER_ENVIRONMENT_ALLOW_OVERWRITE_OPTIONS_SECTION": "0"})
93+
@patch.dict(
94+
odoo_config.options,
95+
{
96+
"running_env": "testing",
97+
"server_environment_allow_overwrite_options_section": True,
98+
"odoo_test_option": "fake odoo config",
99+
},
100+
)
101+
def test_server_environment_allow_overwrite_options_section(self):
102+
with self.set_config_dir("testfiles"):
103+
server_env._load_config()
104+
self.assertEqual(
105+
odoo_config["odoo_test_option"], "Set in config file for testing env"
106+
)
107+
108+
@patch.dict(os.environ, {"SERVER_ENVIRONMENT_ALLOW_OVERWRITE_OPTIONS_SECTION": "1"})
109+
@patch.dict(
110+
odoo_config.options,
111+
{
112+
"running_env": "testing",
113+
"server_environment_allow_overwrite_options_section": False,
114+
"odoo_test_option": "fake odoo config",
115+
},
116+
)
117+
def test_server_environment_disabled_overwrite_options_section(self):
118+
with self.set_config_dir("testfiles"):
119+
server_env._load_config()
120+
self.assertEqual(odoo_config["odoo_test_option"], "fake odoo config")
121+
122+
@patch.dict(os.environ, {"SERVER_ENVIRONMENT_ALLOW_OVERWRITE_OPTIONS_SECTION": "1"})
123+
@patch.dict(
124+
odoo_config.options,
125+
{
126+
"running_env": "testing",
127+
"odoo_test_option": "fake odoo config",
128+
},
129+
)
130+
def test_server_environment_allow_overwrite_options_section_by_env(self):
131+
with self.set_config_dir("testfiles"):
132+
server_env._load_config()
133+
self.assertEqual(
134+
odoo_config["odoo_test_option"], "Set in config file for testing env"
135+
)
136+
137+
@patch.dict(os.environ, {"SERVER_ENVIRONMENT_ALLOW_OVERWRITE_OPTIONS_SECTION": "0"})
138+
@patch.dict(
139+
odoo_config.options,
140+
{
141+
"running_env": "testing",
142+
"odoo_test_option": "fake odoo config",
143+
},
144+
)
145+
def test_server_environment_disabled_overwrite_options_section_by_env(self):
146+
with self.set_config_dir("testfiles"):
147+
server_env._load_config()
148+
self.assertEqual(odoo_config["odoo_test_option"], "fake odoo config")
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,5 @@
1+
[options]
2+
odoo_test_option = Set in config file for testing env
3+
14
[external_service.ftp]
25
user = testing

0 commit comments

Comments
 (0)