diff --git a/README.md b/README.md index d774c0f..a9f82f2 100755 --- a/README.md +++ b/README.md @@ -258,6 +258,20 @@ If you have multiple IP addresses and want to load balance between them, you can } ``` +### Docker environment variable support + +Define environmental variables starts with `CF_DDNS_` and use it in config.json + +For ex: + +```json +{ + "cloudflare": [ + { + "authentication": { + "api_token": "${CF_DDNS_API_TOKEN}", +``` + ### 🧹 Optional features `purgeUnknownRecords` removes stale DNS records from Cloudflare. This is useful if you have a dynamic DNS record that you no longer want to use. If you have a dynamic DNS record that you no longer want to use, you can set `purgeUnknownRecords` to `true` and the script will remove the stale DNS record from Cloudflare. diff --git a/cloudflare-ddns.py b/cloudflare-ddns.py index 1d56863..64dc6e9 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): @@ -260,7 +263,10 @@ def updateIPs(ips): config = None try: with open(os.path.join(CONFIG_PATH, "config.json")) as config_file: - config = json.loads(config_file.read()) + 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 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}/../