Using OctoDNS To Update DNS: Difference between revisions

From Sympl Wiki
Jump to navigation Jump to search
(Initial outline)
 
No edit summary
Tag: 2017 source edit
(6 intermediate revisions by 2 users not shown)
Line 1: Line 1:
* Install OctoDNS
{{Beta Documentation}}
* Create config file
 
* Update Bytemark upload file
== Introduction==
* Cleanup published changes to remove DNS entries
 
The [https://www.mythic-beasts.com/support/api/primary Mythic Beasts DNS API] can be used in conjunction with [https://github.com/github/octodns 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
 
---
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 <code>/home/sympl/dns-cleanup.py</code>
 
#!/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 <code>/root/BytemarkDNS/upload</code> immediately before the <code>exit 0</code> 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 <code>sympl-dns-generate</code> is run, it will upload changes using the Mythic Beasts API. When creating new domains, update the configuration file and then manually run <code>sympl-dns-generate</code>.


[[Category:How To]]
[[Category:How To]]
[[Category:Stub]]

Revision as of 15:49, 15 January 2020

This article is Beta Documentation. It may have missing elements, or be unclear in places.
Please feel free to make corrections or amendments, or ask for help on the Sympl Forum

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

---

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.