Skip to content

Instantly share code, notes, and snippets.

@kevinbringard
Created September 24, 2013 13:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kevinbringard/6685037 to your computer and use it in GitHub Desktop.
Save kevinbringard/6685037 to your computer and use it in GitHub Desktop.
diff --git a/dhcp.py b/dhcp.py
index 2844b1a..fc72f37 100644
--- a/dhcp.py
+++ b/dhcp.py
@@ -31,6 +31,7 @@ from quantum.agent.linux import utils
from quantum.openstack.common import jsonutils
from quantum.openstack.common import log as logging
from quantum.openstack.common import uuidutils
+from novaclient.v1_1 import client
LOG = logging.getLogger(__name__)
@@ -355,13 +356,46 @@ class Dnsmasq(DhcpLocalProcess):
r = re.compile('[:.]')
buf = StringIO.StringIO()
+ # get credentials from /root/openrc
+ env_creds = {}
+
+ if os.path.isfile('/var/lib/quantum/openrc'):
+
+ try:
+ with open('/var/lib/quantum/openrc', 'r') as f:
+ env_lines = f.readlines()
+ for line in env_lines:
+ match = re.search(r'export (\S+)=(\S+)', line)
+ if match:
+ key, value = match.groups()
+ env_creds[key] = value
+ except:
+ LOG.exception("There was an error processing /var/lib/quantum/openrc for credentials")
+
+ # create a connection to nova so we can map port IDs to vm hostnames
+ try:
+ nova = client.Client(username=env_creds['OS_USERNAME'],
+ api_key=env_creds['OS_PASSWORD'],
+ auth_url=env_creds['OS_AUTH_URL'],
+ project_id=env_creds['OS_TENANT_NAME'])
+ except:
+ LOG.exception("There was an error initializing the nova client to lookup associated port hostnames")
+
for port in self.network.ports:
for alloc in port.fixed_ips:
- name = '%s.%s' % (r.sub('-', alloc.ip_address),
- self.conf.dhcp_domain)
+ try:
+ server = nova.servers.get(port.device_id)
+ name = '%s.%s' % (server.name,
+ self.conf.dhcp_domain)
+ # do some simple replacement of common invalid dns characters
+ name = name.replace(' ', '-')
+ name = name.replace('_', '-')
+ except:
+ LOG.debug("An error occurred while fetching the hostname for port.device_id = %s falling back to using the mac address" % port.device_id)
+ name = '%s.%s' % (r.sub('-', alloc.ip_address),
+ self.conf.dhcp_domain)
buf.write('%s,%s,%s\n' %
(port.mac_address, name, alloc.ip_address))
-
name = self.get_conf_file_name('host')
utils.replace_file(name, buf.getvalue())
return name
@winks
Copy link

winks commented Sep 24, 2013

This helped me a lot - thank you very much!

You might want to harden line 26-28 though, I had
export OS_AUTH_URL="http..."
(with quotation marks) in my openrc file and it broke everything

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment