~moon127/charms/trusty/ceilometer/add-execd-preinstall

« back to all changes in this revision

Viewing changes to hooks/charmhelpers/contrib/openstack/utils.py

  • Committer: James Page
  • Date: 2013-11-06 01:06:52 UTC
  • Revision ID: james.page@canonical.com-20131106010652-akof8nnkati19skk
Resync charm-helpers

Show diffs side-by-side

added added

removed removed

Lines of Context:
13
13
    config,
14
14
    log as juju_log,
15
15
    charm_dir,
16
 
)
17
 
 
18
 
from charmhelpers.core.host import (
19
 
    lsb_release,
20
 
)
21
 
 
22
 
from charmhelpers.fetch import (
23
 
    apt_install,
24
 
)
 
16
    ERROR,
 
17
    INFO
 
18
)
 
19
 
 
20
from charmhelpers.contrib.storage.linux.lvm import (
 
21
    deactivate_lvm_volume_group,
 
22
    is_lvm_physical_volume,
 
23
    remove_lvm_physical_volume,
 
24
)
 
25
 
 
26
from charmhelpers.core.host import lsb_release, mounts, umount
 
27
from charmhelpers.fetch import apt_install
 
28
from charmhelpers.contrib.storage.linux.utils import is_block_device, zap_disk
 
29
from charmhelpers.contrib.storage.linux.loopback import ensure_loopback_device
25
30
 
26
31
CLOUD_ARCHIVE_URL = "http://ubuntu-cloud.archive.canonical.com/ubuntu"
27
32
CLOUD_ARCHIVE_KEY_ID = '5EDB1B62EC4926EA'
28
33
 
 
34
DISTRO_PROPOSED = ('deb http://archive.ubuntu.com/ubuntu/ %s-proposed '
 
35
                   'restricted main multiverse universe')
 
36
 
 
37
 
29
38
UBUNTU_OPENSTACK_RELEASE = OrderedDict([
30
39
    ('oneiric', 'diablo'),
31
40
    ('precise', 'essex'),
57
66
    ('1.9.0', 'havana'),
58
67
])
59
68
 
 
69
DEFAULT_LOOPBACK_SIZE = '5G'
 
70
 
60
71
 
61
72
def error_out(msg):
62
73
    juju_log("FATAL ERROR: %s" % msg, level='ERROR')
67
78
    '''Derive OpenStack release codename from a given installation source.'''
68
79
    ubuntu_rel = lsb_release()['DISTRIB_CODENAME']
69
80
    rel = ''
70
 
    if src == 'distro':
 
81
    if src in ['distro', 'distro-proposed']:
71
82
        try:
72
83
            rel = UBUNTU_OPENSTACK_RELEASE[ubuntu_rel]
73
84
        except KeyError:
202
213
    '''Configure apt installation source.'''
203
214
    if rel == 'distro':
204
215
        return
 
216
    elif rel == 'distro-proposed':
 
217
        ubuntu_rel = lsb_release()['DISTRIB_CODENAME']
 
218
        with open('/etc/apt/sources.list.d/juju_deb.list', 'w') as f:
 
219
            f.write(DISTRO_PROPOSED % ubuntu_rel)
205
220
    elif rel[:4] == "ppa:":
206
221
        src = rel
207
222
        subprocess.check_call(["add-apt-repository", "-y", src])
299
314
    return apt.version_compare(available_vers, cur_vers) == 1
300
315
 
301
316
 
 
317
def ensure_block_device(block_device):
 
318
    '''
 
319
    Confirm block_device, create as loopback if necessary.
 
320
 
 
321
    :param block_device: str: Full path of block device to ensure.
 
322
 
 
323
    :returns: str: Full path of ensured block device.
 
324
    '''
 
325
    _none = ['None', 'none', None]
 
326
    if (block_device in _none):
 
327
        error_out('prepare_storage(): Missing required input: '
 
328
                  'block_device=%s.' % block_device, level=ERROR)
 
329
 
 
330
    if block_device.startswith('/dev/'):
 
331
        bdev = block_device
 
332
    elif block_device.startswith('/'):
 
333
        _bd = block_device.split('|')
 
334
        if len(_bd) == 2:
 
335
            bdev, size = _bd
 
336
        else:
 
337
            bdev = block_device
 
338
            size = DEFAULT_LOOPBACK_SIZE
 
339
        bdev = ensure_loopback_device(bdev, size)
 
340
    else:
 
341
        bdev = '/dev/%s' % block_device
 
342
 
 
343
    if not is_block_device(bdev):
 
344
        error_out('Failed to locate valid block device at %s' % bdev,
 
345
                  level=ERROR)
 
346
 
 
347
    return bdev
 
348
 
 
349
 
 
350
def clean_storage(block_device):
 
351
    '''
 
352
    Ensures a block device is clean.  That is:
 
353
        - unmounted
 
354
        - any lvm volume groups are deactivated
 
355
        - any lvm physical device signatures removed
 
356
        - partition table wiped
 
357
 
 
358
    :param block_device: str: Full path to block device to clean.
 
359
    '''
 
360
    for mp, d in mounts():
 
361
        if d == block_device:
 
362
            juju_log('clean_storage(): %s is mounted @ %s, unmounting.' %
 
363
                     (d, mp), level=INFO)
 
364
            umount(mp, persist=True)
 
365
 
 
366
    if is_lvm_physical_volume(block_device):
 
367
        deactivate_lvm_volume_group(block_device)
 
368
        remove_lvm_physical_volume(block_device)
 
369
    else:
 
370
        zap_disk(block_device)
 
371
 
 
372
 
302
373
def is_ip(address):
303
374
    """
304
375
    Returns True if address is a valid IP address.