~charm-helpers/charm-helpers/devel

« back to all changes in this revision

Viewing changes to charmhelpers/core/host.py

  • Committer: Liam young
  • Date: 2013-11-14 11:09:28 UTC
  • mfrom: (96.1.1 charm-helpers)
  • Revision ID: liam.young@canonical.com-20131114110928-nt2lznxcpd62b8q9
[gnuoy,r=mthaddon] Add support for listing nics and setting nic mtu value

Show diffs side-by-side

added added

removed removed

Lines of Context:
245
245
    random_chars = [
246
246
        random.choice(alphanumeric_chars) for _ in range(length)]
247
247
    return(''.join(random_chars))
 
248
 
 
249
 
 
250
def list_nics(nic_type):
 
251
    '''Return a list of nics of given type(s)'''
 
252
    if isinstance(nic_type, basestring):
 
253
        int_types = [nic_type]
 
254
    else:
 
255
        int_types = nic_type
 
256
    interfaces = []
 
257
    for int_type in int_types:
 
258
        cmd = ['ip', 'addr', 'show', 'label', int_type + '*']
 
259
        ip_output = subprocess.check_output(cmd).split('\n')
 
260
        ip_output = (line for line in ip_output if line)
 
261
        for line in ip_output:
 
262
            if line.split()[1].startswith(int_type):
 
263
                interfaces.append(line.split()[1].replace(":", ""))
 
264
    return interfaces
 
265
 
 
266
 
 
267
def set_nic_mtu(nic, mtu):
 
268
    '''Set MTU on a network interface'''
 
269
    cmd = ['ip', 'link', 'set', nic, 'mtu', mtu]
 
270
    subprocess.check_call(cmd)
 
271
 
 
272
 
 
273
def get_nic_mtu(nic):
 
274
    cmd = ['ip', 'addr', 'show', nic]
 
275
    ip_output = subprocess.check_output(cmd).split('\n')
 
276
    mtu = ""
 
277
    for line in ip_output:
 
278
        words = line.split()
 
279
        if 'mtu' in words:
 
280
            mtu = words[words.index("mtu") + 1]
 
281
    return mtu