Verified Commit 3a63aebc authored by Štěpán Henek's avatar Štěpán Henek 🌩

mqtt: can set path to credentials file

parent 224d43c8
import argparse
import bottle
import logging
import re
import typing
from foris import __version__
from foris.state import current_state
......@@ -43,6 +45,11 @@ def get_arg_parser():
group.add_argument(
"--mqtt-host", default="localhost", help="mqtt host (default 'localhost')"
)
group.add_argument(
"--mqtt-passwd-file", type=lambda x: read_passwd_file(x),
help="path to passwd file (first record will be used to authenticate)",
default=None,
)
group.add_argument(
"--bus-socket", default="/var/run/ubus.sock", help="message bus socket path"
)
......@@ -80,7 +87,11 @@ def main():
current_state.set_backend(Backend(args.message_bus, path=args.bus_socket))
elif args.message_bus == "mqtt":
current_state.set_backend(
Backend(args.message_bus, host=args.mqtt_host, port=args.mqtt_port))
Backend(
args.message_bus, host=args.mqtt_host, port=args.mqtt_port,
credentials=args.mqtt_passwd_file
)
)
# update websocket
current_state.set_websocket(args.ws_port, args.ws_path, args.wss_port, args.wss_path)
......@@ -105,5 +116,12 @@ def main():
bottle.run(app=main_app, server="cgi", debug=args.debug)
def read_passwd_file(path: str) -> typing.Tuple[str]:
""" Returns username and password from passwd file
"""
with open(path, "r") as f:
return re.match(r"^([^:]+):(.*)$", f.readlines()[0][:-1]).groups()
if __name__ == "__main__":
main()
......@@ -50,8 +50,11 @@ class Backend(object):
from foris_client.buses.mqtt import MqttSender
self.host = kwargs["host"]
self.port = kwargs["port"]
self.credentials = kwargs["credentials"]
self._instance = MqttSender(
kwargs["host"], kwargs["port"], default_timeout=self.DEFAULT_TIMEOUT)
kwargs["host"], kwargs["port"], default_timeout=self.DEFAULT_TIMEOUT,
credentials=kwargs["credentials"],
)
def __repr__(self):
if self.name in ["unix-socket", "ubus"]:
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment