Added systemd support for dish_grpc_mqtt

This commit is contained in:
Dean Cording 2022-03-02 15:15:37 +10:00
parent 3977f0b422
commit 16bdf3df9d
2 changed files with 50 additions and 2 deletions

View file

@ -15,6 +15,8 @@ Where *id_value* is the *id* value from the dish status information.
"""
import logging
import os
import signal
import sys
import time
import json
@ -32,6 +34,14 @@ import dish_common
HOST_DEFAULT = "localhost"
class Terminated(Exception):
pass
def handle_sigterm(signum, frame):
# Turn SIGTERM into an exception so main loop can clean up
raise Terminated
def parse_args():
parser = dish_common.create_arg_parser(output_description="publish it to a MQTT broker",
@ -72,6 +82,29 @@ def parse_args():
else:
parser.epilog += "\nSSL support options not available due to missing ssl module"
env_map = (
("MQTT_HOST", "hostname"),
("MQTT_PORT", "port"),
("MQTT_USERNAME", "username"),
("MQTT_PASSWORD", "password"),
("MQTT_SSL", "tls"),
)
env_defaults = {}
for var, opt in env_map:
# check both set and not empty string
val = os.environ.get(var)
if val:
if var == "MQTT_SSL":
if val == "insecure":
env_defaults[opt] = False
elif val == "secure":
env_defaults[opt] = True
else:
env_defaults["ssl_ca_cert"] = val
else:
env_defaults[opt] = val
parser.set_defaults(**env_defaults)
opts = dish_common.run_arg_parser(parser, need_id=True)
if opts.username is None and opts.password is not None:
@ -145,6 +178,8 @@ def main():
gstate = dish_common.GlobalState(target=opts.target)
signal.signal(signal.SIGTERM, handle_sigterm)
try:
next_loop = time.monotonic()
while True:
@ -155,10 +190,11 @@ def main():
time.sleep(next_loop - now)
else:
break
except Terminated:
pass
finally:
gstate.shutdown()
sys.exit(rc)
sys.exit(rc)
if __name__ == '__main__':

View file

@ -0,0 +1,12 @@
[Unit]
Description=Starlink GRPC to MQTT exporter
After=network.target
[Service]
Type=simple
WorkingDirectory=/opt/starlink-grpc-tools/
Environment=MQTT_HOST=localhost MQTT_PORT=1883 MQTT_USERNAME=<changeme> MQTT_PASSORD=<changeme> MQTT_SSL=false
ExecStart=/opt/starlink-grpc-tools/venv/bin/python3 dish_grpc_mqtt.py -t 10 status alert_detail
[Install]
WantedBy=multi-user.target