Merge #1681 Improve handling of configuration file

This commit is contained in:
Brendan Reilly
2023-02-15 19:16:56 +00:00
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: recognized:
- ``MBS_CONFIG_FILE``: Overrides default configuration file location, - ``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. - ``MBS_CONFIG_SECTION``: Overrides configuration section.
It is possible to set these values in httpd using ``SetEnv``, It is possible to set these values in httpd using ``SetEnv``,

View File

@@ -1,6 +1,7 @@
# -*- coding: utf-8 -*- # -*- coding: utf-8 -*-
# SPDX-License-Identifier: MIT # SPDX-License-Identifier: MIT
from __future__ import absolute_import from __future__ import absolute_import
import errno
import imp import imp
import logging import logging
import os import os
@@ -108,15 +109,27 @@ def init_config():
to configure Flask with. to configure Flask with.
:rtype: tuple(Config, object) :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 = None
config_module = imp.load_source("mbs_runtime_config", config_file)
log.info("Using the configuration file at %s", config_file) # MBS_CONFIG_FILE="" entirely suppresses looking a config file
except Exception: if config_file != "":
log.warning("The configuration file at %s was not present", 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 # Default to this file as the configuration module
config_module = sys.modules[__name__] config_module = sys.modules[__name__]
log.debug("Using default configuration")
true_options = ("1", "on", "true", "y", "yes") true_options = ("1", "on", "true", "y", "yes")
if any(["py.test" in arg or "pytest" in arg for arg in sys.argv]): if any(["py.test" in arg or "pytest" in arg for arg in sys.argv]):