From 2421e5dc744c60ac57368125fcf7a8a6ee0e2632 Mon Sep 17 00:00:00 2001 From: Richard Tibbles Date: Thu, 14 Nov 2024 09:15:52 -0800 Subject: [PATCH] Allow plugins to be enabled, disabled, and applied via env vars. --- kolibri/plugins/__init__.py | 51 ++++++++++++++++++++++++++++++++++++- kolibri/utils/env.py | 18 +++++++++++++ 2 files changed, 68 insertions(+), 1 deletion(-) diff --git a/kolibri/plugins/__init__.py b/kolibri/plugins/__init__.py index b8b68ed092d..28cc89f8062 100644 --- a/kolibri/plugins/__init__.py +++ b/kolibri/plugins/__init__.py @@ -39,6 +39,24 @@ def __init__(self): ) self.save() logger.info("Initialized plugins.json") + if self.ENV_VAR_APPLIED_PLUGINS: + logger.info( + "Applied plugins from environment variable: {}".format( + self.ENV_VAR_APPLIED_PLUGINS + ) + ) + if self.ENV_VAR_ENABLED_PLUGINS: + logger.info( + "Enabled plugins from environment variable: {}".format( + self.ENV_VAR_ENABLED_PLUGINS + ) + ) + if self.ENV_VAR_DISABLED_PLUGINS: + logger.info( + "Disabled plugins from environment variable: {}".format( + self.ENV_VAR_DISABLED_PLUGINS + ) + ) def set_defaults(self): self.update( @@ -55,9 +73,40 @@ def set_defaults(self): } ) + @property + def ENV_VAR_APPLIED_PLUGINS(self): + return [ + p.strip() + for p in os.environ.get("KOLIBRI_PLUGIN_APPLY", "").split(",") + if p.strip() + ] + + @property + def ENV_VAR_ENABLED_PLUGINS(self): + return set( + p.strip() + for p in os.environ.get("KOLIBRI_PLUGIN_ENABLE", "").split(",") + if p.strip() + ) + + @property + def ENV_VAR_DISABLED_PLUGINS(self): + return set( + p.strip() + for p in os.environ.get("KOLIBRI_PLUGIN_DISABLE", "").split(",") + if p.strip() + ) + @property def ACTIVE_PLUGINS(self): - return list(self["INSTALLED_PLUGINS"] - self["DISABLED_PLUGINS"]) + if self.ENV_VAR_APPLIED_PLUGINS: + return self.ENV_VAR_APPLIED_PLUGINS + return list( + (self["INSTALLED_PLUGINS"] - self["DISABLED_PLUGINS"]).union( + self.ENV_VAR_ENABLED_PLUGINS + ) + - self.ENV_VAR_DISABLED_PLUGINS + ) def update(self, new_values): """ diff --git a/kolibri/utils/env.py b/kolibri/utils/env.py index 3009c5f75b1..154080db30b 100644 --- a/kolibri/utils/env.py +++ b/kolibri/utils/env.py @@ -48,6 +48,24 @@ def settings_module(): See the sd_notify(3) man page for more details. """, }, + "KOLIBRI_PLUGIN_APPLY": { + "description": """ + A comma-separated list of plugins to apply. If this variable is set, + only the specified plugins will be applied. + """, + }, + "KOLIBRI_PLUGIN_ENABLE": { + "description": """ + A comma-separated list of plugins to enable. If this variable is set, + the specified plugins will be enabled, overriding plugins disabled via the CLI. + """, + }, + "KOLIBRI_PLUGIN_DISABLE": { + "description": """ + A comma-separated list of plugins to disable. If this variable is set, + the specified plugins will be disabled, overriding plugins enabled via the CLI. + """, + }, }