Add API token param for cron
This commit is contained in:
parent
808ba8ec94
commit
e2c3a2fcdc
@ -130,6 +130,7 @@ def main() -> int:
|
|||||||
help="Print actions instead of performing them",
|
help="Print actions instead of performing them",
|
||||||
action="store_true",
|
action="store_true",
|
||||||
)
|
)
|
||||||
|
ap.add_argument("--api-token", help="Cloudflare API token")
|
||||||
ap.add_argument(
|
ap.add_argument(
|
||||||
"-v", "--verbose", help="Enable verbose logging", action="store_true"
|
"-v", "--verbose", help="Enable verbose logging", action="store_true"
|
||||||
)
|
)
|
||||||
@ -142,7 +143,7 @@ def main() -> int:
|
|||||||
)
|
)
|
||||||
|
|
||||||
# Read env vars needed for the Cloudflare API
|
# Read env vars needed for the Cloudflare API
|
||||||
cf_api_token = read_secret("api-token")
|
cf_api_token = args.api_token or read_secret("api-token")
|
||||||
if not cf_api_token:
|
if not cf_api_token:
|
||||||
logger.error(
|
logger.error(
|
||||||
"Failed to read the Cloudflare API token. Either set $CFDDNS_API_TOKEN or use ewp-secrets"
|
"Failed to read the Cloudflare API token. Either set $CFDDNS_API_TOKEN or use ewp-secrets"
|
||||||
@ -196,11 +197,17 @@ def main() -> int:
|
|||||||
apex_name = None
|
apex_name = None
|
||||||
for record in zone_contents["result"]:
|
for record in zone_contents["result"]:
|
||||||
if record["name"] == args.base_zone and record["type"] in ["A", "AAAA"]:
|
if record["name"] == args.base_zone and record["type"] in ["A", "AAAA"]:
|
||||||
logger.info(f"Found existing {record['type']} record for {record['name']}. Value: {record['content']}")
|
logger.info(
|
||||||
|
f"Found existing {record['type']} record for {record['name']}. Value: {record['content']}"
|
||||||
|
)
|
||||||
|
|
||||||
# If this doesn't match the corresponding IP, delete it
|
# If this doesn't match the corresponding IP, delete it
|
||||||
if (record["type"] == "A" and record["content"] != str(ipv4)) or (record["type"] == "AAAA" and record["content"] != str(ipv6)):
|
if (record["type"] == "A" and record["content"] != str(ipv4)) or (
|
||||||
logger.info(f"Deleting stale record {record['id']} ({record['content']})")
|
record["type"] == "AAAA" and record["content"] != str(ipv6)
|
||||||
|
):
|
||||||
|
logger.info(
|
||||||
|
f"Deleting stale record {record['id']} ({record['content']})"
|
||||||
|
)
|
||||||
if not args.dry_run:
|
if not args.dry_run:
|
||||||
delete_response = requests.delete(
|
delete_response = requests.delete(
|
||||||
f"https://api.cloudflare.com/client/v4/zones/{args.zone_id}/dns_records/{record['id']}",
|
f"https://api.cloudflare.com/client/v4/zones/{args.zone_id}/dns_records/{record['id']}",
|
||||||
@ -248,7 +255,7 @@ def main() -> int:
|
|||||||
"content": str(ipv4),
|
"content": str(ipv4),
|
||||||
"ttl": 60,
|
"ttl": 60,
|
||||||
"proxied": False,
|
"proxied": False,
|
||||||
"comment": f"Auto-generated by cfddns on {now}"
|
"comment": f"Auto-generated by cfddns on {now}",
|
||||||
},
|
},
|
||||||
)
|
)
|
||||||
try:
|
try:
|
||||||
@ -271,7 +278,7 @@ def main() -> int:
|
|||||||
"content": str(ipv6),
|
"content": str(ipv6),
|
||||||
"ttl": 60,
|
"ttl": 60,
|
||||||
"proxied": False,
|
"proxied": False,
|
||||||
"comment": f"Auto-generated by cfddns on {now}"
|
"comment": f"Auto-generated by cfddns on {now}",
|
||||||
},
|
},
|
||||||
)
|
)
|
||||||
try:
|
try:
|
||||||
@ -286,7 +293,9 @@ def main() -> int:
|
|||||||
already_exists = False
|
already_exists = False
|
||||||
for record in zone_contents["result"]:
|
for record in zone_contents["result"]:
|
||||||
if record["name"] == f"ipv4.{args.base_zone}" and record["type"] == "A":
|
if record["name"] == f"ipv4.{args.base_zone}" and record["type"] == "A":
|
||||||
logger.info(f"Found existing A record for {record['name']}. Value: {record['content']}")
|
logger.info(
|
||||||
|
f"Found existing A record for {record['name']}. Value: {record['content']}"
|
||||||
|
)
|
||||||
|
|
||||||
# If the record matches, we're done
|
# If the record matches, we're done
|
||||||
if record["content"] == str(ipv4):
|
if record["content"] == str(ipv4):
|
||||||
@ -294,7 +303,9 @@ def main() -> int:
|
|||||||
break
|
break
|
||||||
|
|
||||||
# Otherwise, delete it
|
# Otherwise, delete it
|
||||||
logger.info(f"Deleting stale record {record['id']} ({record['content']})")
|
logger.info(
|
||||||
|
f"Deleting stale record {record['id']} ({record['content']})"
|
||||||
|
)
|
||||||
if not args.dry_run:
|
if not args.dry_run:
|
||||||
delete_response = requests.delete(
|
delete_response = requests.delete(
|
||||||
f"https://api.cloudflare.com/client/v4/zones/{args.zone_id}/dns_records/{record['id']}",
|
f"https://api.cloudflare.com/client/v4/zones/{args.zone_id}/dns_records/{record['id']}",
|
||||||
@ -311,7 +322,9 @@ def main() -> int:
|
|||||||
|
|
||||||
# If the record doesn't exist, create it
|
# If the record doesn't exist, create it
|
||||||
if not already_exists:
|
if not already_exists:
|
||||||
logger.info(f"Creating new A record for ipv4.{args.base_zone} with value {ipv4}")
|
logger.info(
|
||||||
|
f"Creating new A record for ipv4.{args.base_zone} with value {ipv4}"
|
||||||
|
)
|
||||||
if not args.dry_run:
|
if not args.dry_run:
|
||||||
create_response = requests.post(
|
create_response = requests.post(
|
||||||
f"https://api.cloudflare.com/client/v4/zones/{args.zone_id}/dns_records",
|
f"https://api.cloudflare.com/client/v4/zones/{args.zone_id}/dns_records",
|
||||||
@ -325,7 +338,7 @@ def main() -> int:
|
|||||||
"content": str(ipv4),
|
"content": str(ipv4),
|
||||||
"ttl": 60,
|
"ttl": 60,
|
||||||
"proxied": False,
|
"proxied": False,
|
||||||
"comment": f"Auto-generated by cfddns on {now}"
|
"comment": f"Auto-generated by cfddns on {now}",
|
||||||
},
|
},
|
||||||
)
|
)
|
||||||
try:
|
try:
|
||||||
@ -338,7 +351,9 @@ def main() -> int:
|
|||||||
already_exists = False
|
already_exists = False
|
||||||
for record in zone_contents["result"]:
|
for record in zone_contents["result"]:
|
||||||
if record["name"] == f"ipv6.{args.base_zone}" and record["type"] == "AAAA":
|
if record["name"] == f"ipv6.{args.base_zone}" and record["type"] == "AAAA":
|
||||||
logger.info(f"Found existing AAAA record for {record['name']}. Value: {record['content']}")
|
logger.info(
|
||||||
|
f"Found existing AAAA record for {record['name']}. Value: {record['content']}"
|
||||||
|
)
|
||||||
|
|
||||||
# If the record matches, we're done
|
# If the record matches, we're done
|
||||||
if record["content"] == str(ipv6):
|
if record["content"] == str(ipv6):
|
||||||
@ -346,7 +361,9 @@ def main() -> int:
|
|||||||
break
|
break
|
||||||
|
|
||||||
# Otherwise, delete it
|
# Otherwise, delete it
|
||||||
logger.info(f"Deleting stale record {record['id']} ({record['content']})")
|
logger.info(
|
||||||
|
f"Deleting stale record {record['id']} ({record['content']})"
|
||||||
|
)
|
||||||
if not args.dry_run:
|
if not args.dry_run:
|
||||||
delete_response = requests.delete(
|
delete_response = requests.delete(
|
||||||
f"https://api.cloudflare.com/client/v4/zones/{args.zone_id}/dns_records/{record['id']}",
|
f"https://api.cloudflare.com/client/v4/zones/{args.zone_id}/dns_records/{record['id']}",
|
||||||
@ -363,7 +380,9 @@ def main() -> int:
|
|||||||
|
|
||||||
# If the record doesn't exist, create it
|
# If the record doesn't exist, create it
|
||||||
if not already_exists:
|
if not already_exists:
|
||||||
logger.info(f"Creating new AAAA record for ipv6.{args.base_zone} with value {ipv6}")
|
logger.info(
|
||||||
|
f"Creating new AAAA record for ipv6.{args.base_zone} with value {ipv6}"
|
||||||
|
)
|
||||||
if not args.dry_run:
|
if not args.dry_run:
|
||||||
create_response = requests.post(
|
create_response = requests.post(
|
||||||
f"https://api.cloudflare.com/client/v4/zones/{args.zone_id}/dns_records",
|
f"https://api.cloudflare.com/client/v4/zones/{args.zone_id}/dns_records",
|
||||||
@ -377,7 +396,7 @@ def main() -> int:
|
|||||||
"content": str(ipv6),
|
"content": str(ipv6),
|
||||||
"ttl": 60,
|
"ttl": 60,
|
||||||
"proxied": False,
|
"proxied": False,
|
||||||
"comment": f"Auto-generated by cfddns on {now}"
|
"comment": f"Auto-generated by cfddns on {now}",
|
||||||
},
|
},
|
||||||
)
|
)
|
||||||
try:
|
try:
|
||||||
|
Loading…
x
Reference in New Issue
Block a user