From 209b929ca5ca98e9fd70d0c9ae5c2496ff37e1f3 Mon Sep 17 00:00:00 2001 From: Evgeny Vlasenko Date: Sat, 28 Sep 2024 22:37:43 +0400 Subject: [PATCH 1/2] feat: use host/user/pass from env vars #406 --- mqtt_io/__main__.py | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/mqtt_io/__main__.py b/mqtt_io/__main__.py index bf442e92..cd6e5916 100644 --- a/mqtt_io/__main__.py +++ b/mqtt_io/__main__.py @@ -4,6 +4,7 @@ import argparse import logging.config import sys +from os import getenv from copy import deepcopy from hashlib import sha256 from typing import Any, Optional @@ -76,6 +77,16 @@ def main() -> None: # Load, validate and normalise config, or quit. try: raw_config = load_config(args.config, args.render) + if raw_config: + if "mqtt" not in raw_config or raw_config["mqtt"] is None: + raw_config["mqtt"] = {} + raw_config["mqtt"]["host"] = getenv("MQTT_IO_HOST", raw_config["mqtt"].get("host")) + raw_config["mqtt"]["port"] = getenv("MQTT_IO_PORT", raw_config["mqtt"].get("port")) + raw_config["mqtt"]["user"] = getenv("MQTT_IO_USER", raw_config["mqtt"].get("user")) + raw_config["mqtt"]["password"] = getenv("MQTT_IO_PASSWORD", + raw_config["mqtt"].get("password")) + raw_config["mqtt"]["protocol"] = getenv("MQTT_IO_PROTOCOL", + raw_config["mqtt"].get("protocol")) config = validate_and_normalise_main_config(raw_config) except ConfigValidationFailed as exc: print(str(exc), file=sys.stderr) From 479d1da13cb46b27c803eb68ef6751103f02f772 Mon Sep 17 00:00:00 2001 From: Evgeny Vlasenko Date: Fri, 18 Oct 2024 00:38:40 +0400 Subject: [PATCH 2/2] chore: readme env vars --- README.md | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/README.md b/README.md index e4217aa8..d7b252a0 100644 --- a/README.md +++ b/README.md @@ -69,6 +69,16 @@ _Requires Python 3.6+_ `python3 -m mqtt_io config.yml` +Some configuration parameters can be passed as environment variables: + +- `MQTT_IO_HOST` - Host name or IP address of the MQTT server. +- `MQTT_IO_PORT` - Port number to connect to on the MQTT server. +- `MQTT_IO_USER` - Username to authenticate with on the MQTT server. +- `MQTT_IO_PASSWORD` - Password to authenticate with on the MQTT server. +- `MQTT_IO_PROTOCOL` - Version of the MQTT protocol to use. + +Environment variables take precedence over configuration files. + ## Configuration Example Configuration is written in a YAML file which is passed as an argument to the server on startup.