Skip to content

Commit f0d9510

Browse files
Merge pull request #117 from arulrajnet/env-support
[feature] Support for environmental substitution in config.json
2 parents 4ea9ba5 + d3fe394 commit f0d9510

File tree

5 files changed

+28
-5
lines changed

5 files changed

+28
-5
lines changed

README.md

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -258,6 +258,20 @@ If you have multiple IP addresses and want to load balance between them, you can
258258
}
259259
```
260260

261+
### Docker environment variable support
262+
263+
Define environmental variables starts with `CF_DDNS_` and use it in config.json
264+
265+
For ex:
266+
267+
```json
268+
{
269+
"cloudflare": [
270+
{
271+
"authentication": {
272+
"api_token": "${CF_DDNS_API_TOKEN}",
273+
```
274+
261275
### 🧹 Optional features
262276

263277
`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.

cloudflare-ddns.py

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88

99
__version__ = "1.0.2"
1010

11+
from string import Template
12+
1113
import json
1214
import os
1315
import signal
@@ -17,7 +19,8 @@
1719
import requests
1820

1921
CONFIG_PATH = os.environ.get('CONFIG_PATH', os.getcwd())
20-
22+
# Read in all environment variables that have the correct prefix
23+
ENV_VARS = {key: value for (key, value) in os.environ.items() if key.startswith('CF_DDNS_')}
2124

2225
class GracefulExit:
2326
def __init__(self):
@@ -260,7 +263,10 @@ def updateIPs(ips):
260263
config = None
261264
try:
262265
with open(os.path.join(CONFIG_PATH, "config.json")) as config_file:
263-
config = json.loads(config_file.read())
266+
if len(ENV_VARS) != 0:
267+
config = json.loads(Template(config_file.read()).safe_substitute(ENV_VARS))
268+
else:
269+
config = json.loads(config_file.read())
264270
except:
265271
print("😡 Error reading config.json")
266272
# wait 10 seconds to prevent excessive logging on docker auto restart

scripts/docker-build-all.sh

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
#!/bin/bash
2-
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 ../
2+
BASH_DIR=$(dirname $(realpath "${BASH_SOURCE}"))
3+
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}/../
34
# TODO: Support linux/riscv64

scripts/docker-build.sh

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
#!/bin/bash
2-
docker build --platform linux/amd64 --tag timothyjmiller/cloudflare-ddns:latest ../
2+
BASH_DIR=$(dirname $(realpath "${BASH_SOURCE}"))
3+
docker build --platform linux/amd64 --tag timothyjmiller/cloudflare-ddns:latest ${BASH_DIR}/../

scripts/docker-publish.sh

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
#!/bin/bash
2-
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 ../
2+
BASH_DIR=$(dirname $(realpath "${BASH_SOURCE}"))
3+
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}/../

0 commit comments

Comments
 (0)