Skip to content

Instantly share code, notes, and snippets.

@crabdancing
Created February 16, 2019 21:23
Show Gist options
  • Save crabdancing/cdd7d6061958e37da8e5ee64e474b5c2 to your computer and use it in GitHub Desktop.
Save crabdancing/cdd7d6061958e37da8e5ee64e474b5c2 to your computer and use it in GitHub Desktop.
Some snippits for rapidly configuring Linode nameserver records
#!/usr/bin/env python3
import json, subprocess as sp
import yaml
config = yaml.load(open("config.yml"))
ipv4 = config['ipv4']
ipv6 = config['ipv6']
soa_email = config['soa_email']
domain_type = config['domain_type']
ErrorFindingDomainName = Exception('Error finding domain name!')
def linode(*args):
cmd = ['linode-cli', '--suppress-warnings', '--json'] + list(args)
print('CMD: ' + ' '.join(cmd))
spc = sp.run(cmd, stderr=sp.STDOUT, stdout=sp.PIPE, text=True)
out = spc.stdout.strip()
# Remove the garbage 'Request failed: [blah]' line
# that we get sometimes before the actual json
if '\n' in out:
out = out.split('\n')[1]
if out:
print('JSON string: %s' % out)
return json.loads(out)
else:
return None
def findDomainInfoByName(domain_name):
for entry in linode('domains', 'list'):
if entry['domain'] == domain_name:
return entry
raise ErrorFindingDomainName
def configureAllExistingDomains():
domain_info = getDomainInfo()
for entry in domain_info:
configureDomain(entry['id'], entry['domain'])
def configureDomain(domain_id, domain_name):
domain_id = str(domain_id)
for ip in (ipv4, ipv6):
linode('domains', 'records-create', domain_id, '--type', 'A', '--target', ip)
linode('domains', 'records-create', domain_id, '--type', 'A', '--target', ip, '--name', 'mail')
linode('domains', 'records-create', domain_id, '--type', 'A', '--target', ip, '--name', 'www')
linode('domains', 'records-create', domain_id, '--type', 'MX', '--target', 'mail.' + domain_name, '--priority', '10')
def deleteDomainByName(domain_name):
print('Deleting %s...' % domain_name)
domain_id = str(findDomainInfoByName(domain_name)['id'])
linode('domains', 'delete', domain_id)
def createDomain(domain_name):
print('Attempting to create %s...' % domain_name)
domain_info = linode('domains', 'create', '--domain', domain_name, '--type', domain_type, '--soa_email', soa_email)
# in event that domain name already exists, our output json will look like:
# [{'field': 'domain', 'reason': 'DNS zone already exists.'}]
if 'reason' in domain_info[0].keys():
if domain_info[0]['reason'] == 'DNS zone already exists.':
print('Error! Domain already exists. Deleting and then recreating domain...')
deleteDomainByName(domain_name)
# Now we just recursively try again
return createDomain(domain_name)
else:
return domain_info
def initDomains(domain_name_list):
for domain_name in domain_name_list:
domain_info = createDomain(domain_name)
configureDomain(str(domain_info[0]['id']), domain_info[0]['domain'])
domains = open('domains.list').read().split()
initDomains(domains)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment