~xianghui/charms/trusty/hacluster/support-ipv6

« back to all changes in this revision

Viewing changes to hooks/hacluster.py

  • Committer: Hui Xiang
  • Date: 2014-09-20 01:58:21 UTC
  • Revision ID: hui.xiang@canonical.com-20140920015821-69s3394kh9zzi6qx
Sync charm-helpers

Show diffs side-by-side

added added

removed removed

Lines of Context:
12
12
import socket
13
13
import fcntl
14
14
import struct
 
15
 
15
16
from charmhelpers.fetch import apt_install
 
17
from charmhelpers.contrib.network import ip
16
18
 
17
19
try:
18
20
    from netaddr import IPNetwork
81
83
        return None
82
84
 
83
85
 
84
 
def get_ipv6_addr(iface="eth0"):
85
 
    try:
86
 
        try:
87
 
            import netifaces
88
 
        except ImportError:
89
 
            apt_install('python-netifaces')
90
 
            import netifaces
91
 
 
92
 
        iface = str(iface)
93
 
        iface_addrs = netifaces.ifaddresses(iface)
94
 
        if netifaces.AF_INET6 not in iface_addrs:
95
 
            raise Exception("Interface '%s' doesn't have an ipv6 address."
96
 
                            % iface)
97
 
 
98
 
        addresses = netifaces.ifaddresses(iface)[netifaces.AF_INET6]
99
 
        ipv6_address = [a for a in addresses
100
 
                        if not a['addr'].startswith('fe80')][0]
101
 
        if not ipv6_address:
102
 
            raise Exception("Interface '%s' doesn't have global ipv6 address."
103
 
                            % iface)
104
 
 
105
 
        ipv6_addr = ipv6_address['addr']
106
 
        ipv6_netmask = ipv6_address['netmask']
107
 
 
108
 
        network = "{}/{}".format(ipv6_addr, ipv6_netmask)
109
 
        ip = IPNetwork(network)
110
 
        return str(ip.network)
111
 
 
112
 
    except ValueError:
113
 
        raise Exception("Invalid interface '%s'" % iface)
 
86
def get_ipv6_addr():
 
87
    ipv6_address = ip.get_ipv6_addr()[0]
 
88
    ipv6_addr = ipv6_address['addr']
 
89
    ipv6_netmask = ipv6_address['netmask']
 
90
    network = "{}/{}".format(ipv6_addr, ipv6_netmask)
 
91
    addr = IPNetwork(network)
 
92
    return str(addr.network)