~junaidali/charms/trusty/plumgrid-gateway/oil_bug

« back to all changes in this revision

Viewing changes to hooks/pg_gw_utils.py

  • Committer: Bilal Baqar
  • Date: 2015-09-15 12:09:58 UTC
  • mfrom: (14.1.1 plumgrid-gateway)
  • Revision ID: bbaqar@plumgrid.com-20150915120958-3q39bm3ouud1df2a
Configurable Managment interface support

Show diffs side-by-side

added added

removed removed

Lines of Context:
7
7
from charmhelpers.core.hookenv import (
8
8
    log,
9
9
    config,
 
10
    unit_get
 
11
)
 
12
from charmhelpers.contrib.network.ip import (
 
13
    get_iface_from_addr,
 
14
    get_bridges,
 
15
    get_bridge_nics,
10
16
)
11
17
from charmhelpers.core.host import (
12
18
    write_file,
25
31
import subprocess
26
32
import time
27
33
import os
28
 
import re
29
34
import json
30
35
 
31
36
LXC_CONF = "/etc/libvirt/lxc.conf"
143
148
              error_msg='Error Loading Iovisor Kernel Module')
144
149
 
145
150
 
146
 
def check_interface_type():
147
 
    '''
148
 
    Checks the interface. Support added for AWS deployments. There are 2
149
 
    possible interfaces "juju-br0" and "eth0". The default being juju-br0
150
 
    '''
151
 
    log("Checking Interface Type")
152
 
    default_interface = "juju-br0"
153
 
    AWS_interface = "eth0"
154
 
    shell_output = subprocess.check_output(['brctl', 'show', 'juju-br0'])
155
 
    output = re.split(' |\n|\t', shell_output)
156
 
    if output[10] == '':
157
 
        return AWS_interface
 
151
def get_mgmt_interface():
 
152
    '''
 
153
    Returns the managment interface.
 
154
    '''
 
155
    def interface_exists(interface):
 
156
        '''
 
157
        Checks if interface exists on node.
 
158
        '''
 
159
        try:
 
160
            subprocess.check_call(['ip', 'link', 'show', interface],
 
161
                                  stdout=open(os.devnull, 'w'),
 
162
                                  stderr=subprocess.STDOUT)
 
163
        except subprocess.CalledProcessError:
 
164
            return False
 
165
        return True
 
166
 
 
167
    mgmt_interface = config('mgmt-interface')
 
168
    if interface_exists(mgmt_interface):
 
169
        return mgmt_interface
158
170
    else:
159
 
        return default_interface
 
171
        log('Provided managment interface %s does not exist'
 
172
            % mgmt_interface)
 
173
        return get_iface_from_addr(unit_get('private-address'))
160
174
 
161
175
 
162
176
def get_gw_interfaces():
180
194
    '''
181
195
    Ensures required MTU of the underlying networking of the node.
182
196
    '''
183
 
    log("Changing MTU of juju-br0 and all attached interfaces")
184
197
    interface_mtu = config('network-device-mtu')
185
 
    interface_type = check_interface_type()
186
 
    if interface_type == "juju-br0":
187
 
        cmd = subprocess.check_output(["brctl", "show", interface_type])
188
 
        words = cmd.split()
189
 
        for word in words:
190
 
            if 'eth' in word:
191
 
                set_nic_mtu(word, interface_mtu)
192
 
    set_nic_mtu(interface_type, interface_mtu)
 
198
    mgmt_interface = get_mgmt_interface()
 
199
    if mgmt_interface in get_bridges():
 
200
        attached_interfaces = get_bridge_nics(mgmt_interface)
 
201
        for interface in attached_interfaces:
 
202
            set_nic_mtu(interface, interface_mtu)
 
203
    set_nic_mtu(mgmt_interface, interface_mtu)
193
204
 
194
205
 
195
206
def _exec_cmd(cmd=None, error_msg='Command exited with ERRORs', fatal=False):