This repository has been archived by the owner on Apr 14, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
easyenc.py
executable file
·58 lines (49 loc) · 1.54 KB
/
easyenc.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
#!/usr/bin/env python
# vim: set fileencoding=utf-8 :
#
# Author : Mikael Löfstrand
# Home page : https://github.com/mld/easyenc/
# Inspired by : Basil Kurian <basilkurian[at]gmail[dot]com>
# http://wiki.unixh4cks.com/index.php/Simple_External_Node_Classifier(ENC)_for_puppet_in_python
import yaml
import sys
def main():
if len(sys.argv) > 2:
dbfile = sys.argv[1]
host = sys.argv[2]
elif len(sys.argv) > 1:
dbfile = "./db.yaml"
host = sys.argv[1]
else:
sys.stderr.write("Error - need to at least supply hostname!\n {0} [<dbfile>] <fqdn>\n".format(sys.argv[0]))
sys.stderr.flush()
sys.exit(1)
db = open(dbfile, "r")
doc = yaml.load(db)
environment = False
classes = False
parameters = False
if 'default' in doc:
if 'environment' in doc["default"]:
environment = doc["default"]["environment"]
if 'class' in doc["default"]:
classes = doc["default"]["class"]
if 'parameters' in doc["default"]:
parameters = doc["default"]["parameters"]
if host in doc:
if 'class' in doc[host]:
classes = doc[host]["class"]
if 'environment' in doc[host]:
environment = doc[host]["environment"]
if 'parameters' in doc[host]:
parameters = doc[host]["parameters"]
new_yaml = {}
if classes:
new_yaml['classes'] = classes
if environment:
new_yaml['environment'] = environment
if parameters:
new_yaml['parameters'] = parameters
print yaml.dump(new_yaml, explicit_start=True,default_flow_style=False)
if __name__ == '__main__':
main()