~openstack-charmers-next/charms/precise/hacluster/trunk

« back to all changes in this revision

Viewing changes to hooks/charmhelpers/contrib/network/ip.py

  • Committer: Edward Hope-Morley
  • Date: 2016-06-21 10:21:57 UTC
  • Revision ID: edward.hope-morley@canonical.com-20160621102157-z72v10d7us5tiwf4
Sync charmhelpers to get fix for LP 1581598

Change-Id: Ie180c0d992c408fef0d5bb9143e706a4f195116e
Closes-Bug: 1581598

Show diffs side-by-side

added added

removed removed

Lines of Context:
214
214
 
215
215
def get_iface_addr(iface='eth0', inet_type='AF_INET', inc_aliases=False,
216
216
                   fatal=True, exc_list=None):
217
 
    """Return the assigned IP address for a given interface, if any."""
 
217
    """Return the assigned IP address for a given interface, if any.
 
218
 
 
219
    :param iface: network interface on which address(es) are expected to
 
220
                  be found.
 
221
    :param inet_type: inet address family
 
222
    :param inc_aliases: include alias interfaces in search
 
223
    :param fatal: if True, raise exception if address not found
 
224
    :param exc_list: list of addresses to ignore
 
225
    :return: list of ip addresses
 
226
    """
218
227
    # Extract nic if passed /dev/ethX
219
228
    if '/' in iface:
220
229
        iface = iface.split('/')[-1]
315
324
    We currently only support scope global IPv6 addresses i.e. non-temporary
316
325
    addresses. If no global IPv6 address is found, return the first one found
317
326
    in the ipv6 address list.
 
327
 
 
328
    :param iface: network interface on which ipv6 address(es) are expected to
 
329
                  be found.
 
330
    :param inc_aliases: include alias interfaces in search
 
331
    :param fatal: if True, raise exception if address not found
 
332
    :param exc_list: list of addresses to ignore
 
333
    :param dynamic_only: only recognise dynamic addresses
 
334
    :return: list of ipv6 addresses
318
335
    """
319
336
    addresses = get_iface_addr(iface=iface, inet_type='AF_INET6',
320
337
                               inc_aliases=inc_aliases, fatal=fatal,
336
353
            cmd = ['ip', 'addr', 'show', iface]
337
354
            out = subprocess.check_output(cmd).decode('UTF-8')
338
355
            if dynamic_only:
339
 
                key = re.compile("inet6 (.+)/[0-9]+ scope global dynamic.*")
 
356
                key = re.compile("inet6 (.+)/[0-9]+ scope global.* dynamic.*")
340
357
            else:
341
358
                key = re.compile("inet6 (.+)/[0-9]+ scope global.*")
342
359
 
388
405
    Returns True if address is a valid IP address.
389
406
    """
390
407
    try:
391
 
        # Test to see if already an IPv4 address
392
 
        socket.inet_aton(address)
 
408
        # Test to see if already an IPv4/IPv6 address
 
409
        address = netaddr.IPAddress(address)
393
410
        return True
394
 
    except socket.error:
 
411
    except netaddr.AddrFormatError:
395
412
        return False
396
413
 
397
414