38 lines
974 B
Python
38 lines
974 B
Python
|
#!/usr/bin/env python
|
||
|
|
||
|
from __future__ import print_function
|
||
|
|
||
|
import os, sys
|
||
|
from optparse import OptionParser
|
||
|
import yaml
|
||
|
from mako.template import Template
|
||
|
|
||
|
import pprint
|
||
|
|
||
|
def main():
|
||
|
# Arguments
|
||
|
parser = OptionParser()
|
||
|
|
||
|
parser.add_option('-c', '--config', dest='config', default='config.yml',
|
||
|
help='Configuration file', metavar="FILE")
|
||
|
parser.add_option('-t', '--template', dest='template', default='haproxy.cfg.mako',
|
||
|
help='haproxy.cfg Mako template')
|
||
|
|
||
|
|
||
|
(options, args) = parser.parse_args()
|
||
|
|
||
|
try:
|
||
|
config = yaml.load(open(options.config, 'r'))
|
||
|
except Exception as e:
|
||
|
print(e, file=sys.stderr)
|
||
|
config = None
|
||
|
|
||
|
if not config or type(config) != dict:
|
||
|
print('Unable to load configuration: "{}"'.format(options.config), file=sys.stderr)
|
||
|
sys.exit(-1)
|
||
|
|
||
|
print(Template(filename=options.template).render(config=config))
|
||
|
|
||
|
if __name__ == '__main__':
|
||
|
main()
|