From b6c11f83d8159f89ccc11a43dc31ace3ae7f8fa0 Mon Sep 17 00:00:00 2001 From: Owen W. Taylor Date: Jan 23 2023 23:39:25 +0000 Subject: Improve handling of configuration file * Allow MBS_CONFIG_FILE="" to entirely suppress loading any configuration file (useful for running tests and avoiding loading a system-wide configuration file.) * When loading the configuration file: * If the default configuration file path doesn't exist, silently fall back to the default configuration * For any other OSError, print the exact error * Let any other exception throw through, to allow people to debug their configuration file --- diff --git a/README.rst b/README.rst index 7b2d3c6..38689ac 100644 --- a/README.rst +++ b/README.rst @@ -903,7 +903,9 @@ the following rules (all of them are evaluated from top to bottom): recognized: - ``MBS_CONFIG_FILE``: Overrides default configuration file location, - typically ``/etc/module-build-service/config.py``. + typically ``/etc/module-build-service/config.py``. If set to the + empty string, no configuration file will be read and default + values will be used. - ``MBS_CONFIG_SECTION``: Overrides configuration section. It is possible to set these values in httpd using ``SetEnv``, diff --git a/module_build_service/common/config.py b/module_build_service/common/config.py index 6f3ce62..6b0f888 100644 --- a/module_build_service/common/config.py +++ b/module_build_service/common/config.py @@ -1,6 +1,7 @@ # -*- coding: utf-8 -*- # SPDX-License-Identifier: MIT from __future__ import absolute_import +import errno import imp import logging import os @@ -108,15 +109,27 @@ def init_config(): to configure Flask with. :rtype: tuple(Config, object) """ - config_file = os.environ.get("MBS_CONFIG_FILE", "/etc/module-build-service/config.py") + env_config_file = os.environ.get("MBS_CONFIG_FILE") + if env_config_file is None: + config_file = "/etc/module-build-service/config.py" + else: + config_file = env_config_file + + config_module = None + + # MBS_CONFIG_FILE="" entirely suppresses looking a config file + if config_file != "": + try: + config_module = imp.load_source("mbs_runtime_config", config_file) + log.info("Using the configuration file at %s", config_file) + except OSError as e: + if e.errno != errno.ENOENT or env_config_file: + log.error("Can't open config file: %s", e) - try: - config_module = imp.load_source("mbs_runtime_config", config_file) - log.info("Using the configuration file at %s", config_file) - except Exception: - log.warning("The configuration file at %s was not present", config_file) + if config_module is None: # Default to this file as the configuration module config_module = sys.modules[__name__] + log.debug("Using default configuration") true_options = ("1", "on", "true", "y", "yes") if any(["py.test" in arg or "pytest" in arg for arg in sys.argv]):