diff --git a/server_environment/README.rst b/server_environment/README.rst index cb1578211..ae15f161e 100644 --- a/server_environment/README.rst +++ b/server_environment/README.rst @@ -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 @@ -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 -------------------- diff --git a/server_environment/server_env.py b/server_environment/server_env.py index 4eb016f3a..65c2b448b 100644 --- a/server_environment/server_env.py +++ b/server_environment/server_env.py @@ -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"]) @@ -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): diff --git a/server_environment/static/description/index.html b/server_environment/static/description/index.html index 1a5372735..624e9a3b3 100644 --- a/server_environment/static/description/index.html +++ b/server_environment/static/description/index.html @@ -371,17 +371,18 @@

server configuration environment files

!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! -->

Production/Stable License: LGPL-3 OCA/server-env Translate me on Weblate Try me on Runboat

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 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 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 : ‘passw’, ‘key’, ‘secret’ and ‘token’).

+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 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. 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 be able to +see the values contained in the defined secret keys +(by default : ‘passw’, ‘key’, ‘secret’ and ‘token’).

Table of contents

diff --git a/server_environment/tests/test_server_environment.py b/server_environment/tests/test_server_environment.py index cfa6878c9..f518219e0 100644 --- a/server_environment/tests/test_server_environment.py +++ b/server_environment/tests/test_server_environment.py @@ -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") diff --git a/server_environment/tests/testfiles/testing/base.conf b/server_environment/tests/testfiles/testing/base.conf index 544e95b26..46da78487 100644 --- a/server_environment/tests/testfiles/testing/base.conf +++ b/server_environment/tests/testfiles/testing/base.conf @@ -1,2 +1,5 @@ +[options] +odoo_test_option = Set in config file for testing env + [external_service.ftp] user = testing