Using OctoDNS To Update DNS: Difference between revisions

From Sympl Wiki
Jump to navigation Jump to search
No edit summary
No edit summary
Tag: 2017 source edit
Line 1: Line 1:
*Install OctoDNS
== Install OctoDNS ==
*Create config file
 
*Update Bytemark upload file
sudo apt install python3-venv  && \
*Cleanup published changes to remove DNS entries
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
 
== Cleanup published changes to remove DNS 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]]
[[Category:Stub]]

Revision as of 15:26, 15 January 2020

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

Cleanup published changes to remove DNS 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.