Skip to content
This repository was archived by the owner on Oct 30, 2018. It is now read-only.

Commit 9b2f9b8

Browse files
author
Daniele Sluijters
committed
Initial commit.
When passed one or more resource types those resources are rendered through templates. The templates are loaded based on a template matching the exact resource type name with a .jinja2 extension or an optionally passed template.
0 parents  commit 9b2f9b8

File tree

1 file changed

+50
-0
lines changed

1 file changed

+50
-0
lines changed

puppetdb-stencil.py

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
#!/usr/bin/env python
2+
from __future__ import print_function
3+
from __future__ import unicode_literals
4+
5+
import argparse
6+
import codecs
7+
import logging
8+
import pypuppetdb
9+
import jinja2
10+
11+
log = logging.getLogger('puppetdb-stencil')
12+
13+
METAPARAMS = ('require', 'before', 'subscribe', 'notify', 'audit', 'loglevel',
14+
'noop', 'schedule', 'stage', 'alias', 'tag')
15+
16+
# Allow templates from anywhere on the filesystem
17+
loader = jinja2.FileSystemLoader(['.', '/'])
18+
environment = jinja2.Environment(trim_blocks=True, lstrip_blocks=True,
19+
loader=loader,
20+
extensions=['jinja2.ext.with_', 'jinja2.ext.loopcontrols'])
21+
22+
def render_resources(db, resource_type, template_names):
23+
resources = db.resources(resource_type)
24+
try:
25+
template = environment.select_template(templates)
26+
except jinja2.TemplatesNotFound:
27+
log.error('No template found for {0}'.format(resource_type))
28+
else:
29+
return template.render(resource_type=resource_type,
30+
resources=resources, metaparams=METAPARAMS)
31+
32+
33+
if __name__ == '__main__':
34+
parser = argparse.ArgumentParser(prog='puppetdb-stencil')
35+
parser.add_argument('resource_types', metavar='RESOURCE_TYPE', nargs='+')
36+
parser.add_argument('--templates', '-t', metavar='TEMPLATE', nargs='*')
37+
parser.add_argument('--debug', '-d', action='store_true')
38+
parser.add_argument('--host', '-H', default='localhost')
39+
parser.add_argument('--port', '-p', default='8080')
40+
41+
args = parser.parse_args()
42+
logging.basicConfig(level=logging.DEBUG if args.debug else logging.WARN)
43+
44+
db = pypuppetdb.connect(host=args.host, port=args.port)
45+
46+
for resource_type in args.resource_types:
47+
templates = ['{0}.jinja2'.format(resource_type)]
48+
if args.templates:
49+
templates += args.templates
50+
print(render_resources(db, resource_type, templates))

0 commit comments

Comments
 (0)