fixed get_mon_hosts()

This commit is contained in:
Edward Hope-Morley 2014-09-19 09:10:31 +01:00
parent a50446230e
commit 87f5dd2745
2 changed files with 27 additions and 2 deletions

View File

@ -41,6 +41,7 @@ from charmhelpers.fetch import (
from utils import (
render_template,
get_host_ip,
setup_ipv6
)
@ -143,9 +144,10 @@ def get_mon_hosts():
for unit in related_units(relid):
if config('prefer-ipv6'):
addr = relation_get('ceph-public-address', unit, relid) or \
get_ipv6_addr()
relation_get('private-address', unit, relid)
else:
addr = relation_get('private-address', unit, relid)
addr = relation_get('ceph-public-address', unit, relid) or \
get_host_ip(relation_get('private-address', unit, relid))
if addr is not None:
if is_ipv6(addr):

View File

@ -10,6 +10,7 @@
import socket
import re
from charmhelpers.core.hookenv import (
unit_get,
cached
)
from charmhelpers.fetch import (
@ -30,6 +31,13 @@ except ImportError:
fatal=True)
import jinja2
try:
import dns.resolver
except ImportError:
apt_install(filter_installed_packages(['python-dnspython']),
fatal=True)
import dns.resolver
def render_template(template_name, context, template_dir=TEMPLATES_DIR):
templates = jinja2.Environment(
@ -55,6 +63,21 @@ def get_unit_hostname():
return socket.gethostname()
@cached
def get_host_ip(hostname=None):
hostname = hostname or unit_get('private-address')
try:
# Test to see if already an IPv4 address
socket.inet_aton(hostname)
return hostname
except socket.error:
# This may throw an NXDOMAIN exception; in which case
# things are badly broken so just let it kill the hook
answers = dns.resolver.query(hostname, 'A')
if answers:
return answers[0].address
def setup_ipv6():
ubuntu_rel = float(lsb_release()['DISTRIB_RELEASE'])
if ubuntu_rel < 14.04: