From 1fa1277e1bc525ae5df6659daae1358b1493ef1a Mon Sep 17 00:00:00 2001
From: Vincent Hatakeyama <vincent.hatakeyama@xcg-consulting.fr>
Date: Thu, 18 Jan 2024 16:41:14 +0100
Subject: [PATCH] [IMP] module_change_auto_install: also allow using
 environment variables

---
 module_change_auto_install/patch.py           | 11 +++++++--
 .../readme/CONFIGURE.rst                      | 23 +++++++++++++++++--
 .../readme/CONTRIBUTORS.rst                   |  3 +++
 module_change_auto_install/readme/INSTALL.rst |  2 +-
 4 files changed, 34 insertions(+), 5 deletions(-)

diff --git a/module_change_auto_install/patch.py b/module_change_auto_install/patch.py
index 8b0ea5bae..b0ddb4930 100644
--- a/module_change_auto_install/patch.py
+++ b/module_change_auto_install/patch.py
@@ -3,6 +3,7 @@
 # License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html).
 
 import logging
+import os
 
 from odoo import modules
 from odoo.tools import config
@@ -52,10 +53,16 @@ def _overload_load_manifest(module, mod_path=None):
     auto_install = res.get("auto_install", False)
 
     modules_auto_install_enabled_dict = _get_modules_dict_auto_install_config(
-        config.get("modules_auto_install_enabled")
+        config.get(
+            "modules_auto_install_enabled",
+            os.environ.get("ODOO_MODULES_AUTO_INSTALL_ENABLED"),
+        )
     )
     modules_auto_install_disabled_dict = _get_modules_dict_auto_install_config(
-        config.get("modules_auto_install_disabled")
+        config.get(
+            "modules_auto_install_disabled",
+            os.environ.get("ODOO_MODULES_AUTO_INSTALL_DISABLED"),
+        )
     )
 
     if auto_install and module in modules_auto_install_disabled_dict.keys():
diff --git a/module_change_auto_install/readme/CONFIGURE.rst b/module_change_auto_install/readme/CONFIGURE.rst
index 7bb8d37bd..17c4e0475 100644
--- a/module_change_auto_install/readme/CONFIGURE.rst
+++ b/module_change_auto_install/readme/CONFIGURE.rst
@@ -4,15 +4,20 @@
 
 * (optional) Add a new entry ``modules_auto_install_disabled`` to mark
   a list of modules as NOT auto installable.
+  The environment variable ``ODOO_MODULES_AUTO_INSTALL_DISABLED`` can also be set.
 
 * (optional) Add a new entry ``modules_auto_install_enabled`` to mark
   a list of modules as auto installable. This feature can be usefull for companies
   that are hosting a lot of Odoo instances for many customers, and want some modules
   to be always installed.
+  The environment variable ``ODOO_MODULES_AUTO_INSTALL_ENABLED`` can also be set.
+
+The values in the configuration file takes precedence over the environment variable
+values.
 
 **Typical Settings**
 
-.. code-block:: shell
+.. code-block:: cfg
 
     server_wide_modules = web,module_change_auto_install
 
@@ -27,6 +32,13 @@
         disable_odoo_online,
         account_usability
 
+When using environment variables, the same configuration is:
+
+.. code-block:: shell
+
+   export ODOO_MODULES_AUTO_INSTALL_DISABLED=partner_autocomplete,iap,mail_bot
+   export ODOO_MODULES_AUTO_INSTALL_ENABLED=web_responsive:web,base_technical_features,disable_odoo_online,account_usability
+
 Run your instance and check logs. Modules that has been altered should be present in your log, at the load of your instance:
 
 .. code-block:: shell
@@ -40,7 +52,7 @@ Run your instance and check logs. Modules that has been altered should be presen
 
 if your ``odoo.cfg`` file contains the following configuration:
 
-.. code-block:: shell
+.. code-block:: cfg
 
     modules_auto_install_enabled =
         account_usability,
@@ -57,3 +69,10 @@ The behaviour will be the following:
 * ``base_technical_features`` will be ALWAYS installed
 
 * ``point_of_sale`` module will be installed as soon as ``sale`` and ``purchase`` module are installed.
+
+When using environment variables, the same configuration is:
+
+.. code-block:: shell
+
+   export ODOO_MODULES_AUTO_INSTALL_ENABLED=account_usability,web_responsive:web,base_technical_features:,point_of_sale:sale/purchase
+
diff --git a/module_change_auto_install/readme/CONTRIBUTORS.rst b/module_change_auto_install/readme/CONTRIBUTORS.rst
index 9f76a75bc..73482c9b6 100644
--- a/module_change_auto_install/readme/CONTRIBUTORS.rst
+++ b/module_change_auto_install/readme/CONTRIBUTORS.rst
@@ -1 +1,4 @@
 * Sylvain LE GAL <https://twitter.com/legalsylvain>
+* XCG Consulting, part of `Orbeet <https://orbeet.io/>`__:
+
+  * Vincent Hatakeyama <vincent.hatakeyama@xcg-consulting.fr>
diff --git a/module_change_auto_install/readme/INSTALL.rst b/module_change_auto_install/readme/INSTALL.rst
index c7804edad..fd4a76332 100644
--- a/module_change_auto_install/readme/INSTALL.rst
+++ b/module_change_auto_install/readme/INSTALL.rst
@@ -1,4 +1,4 @@
 You don't have to install this module. To make the features working :
 
 * make the module ``module_change_auto_install`` available in your addons path
-* update your ``odoo.cfg`` following  the "Configure" section
+* either update your ``odoo.cfg`` or set the environment variables following the "Configure" section