-
Notifications
You must be signed in to change notification settings - Fork 3
/
netlify-dns.py
executable file
·81 lines (62 loc) · 2.62 KB
/
netlify-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
78
79
80
81
#!/usr/bin/python3
import os
from urllib import request, parse
import json
import sys
LOG_FILE = open('/var/log/netlifydns.log', 'w')
oldid = None
oldip = None
def getNetlifyDNS():
global oldid, oldip
getreq = request.Request('https://api.netlify.com/api/v1/dns_zones/' + urlv1 + '/dns_records',
headers={'Content-Type': 'application/json;charset=utf-8',
'Authorization': 'Bearer ' + API_TOKEN})
current = json.loads(request.urlopen(getreq).read().decode("utf-8"))
oldid = current[0]['id']
oldip = current[0]['value']
def AddNetlifyDNS():
global oldid, oldip
getNetlifyDNS()
if ipresp['ip'] == oldip:
LOG_FILE.write("IP hasn't changed...")
sys.exit(0)
else:
LOG_FILE.write("Changing IP's from " + oldip + " to " + ipresp['ip'])
netlify_add_req = {}
netlify_add_req['hostname'] = URL
netlify_add_req['ttl'] = 3600
netlify_add_req['type'] = "A"
netlify_add_req['value'] = ipresp['ip']
netlify_del_req = {}
print(netlify_del_req)
request_body = json.dumps(netlify_add_req).encode('utf-8')
request_del_body = json.dumps(netlify_del_req).encode('utf-8')
print("url::" + urlv1)
actualreq = request.Request('https://api.netlify.com/api/v1/dns_zones/' + urlv1 + '/dns_records')
actualreq.add_header("Content-Type", "application/json; charset=utf-8")
actualreq.add_header("Authorization", "Bearer " + API_TOKEN)
actualreq.add_header('Content-Length', len(request_body))
response = request.urlopen(actualreq, request_body)
delreq = request.Request('https://api.netlify.com/api/v1/dns_zones/' + urlv1 + '/dns_records/' + oldid, method='DELETE')
delreq.add_header("Content-Type", "application/json; charset=utf-8")
delreq.add_header("Authorization", "Bearer " + API_TOKEN)
delreq.add_header('Content-Length', len(request_del_body))
response_del = request.urlopen(delreq, request_del_body)
print(response.read())
print(response_del.read())
if __name__ == '__main__':
# export NETLIFY_API_TOKEN=XXXXXXXXXXXXX
try:
API_TOKEN = os.environ['NETLIFY_API_TOKEN']
except:
print("Couldn't Get API_TOKEN evn var")
sys.exit(1)
# export NETLIFY_URL
try:
URL = os.environ['NETLIFY_URL']
urlv1 = URL.replace(".com", "_com")
except:
print("Couldn't Get API_TOKEN evn var")
sys.exit(1)
ipresp = json.loads(request.urlopen('https://api.ipify.org?format=json').read().decode("utf-8"))
AddNetlifyDNS()