#!/usr/bin/env python3
-
import json
import pathlib
+"""
+This is an ugly hack.
+
+Puppet exported resources are very nice to generate monitoring configuration
+along with your Puppet resources. As you define something like an Apache
+virtual host, you can create a Nagios service check for it.
+
+But this requires a PuppetDB, and does not play nice with having no central
+Puppet infra.
+
+With its sibling script up.py, this script takes the JSON files generated by
+that, and manipulates them. This script moves Nagios resources to a specific
+host and does ugly trickery to fool Puppet into accepting that.
+
+This is like exported resources, but you don't need to declare a resource as
+exported.
+"""
+
+
def load_json(path):
with open(path) as f:
return json.load(f)
+
def save_json(r, path):
with open(path, "w") as f:
json.dump(r, f)
+
nagios_catalog_file = pathlib.Path("build/puppet/build/output/nagios.h1.int.pdp7.net/catalog.json")
if nagios_catalog_file.exists():
nagios_resources = []
nagios_edge_targets = []
+
def is_nagios_resource(r):
return r["type"].startswith("Nagios")
def is_nagios_edge(e):
return e["target"].startswith("Nagios")
+
for catalog_file in catalog_files:
if catalog_file == nagios_catalog_file:
continue
nagios_edge_targets += [e["target"] for e in catalog["edges"] if is_nagios_edge(e)]
catalog["edges"] = [e for e in catalog["edges"] if not is_nagios_edge(e)]
save_json(catalog, catalog_file)
-
+
if nagios_catalog_file.exists():
nagios_contact_position = nagios_catalog["resources"].index(nagios_contact)
import textwrap
import yaml
+
"""
+This script performs Puppet catalog compilation without a central server.
+
+It receives the following arguments:
+
+* directory: a working directory. The script expects to find some data, like
+ variables to use in the compilation process, facts, etc. The script also
+ generates intermediate files and output there.
+
+* modulepath: path to your modules directory
+* manifest: path to your site directory
+* host: the hosts to compile catalogs to
+
+The script expects the following content on the working directory:
+
directory/
global_vars/*.json: these JSON files will be available to all hosts
host_vars/{host}/*.json: these JSON files will be available in each host
facts/{host}.json: output from "facter -y" for each host
+And produces the following files:
+
directory/
output/
{host}/
- catalog.json
- modules/
+ catalog.json: the compiled catalog for the host
+ modules: a copy of the module directory
+
+Just ship the {host} directory to each host and run:
+
+$ puppet apply --catalog .../catalog.json --modulepath=.../modules/
+
+Check the apply_catalog Ansible role for example usage.
+
+As we have the catalogs, we can manipulate them. See
+pseudo_resource_exporter.py for an example hack. We can simulate exported
+resources without PuppetDB.
"""