This repository has been archived by the owner on Oct 30, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathpuppetdb_stencil.py
66 lines (52 loc) · 2.19 KB
/
puppetdb_stencil.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
#!/usr/bin/env python
"""
puppetdb-stencil is a tool to render puppet resources using templates.
"""
from __future__ import print_function
from __future__ import unicode_literals
import argparse
import logging
import pypuppetdb
import jinja2
LOG = logging.getLogger('puppetdb_stencil')
METAPARAMS = ('require', 'before', 'subscribe', 'notify', 'audit', 'loglevel',
'noop', 'schedule', 'stage', 'alias', 'tag')
# Allow templates from anywhere on the filesystem
LOADER = jinja2.FileSystemLoader(['.', '/'])
EXTENSIONS = ['jinja2.ext.with_', 'jinja2.ext.loopcontrols']
ENVIRONMENT = jinja2.Environment(trim_blocks=True, lstrip_blocks=True,
loader=LOADER, extensions=EXTENSIONS)
def render_resources(database, resource_type, template_names):
"""
Render resources of the given type. They are queried from the given
database and rendered using the first template from template_names that can
be loaded.
"""
resources = database.resources(resource_type)
try:
template = ENVIRONMENT.select_template(template_names)
except jinja2.TemplatesNotFound:
LOG.error('No template found for {0}'.format(resource_type))
else:
return template.render(resource_type=resource_type,
resources=resources, metaparams=METAPARAMS)
def main():
"""
Main function
"""
parser = argparse.ArgumentParser(prog='puppetdb_stencil')
parser.add_argument('resource_types', metavar='RESOURCE_TYPE', nargs='+')
parser.add_argument('--templates', '-t', metavar='TEMPLATE', nargs='*')
parser.add_argument('--debug', '-d', action='store_true')
parser.add_argument('--host', '-H', default='localhost')
parser.add_argument('--port', '-p', default='8080')
args = parser.parse_args()
logging.basicConfig(level=logging.DEBUG if args.debug else logging.WARN)
database = pypuppetdb.connect(host=args.host, port=args.port)
for resource_type in args.resource_types:
templates = ['{0}.jinja2'.format(resource_type)]
if args.templates:
templates += args.templates
print(render_resources(database, resource_type, templates))
if __name__ == '__main__':
main()