Skip to content

Commit

Permalink
Added a python-based commandline tool rsdns.py) for manipulating all …
Browse files Browse the repository at this point in the history
…of the domain services from Rackspace.
  • Loading branch information
mbjones committed Oct 29, 2013
1 parent 0b1f378 commit 9334c4d
Show file tree
Hide file tree
Showing 2 changed files with 167 additions and 4 deletions.
20 changes: 16 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,26 @@ RS DNS Tools
============

Rackspace provides a [distributed DNS service](http://docs.rackspace.com/cdns/api/v1.0/cdns-devguide/content/overview.html)
with a REST API for accessing and modifying zone information. RS DNS Tools is a simple set of utility shell scripts for
manipulating a Rackspace-managed DNS zone. At this point, these scripts were developed solely to make it easier for me
to automate some tasks, but they may be useful to others. Have at it. Contributions including code, bug reports, and
feedback are all welcome.
with a REST API for accessing and modifying zone information. RS DNS Tools is a simple set of
utility scripts for manipulating a Rackspace-managed DNS zone. At this point, these scripts
were developed solely to make it easier for me to automate some tasks, but they may be useful
to others. Have at it. Contributions including code, bug reports, and feedback are all welcome.

There are two main types of tools in this package:
* rsdns.py: a Python commandline tool that allows you to list domains, list records in a domain, create a record in a domain, update a record in a domain, delete a record from a domain, import a domain from a BIND9 file, and to delete a whole domain
* bash scripts: a set of Bash scripts that let you call the Rackspace API directly to authenticate (auth.sh), export a domain (export.sh), and show records for a domain (records.sh). These are not as useful as the python commandline tool, but are included due to the export feature.

* Contributors: Matthew Jones
* Bug reports: http://github.com/mbjones/rsdns/issues

Installation
------------
For rsdns.py, you must first install python\_clouddns using a command such as:
```sh
sudo pip install python_clouddns
```
The argparse module ships with Python 2.7 and should be standard.

License
-------
```
Expand Down
151 changes: 151 additions & 0 deletions rsdns.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,151 @@
#!/usr/bin/python
#
# rsdns.py: a command line utility for managing the Rackspace CloudDNS service
#
# Copyright [2013] [Matthew B. Jones]
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
# Requires: python_clouddns
#
import clouddns
import argparse

# Execute the main program, dispatching to an appropriate handler for each command
def main():
args = options()
print args
for case in switch(args.cmd):
if case('list'):
list()
break
if case('records'):
records(args.domain)
break
if case('create'):
create_rec(args.domain, args.host, args.ip, args.rectype)
break
if case('update'):
update_rec(args.domain, args.host, args.ip)
break
if case('delete'):
delete_rec(args.domain, args.host)
break
if case('import'):
import_domain(args.file)
break
if case('deldomain'):
delete_domain(args.domain)
break
if case(): # default
args.print_help()

# Parse our command line arguments
def options():
parser = argparse.ArgumentParser(description='Commandline tool for managing DNS zones')
parser.add_argument('cmd', help='Command to be executed', choices=['list', 'records', 'create','update','delete','import','deldomain'])
parser.add_argument('domain', help='Name of the domain on which to act', nargs='?')
parser.add_argument('host', help='Hostname to be created or updated', nargs='?')
parser.add_argument('ip', help='Address to be created or updated', nargs='?')
parser.add_argument('rectype', help='Address type to be created or updated', nargs='?')
parser.add_argument('--file', help='file containing a BIND9 zone to be imported')
args = parser.parse_args()
return args

# Authenticate using credentials from the file 'credentials'
def authenticate():
from configobj import ConfigObj
credentials = 'credentials'

# Read our credentials
config = ConfigObj(credentials)
uname = config['UNAME']
apikey = config['APIKEY']

# Open a connection
dns = clouddns.connection.Connection(uname, apikey)
return dns

# Import a BIND9 zone file
def import_domain(zonefile):
dns = authenticate()
with open(zonefile, 'r') as f:
dns.import_domain(f)

# List the domains on this account
def list():
dns = authenticate()
for domain in dns.get_domains():
print domain.name

# List all records for a domain::
def records(domain):
dns = authenticate()
domain = dns.get_domain(name=domain)
for record in domain.get_records():
print '(%s) %s -> %s' % (record.type, record.name, record.data)

# Create new record::
def create_rec(domain, host, ip, rectype):
dns = authenticate()
domain = dns.get_domain(name=domain)
domain.create_record(host, ip, rectype)
record = domain.get_record(name=host)
print '(%s) %s -> %s' % (record.type, record.name, record.data)

# Update a record::
def update_rec(domain, host, ip):
dns = authenticate()
domain = dns.get_domain(name=domain)
record = domain.get_record(name=host)
record.update(data=ip, ttl=600)
record = domain.get_record(name=host)
print '(%s) %s -> %s' % (record.type, record.name, record.data)

# Delete a record::
def delete_rec(domain, host):
dns = authenticate()
domain = dns.get_domain(name=domain)
record = domain.get_record(name=host)
domain.delete_record(record.id)

# Delete a domain
def delete_domain(domain):
dns = authenticate()
domain = dns.get_domain(name=domain)
dns.delete_domain(domain.id)

# switch class provided by Brian Beck under the PSF License from:
# http://code.activestate.com/recipes/410692/
class switch(object):
def __init__(self, value):
self.value = value
self.fall = False

def __iter__(self):
"""Return the match method once, then stop"""
yield self.match
raise StopIteration

def match(self, *args):
"""Indicate whether or not to enter a case suite"""
if self.fall or not args:
return True
elif self.value in args: # changed for v1.5, see below
self.fall = True
return True
else:
return False


main()

0 comments on commit 9334c4d

Please sign in to comment.