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
This commit is contained in:
Owen W. Taylor
2021-02-23 10:15:09 -05:00
parent ab41f01209
commit b6c11f83d8
2 changed files with 22 additions and 7 deletions

View File

@@ -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``,

View File

@@ -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
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)
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)
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]):