From 8305b4de03f6184500532c2979c27f03bb22a06a Mon Sep 17 00:00:00 2001 From: Pierre Verkest Date: Fri, 16 Jun 2023 14:43:55 +0200 Subject: [PATCH] [IMP] server_environmnet: allow to overwrite odoo config options from server_environment_files --- .../blacklist/server_environment.json | 6 ++ server_environment/README.rst | 17 +++--- server_environment/readme/CONFIGURE.md | 7 +++ server_environment/readme/DESCRIPTION.md | 4 +- server_environment/server_env.py | 14 +++++ .../static/description/index.html | 51 ++++++++-------- .../tests/test_server_environment.py | 58 +++++++++++++++++++ .../tests/testfiles/testing/base.conf | 3 + 8 files changed, 126 insertions(+), 34 deletions(-) create mode 100644 .oca/oca-port/blacklist/server_environment.json diff --git a/.oca/oca-port/blacklist/server_environment.json b/.oca/oca-port/blacklist/server_environment.json new file mode 100644 index 000000000..dd9fecca9 --- /dev/null +++ b/.oca/oca-port/blacklist/server_environment.json @@ -0,0 +1,6 @@ +{ + "pull_requests": { + "OCA/server-env#176": "(auto) Nothing to port from PR #176", + "OCA/server-env#190": "(auto) Nothing to port from PR #190" + } +} diff --git a/server_environment/README.rst b/server_environment/README.rst index abcfbcfef..e38341d4d 100644 --- a/server_environment/README.rst +++ b/server_environment/README.rst @@ -1,7 +1,3 @@ -.. image:: https://odoo-community.org/readme-banner-image - :target: https://odoo-community.org/get-involved?utm_source=readme - :alt: Odoo Community Association - ====================================== server configuration environment files ====================================== @@ -11,13 +7,13 @@ server configuration environment files !! This file is generated by oca-gen-addon-readme !! !! changes will be overwritten. !! !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! - !! source digest: sha256:39cd61c3a9621d0b7ee7a346adf45f4a2c90e80e6a61e18f182386c6fc90fd19 + !! source digest: sha256:dcc784d558683190d604281faea315ff44a3c8ffe998f31f749b06e4def888e1 !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! .. |badge1| image:: https://img.shields.io/badge/maturity-Production%2FStable-green.png :target: https://odoo-community.org/page/development-status :alt: Production/Stable -.. |badge2| image:: https://img.shields.io/badge/license-LGPL--3-blue.png +.. |badge2| image:: https://img.shields.io/badge/licence-LGPL--3-blue.png :target: http://www.gnu.org/licenses/lgpl-3.0-standalone.html :alt: License: LGPL-3 .. |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. 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 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 @@ -106,6 +102,13 @@ example: 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 useful to overwrite options written in the + ``[options]`` section. You must allow the override 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 takes precedence). Environment variable -------------------- diff --git a/server_environment/readme/CONFIGURE.md b/server_environment/readme/CONFIGURE.md index 76260f2b5..7b119560f 100644 --- a/server_environment/readme/CONFIGURE.md +++ b/server_environment/readme/CONFIGURE.md @@ -29,6 +29,13 @@ addon. The `server_environment_files_sample` can be used as an example: 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 useful to overwrite options written in the `[options]` section. You must + allow the override 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 takes precedence). + ## Environment variable diff --git a/server_environment/readme/DESCRIPTION.md b/server_environment/readme/DESCRIPTION.md index f08c22d44..fec8649e7 100644 --- a/server_environment/readme/DESCRIPTION.md +++ b/server_environment/readme/DESCRIPTION.md @@ -5,8 +5,8 @@ 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. +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 diff --git a/server_environment/server_env.py b/server_environment/server_env.py index 9f2208c2a..317577072 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 dfb4a71bd..15d45eefe 100644 --- a/server_environment/static/description/index.html +++ b/server_environment/static/description/index.html @@ -3,7 +3,7 @@ -README.rst +server configuration environment files -
+
+

server configuration environment files

- - -Odoo Community Association - -
-

server configuration environment files

-

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

+

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 @@ -382,7 +377,7 @@

server configuration environment files

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 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 @@ -410,7 +405,7 @@

server configuration environment files

-

Installation

+

Installation

By itself, this module does little. See for instance the mail_environment addon which depends on this one to allow configuring the incoming and outgoing mail servers depending on the @@ -422,7 +417,7 @@

Installation

SERVER_ENV_CONFIG and SERVER_ENV_CONFIG_SECRET.

-

Configuration

+

Configuration

To configure this module, you need to edit the main configuration file of your instance, and add a directive called running_env. Commonly used values are ‘dev’, ‘test’, ‘production’:

@@ -441,7 +436,7 @@

Configuration

If you don’t provide any value, test is used as a safe default.

You have several possibilities to set configuration values:

-

server_environment_files

+

server_environment_files

You can edit the settings you need in the server_environment_files addon. The server_environment_files_sample can be used as an example:

@@ -452,10 +447,17 @@

server_environment_files

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 useful to overwrite options written in the +[options] section. You must allow the override 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 takes precedence).
  • -

    Environment variable

    +

    Environment variable

    You can define configuration in the environment variable SERVER_ENV_CONFIG and/or SERVER_ENV_CONFIG_SECRET. The 2 variables are handled the exact same way, this is only a convenience for @@ -505,7 +507,7 @@

    Environment variable

    reference records. See “USAGE”.
    -

    Default values

    +

    Default values

    When using the server.env.mixin mixin, for each env-computed field, a companion field <field>_env_default is created. This field is not environment-dependent. It’s a fallback value used when no key is set in @@ -514,7 +516,7 @@

    Default values

    Note: empty environment keys always take precedence over default fields

    -

    Server environment integration

    +

    Server environment integration

    Read the documentation of the class models/server_env_mixin.py and [models/server_env_tech_name_mixin.py] @@ -522,7 +524,7 @@

    Server environment integration

    -

    Usage

    +

    Usage

    You can include a mixin in your model and configure the env-computed fields by an override of _server_env_fields.

    @@ -546,7 +548,7 @@ 

    Usage

    -

    Known issues / Roadmap

    +

    Known issues / Roadmap

    • it is not possible to set the environment from the command line. A configuration file must be used.
    • @@ -558,7 +560,7 @@

      Known issues / Roadmap

    -

    Bug Tracker

    +

    Bug Tracker

    Bugs are tracked on GitHub Issues. In case of trouble, please check there if your issue has already been reported. If you spotted it first, help us to smash it by providing a detailed and welcomed @@ -566,15 +568,15 @@

    Bug Tracker

    Do not contact contributors directly about support or help with technical issues.

    -

    Credits

    +

    Credits

    -

    Authors

    +

    Authors

    • Camptocamp
    -

    Contributors

    +

    Contributors

    -

    Maintainers

    +

    Maintainers

    This module is maintained by the OCA.

    Odoo Community Association @@ -604,6 +606,5 @@

    Maintainers

    -
    diff --git a/server_environment/tests/test_server_environment.py b/server_environment/tests/test_server_environment.py index 9f605942a..3d525a3eb 100644 --- a/server_environment/tests/test_server_environment.py +++ b/server_environment/tests/test_server_environment.py @@ -88,3 +88,61 @@ def test_default_hidden_password(self): self.assertIn("odoo_I_db_password", defaults) self.assertIn("odoo_I_smtp_password", defaults) self.assertIn("outgoing_mail_provider_promail_I_smtp_pass", defaults) + + @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