forked from HackEmergency/hack-emergency
-
Notifications
You must be signed in to change notification settings - Fork 0
/
zone_record_changer.py
99 lines (86 loc) · 2.83 KB
/
zone_record_changer.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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
#!/usr/bin/python
"""
Update a dynamic DNS entry from your external IP address.
Make an API_KEY in the Memset control panel. This will need to have
these permissions at minimum.
dns.zone_domain_list
dns.zone_info
dns.zone_record_update
dns.reload
job.status
Pass in API_KEY and HOSTNAME and this script will set the HOSTNAME to
have your current external IP address.
"""
uri = "https://%s:[email protected]/v1/xmlrpc"
import os
import sys
from xmlrpclib import ServerProxy
from pprint import pformat
from urllib2 import urlopen
from time import sleep
def change_ip(s, hostname, new_ip):
"""
Find the zone record for domain and adjust the A record for
hostname to new_ip
Returns a boolean if the record changed
"""
# import ipdb; ipdb.set_trace()
chunks = hostname.split(".")
domain = ".".join(chunks[-2:])
leafname = ".".join(chunks[:-2])
# leafname, domain = hostname.split(".", 1)
# Find the zone that the domain is in first
zone_domains = s.dns.zone_domain_list()
for zone_domain in zone_domains:
if zone_domain['domain'] == domain:
break
else:
raise ValueError("Couldn't find domain %r" % domain)
zone_id = zone_domain['zone_id']
print "Domain", pformat(domain)
# List the zone to find the record to change
zone = s.dns.zone_info(dict(id=zone_id))
for record in zone['records']:
if record['record'] == leafname and record['type'] == 'A':
break
else:
raise ValueError("Couldn't find record for %r" % hostname)
# Change the zone record
print "Old Zone Record", pformat(record)
new_record = s.dns.zone_record_update(dict(id=record['id'], address=new_ip))
print "New Zone Record", pformat(new_record)
return record != new_record
def reload_dns(s):
"""
Reload the DNS servers
"""
print "Reloading DNS",
job = s.dns.reload()
while not job['finished']:
sys.stdout.flush()
job = s.job.status(dict(id=job['id']))
print ".",
sleep(5)
if not job['error']:
print "OK"
else:
print "FAILED"
def main():
"""
Find the external IP and update the zone with it
"""
if len(sys.argv) != 4:
print >>sys.stderr, "Syntax: %s <api_key> <hostname> <docker-machine>" % sys.argv[0]
sys.exit(1)
api_key, parent_hostname, docker_machine = sys.argv[1:]
hostname = ".".join([docker_machine, parent_hostname])
# external_ip = urlopen("http://api.externalip.net/ip/").read().strip()
docker_ip = os.popen("docker-machine ip {}".format(docker_machine)).read().strip()
print "Docker Machine IP is {}".format(docker_ip)
s = ServerProxy(uri % api_key)
if change_ip(s, hostname, docker_ip):
reload_dns(s)
else:
print "Zone record unchanged, not reloading"
if __name__ == "__main__":
main()