-
Notifications
You must be signed in to change notification settings - Fork 1
/
file-tree.py
executable file
·59 lines (48 loc) · 1.47 KB
/
file-tree.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
59
#!/usr/bin/env python
import ansigraph
import os
import yaml
import re
def parse_configuration():
directory = os.environ.get("INVENTORY_DIRECTORY", None)
errors = []
if not directory:
errors.append("Missing INVENTORY_DIRECTORY in environment")
if errors:
raise RuntimeError("\n".join(errors))
return dict(
directory=directory
)
def get_files(dir, files=[]):
for name in os.listdir(dir):
path = os.path.join(dir, name)
if os.path.isfile(path):
files.append(path)
else:
get_files(path, files)
return files
def read_file(file):
with open(file, 'r') as stream:
try:
return yaml.load(stream)
except yaml.YAMLError as exc:
print(exc)
def parse_files(inventory, files):
# parse each file and add to inventory
for file in files:
content = read_file(file)
groups = file.split('/')
# parse groups from file names
for index, group in enumerate(groups[:-1]):
inventory.add_child_group(group, re.sub('(^.*).(yml|yaml)$', '\\1', groups[index+1]))
# parse hosts from inside files
for group in content.keys():
inventory.add_host(content[group], re.sub('(^.*).(yml|yaml)$', '\\1', groups[-1]))
inventory.add_host(content[group], group)
def main():
config = parse_configuration()
inventory = ansigraph.ansigraph()
parse_files(inventory, get_files(config['directory']))
print inventory.dump_json(inventory.get_hosts())
if __name__ == '__main__':
main()