From af6705fb1e687b48720ac862186be4d078b73308 Mon Sep 17 00:00:00 2001 From: Jerome Petazzoni Date: Thu, 19 Mar 2020 17:15:43 -0500 Subject: [PATCH] Add script to map DNS This script needs: - a list of domains managed by GANDI LiveDNS - a list of IP addresses of clusters (like in tags/*/ips.txt) It will replace the current configuration for these domains so that they point to the clusters. The apex of each domain and a wildcard entry will have round-robin records pointing to all the nodes of the cluster. In addition, there will be records node[1234...] pointing to each individual node. --- prepare-vms/map-dns.py | 49 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 49 insertions(+) create mode 100755 prepare-vms/map-dns.py diff --git a/prepare-vms/map-dns.py b/prepare-vms/map-dns.py new file mode 100755 index 00000000..698cad56 --- /dev/null +++ b/prepare-vms/map-dns.py @@ -0,0 +1,49 @@ +#!/usr/bin/env python +import os +import requests +import yaml + +# configurable stuff +domains_file = "../../plentydomains/domains.txt" +config_file = os.path.join( + os.environ["HOME"], ".config/gandi/config.yaml") +tag = "test" +apiurl = "https://dns.api.gandi.net/api/v5/domains" + +# inferred stuff +domains = open(domains_file).read().split() +apikey = yaml.safe_load(open(config_file))["apirest"]["key"] +ips = open(f"tags/{tag}/ips.txt").read().split() +settings_file = f"tags/{tag}/settings.yaml" +clustersize = yaml.safe_load(open(settings_file))["clustersize"] + +# now do the fucking work +while domains and ips: + domain = domains[0] + domains = domains[1:] + cluster = ips[:clustersize] + ips = ips[clustersize:] + print(f"{domain} => {cluster}") + zone = "" + node = 0 + for ip in cluster: + node += 1 + zone += f"@ 300 IN A {ip}\n" + zone += f"* 300 IN A {ip}\n" + zone += f"node{node} 300 IN A {ip}\n" + r = requests.put( + f"{apiurl}/{domain}/records", + headers={"x-api-key": apikey}, + data=zone) + print(r.text) + + #r = requests.get( + # f"{apiurl}/{domain}/records", + # headers={"x-api-key": apikey}, + # ) + +if domains: + print(f"Good, we have {len(domains)} domains left.") + +if ips: + print(f"Crap, we have {len(ips)} IP addresses left.")