drive-ops/global/overlay/etc/puppet/cosmos_enc.py

38 lines
842 B
Python
Raw Normal View History

2019-01-15 12:18:22 +00:00
#!/usr/bin/env python3
#
# Puppet 'External Node Classifier' to tell puppet what classes to apply to this node.
#
# Docs: https://puppet.com/docs/puppet/5.3/nodes_external.html
#
2013-09-02 14:01:50 +00:00
import os
import re
import sys
import yaml
rules_path = os.environ.get("COSMOS_RULES_PATH", "/etc/puppet")
2013-09-02 14:01:50 +00:00
node_name = sys.argv[1]
rules = dict()
for p in rules_path.split(":"):
rules_file = os.path.join(p, "cosmos-rules.yaml")
if os.path.exists(rules_file):
with open(rules_file) as fd:
rules.update(yaml.safe_load(fd))
found = False
classes = dict()
for reg, cls in rules.items():
if re.search(reg, node_name):
classes.update(cls)
found = True
2014-02-22 20:43:18 +00:00
if not found:
sys.stderr.write(f"{sys.argv[0]}: {node_name} not found in cosmos-rules.yaml\n")
2013-09-02 14:01:50 +00:00
print("---\n" + yaml.dump(dict(classes=classes)))
2013-09-02 14:01:50 +00:00
sys.exit(0)