Blame cloudinit/templater.py

Packit Service a04d08
# Copyright (C) 2012 Canonical Ltd.
Packit Service a04d08
# Copyright (C) 2012 Hewlett-Packard Development Company, L.P.
Packit Service a04d08
# Copyright (C) 2012 Yahoo! Inc.
Packit Service a04d08
# Copyright (C) 2016 Amazon.com, Inc. or its affiliates.
Packit Service a04d08
#
Packit Service a04d08
# Author: Scott Moser <scott.moser@canonical.com>
Packit Service a04d08
# Author: Juerg Haefliger <juerg.haefliger@hp.com>
Packit Service a04d08
# Author: Joshua Harlow <harlowja@yahoo-inc.com>
Packit Service a04d08
# Author: Andrew Jorgensen <ajorgens@amazon.com>
Packit Service a04d08
#
Packit Service a04d08
# This file is part of cloud-init. See LICENSE file for license information.
Packit Service a04d08
Packit Service a04d08
import collections
Packit Service a04d08
import re
Packit Service a04d08
Packit Service a04d08
Packit Service a04d08
try:
Packit Service a04d08
    from Cheetah.Template import Template as CTemplate
Packit Service a04d08
    CHEETAH_AVAILABLE = True
Packit Service a04d08
except (ImportError, AttributeError):
Packit Service a04d08
    CHEETAH_AVAILABLE = False
Packit Service a04d08
Packit Service a04d08
try:
Packit Service a04d08
    from jinja2 import Template as JTemplate
Packit Service a04d08
    from jinja2 import DebugUndefined as JUndefined
Packit Service a04d08
    JINJA_AVAILABLE = True
Packit Service a04d08
except (ImportError, AttributeError):
Packit Service a04d08
    JINJA_AVAILABLE = False
Packit Service a04d08
    JUndefined = object
Packit Service a04d08
Packit Service a04d08
from cloudinit import log as logging
Packit Service a04d08
from cloudinit import type_utils as tu
Packit Service a04d08
from cloudinit import util
Packit Service a04d08
Packit Service a04d08
Packit Service a04d08
LOG = logging.getLogger(__name__)
Packit Service a04d08
TYPE_MATCHER = re.compile(r"##\s*template:(.*)", re.I)
Packit Service a04d08
BASIC_MATCHER = re.compile(r'\$\{([A-Za-z0-9_.]+)\}|\$([A-Za-z0-9_.]+)')
Packit Service a04d08
MISSING_JINJA_PREFIX = u'CI_MISSING_JINJA_VAR/'
Packit Service a04d08
Packit Service a04d08
Packit Service a04d08
class UndefinedJinjaVariable(JUndefined):
Packit Service a04d08
    """Class used to represent any undefined jinja template variable."""
Packit Service a04d08
Packit Service a04d08
    def __str__(self):
Packit Service a04d08
        return u'%s%s' % (MISSING_JINJA_PREFIX, self._undefined_name)
Packit Service a04d08
Packit Service a04d08
    def __sub__(self, other):
Packit Service a04d08
        other = str(other).replace(MISSING_JINJA_PREFIX, '')
Packit Service a04d08
        raise TypeError(
Packit Service a04d08
            'Undefined jinja variable: "{this}-{other}". Jinja tried'
Packit Service a04d08
            ' subtraction. Perhaps you meant "{this}_{other}"?'.format(
Packit Service a04d08
                this=self._undefined_name, other=other))
Packit Service a04d08
Packit Service a04d08
Packit Service a04d08
def basic_render(content, params):
Packit Service a04d08
    """This does simple replacement of bash variable like templates.
Packit Service a04d08
Packit Service a04d08
    It identifies patterns like ${a} or $a and can also identify patterns like
Packit Service a04d08
    ${a.b} or $a.b which will look for a key 'b' in the dictionary rooted
Packit Service a04d08
    by key 'a'.
Packit Service a04d08
    """
Packit Service a04d08
Packit Service a04d08
    def replacer(match):
Packit Service a04d08
        # Only 1 of the 2 groups will actually have a valid entry.
Packit Service a04d08
        name = match.group(1)
Packit Service a04d08
        if name is None:
Packit Service a04d08
            name = match.group(2)
Packit Service a04d08
        if name is None:
Packit Service a04d08
            raise RuntimeError("Match encountered but no valid group present")
Packit Service a04d08
        path = collections.deque(name.split("."))
Packit Service a04d08
        selected_params = params
Packit Service a04d08
        while len(path) > 1:
Packit Service a04d08
            key = path.popleft()
Packit Service a04d08
            if not isinstance(selected_params, dict):
Packit Service a04d08
                raise TypeError("Can not traverse into"
Packit Service a04d08
                                " non-dictionary '%s' of type %s while"
Packit Service a04d08
                                " looking for subkey '%s'"
Packit Service a04d08
                                % (selected_params,
Packit Service a04d08
                                   tu.obj_name(selected_params),
Packit Service a04d08
                                   key))
Packit Service a04d08
            selected_params = selected_params[key]
Packit Service a04d08
        key = path.popleft()
Packit Service a04d08
        if not isinstance(selected_params, dict):
Packit Service a04d08
            raise TypeError("Can not extract key '%s' from non-dictionary"
Packit Service a04d08
                            " '%s' of type %s"
Packit Service a04d08
                            % (key, selected_params,
Packit Service a04d08
                               tu.obj_name(selected_params)))
