2019-01-15 12:18:22 +00:00
|
|
|
#!/usr/bin/env python3
|
2023-02-07 13:21:29 +00:00
|
|
|
#
|
|
|
|
# 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
|
2023-02-07 13:21:29 +00:00
|
|
|
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]
|
|
|
|
|
2023-02-07 13:21:29 +00:00
|
|
|
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):
|
2024-04-12 13:32:40 +00:00
|
|
|
if cls:
|
|
|
|
classes.update(cls)
|
2023-02-07 13:21:29 +00:00
|
|
|
found = True
|
2014-02-22 20:43:18 +00:00
|
|
|
|
2023-02-07 13:21:29 +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
|
|
|
|
2023-02-07 13:21:29 +00:00
|
|
|
print("---\n" + yaml.dump(dict(classes=classes)))
|
2013-09-02 14:01:50 +00:00
|
|
|
|
2023-02-07 13:21:29 +00:00
|
|
|
sys.exit(0)
|