aboutsummaryrefslogtreecommitdiffstats
path: root/charms/trusty/ceilometer/charmhelpers/contrib/charmsupport/volumes.py
blob: 320961b9353ed47d8c788627d2319951250887f0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
# Copyright 2014-2015 Canonical Limited.
#
# This file is part of charm-helpers.
#
# charm-helpers is free software: you can redistribute it and/or modify
# it under the terms of the GNU Lesser General Public License version 3 as
# published by the Free Software Foundation.
#
# charm-helpers is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public License
# along with charm-helpers.  If not, see <http://www.gnu.org/licenses/>.

'''
Functions for managing volumes in juju units. One volume is supported per unit.
Subordinates may have their own storage, provided it is on its own partition.

Configuration stanzas::

  volume-ephemeral:
    type: boolean
    default: true
    description: >
      If false, a volume is mounted as sepecified in "volume-map"
      If true, ephemeral storage will be used, meaning that log data
         will only exist as long as the machine. YOU HAVE BEEN WARNED.
  volume-map:
    type: string
    default: {}
    description: >
      YAML map of units to device names, e.g:
        "{ rsyslog/0: /dev/vdb, rsyslog/1: /dev/vdb }"
      Service units will raise a configure-error if volume-ephemeral
      is 'true' and no volume-map value is set. Use 'juju set' to set a
      value and 'juju resolved' to complete configuration.

Usage::

    from charmsupport.volumes import configure_volume, VolumeConfigurationError
    from charmsupport.hookenv import log, ERROR
    def post_mount_hook():
        stop_service('myservice')
    def post_mount_hook():
        start_service('myservice')

    if __name__ == '__main__':
        try:
            configure_volume(before_change=pre_mount_hook,
                             after_change=post_mount_hook)
        except VolumeConfigurationError:
            log('Storage could not be configured', ERROR)

'''

# XXX: Known limitations
# - fstab is neither consulted nor updated

import os
from charmhelpers.core import hookenv
from charmhelpers.core import host
import yaml


MOUNT_BASE = '/srv/juju/volumes'


class VolumeConfigurationError(Exception):
    '''Volume configuration data is missing or invalid'''
    pass


def get_config():
    '''Gather and sanity-check volume configuration data'''
    volume_config = {}
    config = hookenv.config()

    errors = False

    if config.get('volume-ephemeral') in (True, 'True', 'true', 'Yes', 'yes'):
        volume_config['ephemeral'] = True
    else:
        volume_config['ephemeral'] = False

    try:
        volume_map = yaml.safe_load(config.get('volume-map', '{}'))
    except yaml.YAMLError as e:
        hookenv.log("Error parsing YAML volume-map: {}".format(e),
                    hookenv.ERROR)
        errors = True
    if volume_map is None:
        # probably an empty string
        volume_map = {}
    elif not isinstance(volume_map, dict):
        hookenv.log("Volume-map should be a dictionary, not {}".format(
            type(volume_map)))
        errors = True

    volume_config['device'] = volume_map.get(os.environ['JUJU_UNIT_NAME'])
    if volume_config['device'] and volume_config['ephemeral']:
        # asked for ephemeral storage but also defined a volume ID
        hookenv.log('A volume is defined for this unit, but ephemeral '
                    'storage was requested', hookenv.ERROR)
        errors = True
    elif not volume_config['device'] and not volume_config['ephemeral']:
        # asked for permanent storage but did not define volume ID
        hookenv.log('Ephemeral storage was requested, but there is no volume '
                    'defined for this unit.', hookenv.ERROR)
        errors = True

    unit_mount_name = hookenv.local_unit().replace('/', '-')
    volume_config['mountpoint'] = os.path.join(MOUNT_BASE, unit_mount_name)

    if errors:
        return None
    return volume_config


def mount_volume(config):
    if os.path.exists(config['mountpoint']):
        if not os.path.isdir(config['mountpoint']):
            hookenv.log('Not a directory: {}'.format(config['mountpoint']))
            raise VolumeConfigurationError()
    else:
        host.mkdir(config['mountpoint'])
    if os.path.ismount(config['mountpoint']):
        unmount_volume(config)
    if not host.mount(config['device'], config['mountpoint'], persist=True):
        raise VolumeConfigurationError()


def unmount_volume(config):
    if os.path.ismount(config['mountpoint']):
        if not host.umount(config['mountpoint'], persist=True):
            raise VolumeConfigurationError()


def managed_mounts():
    '''List of all mounted managed volumes'''
    return filter(lambda mount: mount[0].startswith(MOUNT_BASE), host.mounts())


def configure_volume(before_change=lambda: None, after_change=lambda: None):
    '''Set up storage (or don't) according to the charm's volume configuration.
       Returns the mount point or "ephemeral". before_change and after_change
       are optional functions to be called if the volume configuration changes.
    '''

    config = get_config()
    if not config:
        hookenv.log('Failed to read volume configuration', hookenv.CRITICAL)
        raise VolumeConfigurationError()

    if config['ephemeral']:
        if os.path.ismount(config['mountpoint']):
            before_change()
            unmount_volume(config)
            after_change()
        return 'ephemeral'
    else:
        # persistent storage
        if os.path.ismount(config['mountpoint']):
            mounts = dict(managed_mounts())
            if mounts.get(config['mountpoint']) != config['device']:
                before_change()
                unmount_volume(config)
                mount_volume(config)
                after_change()
        else:
            before_change()
            mount_volume(config)
            after_change()
        return config['mountpoint']