-
Notifications
You must be signed in to change notification settings - Fork 1
/
route53.py
61 lines (54 loc) · 2.33 KB
/
route53.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
#!/usr/bin/python
import sys, getopt, time, boto3, ast, os
from get_docker_secret import get_docker_secret
def main(argv):
ip=''
try:
opts, args = getopt.getopt(argv, "hi:d", ["help", "ip="])
except getopt.GetoptError:
sys.exit(2)
if len(args) > 0:
ip=args[0]
else:
sys.exit(2)
print(f"Refreshing IP {ip}")
unparsed_zones = os.environ["AWS_ROUTE53_ZONES"]
a_record = os.environ["AWS_ROUTE53_A_RECORD"]
zones = list(ast.literal_eval(unparsed_zones))
spf_records = ["SPF", "TXT"]
spf_format = "\"v=spf1 include:_spf.google.com ipv4:"+ip+" ~all\""
client = boto3.client(
"route53",
aws_access_key_id=get_docker_secret("AWS_ACCESS_KEY_ID"),
aws_secret_access_key=get_docker_secret("AWS_SECRET_ACCESS_KEY"),
)
for zone_id, zone_name in zones:
print(f"Processing {zone_id} ({zone_name})")
response = client.list_resource_record_sets(HostedZoneId=zone_id)
change_request = []
for record in response['ResourceRecordSets']:
if record['Type'] == 'A' and record["Name"] == f"{a_record}.{zone_name}.":
for resource in record.get("ResourceRecords") or []:
if resource['Value'] != ip:
print(f"Updating {a_record}.{zone_name} type {record['Type']} from {resource['Value']} to {ip}")
resource['Value'] = ip
change_request.append({'Action': 'UPSERT', 'ResourceRecordSet': record})
if record['Type'] in spf_records:
for resource in record.get("ResourceRecords") or []:
if resource['Value'] != spf_format:
print(f"Updating {a_record}.{zone_name} type {record['Type']} from {resource['Value']} to {ip}")
resource['Value'] = spf_format
change_request.append({'Action': 'UPSERT', 'ResourceRecordSet': record})
if change_request:
client.change_resource_record_sets(
HostedZoneId=zone_id,
ChangeBatch= {
'Comment': f"autoupdate dynamic ip with {ip}",
'Changes': change_request
}
)
f = open("/var/lastip", "w")
f.write(ip)
f.close()
if __name__ == "__main__":
main(sys.argv[1:])