University Marketing & Communications

April 1, 2011

Buildout Recipes and Genshi

By Chris Heiland

This was originally posted on Chris Heiland’s staff blog while he was a member of the Web Team. Chris migrated this post to the Web Team blog before he left because we thought the content still had value to the UW community.


We had a problem where we needed two different varnish configuration files between our staging and production plone environments. Generating two files that do similar things is just too much work and unnecessary. However, there is a solution.

Utilizing collective.recipe.template to generate a configuration file is very easy and well documented. Using variables is a huge time-saver and is very effective when dealing with subtitle changes in files. However, for this problem, something more was necessary.

The major difference between staging and production is the number of instances running. Since we are using varnish to load balance it was necessary to specify all the instances to send requests to. This was a bit more complex than just specifying a number.

To auto-generate files with that much difference, we needed some sort of looping mechanism. Believe it or not, it’s possible, but only mentioned sparingly in the documentation:

Starting with version 1.7 you can use genshi text templates.

Now what does this mean? As of that version number we can use Genshi templating, specifically, the NewTextTemplate. Which means that generating whole sections of repeating code is super easy, as long as the syntax used is done right.

Example:

buildout.cfg

[varnish.vcl]
recipe = collective.recipe.template[genshi]:genshi
input = etc/varnish.vcl.tmpl
output = etc/varnish.vcl
#Note the numbers are crammed together
#Any formatting messes up the loop
instances = 1234

varnish.vcl.tmpl

{% for num in options['instances'] %}
backend instance${num} {
.host = "localhost";
.port = "808${num}";
...snip....
}
{% end %}

Now there is an assumption Genshi is installed in your python path that plone can access. Provided plone is installed in a virtualenv it’s a one liner:

bin/easy_install Genshi