~free.ekanayaka/landscape-charm/faster-hashids-test

« back to all changes in this revision

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

  • Committer: Landscape Builder
  • Author(s): Free Ekanayaka
  • Date: 2015-01-28 16:09:54 UTC
  • mfrom: (218.2.10 testable-install-hook)
  • Revision ID: landscape_builder-20150128160954-bu6exkqv89tacaj8
Merge testable-install-hook [f=] [r=ack,tealeg] [a=Free Ekanayaka]
This MP is the first step in getting a minimal hello-world Landscape charm that we can push to IS using mojo and see it live at edge.landscape.canonical.com.

It implements the install hook in Python (so it can be tested, whereas the former install hook was in batch). So all the charm does at the moment is to install the Landscape package from the selected PPA.

It takes also advantage of charm-helpers and lays down the basic structure that we'll expand on in follow-up branches.

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):
 
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)
 
68
    host.write_file(target, content, owner, group, perms)