Skip to content

Commit

Permalink
[IMP] server_environment: allow env variable conf
Browse files Browse the repository at this point in the history
  • Loading branch information
petrus-v authored and denaux committed Jun 27, 2024
1 parent 40c12a6 commit 38090e1
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 7 deletions.
8 changes: 8 additions & 0 deletions server_environment/README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,14 @@ used values are 'dev', 'test', 'production':
[options]
running_env=dev

Or set the ``RUNNING_ENV`` or ``ODOO_STAGE`` environment variable. If
both all are set config file will take the precedence on environment and
``RUNNING_ENV`` over ``ODOO_STAGE``.

``ODOO_STAGE`` is used for odoo.sh platform where we can't set
``RUNNING_ENV``, possible observed values are ``production``,
``staging`` and ``dev``

Values associated to keys containing 'passw' are only displayed in the
'dev' environment.

Expand Down
6 changes: 6 additions & 0 deletions server_environment/readme/CONFIGURE.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,12 @@ used values are 'dev', 'test', 'production':
[options]
running_env=dev

Or set the `RUNNING_ENV` or `ODOO_STAGE` environment variable. If both all are set config file
will take the precedence on environment and `RUNNING_ENV` over `ODOO_STAGE`.

`ODOO_STAGE` is used for odoo.sh platform where we can't set `RUNNING_ENV`, possible
observed values are `production`, `staging` and `dev`

Values associated to keys containing 'passw' are only displayed in the
'dev' environment.

Expand Down
21 changes: 14 additions & 7 deletions server_environment/server_env.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,13 +45,20 @@

def _load_running_env():
if not system_base_config.get("running_env"):
_logger.info("`running_env` not found. Using default = `test`.")
_logger.info(
"We strongly recommend against using the rc file but instead use an "
"explicit config file or env variable."
)
# safe default
system_base_config["running_env"] = "test"
env_running_env = os.environ.get("RUNNING_ENV", os.environ.get("ODOO_STAGE"))
if env_running_env:
system_base_config["running_env"] = env_running_env
else:
_logger.info(
"`running_env` or `RUNNING_ENV`, `ODOO_STAGE` not found. "
"Using default = `test`."
)
_logger.info(
"We strongly recommend against using the rc file but instead use an "
"explicit config file or env variable."
)
# safe default
system_base_config["running_env"] = "test"


_load_running_env()
Expand Down
6 changes: 6 additions & 0 deletions server_environment/static/description/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -424,6 +424,12 @@ <h1><a class="toc-backref" href="#toc-entry-2">Configuration</a></h1>
[options]
running_env=dev
</pre>
<p>Or set the <tt class="docutils literal">RUNNING_ENV</tt> or <tt class="docutils literal">ODOO_STAGE</tt> environment variable. If
both all are set config file will take the precedence on environment and
<tt class="docutils literal">RUNNING_ENV</tt> over <tt class="docutils literal">ODOO_STAGE</tt>.</p>
<p><tt class="docutils literal">ODOO_STAGE</tt> is used for odoo.sh platform where we can’t set
<tt class="docutils literal">RUNNING_ENV</tt>, possible observed values are <tt class="docutils literal">production</tt>,
<tt class="docutils literal">staging</tt> and <tt class="docutils literal">dev</tt></p>
<p>Values associated to keys containing ‘passw’ are only displayed in the
‘dev’ environment.</p>
<p>If you don’t provide any value, test is used as a safe default.</p>
Expand Down
14 changes: 14 additions & 0 deletions server_environment/tests/test_server_environment.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# Copyright 2018 Camptocamp (https://www.camptocamp.com).
# License LGPL-3.0 or later (https://www.gnu.org/licenses/lgpl.html)

import os
from unittest.mock import patch

from odoo.tools.config import config as odoo_config
Expand Down Expand Up @@ -37,8 +38,21 @@ def test_default_dev(self):

@patch.dict(odoo_config.options, {"running_env": "whatever"})
def test_default_non_dev_env(self):
server_env._load_running_env()
self._test_default(hidden_pwd=True)

@patch.dict(odoo_config.options, {"running_env": None})
@patch.dict(os.environ, {"RUNNING_ENV": "dev"})
def test_default_dev_from_environ(self):
server_env._load_running_env()
self._test_default()

@patch.dict(odoo_config.options, {"running_env": None})
@patch.dict(os.environ, {"ODOO_STAGE": "dev"})
def test_odoosh_dev_from_environ(self):
server_env._load_running_env()
self._test_default()

@patch.dict(odoo_config.options, {"running_env": "testing"})
def test_value_retrival(self):
with self.set_config_dir("testfiles"):
Expand Down

0 comments on commit 38090e1

Please sign in to comment.