Switch to jinja & adjust tpls

This commit is contained in:
Joshua Harlow 2014-03-05 15:05:59 -08:00
parent 6d40f47b44
commit 3ddc5d29f8
9 changed files with 173 additions and 162 deletions

View File

@ -23,33 +23,41 @@
import re
from Cheetah.Template import Template as CTemplate
from mako.template import Template as MTemplate
import jinja2
from jinja2 import Template as JTemplate
from cloudinit import log as logging
from cloudinit import util
LOG = logging.getLogger(__name__)
DEF_RENDERER = (lambda content, params:
CTemplate(content, searchList=[params]).respond())
DEF_RENDERER = 'cheetah'
RENDERERS = {
'mako': lambda content, params: MTemplate(content).render(**params),
'cheetah': DEF_RENDERER,
'jinja': (lambda content, params:
JTemplate(content,
undefined=jinja2.StrictUndefined,
trim_blocks=True).render(**params)),
'cheetah': (lambda content, params:
CTemplate(content, searchList=[params]).respond()),
}
TYPE_MATCHER = re.compile(r"##\s*template:(.*)", re.I)
def detect_template(text):
lines = text.splitlines()
if not lines:
return DEF_RENDERER
line = lines[0]
type_match = TYPE_MATCHER.match(line)
if not type_match:
return DEF_RENDERER
template_type = type_match.group(1).lower().strip()
if template_type not in RENDERERS:
LOG.warn("Unknown template type requested: %s", template_type)
return RENDERERS.get(template_type, DEF_RENDERER)
try:
ident, rest = text.split("\n", 1)
except ValueError:
return (DEF_RENDERER, text)
else:
type_match = TYPE_MATCHER.match(ident)
if not type_match:
return (DEF_RENDERER, text)
template_type = type_match.group(1).lower().strip()
if template_type not in RENDERERS:
raise ValueError("Unknown template type '%s' requested"
% template_type)
else:
return (template_type, rest)
def render_from_file(fn, params):
@ -64,5 +72,9 @@ def render_to_file(fn, outfn, params, mode=0644):
def render_string(content, params):
if not params:
params = {}
renderer = detect_template(content)
return renderer(content, params)
try:
renderer, content = detect_template(content)
except ValueError:
renderer = DEF_RENDERER
LOG.debug("Rendering %s using renderer '%s'", content, renderer)
return RENDERERS[renderer](content, params)

View File

@ -2,7 +2,7 @@
# Used for untemplating any files or strings with parameters.
cheetah
mako
jinja2
# This is used for any pretty printing of tabular data.
PrettyTable

View File

