~zulcss/charm-helpers/flex-support

« back to all changes in this revision

Viewing changes to charmhelpers/contrib/openstack/flex.py

  • Committer: Chuck Short
  • Date: 2014-09-23 16:56:05 UTC
  • Revision ID: zulcss@ubuntu.com-20140923165605-0zofi1u1lz57u5b9
Add flex support for openstack

Add the flex containers support for openstack.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#
 
2
# Copyright 2014 Canonical Ltd.
 
3
#
 
4
# Authors:
 
5
#  Chuck Short <zulcss@ubuntu.com>
 
6
 
 
7
import os
 
8
import shutil
 
9
 
 
10
from subprocess import (
 
11
    check_call,
 
12
    check_output,
 
13
    CalledProcessError
 
14
)
 
15
 
 
16
from charmhelpers.core.hookenv import (
 
17
    config,
 
18
    relation_get,
 
19
    relation_ids,
 
20
    related_units,
 
21
    log,
 
22
    INFO,
 
23
    WARNING,
 
24
    ERROR
 
25
)
 
26
 
 
27
from charmhelpers.core.host import (
 
28
    mount,
 
29
    mounts,
 
30
    service_start,
 
31
    service_stop,
 
32
    service_running,
 
33
    umount,
 
34
)
 
35
 
 
36
 
 
37
from charmhelpers.fetch import (
 
38
    apt_install,
 
39
    apt_update,
 
40
)
 
41
 
 
42
def configure_ppa():
 
43
    cmd = [
 
44
        'apt-add-repository',
 
45
        '-y', 
 
46
        'ppa:zulcss/flex-testing'
 
47
    ]
 
48
    check_call(cmd)
 
49
    apt_update()
 
50
 
 
51
def install_flex():
 
52
    log('Configuring flex')
 
53
 
 
54
    # This can be removed once in the archive
 
55
    configure_ppa()
 
56
 
 
57
def configure_flex():
 
58
    flex_block_device = config('flex-block-device')
 
59
    flex_mount_point = config('flex-mount-point')
 
60
 
 
61
    instance_path = '/var/lib/nova/instances'
 
62
    if config('instances-path') is not None:
 
63
        instance_path = config('instances-path')
 
64
 
 
65
    # configure the btrfs block device
 
66
    apt_install('btrfs-tools', fatal=True)
 
67
    if filesystem_mounted(flex_mount_point):
 
68
        umount(flex_mount_point)
 
69
 
 
70
    make_filesystem(flex_block_device)
 
71
    mount(flex_block_device, instance_path, persist=True)
 
72
    cmd = ['chown', 'nova:nova', instance_path]
 
73
    check_call(cmd)
 
74
 
 
75
    configure_flex_user()
 
76
    cmd = ['chmod', '+x', instance_path]
 
77
    check_call(cmd)
 
78
    service_start('nova-compute')
 
79
 
 
80
def configure_flex_user():
 
81
     cmd = ['usermod', '--add-subuids', '100000-165536', 'nova']
 
82
     check_call(cmd)
 
83
     cmd = ['usermod', '--add-subgids', '100000-165536', 'nova']
 
84
     check_call(cmd)
 
85
 
 
86
def filesystem_mounted(fs):
 
87
    return fs in [f for f, m in mounts()]
 
88
 
 
89
def make_filesystem(blk_device, timeout=10):
 
90
    count = 0
 
91
    e_noent = os.errno.ENOENT
 
92
    while not os.path.exists(blk_device):
 
93
        if count >= timeout:
 
94
            log('gave up waiting on block device %s' % blk_device,
 
95
                level=ERROR)
 
96
            raise IOError(e_noent, os.strerror(e_noent), blk_device)
 
97
        log('waiting for block device %s to appear' % blk_device,
 
98
            level=INFO)
 
99
        count += 1
 
100
        time.sleep(1)
 
101
    else:
 
102
        log('Formatting block device %s.' % blk_device, level=INFO)
 
103
        check_call(['mkfs.btrfs', '-f', blk_device])
 
104