views:

51

answers:

1

I am trying to template-ize my Apache httpd configuration for deployment to different environments and I would like to use the Python language Cheetah application to do so. However, I am having difficulty with the command line cheetah program and I believe its a combination of my misunderstanding Cheetah along with a lack of documentation.

My goal is to have a single httpd.conf template file and substitute variables from an environment specific definition file.

httpd.tmpl:

Listen $HTTP_PORT
...
#if $ENABLE_HTTPS == true
<Virtual Host *:$HTTPS_PORT>
    ...
</VirtualHost>
#end if

production.env:

HTTP_PORT=34120
HTTPS_PORT=34121
ENABLE_HTTPS=true

What is the command line needed to fill this Cheetah template? I've used:

cheetah f --oext conf --debug httpd

But obviously prod.env is not read as an input file. Adding an #include to the top of my template file:

#include "prod.env"

And none of my names are found:

Cheetah.NameMapper.NotFound: cannot find 'APACHE_PORT'

This is not the ideal situation anyway, because I want the ability to specify the name/value mapping file on the command line for each invocation of cheetah.

Thanks!

EDIT: I am aware that I can write a python script to perform the file reading and then substitute using the Cheetah API. However, I'm looking for a way to use the command line to fill my template.


SOLVED

Thanks to the documentation link provided by @pyfunc I now understand how to accomplish this. The main issue is to supply --env on the cheetah command line which passes the current environment variables to cheetah. These environment variables must be exported first however.

A: 

Cheetah wraps each chunk of #include text inside a nested Template object.

Use

#include raw "prod.env"

also

#set global $HTTP_PORT="34120"

To include different env files, you will have too templatize that too.

Please look at the following for examples that should help you.

pyfunc
Thanks for posting. How would you address substituting the environment filename ("prod.env") for different environments? I want to avoid hardcoding values in the templates.
purecharger
I have edited my answer. The above link demonstrates and can help youdo that.
pyfunc
Thanks for the pointer! So the following produces the expected output:`APACHE_PORT=10240 ENABLE_HTTPS=true cheetah f --env --oext conf httpd`However, setting these environment variables in a separate step does NOT produce the desired output: $ source prod.env $ set | egrep "(APACHE|HTTPS)" APACHE_PORT=10800 ENABLE_HTTPS=true $ cheetah f --env --oext conf httpd ... Cheetah.NameMapper.NotFound: cannot find 'APACHE_PORT'Any ideas?EDIT: arrg! comments don't allow full formatting, sorry!
purecharger