Using OctoDNS To Update DNS

From Sympl Wiki
Jump to navigation Jump to search
This article is Community Documentation and any changes may not be compatible with future updates of Sympl, and is therefore not officially supported.
The Sympl Forum may have more information.

Introduction

The Mythic Beasts DNS API can be used in conjunction with OctoDNS to automatically update DNS entries for domains running on Sympl. This guide details the steps required to configure this.

This guide assumes that the appropriate domains are using Mythic Beasts to provide Primary DNS service, and the domain has an appropriate API password configured.

Install OctoDNS

sudo apt install python3-venv  && \
mkdir -p /home/sympl/dns && \
python3 -m venv /home/sympl/dns/ && \
/home/sympl/dns/bin/pip3 install pip octodns

Create config file

Sample configuration file (should be saved as /home/sympl/dns.yaml)

---

providers:
 config_example.com:
   class: octodns.source.tinydns.TinyDnsFileSource
   directory: /srv/example.com/config/dns
   default_ttl: 3600

 config_subdomain1.example.com:
   class: octodns.source.tinydns.TinyDnsFileSource
   directory: /srv/subdomain1.example.com/config/dns
   default_ttl: 3600

 config_subdomain2.example.com:
   class: octodns.source.tinydns.TinyDnsFileSource
   directory: /srv/subdomain2.example.com/config/dns
   default_ttl: 3600

 mythicbeasts:
   class: octodns.provider.mythicbeasts.MythicBeastsProvider
   passwords:
     example.com.: 'mythicdnsapipasswordgoeshere'

zones:
 example.com.:
   sources:
     - config_example.com
     - config_subdomain1.example.com
     - config_subdomain2.example.com

   targets:
     - mythicbeasts

Create script to cleanup published changes to remove DNS server entries

Add the following cleanup script /home/sympl/dns-cleanup.py

#!/usr/bin/python3

import requests
from pprint import pprint

from yaml import load, dump
try:
   from yaml import load, CLoader as Loader, CDumper as Dumper
except ImportError:
   from yaml import Loader, Dumper

def doRequest(values):
   response = requests.post(url, data = values)
   responseContents = response.text
   return responseContents

with open('/home/sympl/dns.yaml', 'r') as yamlfile:
   yamldoc = load(yamlfile)

#pprint(yamldoc)

url = 'https://dnsapi.mythic-beasts.com/'

for domain in yamldoc['providers']['mythicbeasts']['passwords'].keys():

   yourDomain = domain.strip("'").rstrip(".")
   yourDomainAPIPass = yamldoc['providers']['mythicbeasts']['passwords'][domain].strip("'")

   print("Cleaning up '" + yourDomain + "'")

   values = {
       'domain'   : yourDomain,
       'password' : yourDomainAPIPass,
       'command'  : 'LIST'
   }

   responseContents=doRequest(values)

   toDelete = False
   values['command']=[]

   for line in responseContents.splitlines():
       elements = line.split()
       if elements[2]=="NS" and "ns.bytemark.co.uk." in elements[3]:
           toDelete=True
           values['command'].append("DELETE " + line)

   if toDelete:
       responseContents=doRequest(values)
       print(responseContents)
   else:
       print("Nothing to delete")

Update Bytemark upload file

Add the following to /root/BytemarkDNS/upload immediately before the exit 0 line at the end.

# BEGIN Run OctoDNS and clean up
. /home/sympl/dns/bin/activate
PATH=$PATH:/home/sympl/dns/bin
octodns-sync --config-file=/home/sympl/dns.yaml --doit --force
/home/sympl/dns-cleanup.py
# END Run OctoDNS and clean up

Now, whenever sympl-dns-generate is run, it will upload changes using the Mythic Beasts API. When creating new domains, update the configuration file and then manually run sympl-dns-generate.