Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[17.0][IMP] server_environment: allow to overwrite odoo config options #197

Open
wants to merge 1 commit into
base: 17.0
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions server_environment/readme/CONFIGURE.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,12 @@ 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 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
2 changes: 1 addition & 1 deletion server_environment/readme/DESCRIPTION.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,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.
database. You will be able to overwrite some odoo options.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can all options be changed?
If only some options can be changed, it would help to document which ones.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes all odoo options of the config file can be overwritten this way.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Then I suggest this change:

Suggested change
database. You will be able to overwrite some odoo options.
database. You will be able to overwrite any Odoo options.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

IMO this sentence is confusing if we don't give extra details (with the key server_environment_allow_overwrite_options_section,...).
But all this is already explained in the "Configured" section.
I think it's better to keep it as it is with a simple description.


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
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
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
Loading