Skip to content

Commit

Permalink
Handle invalid cache
Browse files Browse the repository at this point in the history
When the zone has been deleted and recreated manually between two updates,
the record_id will be different. We detect an invalid cache in this case
and try to get zone records again from CloudFlare or create it if getting
the zone_id failed. Fixes #6 #7.
  • Loading branch information
kissgyorgy committed May 22, 2021
1 parent c999ea6 commit 57f2fee
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 16 deletions.
13 changes: 13 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,19 @@ Or you can use [the Docker image](https://hub.docker.com/repository/docker/kissg
$ docker run --rm -it kissgyorgy/cloudflare-dyndns --help
```

# Note

If you use this script, it "takes over" the handling of the record of those
domains you specified, which means it will update existing records and create
missing ones.

You should not change A or AAAA records manually or with other scripts, because
the changes will be overwritten.

I decided to make it work this way, because I think most users expect this
behavior, but if you have a different use case,
[let me know!](https://github.com/kissgyorgy/cloudflare-dyndns/issues/new)

## Command line interface

```
Expand Down
45 changes: 29 additions & 16 deletions cloudflare_dyndns/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,34 +42,47 @@ def update_domains(
cf: CloudFlareWrapper, domains: List[str], ip_cache: IPCache, current_ip: IPAddress,
):
success = True

for domain in domains:
try:
zone_record = ip_cache.updated_domains[domain]
zone_id = zone_record.zone_id
record_id = zone_record.record_id
except KeyError:
get_record_failed = False
update_record_failed = False

cache_record = ip_cache.updated_domains.get(domain)

if cache_record is not None:
zone_id = cache_record.zone_id
record_id = cache_record.record_id
try:
cf.update_record(domain, current_ip, zone_id, record_id)
except CloudFlare.exceptions.CloudFlareAPIError:
printer.error("Invalid cache, deleting")
del ip_cache.updated_domains[domain]
update_record_failed = True

if cache_record is None or update_record_failed:
try:
zone_id = cf.get_zone_id(domain)
except CloudFlareError as e:
except CloudFlareError:
# TODO: try to create zone?
success = False
continue

try:
record_id = cf.get_record_id(domain, get_record_type(current_ip))
zone_record = ZoneRecord(zone_id=zone_id, record_id=record_id)
except CloudFlareError as e:
except CloudFlareError:
try:
record_id = cf.create_record(domain, current_ip)
except CloudFlare.exceptions.CloudFlareAPIError as e:
except CloudFlare.exceptions.CloudFlareAPIError:
success = False
continue

try:
cf.update_record(domain, current_ip, zone_id, record_id)
except Exception as e:
success = False
continue
continue
else:
try:
cf.update_record(domain, current_ip, zone_id, record_id)
except CloudFlare.exceptions.CloudFlareAPIError:
success = False
continue

zone_record = ZoneRecord(zone_id=zone_id, record_id=record_id)
ip_cache.updated_domains[domain] = zone_record

return success
Expand Down

0 comments on commit 57f2fee

Please sign in to comment.