From a1fa3b9714fab430083b0d1d1b3b3dbeea43b2fa Mon Sep 17 00:00:00 2001 From: Arul Date: Sun, 13 Nov 2022 21:37:44 +0530 Subject: [PATCH 1/2] support for environmental substitution in config.json using python Template refer comment in #35 --- README.md | 14 ++++++++++++-- cloudflare-ddns.py | 7 +++++-- scripts/docker-build-all.sh | 3 ++- scripts/docker-build.sh | 3 ++- scripts/docker-publish.sh | 3 ++- 5 files changed, 23 insertions(+), 7 deletions(-) diff --git a/README.md b/README.md index 90df176..fb8caad 100755 --- a/README.md +++ b/README.md @@ -213,9 +213,19 @@ Do not include the base domain name in your `subdomains` config. Do not use the } ``` -### 🗣️ Call to action: Docker environment variable support +### Docker environment variable support -I am looking for help adding Docker environment variable support to this project. If interested, check out [this comment](https://github.com/timothymiller/cloudflare-ddns/pull/35#issuecomment-974752476) and open a PR. +Define environmental variables starts with `CF_DDNS_` and use it in config.json + +For ex: + +```json +{ + "cloudflare": [ + { + "authentication": { + "api_token": "${CF_DDNS_API_TOKEN}", +``` ## 🐳 Deploy with Docker Compose diff --git a/cloudflare-ddns.py b/cloudflare-ddns.py index a99238b..9b19393 100755 --- a/cloudflare-ddns.py +++ b/cloudflare-ddns.py @@ -8,6 +8,8 @@ __version__ = "1.0.2" +from string import Template + import json import os import signal @@ -17,7 +19,8 @@ import requests CONFIG_PATH = os.environ.get('CONFIG_PATH', os.getcwd()) - +# Read in all environment variables that have the correct prefix +ENV_VARS = {key: value for (key, value) in os.environ.items() if key.startswith('CF_DDNS_')} class GracefulExit: def __init__(self): @@ -216,7 +219,7 @@ def updateIPs(ips): config = None try: with open(os.path.join(CONFIG_PATH, "config.json")) as config_file: - config = json.loads(config_file.read()) + config = json.loads(Template(config_file.read()).safe_substitute(ENV_VARS)) except: print("😡 Error reading config.json") # wait 10 seconds to prevent excessive logging on docker auto restart diff --git a/scripts/docker-build-all.sh b/scripts/docker-build-all.sh index cdc52ba..3bfcb08 100755 --- a/scripts/docker-build-all.sh +++ b/scripts/docker-build-all.sh @@ -1,3 +1,4 @@ #!/bin/bash -docker buildx build --platform linux/ppc64le,linux/s390x,linux/386,linux/arm/v6,linux/arm/v7,linux/arm64/v8,linux/amd64 --tag timothyjmiller/cloudflare-ddns:latest ../ +BASH_DIR=$(dirname $(realpath "${BASH_SOURCE}")) +docker buildx build --platform linux/ppc64le,linux/s390x,linux/386,linux/arm/v6,linux/arm/v7,linux/arm64/v8,linux/amd64 --tag timothyjmiller/cloudflare-ddns:latest ${BASH_DIR}/../ # TODO: Support linux/riscv64 diff --git a/scripts/docker-build.sh b/scripts/docker-build.sh index b0547fe..3943e20 100755 --- a/scripts/docker-build.sh +++ b/scripts/docker-build.sh @@ -1,2 +1,3 @@ #!/bin/bash -docker build --platform linux/amd64 --tag timothyjmiller/cloudflare-ddns:latest ../ +BASH_DIR=$(dirname $(realpath "${BASH_SOURCE}")) +docker build --platform linux/amd64 --tag timothyjmiller/cloudflare-ddns:latest ${BASH_DIR}/../ diff --git a/scripts/docker-publish.sh b/scripts/docker-publish.sh index b54aa78..4e5b09b 100755 --- a/scripts/docker-publish.sh +++ b/scripts/docker-publish.sh @@ -1,2 +1,3 @@ #!/bin/bash -docker buildx build --platform linux/ppc64le,linux/s390x,linux/386,linux/arm/v6,linux/arm/v7,linux/arm64/v8,linux/amd64 --tag timothyjmiller/cloudflare-ddns:latest --push ../ +BASH_DIR=$(dirname $(realpath "${BASH_SOURCE}")) +docker buildx build --platform linux/ppc64le,linux/s390x,linux/386,linux/arm/v6,linux/arm/v7,linux/arm64/v8,linux/amd64 --tag timothyjmiller/cloudflare-ddns:latest --push ${BASH_DIR}/../ From d3fe3940f9438e1e66e583b2a62c368d414276dd Mon Sep 17 00:00:00 2001 From: Arul Date: Tue, 21 Feb 2023 06:53:01 +0530 Subject: [PATCH 2/2] addressing review comments --- cloudflare-ddns.py | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/cloudflare-ddns.py b/cloudflare-ddns.py index ca169c1..d13b4c3 100755 --- a/cloudflare-ddns.py +++ b/cloudflare-ddns.py @@ -263,7 +263,10 @@ def updateIPs(ips): config = None try: with open(os.path.join(CONFIG_PATH, "config.json")) as config_file: - config = json.loads(Template(config_file.read()).safe_substitute(ENV_VARS)) + if len(ENV_VARS) != 0: + config = json.loads(Template(config_file.read()).safe_substitute(ENV_VARS)) + else: + config = json.loads(config_file.read()) except: print("😡 Error reading config.json") # wait 10 seconds to prevent excessive logging on docker auto restart @@ -313,4 +316,4 @@ def updateIPs(ips): print("❓ Unrecognized parameter '" + sys.argv[1] + "'. Stopping now.") else: - updateIPs(getIPs()) \ No newline at end of file + updateIPs(getIPs())