Skip to content

Commit 93aa83c

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

5 files changed

Lines changed: 89 additions & 2 deletions

File tree

server_environment/README.rst

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ are stored in the ``server_environment_files`` companion module.
4040

4141
The ``server_environment_files`` module is optional, the values can be
4242
set using an environment variable with a fallback on default values in
43-
the database.
43+
the database. You will be able to overwrite some odoo options.
4444

4545
The configuration read from the files are visible under the
4646
Configuration menu. If you are not in the 'dev' environment you will not
@@ -106,6 +106,12 @@ example:
106106
can override or extend default values;
107107
- you can override or extend values in the main configuration file of
108108
your instance;
109+
* In some platforms (like odoo.sh where production config file is copied to staging)
110+
it can be usefull to overwrite options write in the `[options]` section. You must
111+
allow the overwrite by adding `server_environment_allow_overwrite_options_section = True``
112+
to the former `odoo.cfg` config file or through the environment variable:
113+
`export SERVER_ENVIRONMENT_ALLOW_OVERWRITE_OPTIONS_SECTION=True` (if both are set
114+
config file take precedent).
109115

110116
Environment variable
111117
--------------------

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: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -382,7 +382,7 @@ <h1>server configuration environment files</h1>
382382
are stored in the <tt class="docutils literal">server_environment_files</tt> companion module.</p>
383383
<p>The <tt class="docutils literal">server_environment_files</tt> module is optional, the values can be
384384
set using an environment variable with a fallback on default values in
385-
the database.</p>
385+
the database. You will be able to overwrite some odoo options.</p>
386386
<p>The configuration read from the files are visible under the
387387
Configuration menu. If you are not in the ‘dev’ environment you will not
388388
be able to see the values contained in the defined secret keys (by
@@ -452,6 +452,12 @@ <h3><a class="toc-backref" href="#toc-entry-3">server_environment_files</a></h3>
452452
can override or extend default values;</li>
453453
<li>you can override or extend values in the main configuration file of
454454
your instance;</li>
455+
<li>In some platforms (like odoo.sh where production config file is copied to staging)
456+
it can be usefull to overwrite options write in the <cite>[options]</cite> section. You must
457+
allow the overwrite by adding <cite>server_environment_allow_overwrite_options_section = True`</cite>
458+
to the former <cite>odoo.cfg</cite> config file or through the environment variable:
459+
<cite>export SERVER_ENVIRONMENT_ALLOW_OVERWRITE_OPTIONS_SECTION=True</cite> (if both are set
460+
config file take precedent).</li>
455461
</ul>
456462
</div>
457463
<div class="section" id="environment-variable">

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)