diff --git a/docker2mqtt/__docker__.py b/docker2mqtt/__docker__.py new file mode 100755 index 0000000..5a4fbbd --- /dev/null +++ b/docker2mqtt/__docker__.py @@ -0,0 +1,76 @@ +#!/usr/bin/env python3 +"""Listens to docker events and stats for containers and sends it to mqtt and supports discovery for home assistant.""" + +import logging +from os import environ +from socket import gethostname + +from .const import ( + DESTROYED_CONTAINER_TTL_DEFAULT, + EVENTS_DEFAULT, + HOMEASSISTANT_PREFIX_DEFAULT, + LOG_LEVEL_DEFAULT, + MQTT_CLIENT_ID_DEFAULT, + MQTT_PORT_DEFAULT, + MQTT_QOS_DEFAULT, + MQTT_TIMEOUT_DEFAULT, + MQTT_TOPIC_PREFIX_DEFAULT, + STATS_DEFAULT, + STATS_RECORD_SECONDS_DEFAULT, +) +from .docker2mqtt import Docker2Mqtt +from .type_definitions import Docker2MqttConfig + +# Configure logging +logging.basicConfig(format="%(asctime)s - %(name)s - %(levelname)s - %(message)s") + +# Loggers +main_logger = logging.getLogger("main") + + +if __name__ == "__main__": + # Env config + + whitelist = environ.get("CONTAINER_WHITELIST", "") + blacklist = environ.get("CONTAINER_BLACKLIST", "") + + cfg = Docker2MqttConfig( + { + "log_level": environ.get("LOG_LEVEL", LOG_LEVEL_DEFAULT), + "destroyed_container_ttl": int( + environ.get("DESTROYED_CONTAINER_TTL", DESTROYED_CONTAINER_TTL_DEFAULT) + ), + "homeassistant_prefix": environ.get( + "HOMEASSISTANT_PREFIX", HOMEASSISTANT_PREFIX_DEFAULT + ), + "docker2mqtt_hostname": environ.get("DOCKER2MQTT_HOSTNAME", gethostname()), + "mqtt_client_id": environ.get( + "MQTT_CLIENT_ID", f"{gethostname()}_{MQTT_CLIENT_ID_DEFAULT}" + ), + "mqtt_user": environ.get("MQTT_USER", ""), + "mqtt_password": environ.get("MQTT_PASSWD", ""), + "mqtt_host": environ.get("MQTT_HOST", "localhost"), + "mqtt_port": int(environ.get("MQTT_PORT", MQTT_PORT_DEFAULT)), + "mqtt_timeout": int(environ.get("MQTT_TIMEOUT", MQTT_TIMEOUT_DEFAULT)), + "mqtt_topic_prefix": environ.get( + "MQTT_TOPIC_PREFIX", MQTT_TOPIC_PREFIX_DEFAULT + ), + "mqtt_qos": int(environ.get("MQTT_QOS", MQTT_QOS_DEFAULT)), + "container_whitelist": whitelist.split(",") if len(whitelist) > 0 else [], + "container_blacklist": blacklist.split(",") if len(blacklist) > 0 else [], + "enable_events": bool(environ.get("EVENTS", EVENTS_DEFAULT)), + "enable_stats": bool(environ.get("STATS", STATS_DEFAULT)), + "stats_record_seconds": int( + environ.get("STATS_RECORD_SECONDS", STATS_RECORD_SECONDS_DEFAULT) + ), + } + ) + + try: + docker2mqtt = Docker2Mqtt(cfg) + docker2mqtt.loop_busy() + + except Exception as ex: + main_logger.info("Error occurred, printing relevant information and exiting...") + main_logger.error("Error processing: %s", str(ex)) + print(ex) diff --git a/docker2mqtt/__main__.py b/docker2mqtt/__main__.py index 5a4fbbd..39ed6ec 100755 --- a/docker2mqtt/__main__.py +++ b/docker2mqtt/__main__.py @@ -1,76 +1,6 @@ -#!/usr/bin/env python3 -"""Listens to docker events and stats for containers and sends it to mqtt and supports discovery for home assistant.""" - -import logging -from os import environ -from socket import gethostname - -from .const import ( - DESTROYED_CONTAINER_TTL_DEFAULT, - EVENTS_DEFAULT, - HOMEASSISTANT_PREFIX_DEFAULT, - LOG_LEVEL_DEFAULT, - MQTT_CLIENT_ID_DEFAULT, - MQTT_PORT_DEFAULT, - MQTT_QOS_DEFAULT, - MQTT_TIMEOUT_DEFAULT, - MQTT_TOPIC_PREFIX_DEFAULT, - STATS_DEFAULT, - STATS_RECORD_SECONDS_DEFAULT, -) -from .docker2mqtt import Docker2Mqtt -from .type_definitions import Docker2MqttConfig - -# Configure logging -logging.basicConfig(format="%(asctime)s - %(name)s - %(levelname)s - %(message)s") - -# Loggers -main_logger = logging.getLogger("main") +"""docker2mqtt main entry.""" +from .docker2mqtt import main if __name__ == "__main__": - # Env config - - whitelist = environ.get("CONTAINER_WHITELIST", "") - blacklist = environ.get("CONTAINER_BLACKLIST", "") - - cfg = Docker2MqttConfig( - { - "log_level": environ.get("LOG_LEVEL", LOG_LEVEL_DEFAULT), - "destroyed_container_ttl": int( - environ.get("DESTROYED_CONTAINER_TTL", DESTROYED_CONTAINER_TTL_DEFAULT) - ), - "homeassistant_prefix": environ.get( - "HOMEASSISTANT_PREFIX", HOMEASSISTANT_PREFIX_DEFAULT - ), - "docker2mqtt_hostname": environ.get("DOCKER2MQTT_HOSTNAME", gethostname()), - "mqtt_client_id": environ.get( - "MQTT_CLIENT_ID", f"{gethostname()}_{MQTT_CLIENT_ID_DEFAULT}" - ), - "mqtt_user": environ.get("MQTT_USER", ""), - "mqtt_password": environ.get("MQTT_PASSWD", ""), - "mqtt_host": environ.get("MQTT_HOST", "localhost"), - "mqtt_port": int(environ.get("MQTT_PORT", MQTT_PORT_DEFAULT)), - "mqtt_timeout": int(environ.get("MQTT_TIMEOUT", MQTT_TIMEOUT_DEFAULT)), - "mqtt_topic_prefix": environ.get( - "MQTT_TOPIC_PREFIX", MQTT_TOPIC_PREFIX_DEFAULT - ), - "mqtt_qos": int(environ.get("MQTT_QOS", MQTT_QOS_DEFAULT)), - "container_whitelist": whitelist.split(",") if len(whitelist) > 0 else [], - "container_blacklist": blacklist.split(",") if len(blacklist) > 0 else [], - "enable_events": bool(environ.get("EVENTS", EVENTS_DEFAULT)), - "enable_stats": bool(environ.get("STATS", STATS_DEFAULT)), - "stats_record_seconds": int( - environ.get("STATS_RECORD_SECONDS", STATS_RECORD_SECONDS_DEFAULT) - ), - } - ) - - try: - docker2mqtt = Docker2Mqtt(cfg) - docker2mqtt.loop_busy() - - except Exception as ex: - main_logger.info("Error occurred, printing relevant information and exiting...") - main_logger.error("Error processing: %s", str(ex)) - print(ex) + main() diff --git a/entrypoint.sh b/entrypoint.sh index 67dc9fb..082767f 100755 --- a/entrypoint.sh +++ b/entrypoint.sh @@ -1,2 +1,2 @@ #!/bin/bash -python3 -i -m docker2mqtt.__main__ \ No newline at end of file +python3 -i -m docker2mqtt.__docker__ \ No newline at end of file diff --git a/pyproject.toml b/pyproject.toml index 475e38d..d1d364c 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -136,6 +136,7 @@ split-on-trailing-comma = false # Allow for main entry & scripts to write to stdout "docker2mqtt/__main__.py" = ["T201"] +"docker2mqtt/__docker__.py" = ["T201"] "script/*" = ["T20"] [tool.ruff.lint.mccabe]