diff --git a/hooks/hooks.py b/hooks/hooks.py index dcbbd54..98cdd34 100755 --- a/hooks/hooks.py +++ b/hooks/hooks.py @@ -48,7 +48,8 @@ from utils import ( render_template, get_public_addr, assert_charm_supports_ipv6, - get_host_ip + get_host_ip, + get_network ) hooks = Hooks() @@ -84,6 +85,14 @@ def emit_cephconf(): 'ceph_public_network': config('ceph-public-network'), 'ceph_cluster_network': config('ceph-cluster-network') } + + if config('prefer-ipv6'): + network = get_network() + if not config('ceph-public-network'): + cephcontext['ceph_public_network'] = network + if not config('ceph-cluster-network'): + cephcontext['ceph_cluster_network'] = network + # Install ceph.conf as an alternative to support # co-existence with other charms that write this file charm_ceph_conf = "/var/lib/charm/{}/ceph.conf".format(service_name()) diff --git a/hooks/utils.py b/hooks/utils.py index 825c079..791dc54 100644 --- a/hooks/utils.py +++ b/hooks/utils.py @@ -98,3 +98,33 @@ def assert_charm_supports_ipv6(): if lsb_release()['DISTRIB_CODENAME'].lower() < "trusty": raise Exception("IPv6 is not supported in the charms for Ubuntu " "versions less than Trusty 14.04") + + +def get_network(iface="eth0"): + try: + try: + import netifaces + except ImportError: + apt_install('python-netifaces') + import netifaces + + try: + from netaddr import IPNetwork + except ImportError: + apt_install('python-netaddr', fatal=True) + from netaddr import IPNetwork + + ipv6_address = get_ipv6_addr(iface)[0] + ifa_addrs = netifaces.ifaddresses(iface) + + for ifaddr in ifa_addrs[netifaces.AF_INET6]: + if ipv6_address == ifaddr['addr']: + network = "{}/{}".format(ifaddr['addr'], + ifaddr['netmask']) + ip = IPNetwork(network) + return str(ip.network) + + except ValueError: + raise Exception("Invalid interface '%s'" % iface) + + raise Exception("No valid network found in interface '%s'" % iface)