feat: add loop support for container

This commit is contained in:
2024-02-17 17:03:14 +01:00
parent 7d3c449c16
commit d6a2f8ec15

View File

@@ -1,6 +1,7 @@
#!/usr/bin/python3 #!/usr/bin/python3
import sys import sys
import time
import argparse import argparse
import subprocess import subprocess
import os import os
@@ -121,6 +122,8 @@ if __name__ == "__main__":
parser.add_argument('--smtp-user') parser.add_argument('--smtp-user')
parser.add_argument('--smtp-pass') parser.add_argument('--smtp-pass')
parser.add_argument('--loop', default=True, action=argparse.BooleanOptionalAction)
args = parser.parse_args() args = parser.parse_args()
# set dispatch server & authentication # # set dispatch server & authentication #
@@ -142,50 +145,58 @@ if __name__ == "__main__":
smtp_user = args.smtp_user or os.environ.get("SMTP_USER") smtp_user = args.smtp_user or os.environ.get("SMTP_USER")
smtp_pass = args.smtp_pass or os.environ.get("SMTP_PASS") smtp_pass = args.smtp_pass or os.environ.get("SMTP_PASS")
# request dispatches # first_run = True
response = requests.get(args.dispatch_server + "/get-dispatch?method=all&timeout=0", auth=AUTH) while args.loop or first_run:
# check status # # request dispatches #
if response.status_code == HTTP_NOT_FOUND: response = requests.get(args.dispatch_server + "/get-dispatch?method=all&timeout=0", auth=AUTH)
sys.exit(0)
# fallback check for status # # check status #
response.raise_for_status() if response.status_code == HTTP_NOT_FOUND:
sys.exit(0)
# track dispatches that were confirmed to avoid duplicate confirmation # # fallback check for status #
dispatch_confirmed = [] response.raise_for_status()
# track failed dispatches # # track dispatches that were confirmed to avoid duplicate confirmation #
errors = dict() dispatch_confirmed = []
# iterate over dispatch requests # # track failed dispatches #
for entry in response.json(): errors = dict()
user = entry["username"] # iterate over dispatch requests #
dispatch_uuid = entry["uuid"] for entry in response.json():
method = entry["method"]
message = entry["message"]
title = entry.get("title")
# method dependent fields # user = entry["username"]
phone = entry.get("phone") dispatch_uuid = entry["uuid"]
email_address = entry.get("email") method = entry["method"]
message = entry["message"]
title = entry.get("title")
# send message # # method dependent fields #
if method == "signal": phone = entry.get("phone")
pass email_address = entry.get("email")
elif method == "ntfy":
user_topic = ntfy_api_get_topic(ntfy_api_server, ntfy_api_token, user)
ntfy_send(dispatch_uuid, user_topic, title, message,
ntfy_push_target, ntfy_user, ntfy_pass)
elif method == "email":
email_send(dispatch_uuid, email_address, message, smtp_target, smtp_user, smtp_pass)
elif method == "debug":
debug_send(dispatch_uuid, entry)
elif method == "debug-fail":
debug_send(dispatch_uuid, entry, fail_it=True)
else:
print("Unsupported dispatch method {}".format(entry["method"]), sys=sys.stderr)
continue
sys.exit(0) # send message #
if method == "signal":
pass
elif method == "ntfy":
user_topic = ntfy_api_get_topic(ntfy_api_server, ntfy_api_token, user)
ntfy_send(dispatch_uuid, user_topic, title, message,
ntfy_push_target, ntfy_user, ntfy_pass)
elif method == "email":
email_send(dispatch_uuid, email_address, message, smtp_target, smtp_user, smtp_pass)
elif method == "debug":
debug_send(dispatch_uuid, entry)
elif method == "debug-fail":
debug_send(dispatch_uuid, entry, fail_it=True)
else:
print("Unsupported dispatch method {}".format(entry["method"]), sys=sys.stderr)
continue
# wait a moment #
if args.loop:
time.sleep(5)
# handle non-loop runs #
first_run = False