Packit Service a04d08
        return str(selected_params[key])
Packit Service a04d08
Packit Service a04d08
    return BASIC_MATCHER.sub(replacer, content)
Packit Service a04d08
Packit Service a04d08
Packit Service a04d08
def detect_template(text):
Packit Service a04d08
Packit Service a04d08
    def cheetah_render(content, params):
Packit Service a04d08
        return CTemplate(content, searchList=[params]).respond()
Packit Service a04d08
Packit Service a04d08
    def jinja_render(content, params):
Packit Service a04d08
        # keep_trailing_newline is in jinja2 2.7+, not 2.6
Packit Service a04d08
        add = "\n" if content.endswith("\n") else ""
Packit Service a04d08
        return JTemplate(content,
Packit Service a04d08
                         undefined=UndefinedJinjaVariable,
Packit Service a04d08
                         trim_blocks=True).render(**params) + add
Packit Service a04d08
Packit Service a04d08
    if text.find("\n") != -1:
Packit Service a04d08
        ident, rest = text.split("\n", 1)
Packit Service a04d08
    else:
Packit Service a04d08
        ident = text
Packit Service a04d08
        rest = ''
Packit Service a04d08
    type_match = TYPE_MATCHER.match(ident)
Packit Service a04d08
    if not type_match:
Packit Service a04d08
        if CHEETAH_AVAILABLE:
Packit Service a04d08
            LOG.debug("Using Cheetah as the renderer for unknown template.")
Packit Service a04d08
            return ('cheetah', cheetah_render, text)
Packit Service a04d08
        else:
Packit Service a04d08
            return ('basic', basic_render, text)
Packit Service a04d08
    else:
Packit Service a04d08
        template_type = type_match.group(1).lower().strip()
Packit Service a04d08
        if template_type not in ('jinja', 'cheetah', 'basic'):
Packit Service a04d08
            raise ValueError("Unknown template rendering type '%s' requested"
Packit Service a04d08
                             % template_type)
Packit Service a04d08
        if template_type == 'jinja' and not JINJA_AVAILABLE:
Packit Service a04d08
            LOG.warning("Jinja not available as the selected renderer for"
Packit Service a04d08
                        " desired template, reverting to the basic renderer.")
Packit Service a04d08
            return ('basic', basic_render, rest)
Packit Service a04d08
        elif template_type == 'jinja' and JINJA_AVAILABLE:
Packit Service a04d08
            return ('jinja', jinja_render, rest)
Packit Service a04d08
        if template_type == 'cheetah' and not CHEETAH_AVAILABLE:
Packit Service a04d08
            LOG.warning("Cheetah not available as the selected renderer for"
Packit Service a04d08
                        " desired template, reverting to the basic renderer.")
Packit Service a04d08
            return ('basic', basic_render, rest)
Packit Service a04d08
        elif template_type == 'cheetah' and CHEETAH_AVAILABLE:
Packit Service a04d08
            return ('cheetah', cheetah_render, rest)
Packit Service a04d08
        # Only thing left over is the basic renderer (it is always available).
Packit Service a04d08
        return ('basic', basic_render, rest)
Packit Service a04d08
Packit Service a04d08
Packit Service a04d08
def render_from_file(fn, params):
Packit Service a04d08
    if not params:
Packit Service a04d08
        params = {}
Packit Service a04d08
    # jinja in python2 uses unicode internally.  All py2 str will be decoded.
Packit Service a04d08
    # If it is given a str that has non-ascii then it will raise a
Packit Service a04d08
    # UnicodeDecodeError.  So we explicitly convert to unicode type here.
Packit Service a04d08
    template_type, renderer, content = detect_template(
Packit Service a04d08
        util.load_file(fn, decode=False).decode('utf-8'))
Packit Service a04d08
    LOG.debug("Rendering content of '%s' using renderer %s", fn, template_type)
Packit Service a04d08
    return renderer(content, params)
Packit Service a04d08
Packit Service a04d08
Packit Service a04d08
def render_to_file(fn, outfn, params, mode=0o644):
Packit Service a04d08
    contents = render_from_file(fn, params)
Packit Service a04d08
    util.write_file(outfn, contents, mode=mode)
Packit Service a04d08
Packit Service a04d08
Packit Service a04d08
def render_string_to_file(content, outfn, params, mode=0o644):
Packit Service a04d08
    """Render string (or py2 unicode) to file.
Packit Service a04d08
    Warning: py2 str with non-ascii chars will cause UnicodeDecodeError."""
Packit Service a04d08
    contents = render_string(content, params)
Packit Service a04d08
    util.write_file(outfn, contents, mode=mode)
Packit Service a04d08
Packit Service a04d08
Packit Service a04d08
def render_string(content, params):
Packit Service a04d08
    """Render string (or py2 unicode).
Packit Service a04d08
    Warning: py2 str with non-ascii chars will cause UnicodeDecodeError."""
Packit Service a04d08
    if not params:
Packit Service a04d08
        params = {}
Packit Service a04d08
    _template_type, renderer, content = detect_template(content)
Packit Service a04d08
    return renderer(content, params)
Packit Service a04d08
Packit Service a04d08
# vi: ts=4 expandtab