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",
|
||||
action="store_true",
|
||||
)
|
||||
ap.add_argument("--api-token", help="Cloudflare API token")
|
||||
ap.add_argument(
|
||||
"-v", "--verbose", help="Enable verbose logging", action="store_true"
|
||||
)
|
||||
@ -142,7 +143,7 @@ def main() -> int:
|
||||
)
|
||||
|
||||
# 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:
|
||||
logger.error(
|
||||
"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
|
||||
for record in zone_contents["result"]:
|
||||
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 (record["type"] == "A" and record["content"] != str(ipv4)) or (record["type"] == "AAAA" and record["content"] != str(ipv6)):
|
||||
logger.info(f"Deleting stale record {record['id']} ({record['content']})")
|
||||
if (record["type"] == "A" and record["content"] != str(ipv4)) or (
|
||||
record["type"] == "AAAA" and record["content"] != str(ipv6)
|
||||
):
|
||||
logger.info(
|
||||
f"Deleting stale record {record['id']} ({record['content']})"
|
||||
)
|
||||
if not args.dry_run:
|
||||
delete_response = requests.delete(
|
||||
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),
|
||||
"ttl": 60,
|
||||
"proxied": False,
|
||||
"comment": f"Auto-generated by cfddns on {now}"
|
||||
"comment": f"Auto-generated by cfddns on {now}",
|
||||
},
|
||||
)
|
||||
try:
|
||||
@ -271,7 +278,7 @@ def main() -> int:
|
||||
"content": str(ipv6),
|
||||
"ttl": 60,
|
||||
"proxied": False,
|
||||
"comment": f"Auto-generated by cfddns on {now}"
|
||||
"comment": f"Auto-generated by cfddns on {now}",
|
||||
},
|
||||
)
|
||||
try:
|
||||
@ -286,7 +293,9 @@ def main() -> int:
|
||||
already_exists = False
|
||||
for record in zone_contents["result"]:
|
||||
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 record["content"] == str(ipv4):
|
||||
@ -294,7 +303,9 @@ def main() -> int:
|
||||
break
|
||||
|
||||
# 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:
|
||||
delete_response = requests.delete(
|
||||
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 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:
|
||||
create_response = requests.post(
|
||||
f"https://api.cloudflare.com/client/v4/zones/{args.zone_id}/dns_records",
|
||||
@ -325,7 +338,7 @@ def main() -> int:
|
||||
"content": str(ipv4),
|
||||
"ttl": 60,
|
||||
"proxied": False,
|
||||
"comment": f"Auto-generated by cfddns on {now}"
|
||||
"comment": f"Auto-generated by cfddns on {now}",
|
||||
},
|
||||
)
|
||||
try:
|
||||
@ -338,7 +351,9 @@ def main() -> int:
|
||||
already_exists = False
|
||||
for record in zone_contents["result"]:
|
||||
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 record["content"] == str(ipv6):
|
||||
@ -346,7 +361,9 @@ def main() -> int:
|
||||
break
|
||||
|
||||
# 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:
|
||||
delete_response = requests.delete(
|
||||
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 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:
|
||||
create_response = requests.post(
|
||||
f"https://api.cloudflare.com/client/v4/zones/{args.zone_id}/dns_records",
|
||||
@ -377,7 +396,7 @@ def main() -> int:
|
||||
"content": str(ipv6),
|
||||
"ttl": 60,
|
||||
"proxied": False,
|
||||
"comment": f"Auto-generated by cfddns on {now}"
|
||||
"comment": f"Auto-generated by cfddns on {now}",
|
||||
},
|
||||
)
|
||||
try:
|
||||
|
Loading…
x
Reference in New Issue
Block a user