Last active
September 29, 2016 13:29
-
-
Save DamianZaremba/8183301 to your computer and use it in GitHub Desktop.
Very simple OpenStack Nova notifier to create/delete PowerDNS records for instances on creation/deletion.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
''' | |
Very simple OpenStack Nova notifier to create/delete PowerDNS records for instances on creation/deletion. | |
Format of records is %(instance)s.%(project_name).example.com. | |
To use: | |
* Install this in a python path (site-packages is easiest) | |
* Configure nova using notification_driver=examplenotifier | |
''' | |
import MySQLdb | |
import sys | |
from nova import db | |
from nova import notifications | |
from nova.openstack.common import log as logging | |
from nova.openstack.common.notifier import api as notifier_api | |
log = logging.getLogger(__name__) | |
instance_zone = 'example.com' | |
def notify(context, message): | |
event_type = message['event_type'] | |
if event_type not in ['compute.instance.delete.start', | |
'compute.instance.create.end', | |
'compute.instance.create_ip.end', | |
'compute.instance.delete_ip.end']: | |
return | |
payload = message['payload'] | |
instance_name = payload['display_name'] | |
uuid = payload['instance_id'] | |
tenant_id = payload['tenant_id'] | |
project_name = context.project_name | |
dns = '%s.%s.%s' % (instance_name.lower(), project_name.lower(), instance_zone) | |
db = MySQLdb.connect(host='mysql.example.com', user='pdns', | |
passwd='password', db='pdns') | |
cursor = db.cursor() | |
if event_type in ['compute.instance.create.end', 'compute.instance.create_ip.end']: | |
if not 'fixed_ips' in payload or len(payload['fixed_ips']) == 0: | |
log.error('No IPs found for %s', instance_name) | |
return | |
ip = payload['fixed_ips'][0]['address'] | |
rev_ip_parts = ip.split('.') | |
rev_ip_parts.reverse() | |
ptr = "%s.in-addr.arpa" % '.'.join(rev_ip_parts) | |
ptr_zone = "%s.in-addr.arpa" % '.'.join(rev_ip_parts[1:]) | |
log.info('Creating A record %s with ip %s' % (dns, ip)) | |
cursor.execute('INSERT INTO `records` (domain_id, name, type, content) VALUES ((SELECT `id` FROM `domains` WHERE `name` = %(zone)s LIMIT 0,1), %(dns)s, "A", %(ip)s)', {'dns': dns, 'ip': ip, 'zone': instance_zone}) | |
db.commit() | |
log.info('Creating PTR record %s (%s) with name %s' % (ptr, ptr_zone, dns)) | |
cursor.execute('INSERT INTO `records` (domain_id, name, type, content) VALUES ((SELECT `id` FROM `domains` WHERE `name` = %(zone)s LIMIT 0,1), %(ptr)s, "PTR", %(name)s)', {'ptr': ptr, 'zone': ptr_zone, 'name': dns}) | |
db.commit() | |
if event_type in ['compute.instance.delete_ip.end', 'compute.instance.delete.start']: | |
log.info('Deleting A records for %s' % dns) | |
cursor.execute('DELETE `records` FROM `records` LEFT JOIN `domains` on `records`.`domain_id` = `domains`.`id` WHERE `records`.`name` = %(dns)s AND `records`.`type` = "A" AND `domains`.`name` = %(zone)s', {'dns': dns, 'zone': instance_zone}) | |
db.commit() | |
log.info('Deleting PTR records for %s' % dns) | |
cursor.execute('DELETE FROM `records` WHERE `records`.`content` = %(dns)s AND `records`.`type` = "PTR"', {'dns': dns}) | |
db.commit() | |
cursor.close() | |
db.close() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment