~gnuoy/charms/trusty/nova-compute/1496746

« back to all changes in this revision

Viewing changes to hooks/charmhelpers/core/host.py

  • Committer: Liam Young
  • Date: 2015-08-19 13:51:23 UTC
  • Revision ID: liam.young@canonical.com-20150819135123-cp1fx8g9fzkg6dq7
[gnuoy,trivial] Charmhelper sync (+1'd by mojo)

Show diffs side-by-side

added added

removed removed

Lines of Context:
417
417
    return(''.join(random_chars))
418
418
 
419
419
 
420
 
def list_nics(nic_type):
 
420
def is_phy_iface(interface):
 
421
    """Returns True if interface is not virtual, otherwise False."""
 
422
    if interface:
 
423
        sys_net = '/sys/class/net'
 
424
        if os.path.isdir(sys_net):
 
425
            for iface in glob.glob(os.path.join(sys_net, '*')):
 
426
                if '/virtual/' in os.path.realpath(iface):
 
427
                    continue
 
428
 
 
429
                if interface == os.path.basename(iface):
 
430
                    return True
 
431
 
 
432
    return False
 
433
 
 
434
 
 
435
def get_bond_master(interface):
 
436
    """Returns bond master if interface is bond slave otherwise None.
 
437
 
 
438
    NOTE: the provided interface is expected to be physical
 
439
    """
 
440
    if interface:
 
441
        iface_path = '/sys/class/net/%s' % (interface)
 
442
        if os.path.exists(iface_path):
 
443
            if '/virtual/' in os.path.realpath(iface_path):
 
444
                return None
 
445
 
 
446
            master = os.path.join(iface_path, 'master')
 
447
            if os.path.exists(master):
 
448
                master = os.path.realpath(master)
 
449
                # make sure it is a bond master
 
450
                if os.path.exists(os.path.join(master, 'bonding')):
 
451
                    return os.path.basename(master)
 
452
 
 
453
    return None
 
454
 
 
455
 
 
456
def list_nics(nic_type=None):
421
457
    '''Return a list of nics of given type(s)'''
422
458
    if isinstance(nic_type, six.string_types):
423
459
        int_types = [nic_type]
424
460
    else:
425
461
        int_types = nic_type
 
462
 
426
463
    interfaces = []
427
 
    for int_type in int_types:
428
 
        cmd = ['ip', 'addr', 'show', 'label', int_type + '*']
 
464
    if nic_type:
 
465
        for int_type in int_types:
 
466
            cmd = ['ip', 'addr', 'show', 'label', int_type + '*']
 
467
            ip_output = subprocess.check_output(cmd).decode('UTF-8')
 
468
            ip_output = ip_output.split('\n')
 
469
            ip_output = (line for line in ip_output if line)
 
470
            for line in ip_output:
 
471
                if line.split()[1].startswith(int_type):
 
472
                    matched = re.search('.*: (' + int_type +
 
473
                                        r'[0-9]+\.[0-9]+)@.*', line)
 
474
                    if matched:
 
475
                        iface = matched.groups()[0]
 
476
                    else:
 
477
                        iface = line.split()[1].replace(":", "")
 
478
 
 
479
                    if iface not in interfaces:
 
480
                        interfaces.append(iface)
 
481
    else:
 
482
        cmd = ['ip', 'a']
429
483
        ip_output = subprocess.check_output(cmd).decode('UTF-8').split('\n')
430
 
        ip_output = (line for line in ip_output if line)
 
484
        ip_output = (line.strip() for line in ip_output if line)
 
485
 
 
486
        key = re.compile('^[0-9]+:\s+(.+):')
431
487
        for line in ip_output:
432
 
            if line.split()[1].startswith(int_type):
433
 
                matched = re.search('.*: (' + int_type + r'[0-9]+\.[0-9]+)@.*', line)
434
 
                if matched:
435
 
                    interface = matched.groups()[0]
436
 
                else:
437
 
                    interface = line.split()[1].replace(":", "")
438
 
                interfaces.append(interface)
 
488
            matched = re.search(key, line)
 
489
            if matched:
 
490
                iface = matched.group(1)
 
491
                iface = iface.partition("@")[0]
 
492
                if iface not in interfaces:
 
493
                    interfaces.append(iface)
439
494
 
440
495
    return interfaces
441
496