I ended up using a configuration template file and a small Python program using the string.Template
facility to generate two different configurations. I have included it below.
This substitutes the configuration values into generic.tmpl.conf
(which is the site internal configuration), then makes the result available as ${conf}
for use in test.tmpl.conf
and production.tmpl.conf
, which have the VirtualHost block containing ${conf}
, log files, and other such host-specific information.
#!/usr/bin/python
import string
from optparse import OptionParser
# ---
parser = OptionParser()
parser.add_option("--clean", dest="clean", action="store_true", default=False,
help="Clean files instead of creating them.")
(options, args) = parser.parse_args()
if len(args) != 0:
parser.error("No non-option arguments expected.")
# ---
def readconf(name):
return string.Template(open(name).read())
def writeconf(out, outer, inner, fields):
outerfields = fields.copy()
outerfields["conf"] = inner.substitute(fields).replace("\n", "\n\t")
out.write(outer.substitute(outerfields))
out.close()
# ---
if options.clean:
os.remove("production.conf")
os.remove("test.conf")
else:
commonconf = readconf("generic.tmpl.conf")
liveconf = readconf("production.tmpl.conf")
testconf = readconf("test.tmpl.conf")
writeconf(open("production.conf", "w"), liveconf, commonconf, {
# ... configuration values redacted ...
})
writeconf(open("test.conf", "w"), testconf, commonconf, {
# ... configuration values redacted ...
})