From 0f3708a482af13e65312912a021f35dfd8bc4db7 Mon Sep 17 00:00:00 2001 From: Brendan Jackman Date: Sun, 28 Feb 2021 18:24:25 +0100 Subject: [PATCH] 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))