From cf908822be6a501bcafe1a8d18eaf804eab224b3 Mon Sep 17 00:00:00 2001 From: Knut Ahlers Date: Sat, 15 Dec 2018 01:30:22 +0100 Subject: [PATCH] Load zones to manage from consul Signed-off-by: Knut Ahlers --- consul.py | 26 +++++++++++++++ generateZonefiles.py | 3 ++ zones.yml | 78 -------------------------------------------- 3 files changed, 29 insertions(+), 78 deletions(-) diff --git a/consul.py b/consul.py index d63393e..ff40fb7 100644 --- a/consul.py +++ b/consul.py @@ -4,6 +4,32 @@ import os import requests +def get_zones(): + if os.getenv('CONSUL_HTTP_ADDR') == '' or os.getenv('CONSUL_HTTP_TOKEN') == '': + raise Exception( + 'Consul query does not work with CONSUL_HTTP_ADDR or CONSUL_HTTP_TOKEN unset') + + resp = requests.get( + '{}/v1/kv/dns?keys=true'.format( + os.getenv('CONSUL_HTTP_ADDR'), + ), + headers={ + 'X-Consul-Token': os.getenv('CONSUL_HTTP_TOKEN'), + } + ) + + if resp.status_code == 404: + return [] + + zones = {} + for key in resp.json(): + zone = key.split('/')[1] + if zone not in zones: + zones[zone] = {'from_consul': True} + + return zones + + def query_zone_entries(zone): if os.getenv('CONSUL_HTTP_ADDR') == '' or os.getenv('CONSUL_HTTP_TOKEN') == '': raise Exception( diff --git a/generateZonefiles.py b/generateZonefiles.py index 72b4044..6647e7b 100644 --- a/generateZonefiles.py +++ b/generateZonefiles.py @@ -134,6 +134,9 @@ def write_zone(zone, ttl, soa, nameserver, mailserver, entries): def main(): zone_data = yaml.load(open("zones.yml")) + consul_zones = consul.get_zones() + zone_data['zones'] = {**consul_zones, **zone_data['zones']} + for zone, config in zone_data['zones'].items(): ttl = default(config, "default_ttl", DEFAULT_TTL) diff --git a/zones.yml b/zones.yml index 6b71e33..3817a96 100644 --- a/zones.yml +++ b/zones.yml @@ -16,84 +16,6 @@ nameserver: - ns3.kserver.biz. zones: - 2k0.de: - from_consul: true - - ahlers.me: - from_consul: true - - ahlers.space: - from_consul: true - - ahlers.tk: - from_consul: true - - badges.fyi: - from_consul: true - - cloudkeys.de: - from_consul: true - - holiday-api.fyi: - from_consul: true - - k9s.me: - from_consul: true - - kahlers.biz: - from_consul: true - - kahlers.de: - from_consul: true - - knut-ahlers.de: - from_consul: true - - knut.cc: - from_consul: true - - knut.in: - from_consul: true - - knut.me: - from_consul: true - - knut.page: - from_consul: true - - knut.so: - from_consul: true - - knut.work: - from_consul: true - - knut.ws: - from_consul: true - - knutahlers.de: - from_consul: true - - knutshome.de: - from_consul: true - - luzifer.cc: - from_consul: true - - luzifer.io: - from_consul: true - - luzifer.link: - from_consul: true - - luzifer.rip: - from_consul: true - - mondash.org: - from_consul: true - - ots.fyi: - from_consul: true - server.test: entries: - name: health