From 8c55892f324eacbb0384e18a01bde32a409e1ed8 Mon Sep 17 00:00:00 2001 From: Brendan Jackman Date: Sun, 28 Feb 2021 18:18:03 +0100 Subject: [PATCH 1/2] Switch to argparse The next commit adds a second argument, so raw sys.argv parsing will be a bit cumbersome. Switch to argparse instead. --- cloudflare-ddns.py | 27 ++++++++++++++------------- 1 file changed, 14 insertions(+), 13 deletions(-) diff --git a/cloudflare-ddns.py b/cloudflare-ddns.py index 4b479c7..7577969 100755 --- a/cloudflare-ddns.py +++ b/cloudflare-ddns.py @@ -1,4 +1,4 @@ -import requests, json, sys, signal, os, time +import argparse, requests, json, sys, signal, os, time PATH = os.getcwd() + "/" version = float(str(sys.version_info[0]) + "." + str(sys.version_info[1])) @@ -148,18 +148,19 @@ def updateIPs(): commitRecord(ip) if __name__ == '__main__': - if(len(sys.argv) > 1): - if(sys.argv[1] == "--repeat"): - delay = 5*60 - print("⏲️ Updating IPv4 (A) & IPv6 (AAAA) records every 5 minutes") - next_time = time.time() - killer = GracefulExit() - while not killer.kill_now: - time.sleep(max(0, next_time - time.time())) - updateIPs() - next_time += (time.time() - next_time) // delay * delay + delay - else: - print("😡 Unrecognized parameter '" + sys.argv[1] + "'. Stopping now.") + parser = argparse.ArgumentParser() + parser.add_argument("--repeat", type=bool) + args = parser.parse_args() + + if args.repeat: + delay = 5*60 + print("⏲️ Updating IPv4 (A) & IPv6 (AAAA) records every 5 minutes") + next_time = time.time() + killer = GracefulExit() + while not killer.kill_now: + time.sleep(max(0, next_time - time.time())) + updateIPs() + next_time += (time.time() - next_time) // delay * delay + delay else: updateIPs() From 0f3708a482af13e65312912a021f35dfd8bc4db7 Mon Sep 17 00:00:00 2001 From: Brendan Jackman Date: Sun, 28 Feb 2021 18:24:25 +0100 Subject: [PATCH 2/2] Add a flag to modify config.json location On Kubernetes, it's really awkward to write a Secret into the root directory: https://www.jeffgeerling.com/blog/2019/mounting-kubernetes-secret-single-file-inside-pod Therefore this adds support for reading the config from an arbitrary path. The behaviour is unchanged if you don't set this new flag. --- cloudflare-ddns.py | 14 ++++++-------- 1 file changed, 6 insertions(+), 8 deletions(-) diff --git a/cloudflare-ddns.py b/cloudflare-ddns.py index 7577969..5521a69 100755 --- a/cloudflare-ddns.py +++ b/cloudflare-ddns.py @@ -21,9 +21,6 @@ class GracefulExit: print("🛑 Stopping main thread...") self.kill_now = True -with open(PATH + "config.json") as config_file: - config = json.loads(config_file.read()) - def deleteEntries(type): # Helper function for deleting A or AAAA records # in the case of no IPv4 or IPv6 connection, yet @@ -70,7 +67,7 @@ def getIPs(): }) return ips -def commitRecord(ip): +def commitRecord(ip, config): for c in config["cloudflare"]: subdomains = c["subdomains"] response = cf_api("zones/" + c['zone_id'], "GET", c) @@ -143,13 +140,14 @@ def cf_api(endpoint, method, config, headers={}, data=False): return response.json() -def updateIPs(): +def updateIPs(config): for ip in getIPs(): - commitRecord(ip) + commitRecord(ip, config) if __name__ == '__main__': parser = argparse.ArgumentParser() parser.add_argument("--repeat", type=bool) + parser.add_argument("--config-path", dest="config", type=open, default=os.path.join(PATH, "config.json")) args = parser.parse_args() if args.repeat: @@ -159,8 +157,8 @@ if __name__ == '__main__': killer = GracefulExit() while not killer.kill_now: time.sleep(max(0, next_time - time.time())) - updateIPs() + updateIPs(json.load(args.config)) next_time += (time.time() - next_time) // delay * delay + delay else: - updateIPs() + updateIPs(json.load(args.config))