Merge pull request #1 from puppetlabs/mikkergp_add_json_endpoint

Adding index json document so this can be hit like an API, and used b…
This commit is contained in:
Daniel Parks 2017-12-05 16:17:47 -08:00 committed by GitHub
commit af5e354010
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 13 additions and 2 deletions

1
.gitignore vendored
View file

@ -4,3 +4,4 @@ __pycache__
/dist/ /dist/
/*.egg-info /*.egg-info
/output /output
bin/

View file

@ -3,3 +3,7 @@ SRE Inventory Report
Generate a report on SRE inventory, including hosts, roles, and services. Generate a report on SRE inventory, including hosts, roles, and services.
Developing
==========
Use `python setup.py develop` to install dependencies

View file

@ -4,6 +4,7 @@ import click
import csv import csv
from datetime import datetime from datetime import datetime
import jinja2 import jinja2
import json
import logging import logging
import os import os
import markdown2 import markdown2
@ -90,6 +91,8 @@ def output_html(inventory, directory):
for node in nodes: for node in nodes:
csv_writer.writerow([cell.body_csv(node) for cell in all_columns]) csv_writer.writerow([cell.body_csv(node) for cell in all_columns])
write_json(nodes, directory, "index")
for node in nodes: for node in nodes:
path = "{}/nodes/{}.html".format(directory, node["certname"]) path = "{}/nodes/{}.html".format(directory, node["certname"])
with open(path, "w", encoding="utf-8") as html: with open(path, "w", encoding="utf-8") as html:
@ -127,7 +130,6 @@ def output_html(inventory, directory):
generation_time=generation_time, generation_time=generation_time,
service=service)) service=service))
def render_template(template_name, **kwargs): def render_template(template_name, **kwargs):
data_path = os.path.dirname(os.path.abspath(__file__)) data_path = os.path.dirname(os.path.abspath(__file__))
environment = jinja2.Environment( environment = jinja2.Environment(
@ -174,6 +176,11 @@ def set_up_logging(level=logging.WARNING):
logging.getLogger("paramiko").setLevel(logging.FATAL) logging.getLogger("paramiko").setLevel(logging.FATAL)
def write_json(nodes, directory, filename):
path = "{}/nodes/{}.json".format(directory, filename)
with open(path, "w", encoding="utf-8") as json_out:
json_out.write(json.dumps(nodes))
@click.command() @click.command()
@click.option("--output", "-o", required=True, metavar="PATH", help="Directory to put report in. WARNING: this directory will be removed if it already exists.") @click.option("--output", "-o", required=True, metavar="PATH", help="Directory to put report in. WARNING: this directory will be removed if it already exists.")
@click.option("--host", "-h", default="localhost", metavar="HOST", help="PuppetDB host to query") @click.option("--host", "-h", default="localhost", metavar="HOST", help="PuppetDB host to query")
@ -182,7 +189,6 @@ def set_up_logging(level=logging.WARNING):
@click.version_option() @click.version_option()
def main(host, output, verbose, debug): def main(host, output, verbose, debug):
"""Generate SRE inventory report""" """Generate SRE inventory report"""
if debug: if debug:
set_up_logging(logging.DEBUG) set_up_logging(logging.DEBUG)
elif verbose: elif verbose:

0
setup.py Normal file → Executable file
View file