Move get_host_ip() to helpers for use in nova-c-c, too.

This commit is contained in:
Adam Gandelman 2013-09-05 16:40:47 -07:00
parent 6577d259de
commit 8167f2405b
2 changed files with 34 additions and 26 deletions

View File

@ -1,12 +1,12 @@
#!/usr/bin/python
# Common python helper functions used for OpenStack charms.
from collections import OrderedDict
import apt_pkg as apt
import subprocess
import os
import socket
import sys
from charmhelpers.core.hookenv import (
@ -290,3 +290,34 @@ def openstack_upgrade_available(package):
available_vers = get_os_version_install_source(src)
apt.init()
return apt.version_compare(available_vers, cur_vers) == 1
def is_ip(address):
"""
Returns True if address is a valid IP address.
"""
try:
# Test to see if already an IPv4 address
socket.inet_aton(address)
return True
except socket.error:
return False
def get_host_ip(hostname):
"""
Resolves the IP for a given hostname, or returns
the input if it is already an IP.
"""
try:
import dns.resolver.query
except ImportError:
apt_install('python-dnspython')
import dns.resolver.query
if is_ip(hostname):
return hostname
answers = dns.resolver.query(hostname, 'A')
if answers:
return answers[0].address
return None

View File

@ -1,4 +1,3 @@
import socket
from subprocess import check_call, check_output
@ -18,7 +17,7 @@ from charmhelpers.core.hookenv import (
ERROR,
)
from charmhelpers.contrib.openstack.utils import os_release
from charmhelpers.contrib.openstack.utils import get_host_ip, os_release
# This is just a label and it must be consistent across
@ -290,28 +289,6 @@ class CloudComputeContext(context.OSContextGenerator):
return ctxt
def get_host_ip():
# we used to have a charm-helper to do this, but its disappeared?
# taken from quantum-gateway
try:
import dns.resolver
except ImportError:
apt_install('python-dnspython')
import dns.resolver
hostname = unit_get('private-address')
try:
# Test to see if already an IPv4 address
socket.inet_aton(hostname)
return hostname
except socket.error:
answers = dns.resolver.query(hostname, 'A')
if answers:
return answers[0].address
return None
class NeutronComputeContext(context.NeutronContext):
interfaces = []
@ -349,5 +326,5 @@ class NeutronComputeContext(context.NeutronContext):
self._ensure_bridge()
ovs_ctxt['local_ip'] = get_host_ip()
ovs_ctxt['local_ip'] = get_host_ip(unit_get('private-address'))
return ovs_ctxt