~danilo/charms/trusty/glance-simplestreams-sync/invert-failure-mode

« back to all changes in this revision

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

[bradm, r=gnuoy] Add basic nagios checks

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# Copyright 2014-2015 Canonical Limited.
 
2
#
 
3
# This file is part of charm-helpers.
 
4
#
 
5
# charm-helpers is free software: you can redistribute it and/or modify
 
6
# it under the terms of the GNU Lesser General Public License version 3 as
 
7
# published by the Free Software Foundation.
 
8
#
 
9
# charm-helpers is distributed in the hope that it will be useful,
 
10
# but WITHOUT ANY WARRANTY; without even the implied warranty of
 
11
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
12
# GNU Lesser General Public License for more details.
 
13
#
 
14
# You should have received a copy of the GNU Lesser General Public License
 
15
# along with charm-helpers.  If not, see <http://www.gnu.org/licenses/>.
 
16
 
1
17
import os
2
18
 
 
19
import six
 
20
 
3
21
from charmhelpers.fetch import apt_install
4
 
 
5
22
from charmhelpers.core.hookenv import (
6
23
    log,
7
24
    ERROR,
8
25
    INFO
9
26
)
10
 
 
11
27
from charmhelpers.contrib.openstack.utils import OPENSTACK_CODENAMES
12
28
 
13
29
try:
30
46
    loading dir.
31
47
 
32
48
    A charm may also ship a templates dir with this module
33
 
    and it will be appended to the bottom of the search list, eg:
34
 
    hooks/charmhelpers/contrib/openstack/templates.
35
 
 
36
 
    :param templates_dir: str: Base template directory containing release
37
 
                               sub-directories.
38
 
    :param os_release   : str: OpenStack release codename to construct template
39
 
                               loader.
40
 
 
41
 
    :returns            : jinja2.ChoiceLoader constructed with a list of
42
 
                          jinja2.FilesystemLoaders, ordered in descending
43
 
                          order by OpenStack release.
 
49
    and it will be appended to the bottom of the search list, eg::
 
50
 
 
51
        hooks/charmhelpers/contrib/openstack/templates
 
52
 
 
53
    :param templates_dir (str): Base template directory containing release
 
54
        sub-directories.
 
55
    :param os_release (str): OpenStack release codename to construct template
 
56
        loader.
 
57
    :returns: jinja2.ChoiceLoader constructed with a list of
 
58
        jinja2.FilesystemLoaders, ordered in descending
 
59
        order by OpenStack release.
44
60
    """
45
61
    tmpl_dirs = [(rel, os.path.join(templates_dir, rel))
46
 
                 for rel in OPENSTACK_CODENAMES.itervalues()]
 
62
                 for rel in six.itervalues(OPENSTACK_CODENAMES)]
47
63
 
48
64
    if not os.path.isdir(templates_dir):
49
65
        log('Templates directory not found @ %s.' % templates_dir,
111
127
    and ease the burden of managing config templates across multiple OpenStack
112
128
    releases.
113
129
 
114
 
    Basic usage:
 
130
    Basic usage::
 
131
 
115
132
        # import some common context generates from charmhelpers
116
133
        from charmhelpers.contrib.openstack import context
117
134
 
131
148
        # write out all registered configs
132
149
        configs.write_all()
133
150
 
134
 
    Details:
 
151
    **OpenStack Releases and template loading**
135
152
 
136
 
    OpenStack Releases and template loading
137
 
    ---------------------------------------
138
153
    When the object is instantiated, it is associated with a specific OS
139
154
    release.  This dictates how the template loader will be constructed.
140
155
 
141
156
    The constructed loader attempts to load the template from several places
142
157
    in the following order:
143
 
        - from the most recent OS release-specific template dir (if one exists)
144
 
        - the base templates_dir
145
 
        - a template directory shipped in the charm with this helper file.
146
 
 
147
 
 
148
 
    For the example above, '/tmp/templates' contains the following structure:
 
158
    - from the most recent OS release-specific template dir (if one exists)
 
159
    - the base templates_dir
 
160
    - a template directory shipped in the charm with this helper file.
 
161
 
 
162
    For the example above, '/tmp/templates' contains the following structure::
 
163
 
149
164
        /tmp/templates/nova.conf
150
165
        /tmp/templates/api-paste.ini
151
166
        /tmp/templates/grizzly/api-paste.ini
169
184
    $CHARM/hooks/charmhelpers/contrib/openstack/templates.  This allows
170
185
    us to ship common templates (haproxy, apache) with the helpers.
171
186
 
172
 
    Context generators
173
 
    ---------------------------------------
 
187
    **Context generators**
 
188
 
174
189
    Context generators are used to generate template contexts during hook
175
190
    execution.  Doing so may require inspecting service relations, charm
176
191
    config, etc.  When registered, a config file is associated with a list
259
274
        """
260
275
        Write out all registered config files.
261
276
        """
262
 
        [self.write(k) for k in self.templates.iterkeys()]
 
277
        [self.write(k) for k in six.iterkeys(self.templates)]
263
278
 
264
279
    def set_release(self, openstack_release):
265
280
        """
276
291
        '''
277
292
        interfaces = []
278
293
        [interfaces.extend(i.complete_contexts())
279
 
         for i in self.templates.itervalues()]
 
294
         for i in six.itervalues(self.templates)]
280
295
        return interfaces