@ -1,25 +1,25 @@
#*
This file is only utilized if the module 'cc_chef' is enabled in
cloud-config. Specifically, in order to enable it
you need to add the following to config:
chef:
validation_key: XYZ
validation_cert: XYZ
validation_name: XYZ
server_url: XYZ
*#
## template:jinja
{#
This file is only utilized if the module 'cc_chef' is enabled in
cloud-config. Specifically, in order to enable it
you need to add the following to config:
chef:
validation_key: XYZ
validation_cert: XYZ
validation_name: XYZ
server_url: XYZ
-#}
log_level :info
log_location "/var/log/chef/client.log"
ssl_verify_mode :verify_none
validation_client_name "$validation_name"
validation_client_name "{{validation_name}}"
validation_key "/etc/chef/validation.pem"
client_key "/etc/chef/client.pem"
chef_server_url "$server_url"
environment "$environment"
node_name "$node_name"
chef_server_url "{{server_url}}"
environment "{{environment}}"
node_name "{{node_name}}"
json_attribs "/etc/chef/firstboot.json"
file_cache_path "/var/cache/chef"
file_backup_path "/var/backups/chef"
pid_file "/var/run/chef/client.pid"
Chef::Log::Formatter.show_time = true

View File

@ -1,19 +1,19 @@
## This file (/etc/cloud/templates/hosts.tmpl) is only utilized
## if enabled in cloud-config. Specifically, in order to enable it
## you need to add the following to config:
## manage_etc_hosts: True
##
## Note, double-hash commented lines will not appear in /etc/hosts
#
## template:jinja
{#
This file (/etc/cloud/templates/hosts.tmpl) is only utilized
if enabled in cloud-config. Specifically, in order to enable it
you need to add the following to config:
manage_etc_hosts: True
-#}
# Your system has configured 'manage_etc_hosts' as True.
# As a result, if you wish for changes to this file to persist
# then you will need to either
# a.) make changes to the master file in /etc/cloud/templates/hosts.tmpl
# b.) change or remove the value of 'manage_etc_hosts' in
# /etc/cloud/cloud.cfg or cloud-config from user-data
#
## The value '$hostname' will be replaced with the local-hostname
127.0.1.1 $fqdn $hostname
#
{# The value '{{hostname}}' will be replaced with the local-hostname -#}
127.0.1.1 {{fqdn}} {{hostname}}
127.0.0.1 localhost
# The following lines are desirable for IPv6 capable hosts
@ -23,3 +23,4 @@ ff00::0 ip6-mcastprefix
ff02::1 ip6-allnodes
ff02::2 ip6-allrouters
ff02::3 ip6-allhosts

View File

@ -1,9 +1,10 @@
#*
This file /etc/cloud/templates/hosts.redhat.tmpl is only utilized
if enabled in cloud-config. Specifically, in order to enable it
you need to add the following to config:
manage_etc_hosts: True
*#
## template:jinja
{#
This file /etc/cloud/templates/hosts.redhat.tmpl is only utilized
if enabled in cloud-config. Specifically, in order to enable it
you need to add the following to config:
manage_etc_hosts: True
-#}
# Your system has configured 'manage_etc_hosts' as True.
# As a result, if you wish for changes to this file to persist
# then you will need to either
@ -12,12 +13,12 @@
# /etc/cloud/cloud.cfg or cloud-config from user-data
#
# The following lines are desirable for IPv4 capable hosts
127.0.0.1 ${fqdn} ${hostname}
127.0.0.1 {{fqdn}} {{hostname}}
127.0.0.1 localhost.localdomain localhost
127.0.0.1 localhost4.localdomain4 localhost4
# The following lines are desirable for IPv6 capable hosts
::1 ${fqdn} ${hostname}
::1 {{fqdn}} {{hostname}}
::1 localhost.localdomain localhost
::1 localhost6.localdomain6 localhost6

View File

@ -1,9 +1,10 @@
#*
This file /etc/cloud/templates/hosts.suse.tmpl is only utilized
if enabled in cloud-config. Specifically, in order to enable it
you need to add the following to config:
manage_etc_hosts: True
*#
## template:jinja
{#
This file /etc/cloud/templates/hosts.suse.tmpl is only utilized
if enabled in cloud-config. Specifically, in order to enable it
you need to add the following to config:
manage_etc_hosts: True
-#}
# Your system has configured 'manage_etc_hosts' as True.
# As a result, if you wish for changes to this file to persist
# then you will need to either
@ -22,3 +23,4 @@ ff00::0 ipv6-mcastprefix
ff02::1 ipv6-allnodes
ff02::2 ipv6-allrouters
ff02::3 ipv6-allhosts

View File

@ -1,39 +1,30 @@
#
## template:jinja
# Your system has been configured with 'manage-resolv-conf' set to true.
# As a result, cloud-init has written this file with configuration data
# that it has been provided. Cloud-init, by default, will write this file
# a single time (PER_ONCE).
#
{% if nameservers is defined %}
{% for server in nameservers %}
nameserver {{server}}
{% endfor %}
#if $varExists('nameservers')
#for $server in $nameservers
nameserver $server
#end for
#end if
#if $varExists('searchdomains')
search #slurp
#for $search in $searchdomains
$search #slurp
#end for
{% endif -%}
{% if searchdomains is defined %}
search {% for search in searchdomains %}{{search}} {% endfor %}
#end if
#if $varExists('domain')
domain $domain
#end if
#if $varExists('sortlist')
sortlist #slurp
#for $sort in $sortlist
$sort #slurp
#end for
{% endif %}
{% if domain is defined %}
domain {{domain}}
{% endif %}
{% if sortlist is defined %}
#end if
#if $varExists('options') or $varExists('flags')
options #slurp
#for $flag in $flags
$flag #slurp
#end for
#for $key, $value in $options.items()
$key:$value #slurp
#end for
sortlist {% for sort in sortlist %}{{sort}} {% endfor %}
{% endif %}
{% if options is defined or flags is defined %}
#end if
options {% for flag in flags %}{{flag}} {% endfor %}
{% for key, value in options.iteritems() -%}
{{key}}:{{value}}
{% endfor %}
{% endif %}

View File

@ -1,28 +1,32 @@
\## Note, this file is written by cloud-init on first boot of an instance
\## modifications made here will not survive a re-bundle.
\## if you wish to make changes you can:
\## a.) add 'apt_preserve_sources_list: true' to /etc/cloud/cloud.cfg
\## or do the same in user-data
\## b.) add sources in /etc/apt/sources.list.d
\## c.) make changes to template file /etc/cloud/templates/sources.list.debian.tmpl
\###
## template:jinja
## Note, this file is written by cloud-init on first boot of an instance
## modifications made here will not survive a re-bundle.
## if you wish to make changes you can:
## a.) add 'apt_preserve_sources_list: true' to /etc/cloud/cloud.cfg
## or do the same in user-data
## b.) add sources in /etc/apt/sources.list.d
## c.) make changes to template file /etc/cloud/templates/sources.list.debian.tmpl
###
# See http://www.debian.org/releases/stable/i386/release-notes/ch-upgrading.html
# for how to upgrade to newer versions of the distribution.
deb $mirror $codename main contrib non-free
deb-src $mirror $codename main contrib non-free
deb {{mirror}} {{codename}} main contrib non-free
deb-src {{mirror}} {{codename}} main contrib non-free
\## Major bug fix updates produced after the final release of the
\## distribution.
deb $security $codename/updates main contrib non-free
deb-src $security $codename/updates main contrib non-free
deb $mirror $codename-updates main contrib non-free
deb-src $mirror $codename-updates main contrib non-free
## Major bug fix updates produced after the final release of the
## distribution.
deb {{security}} {{codename}}/updates main contrib non-free
deb-src {{security}} {{codename}}/updates main contrib non-free
deb {{mirror}} {{codename}}-updates main contrib non-free
deb-src {{mirror}} {{codename}}-updates main contrib non-free
\## Uncomment the following two lines to add software from the 'backports'
\## repository.
\## N.B. software from this repository may not have been tested as
\## extensively as that contained in the main release, although it includes
\## newer versions of some applications which may provide useful features.
# deb http://backports.debian.org/debian-backports $codename-backports main contrib non-free
# deb-src http://backports.debian.org/debian-backports $codename-backports main contrib non-free
## Uncomment the following two lines to add software from the 'backports'
## repository.
##
## N.B. software from this repository may not have been tested as
## extensively as that contained in the main release, although it includes
## newer versions of some applications which may provide useful features.
{#
deb http://backports.debian.org/debian-backports {{codename}}-backports main contrib non-free
deb-src http://backports.debian.org/debian-backports {{codename}}-backports main contrib non-free
-#}

View File

@ -1,60 +1,60 @@
\## Note, this file is written by cloud-init on first boot of an instance
\## modifications made here will not survive a re-bundle.
\## if you wish to make changes you can:
\## a.) add 'apt_preserve_sources_list: true' to /etc/cloud/cloud.cfg
\## or do the same in user-data
\## b.) add sources in /etc/apt/sources.list.d
\## c.) make changes to template file /etc/cloud/templates/sources.list.tmpl
\###
## template:jinja
## Note, this file is written by cloud-init on first boot of an instance
## modifications made here will not survive a re-bundle.
## if you wish to make changes you can:
## a.) add 'apt_preserve_sources_list: true' to /etc/cloud/cloud.cfg
## or do the same in user-data
## b.) add sources in /etc/apt/sources.list.d
## c.) make changes to template file /etc/cloud/templates/sources.list.tmpl
# See http://help.ubuntu.com/community/UpgradeNotes for how to upgrade to
# newer versions of the distribution.
deb $mirror $codename main
deb-src $mirror $codename main
deb {{mirror}} {{codename}} main
deb-src {{mirror}} {{codename}} main
\## Major bug fix updates produced after the final release of the
\## distribution.
deb $mirror $codename-updates main
deb-src $mirror $codename-updates main
## Major bug fix updates produced after the final release of the
## distribution.
deb {{mirror}} {{codename}}-updates main
deb-src {{mirror}} {{codename}}-updates main
\## N.B. software from this repository is ENTIRELY UNSUPPORTED by the Ubuntu
\## team. Also, please note that software in universe WILL NOT receive any
\## review or updates from the Ubuntu security team.
deb $mirror $codename universe
deb-src $mirror $codename universe
deb $mirror $codename-updates universe
deb-src $mirror $codename-updates universe
## N.B. software from this repository is ENTIRELY UNSUPPORTED by the Ubuntu
## team. Also, please note that software in universe WILL NOT receive any
## review or updates from the Ubuntu security team.
deb {{mirror}} {{codename}} universe
deb-src {{mirror}} {{codename}} universe
deb {{mirror}} {{codename}}-updates universe
deb-src {{mirror}} {{codename}}-updates universe
\## N.B. software from this repository is ENTIRELY UNSUPPORTED by the Ubuntu
\## team, and may not be under a free licence. Please satisfy yourself as to
\## your rights to use the software. Also, please note that software in
\## multiverse WILL NOT receive any review or updates from the Ubuntu
\## security team.
# deb $mirror $codename multiverse
# deb-src $mirror $codename multiverse
# deb $mirror $codename-updates multiverse
# deb-src $mirror $codename-updates multiverse
## N.B. software from this repository is ENTIRELY UNSUPPORTED by the Ubuntu
## team, and may not be under a free licence. Please satisfy yourself as to
## your rights to use the software. Also, please note that software in
## multiverse WILL NOT receive any review or updates from the Ubuntu
## security team.
# deb {{mirror}} {{codename}} multiverse
# deb-src {{mirror}} {{codename}} multiverse
# deb {{mirror}} {{codename}}-updates multiverse
# deb-src {{mirror}} {{codename}}-updates multiverse
\## Uncomment the following two lines to add software from the 'backports'
\## repository.
\## N.B. software from this repository may not have been tested as
\## extensively as that contained in the main release, although it includes
\## newer versions of some applications which may provide useful features.
\## Also, please note that software in backports WILL NOT receive any review
\## or updates from the Ubuntu security team.
# deb $mirror $codename-backports main restricted universe multiverse
# deb-src $mirror $codename-backports main restricted universe multiverse
## Uncomment the following two lines to add software from the 'backports'
## repository.
## N.B. software from this repository may not have been tested as
## extensively as that contained in the main release, although it includes
## newer versions of some applications which may provide useful features.
## Also, please note that software in backports WILL NOT receive any review
## or updates from the Ubuntu security team.
# deb {{mirror}} {{codename}}-backports main restricted universe multiverse
# deb-src {{mirror}} {{codename}}-backports main restricted universe multiverse
\## Uncomment the following two lines to add software from Canonical's
\## 'partner' repository.
\## This software is not part of Ubuntu, but is offered by Canonical and the
\## respective vendors as a service to Ubuntu users.
# deb http://archive.canonical.com/ubuntu $codename partner
# deb-src http://archive.canonical.com/ubuntu $codename partner
## Uncomment the following two lines to add software from Canonical's
## 'partner' repository.
## This software is not part of Ubuntu, but is offered by Canonical and the
## respective vendors as a service to Ubuntu users.
# deb http://archive.canonical.com/ubuntu {{codename}} partner
# deb-src http://archive.canonical.com/ubuntu {{codename}} partner
deb $security $codename-security main
deb-src $security $codename-security main
deb $security $codename-security universe
deb-src $security $codename-security universe
# deb $security $codename-security multiverse
# deb-src $security $codename-security multiverse
deb {{security}} {{codename}}-security main
deb-src {{security}} {{codename}}-security main
deb {{security}} {{codename}}-security universe
deb-src {{security}} {{codename}}-security universe
# deb {{security}} {{codename}}-security multiverse
# deb-src {{security}} {{codename}}-security multiverse