From 43e37fdf6481e0f613043e491eae63d0a4e925a1 Mon Sep 17 00:00:00 2001 From: Corey Hammerton Date: Mon, 28 Dec 2015 16:07:43 -0500 Subject: [PATCH] Using new Node and Catalog fields available in pypuppetdb 0.2.1 Showing the Code ID field in the catalogs page. This is currently unused in PuppetDB as of 3.2.2 but may be useful when it will be used If available, using the latest_report_hash field of the node object in the index and nodes templates for the link to the latest report available for the node. Updating the report_latest function in app.py to query the nodes endpoint and redirecting using the latest_report_hash field if available. If not query the reports endpoint for the node's latest report. --- puppetboard/app.py | 42 +++++++++++++++++++++--------- puppetboard/templates/catalog.html | 2 ++ puppetboard/templates/index.html | 12 +++++++-- puppetboard/templates/nodes.html | 12 +++++++-- setup.py | 2 +- 5 files changed, 53 insertions(+), 17 deletions(-) diff --git a/puppetboard/app.py b/puppetboard/app.py index 9b216d2..113eaf8 100644 --- a/puppetboard/app.py +++ b/puppetboard/app.py @@ -517,25 +517,43 @@ def report_latest(env, node_name): check_env(env, envs) if env == '*': - query='["and",' \ - '["=", "certname", "{0}"],' \ - '["=", "latest_report?", true]]'.format(node_name) + node_query = '["=", "certname", "{0}"]'.format(node_name) else: - query='["and",' \ - '["=", "environment", "{0}"],' \ - '["=", "certname", "{1}"],' \ - '["=", "latest_report?", true]]'.format( - env, - node_name) + node_query = '["and",' \ + '["=", "report_environment", "{0}"],' \ + '["=", "certname", "{1}"]]'.format(env, node_name) - reports = get_or_abort(puppetdb.reports, query=query) try: - report = next(reports) + node = next(get_or_abort(puppetdb.nodes, + query=node_query, + with_status=True)) except StopIteration: abort(404) + if node.latest_report_hash is not None: + hash_ = node.latest_report_hash + else: + if env == '*': + query='["and",' \ + '["=", "certname", "{0}"],' \ + '["=", "latest_report?", true]]'.format(node.name) + else: + query='["and",' \ + '["=", "environment", "{0}"],' \ + '["=", "certname", "{1}"],' \ + '["=", "latest_report?", true]]'.format( + env, + node.name) + + reports = get_or_abort(puppetdb.reports, query=query) + try: + report = next(reports) + hash_ = report.hash_ + except StopIteration: + abort(404) + return redirect( - url_for('report', env=env, node_name=node_name, report_id=report.hash_)) + url_for('report', env=env, node_name=node_name, report_id=hash_)) @app.route('/report//', defaults={'env': app.config['DEFAULT_ENVIRONMENT']}) diff --git a/puppetboard/templates/catalog.html b/puppetboard/templates/catalog.html index 2a71894..33c4e21 100644 --- a/puppetboard/templates/catalog.html +++ b/puppetboard/templates/catalog.html @@ -10,6 +10,7 @@ Hostname Version Transaction UUID + Code ID @@ -17,6 +18,7 @@ {{catalog.node}} {{catalog.version}} {{catalog.transaction_uuid}} + {{catalog.code_id}} diff --git a/puppetboard/templates/index.html b/puppetboard/templates/index.html index 30aab3a..dcfe7be 100644 --- a/puppetboard/templates/index.html +++ b/puppetboard/templates/index.html @@ -79,14 +79,22 @@ {% if node.status != 'unchanged' %} - {{macros.status_counts(status=node.status, node_name=node.name, events=node.events, unreported_time=node.unreported_time, current_env=current_env)}} + {% if node.latest_report_hash %} + {{macros.status_counts(status=node.status, node_name=node.name, events=node.events, unreported_time=node.unreported_time, current_env=current_env, report_hash=node.latest_report_hash)}} + {% else %} + {{macros.status_counts(status=node.status, node_name=node.name, events=node.events, unreported_time=node.unreported_time, current_env=current_env)}} + {% endif %} {{ node.name }} {% if node.report_timestamp %} - {{ node.report_timestamp }} + {% if node.latest_report_hash %} + {{ node.report_timestamp }} + {% else %} + {{ node.report_timestamp }} + {% endif %} {% else %} {% endif %} diff --git a/puppetboard/templates/nodes.html b/puppetboard/templates/nodes.html index bcf715d..70249a5 100644 --- a/puppetboard/templates/nodes.html +++ b/puppetboard/templates/nodes.html @@ -18,13 +18,21 @@ {% for node in nodes %} - {{macros.status_counts(status=node.status, node_name=node.name, events=node.events, unreported_time=node.unreported_time, current_env=current_env)}} + {% if node.latest_report_hash %} + {{macros.status_counts(status=node.status, node_name=node.name, events=node.events, unreported_time=node.unreported_time, current_env=current_env, report_hash=node.latest_report_hash)}} + {% else %} + {{macros.status_counts(status=node.status, node_name=node.name, events=node.events, unreported_time=node.unreported_time, current_env=current_env)}} + {% endif %} {{node.name}} {{node.catalog_timestamp}} {% if node.report_timestamp %} - {{ node.report_timestamp }} + {% if node.latest_report_hash %} + {{ node.report_timestamp }} + {% else %} + {{ node.report_timestamp }} + {% endif %} {% else %} {% endif %} diff --git a/setup.py b/setup.py index 2f99e61..1ae3990 100644 --- a/setup.py +++ b/setup.py @@ -32,7 +32,7 @@ setup( "Flask >= 0.10.1", "Flask-WTF >= 0.9.4, <= 0.9.5", "WTForms < 2.0", - "pypuppetdb >= 0.2.0, < 0.3.0", + "pypuppetdb >= 0.2.1, < 0.3.0", ], keywords="puppet puppetdb puppetboard", classifiers=[