~codersquid/charms/precise/python-django/fix-test-branches

« back to all changes in this revision

Viewing changes to hooks/charmhelpers/contrib/storage/linux/loopback.py

Patrick Hetu 2014-05-02 fix a wrong configuration variable name
Patrick Hetu 2014-04-30 [merge] merge with trunk
Patrick Hetu 2014-04-30 add configurable amqp vhost
Patrick Hetu 2014-04-30 merge george-edison55's readme corrections
Patrick Hetu 2014-04-29 awk to the rescue: Re-enable http pip requirements support
Patrick Hetu 2014-04-28 drop http pip requirements as I can get it to work with ansible
Patrick Hetu 2014-04-25 add rabbitmq support
Patrick Hetu 2014-04-24 fix single python-path for fabfile.py
Patrick Hetu 2014-04-23 don't make python-path starts with a colon
Patrick Hetu 2014-04-17 add a new pip_extra_args option
Patrick Hetu 2014-04-17 fix settings path in the fabfile
Patrick Hetu 2014-04-17 forgot mirror urls in the playbook
Patrick Hetu 2014-04-17 release version 6
Patrick Hetu 2014-04-17 no need for |bool when checking variables
Patrick Hetu 2014-04-17 add the pip_freeze task in the fabfile
Patrick Hetu 2014-04-17 fix fabfile for latest version of juju-core
Patrick Hetu 2014-04-16 fix configuration reading for Django task
Patrick Hetu 2014-04-16 fix mercurial branch in playbook
Patrick Hetu 2014-04-16 merge changes from Ubuntu's ci-services and add my fixes
Patrick Hetu 2014-04-16 fix configuration injection for python3
Patrick Hetu 2014-04-16 fix mongodb settings
Patrick Hetu 2014-04-16 add redis support
Patrick Hetu 2014-04-16 fix ansible playbooks
Patrick Hetu 2014-04-16 add redis support
Patrick Hetu 2014-04-09 merge and adapt python-django_ci-services branch
Patrick Hetu 2013-11-26 FIX 1226303 : Minor formatting problem in README's markdown
Patrick Hetu 2013-11-26 python3 compatiblity
Patrick Hetu 2013-11-26 use ansible for handling repos and pip
Patrick Hetu 2013-11-18 wsgi configurations that works for gunicorn and uwsgi
Patrick Hetu 2013-11-17 convert the code to use charmhelpers
Patrick Hetu 2013-09-03 mysql don't support autocommit yet
Patrick Hetu 2013-08-07 increment version number
Patrick Hetu 2013-08-07 puts pip's source in a safe place and accept remote requirements files
Patrick Hetu 2013-08-05 add support for a mysql server relation

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
 
 
2
import os
 
3
import re
 
4
 
 
5
from subprocess import (
 
6
    check_call,
 
7
    check_output,
 
8
)
 
9
 
 
10
 
 
11
##################################################
 
12
# loopback device helpers.
 
13
##################################################
 
14
def loopback_devices():
 
15
    '''
 
16
    Parse through 'losetup -a' output to determine currently mapped
 
17
    loopback devices. Output is expected to look like:
 
18
 
 
19
        /dev/loop0: [0807]:961814 (/tmp/my.img)
 
20
 
 
21
    :returns: dict: a dict mapping {loopback_dev: backing_file}
 
22
    '''
 
23
    loopbacks = {}
 
24
    cmd = ['losetup', '-a']
 
25
    devs = [d.strip().split(' ') for d in
 
26
            check_output(cmd).splitlines() if d != '']
 
27
    for dev, _, f in devs:
 
28
        loopbacks[dev.replace(':', '')] = re.search('\((\S+)\)', f).groups()[0]
 
29
    return loopbacks
 
30
 
 
31
 
 
32
def create_loopback(file_path):
 
33
    '''
 
34
    Create a loopback device for a given backing file.
 
35
 
 
36
    :returns: str: Full path to new loopback device (eg, /dev/loop0)
 
37
    '''
 
38
    file_path = os.path.abspath(file_path)
 
39
    check_call(['losetup', '--find', file_path])
 
40
    for d, f in loopback_devices().iteritems():
 
41
        if f == file_path:
 
42
            return d
 
43
 
 
44
 
 
45
def ensure_loopback_device(path, size):
 
46
    '''
 
47
    Ensure a loopback device exists for a given backing file path and size.
 
48
    If it a loopback device is not mapped to file, a new one will be created.
 
49
 
 
50
    TODO: Confirm size of found loopback device.
 
51
 
 
52
    :returns: str: Full path to the ensured loopback device (eg, /dev/loop0)
 
53
    '''
 
54
    for d, f in loopback_devices().iteritems():
 
55
        if f == path:
 
56
            return d
 
57
 
 
58
    if not os.path.exists(path):
 
59
        cmd = ['truncate', '--size', size, path]
 
60
        check_call(cmd)
 
61
 
 
62
    return create_loopback(path)