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 import re
from Cheetah.Template import Template as CTemplate 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 log as logging
from cloudinit import util from cloudinit import util
LOG = logging.getLogger(__name__) LOG = logging.getLogger(__name__)
DEF_RENDERER = (lambda content, params: DEF_RENDERER = 'cheetah'
CTemplate(content, searchList=[params]).respond())
RENDERERS = { RENDERERS = {
'mako': lambda content, params: MTemplate(content).render(**params), 'jinja': (lambda content, params:
'cheetah': DEF_RENDERER, 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) TYPE_MATCHER = re.compile(r"##\s*template:(.*)", re.I)
def detect_template(text): def detect_template(text):
lines = text.splitlines() try:
if not lines: ident, rest = text.split("\n", 1)
return DEF_RENDERER except ValueError:
line = lines[0] return (DEF_RENDERER, text)
type_match = TYPE_MATCHER.match(line) else:
if not type_match: type_match = TYPE_MATCHER.match(ident)
return DEF_RENDERER if not type_match:
template_type = type_match.group(1).lower().strip() return (DEF_RENDERER, text)
if template_type not in RENDERERS: template_type = type_match.group(1).lower().strip()
LOG.warn("Unknown template type requested: %s", template_type) if template_type not in RENDERERS:
return RENDERERS.get(template_type, DEF_RENDERER) raise ValueError("Unknown template type '%s' requested"
% template_type)
else:
return (template_type, rest)
def render_from_file(fn, params): def render_from_file(fn, params):
@@ -64,5 +72,9 @@ def render_to_file(fn, outfn, params, mode=0644):
def render_string(content, params): def render_string(content, params):
if not params: if not params:
params = {} params = {}
renderer = detect_template(content) try:
return renderer(content, params) 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. # Used for untemplating any files or strings with parameters.
cheetah cheetah
mako jinja2
# This is used for any pretty printing of tabular data. # This is used for any pretty printing of tabular data.
PrettyTable PrettyTable

View File

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

View File

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

View File

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

View File

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

View File

@@ -1,39 +1,30 @@
# ## template:jinja
# Your system has been configured with 'manage-resolv-conf' set to true. # Your system has been configured with 'manage-resolv-conf' set to true.
# As a result, cloud-init has written this file with configuration data # 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 # that it has been provided. Cloud-init, by default, will write this file
# a single time (PER_ONCE). # a single time (PER_ONCE).
# #
{% if nameservers is defined %}
{% for server in nameservers %}
nameserver {{server}}
{% endfor %}
#if $varExists('nameservers') {% endif -%}
#for $server in $nameservers {% if searchdomains is defined %}
nameserver $server search {% for search in searchdomains %}{{search}} {% endfor %}
#end for
#end if
#if $varExists('searchdomains')
search #slurp
#for $search in $searchdomains
$search #slurp
#end for
#end if {% endif %}
#if $varExists('domain') {% if domain is defined %}
domain $domain domain {{domain}}
#end if {% endif %}
#if $varExists('sortlist') {% if sortlist is defined %}
sortlist #slurp
#for $sort in $sortlist
$sort #slurp
#end for
#end if sortlist {% for sort in sortlist %}{{sort}} {% endfor %}
#if $varExists('options') or $varExists('flags') {% endif %}
options #slurp {% if options is defined or flags is defined %}
#for $flag in $flags
$flag #slurp
#end for
#for $key, $value in $options.items()
$key:$value #slurp
#end for
#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 ## template:jinja
\## modifications made here will not survive a re-bundle. ## Note, this file is written by cloud-init on first boot of an instance
\## if you wish to make changes you can: ## modifications made here will not survive a re-bundle.
\## a.) add 'apt_preserve_sources_list: true' to /etc/cloud/cloud.cfg ## if you wish to make changes you can:
\## or do the same in user-data ## a.) add 'apt_preserve_sources_list: true' to /etc/cloud/cloud.cfg
\## b.) add sources in /etc/apt/sources.list.d ## or do the same in user-data
\## c.) make changes to template file /etc/cloud/templates/sources.list.debian.tmpl ## 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 # See http://www.debian.org/releases/stable/i386/release-notes/ch-upgrading.html
# for how to upgrade to newer versions of the distribution. # for how to upgrade to newer versions of the distribution.
deb $mirror $codename main contrib non-free deb {{mirror}} {{codename}} main contrib non-free
deb-src $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 ## Major bug fix updates produced after the final release of the
\## distribution. ## distribution.
deb $security $codename/updates main contrib non-free deb {{security}} {{codename}}/updates main contrib non-free
deb-src $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 {{mirror}} {{codename}}-updates main contrib non-free
deb-src $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' ## Uncomment the following two lines to add software from the 'backports'
\## repository. ## repository.
\## N.B. software from this repository may not have been tested as ##
\## extensively as that contained in the main release, although it includes ## N.B. software from this repository may not have been tested as
\## newer versions of some applications which may provide useful features. ## extensively as that contained in the main release, although it includes
# deb http://backports.debian.org/debian-backports $codename-backports main contrib non-free ## newer versions of some applications which may provide useful features.
# deb-src http://backports.debian.org/debian-backports $codename-backports main contrib non-free {#
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 ## template:jinja
\## modifications made here will not survive a re-bundle. ## Note, this file is written by cloud-init on first boot of an instance
\## if you wish to make changes you can: ## modifications made here will not survive a re-bundle.
\## a.) add 'apt_preserve_sources_list: true' to /etc/cloud/cloud.cfg ## if you wish to make changes you can:
\## or do the same in user-data ## a.) add 'apt_preserve_sources_list: true' to /etc/cloud/cloud.cfg
\## b.) add sources in /etc/apt/sources.list.d ## or do the same in user-data
\## c.) make changes to template file /etc/cloud/templates/sources.list.tmpl ## 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 # See http://help.ubuntu.com/community/UpgradeNotes for how to upgrade to
# newer versions of the distribution. # newer versions of the distribution.
deb $mirror $codename main deb {{mirror}} {{codename}} main
deb-src $mirror $codename main deb-src {{mirror}} {{codename}} main
\## Major bug fix updates produced after the final release of the ## Major bug fix updates produced after the final release of the
\## distribution. ## distribution.
deb $mirror $codename-updates main deb {{mirror}} {{codename}}-updates main
deb-src $mirror $codename-updates main deb-src {{mirror}} {{codename}}-updates main
\## N.B. software from this repository is ENTIRELY UNSUPPORTED by the Ubuntu ## N.B. software from this repository is ENTIRELY UNSUPPORTED by the Ubuntu
\## team. Also, please note that software in universe WILL NOT receive any ## team. Also, please note that software in universe WILL NOT receive any
\## review or updates from the Ubuntu security team. ## review or updates from the Ubuntu security team.
deb $mirror $codename universe deb {{mirror}} {{codename}} universe
deb-src $mirror $codename universe deb-src {{mirror}} {{codename}} universe
deb $mirror $codename-updates universe deb {{mirror}} {{codename}}-updates universe
deb-src $mirror $codename-updates universe deb-src {{mirror}} {{codename}}-updates universe
\## N.B. software from this repository is ENTIRELY UNSUPPORTED by the Ubuntu ## 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 ## 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 ## your rights to use the software. Also, please note that software in
\## multiverse WILL NOT receive any review or updates from the Ubuntu ## multiverse WILL NOT receive any review or updates from the Ubuntu
\## security team. ## security team.
# deb $mirror $codename multiverse # deb {{mirror}} {{codename}} multiverse
# deb-src $mirror $codename multiverse # deb-src {{mirror}} {{codename}} multiverse
# deb $mirror $codename-updates multiverse # deb {{mirror}} {{codename}}-updates multiverse
# deb-src $mirror $codename-updates multiverse # deb-src {{mirror}} {{codename}}-updates multiverse
\## Uncomment the following two lines to add software from the 'backports' ## Uncomment the following two lines to add software from the 'backports'
\## repository. ## repository.
\## N.B. software from this repository may not have been tested as ## N.B. software from this repository may not have been tested as
\## extensively as that contained in the main release, although it includes ## extensively as that contained in the main release, although it includes
\## newer versions of some applications which may provide useful features. ## newer versions of some applications which may provide useful features.
\## Also, please note that software in backports WILL NOT receive any review ## Also, please note that software in backports WILL NOT receive any review
\## or updates from the Ubuntu security team. ## or updates from the Ubuntu security team.
# deb $mirror $codename-backports main restricted universe multiverse # deb {{mirror}} {{codename}}-backports main restricted universe multiverse
# deb-src $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 ## Uncomment the following two lines to add software from Canonical's
\## 'partner' repository. ## 'partner' repository.
\## This software is not part of Ubuntu, but is offered by Canonical and the ## This software is not part of Ubuntu, but is offered by Canonical and the
\## respective vendors as a service to Ubuntu users. ## respective vendors as a service to Ubuntu users.
# deb http://archive.canonical.com/ubuntu $codename partner # deb http://archive.canonical.com/ubuntu {{codename}} partner
# deb-src http://archive.canonical.com/ubuntu $codename partner # deb-src http://archive.canonical.com/ubuntu {{codename}} partner
deb $security $codename-security main deb {{security}} {{codename}}-security main
deb-src $security $codename-security main deb-src {{security}} {{codename}}-security main
deb $security $codename-security universe deb {{security}} {{codename}}-security universe
deb-src $security $codename-security universe deb-src {{security}} {{codename}}-security universe
# deb $security $codename-security multiverse # deb {{security}} {{codename}}-security multiverse
# deb-src $security $codename-security multiverse # deb-src {{security}} {{codename}}-security multiverse