Last active
August 29, 2015 13:56
-
-
Save ptomulik/8999349 to your computer and use it in GitHub Desktop.
Simple node clasifier for Puppet written in python.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#! /usr/bin/env python | |
# Python ENC | |
# receives fqdn as argument | |
import yaml | |
import sys | |
enc = { | |
'classes' : { 'base' : {} }, | |
'environment' : 'production', | |
'parameters' : { } | |
} | |
yaml.dump(enc, explicit_start = True, stream = sys.stdout) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#! /usr/bin/env python | |
# Python ENC | |
# receives fqdn as argument | |
import yaml | |
import sys | |
import re | |
try: | |
hostname = sys.argv[1].lower() | |
except IndexError: | |
# need a hostname | |
sys.exit(10) | |
m = re.match(r'^([a-z]+)-([a-z]+)-([a-z])([a-z])([0-9]+)$', hostname) | |
if m is None: | |
enc = { | |
'classes' : { | |
'base' : {}, | |
'hostname_problem' : { 'enc_hostname' : hostname } | |
} | |
} | |
else: | |
role, location, os, environment, instance = m.groups() | |
enc = { | |
'classes' : { | |
'base' : {}, | |
'role' : {} | |
}, | |
'environment' : { | |
# map environment from hostname into environment | |
'p' : 'production', | |
'n' : 'nonprod', | |
'd' : 'devel', | |
's' : 'sbx' | |
}.get(environment, 'undef'), | |
# set top scope variables | |
'parameters' : { | |
'enc_hostname' : hostname, | |
'role' : role, | |
'location' : location, | |
'os' : os, | |
'instance' : instance | |
} | |
} | |
yaml.dump(enc, explicit_start = True, stream = sys.stdout) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment