~le-charmers/charms/trusty/rabbitmq-server/leadership-election

« back to all changes in this revision

Viewing changes to tests/charmhelpers/core/templating.py

  • Committer: Liam Young
  • Date: 2015-05-11 08:03:57 UTC
  • mfrom: (83.1.14 rabbitmq-server)
  • Revision ID: liam.young@canonical.com-20150511080357-3ftop9kxb6o0e3mq
Merged trunk in + LE charmhelper sync

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
 
 
17
import os
 
18
 
 
19
from charmhelpers.core import host
 
20
from charmhelpers.core import hookenv
 
21
 
 
22
 
 
23
def render(source, target, context, owner='root', group='root',
 
24
           perms=0o444, templates_dir=None, encoding='UTF-8'):
 
25
    """
 
26
    Render a template.
 
27
 
 
28
    The `source` path, if not absolute, is relative to the `templates_dir`.
 
29
 
 
30
    The `target` path should be absolute.
 
31
 
 
32
    The context should be a dict containing the values to be replaced in the
 
33
    template.
 
34
 
 
35
    The `owner`, `group`, and `perms` options will be passed to `write_file`.
 
36
 
 
37
    If omitted, `templates_dir` defaults to the `templates` folder in the charm.
 
38
 
 
39
    Note: Using this requires python-jinja2; if it is not installed, calling
 
40
    this will attempt to use charmhelpers.fetch.apt_install to install it.
 
41
    """
 
42
    try:
 
43
        from jinja2 import FileSystemLoader, Environment, exceptions
 
44
    except ImportError:
 
45
        try:
 
46
            from charmhelpers.fetch import apt_install
 
47
        except ImportError:
 
48
            hookenv.log('Could not import jinja2, and could not import '
 
49
                        'charmhelpers.fetch to install it',
 
50
                        level=hookenv.ERROR)
 
51
            raise
 
52
        apt_install('python-jinja2', fatal=True)
 
53
        from jinja2 import FileSystemLoader, Environment, exceptions
 
54
 
 
55
    if templates_dir is None:
 
56
        templates_dir = os.path.join(hookenv.charm_dir(), 'templates')
 
57
    loader = Environment(loader=FileSystemLoader(templates_dir))
 
58
    try:
 
59
        source = source
 
60
        template = loader.get_template(source)
 
61
    except exceptions.TemplateNotFound as e:
 
62
        hookenv.log('Could not load template %s from %s.' %
 
63
                    (source, templates_dir),
 
64
                    level=hookenv.ERROR)
 
65
        raise e
 
66
    content = template.render(context)
 
67
    host.mkdir(os.path.dirname(target), owner, group, perms=0o755)
 
68
    host.write_file(target, content.encode(encoding), owner, group, perms)