summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJoshua Harlow <harlowja@yahoo-inc.com>2014-03-05 15:05:59 -0800
committerJoshua Harlow <harlowja@yahoo-inc.com>2014-03-05 15:05:59 -0800
commitcc79c859115ceb520877c0afaece4c28d6031499 (patch)
tree1deb6b7613faa9d150519222556e9a0c5991bf3f
parent3f3450660f5f7118ac0583de5d137120c1ae4abd (diff)
downloadcloud-init-git-cc79c859115ceb520877c0afaece4c28d6031499.tar.gz
Switch to jinja & adjust tpls
-rw-r--r--cloudinit/templater.py48
-rw-r--r--requirements.txt2
-rw-r--r--templates/chef_client.rb.tmpl30
-rw-r--r--templates/hosts.debian.tmpl21
-rw-r--r--templates/hosts.redhat.tmpl17
-rw-r--r--templates/hosts.suse.tmpl14
-rw-r--r--templates/resolv.conf.tmpl51
-rw-r--r--templates/sources.list.debian.tmpl50
-rw-r--r--templates/sources.list.ubuntu.tmpl102
9 files changed, 173 insertions, 162 deletions
diff --git a/cloudinit/templater.py b/cloudinit/templater.py
index a7527964..915b188e 100644
--- a/cloudinit/templater.py
+++ b/cloudinit/templater.py
@@ -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)
diff --git a/requirements.txt b/requirements.txt
index 2b010075..943dbef7 100644
--- a/requirements.txt
+++ b/requirements.txt
@@ -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
diff --git a/templates/chef_client.rb.tmpl b/templates/chef_client.rb.tmpl
index 7981cba7..538850ca 100644
--- a/templates/chef_client.rb.tmpl
+++ b/templates/chef_client.rb.tmpl
@@ -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
-
diff --git a/templates/hosts.debian.tmpl b/templates/hosts.debian.tmpl
index ae120b02..a1d97212 100644
--- a/templates/hosts.debian.tmpl
+++ b/templates/hosts.debian.tmpl
@@ -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
+
diff --git a/templates/hosts.redhat.tmpl b/templates/hosts.redhat.tmpl
index 80459d95..bc5da32c 100644
--- a/templates/hosts.redhat.tmpl
+++ b/templates/hosts.redhat.tmpl
@@ -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
diff --git a/templates/hosts.suse.tmpl b/templates/hosts.suse.tmpl
index 5d3d57e4..b6082692 100644
--- a/templates/hosts.suse.tmpl
+++ b/templates/hosts.suse.tmpl
@@ -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
+
diff --git a/templates/resolv.conf.tmpl b/templates/resolv.conf.tmpl
index b7e97b13..6f908f30 100644
--- a/templates/resolv.conf.tmpl
+++ b/templates/resolv.conf.tmpl
@@ -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 %}
diff --git a/templates/sources.list.debian.tmpl b/templates/sources.list.debian.tmpl
index 609bc6bd..c8043f76 100644
--- a/templates/sources.list.debian.tmpl
+++ b/templates/sources.list.debian.tmpl
@@ -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
+-#}
diff --git a/templates/sources.list.ubuntu.tmpl b/templates/sources.list.ubuntu.tmpl
index ce395b3d..4b1b019a 100644
--- a/templates/sources.list.ubuntu.tmpl
+++ b/templates/sources.list.ubuntu.tmpl
@@ -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