forked from gokeefe/libcloud-dynamic-dns
/
update_dns.py
77 lines (63 loc) · 2.94 KB
/
update_dns.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
import libcloud
from libcloud.common.types import LibcloudError
from libcloud.dns.drivers.google import GoogleDNSDriver
from libcloud.dns.types import RecordType
import requests
from config import Config
class GoogleDNSUpdater:
dns_driver = None
def __init__(self):
self.create_dns_driver()
def create_dns_driver(self):
self.dns_driver = GoogleDNSDriver(Config.DNS_USER_ID, Config.DNS_KEY, Config.DNS_PROJECT_NAME)
def list_zones(self):
return self.dns_driver.list_zones()
def format_record_name(self, name, zoneDomain):
return "{}.{}".format(name, zoneDomain)
def get_record_id_for_record_name(self, zone, name):
for record in self.dns_driver.list_records(zone):
if record.type != RecordType.A:
continue
if self.format_record_name(name, zone.domain) == record.name:
return record.id
def create_or_update_record(self, zone=None, record_name=None, a_record_value=None, ttl_seconds=3600):
if None in (zone, record_name, a_record_value):
return False
formatted_record_name = self.format_record_name(record_name, zone.domain)
# Try locating existing record with the same name
dns_record = None
try:
record_id = self.get_record_id_for_record_name(zone, record_name)
if record_id:
dns_record = self.dns_driver.get_record(zone.id, record_id)
except LibcloudError as e:
print("Error locating record: {}".format(e.message))
# Set record data
record_data = {
"ttl": ttl_seconds,
"rrdatas": [a_record_value]
}
# Create or update an existing record with record_data
if not dns_record:
return self.dns_driver.create_record(formatted_record_name, zone, RecordType.A, record_data)
elif a_record_value in dns_record.data['rrdatas']:
print(f"{dns_record.name} is already set to {a_record_value}. Not updating.")
return True
else:
if self.dns_driver.delete_record(dns_record):
return self.dns_driver.create_record(formatted_record_name, zone, RecordType.A, record_data)
else:
return False
def update_record_ip(self, zone_name, record_name, ip, ttl_seconds):
for zone in self.list_zones():
if zone.domain == zone_name:
print("Setting A record: {}.{} to point: {}".format(record_name, zone.domain, ip))
return gdns.create_or_update_record(zone, record_name, ip, ttl_seconds)
return False
if __name__ == '__main__':
WHATS_MY_IP_URL = "https://api.ipify.org"
current_ip = requests.get(WHATS_MY_IP_URL).text
gdns = GoogleDNSUpdater()
result = gdns.update_record_ip(Config.A_RECORD_ZONE_NAME, Config.A_RECORD_NAME,
current_ip, Config.A_RECORD_TTL_SECONDS)
print("SUCCESS" if result else "FAILURE")