Skip to content

Commit 0d380db

Browse files
authored
feat: use host/user/pass from env vars #406 (#408)
* feat: use host/user/pass from env vars #406
1 parent 1e6c466 commit 0d380db

File tree

2 files changed

+21
-0
lines changed

2 files changed

+21
-0
lines changed

README.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,16 @@ _Requires Python 3.6+_
6969

7070
`python3 -m mqtt_io config.yml`
7171

72+
Some configuration parameters can be passed as environment variables:
73+
74+
- `MQTT_IO_HOST` - Host name or IP address of the MQTT server.
75+
- `MQTT_IO_PORT` - Port number to connect to on the MQTT server.
76+
- `MQTT_IO_USER` - Username to authenticate with on the MQTT server.
77+
- `MQTT_IO_PASSWORD` - Password to authenticate with on the MQTT server.
78+
- `MQTT_IO_PROTOCOL` - Version of the MQTT protocol to use.
79+
80+
Environment variables take precedence over configuration files.
81+
7282
## Configuration Example
7383

7484
Configuration is written in a YAML file which is passed as an argument to the server on startup.

mqtt_io/__main__.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import argparse
55
import logging.config
66
import sys
7+
from os import getenv
78
from copy import deepcopy
89
from hashlib import sha256
910
from typing import Any, Optional
@@ -76,6 +77,16 @@ def main() -> None:
7677
# Load, validate and normalise config, or quit.
7778
try:
7879
raw_config = load_config(args.config, args.render)
80+
if raw_config:
81+
if "mqtt" not in raw_config or raw_config["mqtt"] is None:
82+
raw_config["mqtt"] = {}
83+
raw_config["mqtt"]["host"] = getenv("MQTT_IO_HOST", raw_config["mqtt"].get("host"))
84+
raw_config["mqtt"]["port"] = getenv("MQTT_IO_PORT", raw_config["mqtt"].get("port"))
85+
raw_config["mqtt"]["user"] = getenv("MQTT_IO_USER", raw_config["mqtt"].get("user"))
86+
raw_config["mqtt"]["password"] = getenv("MQTT_IO_PASSWORD",
87+
raw_config["mqtt"].get("password"))
88+
raw_config["mqtt"]["protocol"] = getenv("MQTT_IO_PROTOCOL",
89+
raw_config["mqtt"].get("protocol"))
7990
config = validate_and_normalise_main_config(raw_config)
8091
except ConfigValidationFailed as exc:
8192
print(str(exc), file=sys.stderr)

0 commit comments

Comments
 (0)