Allow files as config source

Allow "FILE__"+config_name to point to a file containing the config value.
This format matches linuxserver.io containers and is comonly used in other containers as well.

e.g. FILE__QBITTORRENT_HOST contains "/run/secrets/q_host" and the contents of the "/run/secrets/q_host" is "1.2.3.4"
This commit is contained in:
John Hollowell 2021-07-16 22:06:36 +00:00
parent 0ff6a56f18
commit 597307c230

View file

@ -165,16 +165,27 @@ class SignalHandler():
logger.info("Exporter is shutting down") logger.info("Exporter is shutting down")
self.shutdown = True self.shutdown = True
def get_config_value(key, default=""):
input_path = os.environ.get("FILE__" + key, None)
if input_path is not None:
try:
with open(input_path, "r") as input_file:
return input_file.read().strip()
except IOError as e:
logger.error(f"Unable to read value for {key} from {input_path}: {str(e)}")
return os.environ.get(key, default)
def main(): def main():
config = { config = {
"host": os.environ.get("QBITTORRENT_HOST", ""), "host": get_config_value("QBITTORRENT_HOST", ""),
"port": os.environ.get("QBITTORRENT_PORT", ""), "port": get_config_value("QBITTORRENT_PORT", ""),
"username": os.environ.get("QBITTORRENT_USER", ""), "username": get_config_value("QBITTORRENT_USER", ""),
"password": os.environ.get("QBITTORRENT_PASS", ""), "password": get_config_value("QBITTORRENT_PASS", ""),
"exporter_port": int(os.environ.get("EXPORTER_PORT", "8000")), "exporter_port": int(get_config_value("EXPORTER_PORT", "8000")),
"log_level": os.environ.get("EXPORTER_LOG_LEVEL", "INFO"), "log_level": get_config_value("EXPORTER_LOG_LEVEL", "INFO"),
"metrics_prefix": os.environ.get("METRICS_PREFIX", "qbittorrent"), "metrics_prefix": get_config_value("METRICS_PREFIX", "qbittorrent"),
} }
# Register signal handler # Register signal handler