~charmers/charms/trusty/plumgrid-director/trunk

« back to all changes in this revision

Viewing changes to hooks/pg_dir_utils.py

  • Committer: James Page
  • Date: 2016-09-02 09:03:26 UTC
  • mfrom: (16.1.28 plumgrid-director)
  • Revision ID: james.page@ubuntu.com-20160902090326-npp99jiqrbijce64
Updates for switch to docker for PLUMgrid delivery

Show diffs side-by-side

added added

removed removed

Lines of Context:
7
7
import time
8
8
import os
9
9
import json
 
10
import shlex
10
11
from collections import OrderedDict
11
12
from socket import gethostname as get_unit_hostname
12
13
from copy import deepcopy
24
25
    get_bridges,
25
26
    get_bridge_nics,
26
27
    is_ip,
27
 
    is_address_in_network,
28
 
    get_iface_addr
29
28
)
30
29
from charmhelpers.core.host import (
31
30
    service_start,
 
31
    service_restart,
32
32
    service_stop,
33
33
    service_running,
34
34
    path_hash,
42
42
    os_release,
43
43
)
44
44
 
 
45
SOURCES_LIST = '/etc/apt/sources.list'
45
46
LXC_CONF = '/etc/libvirt/lxc.conf'
46
47
TEMPLATES = 'templates/'
47
48
PG_LXC_DATA_PATH = '/var/lib/libvirt/filesystems/plumgrid-data'
88
89
])
89
90
 
90
91
 
 
92
def configure_pg_sources():
 
93
    '''
 
94
    Returns true if install sources is updated in sources.list file
 
95
    '''
 
96
    try:
 
97
        with open(SOURCES_LIST, 'r+') as sources:
 
98
            all_lines = sources.readlines()
 
99
            sources.seek(0)
 
100
            for i in (line for line in all_lines if "plumgrid" not in line):
 
101
                sources.write(i)
 
102
            sources.truncate()
 
103
        sources.close()
 
104
    except IOError:
 
105
        log('Unable to update /etc/apt/sources.list')
 
106
 
 
107
 
91
108
def determine_packages():
92
109
    '''
93
110
    Returns list of packages required by PLUMgrid director as specified
144
161
    return {cfg: rscs['services'] for cfg, rscs in resource_map().iteritems()}
145
162
 
146
163
 
147
 
def restart_pg():
 
164
def start_gateway():
 
165
    '''
 
166
    Brings up PE-gateway interface. Initial hack but will be solved when docker
 
167
    plumgrid-director package will be used. 
 
168
    '''
 
169
    count = 0
 
170
    while (count < 7):
 
171
        cmd = 'ps aux | grep launch_metadata_helper'
 
172
        output = subprocess.check_output([cmd], shell=True)
 
173
        roots = 0
 
174
        v = shlex.split(output)
 
175
        for i in v:
 
176
            if i == 'root':
 
177
                roots += 1
 
178
        if roots < 3:
 
179
            stop_pg()
 
180
            time.sleep(3)
 
181
            service_start('plumgrid')
 
182
        else:
 
183
            break
 
184
        count += 1
 
185
        time.sleep(20)
 
186
 
 
187
 
 
188
def restart_pg(gateway=None):
148
189
    '''
149
190
    Stops and Starts PLUMgrid service after flushing iptables.
150
191
    '''
162
203
                    raise ValueError("plumgrid service couldn't be started")
163
204
            else:
164
205
                raise ValueError("libvirt-bin service couldn't be started")
 
206
    if gateway:
 
207
        start_gateway()
165
208
    status_set('active', 'Unit is ready')
166
209
 
167
210
 
207
250
    Returns the managment interface.
208
251
    '''
209
252
    mgmt_interface = config('mgmt-interface')
210
 
    if interface_exists(mgmt_interface):
 
253
    if not mgmt_interface:
 
254
        return get_iface_from_addr(unit_get('private-address'))
 
255
    elif mgmt_interface and interface_exists(mgmt_interface):
211
256
        return mgmt_interface
212
257
    else:
213
258
        log('Provided managment interface %s does not exist'
251
296
        else:
252
297
            raise ValueError('No fabric interface provided for node')
253
298
        if interface_exists(node_fabric_interface):
254
 
            if is_address_in_network(config('os-data-network'),
255
 
                                     get_iface_addr(node_fabric_interface)[0]):
256
 
                return node_fabric_interface
257
 
            else:
258
 
                raise ValueError('Fabric interface not in fabric network')
 
299
            return node_fabric_interface
259
300
        else:
260
301
            log('Provided fabric interface %s does not exist'
261
302
                % node_fabric_interface)
276
317
    set_nic_mtu(fabric_interface, interface_mtu)
277
318
 
278
319
 
 
320
def disable_apparmor_libvirt():
 
321
    '''
 
322
    Disables Apparmor profile of libvirtd.
 
323
    '''
 
324
    apt_install('apparmor-utils')
 
325
    apt_install('cgroup-bin')
 
326
    _exec_cmd(['sudo', 'aa-disable', '/usr/sbin/libvirtd'],
 
327
              error_msg='Error disabling AppArmor profile of libvirtd')
 
328
    disable_apparmor()
 
329
    service_restart('libvirt-bin')
 
330
 
 
331
 
 
332
def disable_apparmor():
 
333
    '''
 
334
    Disables Apparmor security for lxc.
 
335
    '''
 
336
    try:
 
337
        f = open(LXC_CONF, 'r')
 
338
    except IOError:
 
339
        log('Libvirt not installed yet')
 
340
        return 0
 
341
    filedata = f.read()
 
342
    f.close()
 
343
    newdata = filedata.replace("security_driver = \"apparmor\"",
 
344
                               "#security_driver = \"apparmor\"")
 
345
    f = open(LXC_CONF, 'w')
 
346
    f.write(newdata)
 
347
    f.close()
 
348
 
 
349
 
279
350
def _exec_cmd(cmd=None, error_msg='Command exited with ERRORs', fatal=False):
280
351
    '''
281
352
    Function to execute any bash command on the node.