forked from SaltaIT/python-piman
-
Notifications
You must be signed in to change notification settings - Fork 1
/
hieragen.py
192 lines (155 loc) · 6.46 KB
/
hieragen.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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
#!/usr/bin/env python
from __future__ import print_function
"""
hiera.yaml generator
"""
import os
import sys
import json
import argparse
import collections
from configparser import RawConfigParser
debug = False
write_to = sys.stdout
def eprint(*args, **kwargs):
global debug
if debug:
print(*args, file=sys.stderr, **kwargs)
def mkdir_gitkeep(dirname):
os.makedirs(name=dirname, exist_ok=True)
gitkeep = open(dirname+"/.gitkeep","w+")
gitkeep.close()
def print_hierarchy_item(item_name, globs, auth_string, write_to=sys.stdout):
print(' - name: "'+item_name+'"', file=write_to)
print(' globs:', file=write_to)
for glob in globs:
print(' - "' + auth_string + glob+'/*.yaml"', file=write_to)
print(' - "' + auth_string + glob+'.yaml"', file=write_to)
print('', file=write_to)
def generatehieradataskel(config_file, hieradata_base_dir='', create_skel_auth_strings=[]):
global debug
config = RawConfigParser()
config.read(config_file)
try:
debug = config.getboolean('hieragen', 'debug')
except:
debug = False
try:
unauth_common_area = config.get('hieragen','unauth-common-area')
except:
unauth_common_area = True
if unauth_common_area:
mkdir_gitkeep(hieradata_base_dir+'/common')
for project_id in create_skel_auth_strings:
if debug:
eprint("SKEL for "+project_id+": "+hieradata_base_dir+'/'+project_id)
for dir_name in [ '/env', '/hierarchy', '/type', '/servergroup', '/node', '/config-catalog' ]:
mkdir_gitkeep(hieradata_base_dir+'/'+project_id+'/'+dir_name)
def writehierayaml(write_to=sys.stdout, auth_string='', hierarchy={}, include_override=True, unauth_common_area=True, puppet_agent_common_area=True):
print('---', file=write_to)
print('version: 5', file=write_to)
print('', file=write_to)
print('defaults:', file=write_to)
print(' datadir: hieradata', file=write_to)
print(' data_hash: yaml_data', file=write_to)
print('', file=write_to)
print('hierarchy:', file=write_to)
if include_override:
print(' - name: "override"', file=write_to)
print(' globs:', file=write_to)
print(' - "override/*.yaml"', file=write_to)
print(' - "override.yaml"', file=write_to)
print('', file=write_to)
for key, value in hierarchy.items():
print_hierarchy_item(key, value, auth_string, write_to)
if unauth_common_area:
print(' - name: "common os release"', file=write_to)
print(' globs:', file=write_to)
print(' - "common-%{::osfamily}-%{::operatingsystemmajrelease}/*.yaml"', file=write_to)
print(' - "common-%{::osfamily}-%{::operatingsystemmajrelease}.yaml"', file=write_to)
print('', file=write_to)
print(' - name: "common os family"', file=write_to)
print(' globs:', file=write_to)
print(' - "common-%{::osfamily}/*.yaml"', file=write_to)
print(' - "common-%{::osfamily}.yaml"', file=write_to)
print('', file=write_to)
print(' - name: "common"', file=write_to)
print(' globs:', file=write_to)
print(' - "common/*.yaml"', file=write_to)
print(' - "common.yaml"', file=write_to)
if puppet_agent_common_area:
print('', file=write_to)
print(' - name: "puppet agent config"', file=write_to)
print(' globs:', file=write_to)
print(' - "puppet-agent-config/*.yaml"', file=write_to)
print(' - "puppet-agent-config.yaml"', file=write_to)
def generatehierayaml(config_file, write_hierayaml_to=sys.stdout, hieradata_base_dir='', puppet_fqdn='', puppet_port=None, create_skel_auth_strings=[]):
global debug, write_to
write_to=write_hierayaml_to
config = RawConfigParser()
config.read(config_file)
try:
auth_facts = json.loads(config.get('hieragen','auth-facts'))
except:
auth_facts = []
try:
auth_facts_separator = json.loads(config.get('hieragen','auth-facts-separator'))
except:
auth_facts_separator = '_'
try:
include_override = config.get('hieragen','include-override')
except:
include_override = True
try:
unauth_common_area = config.get('hieragen','unauth-common-area')
except:
unauth_common_area = True
try:
puppet_agent_common_area = config.get('hieragen','puppet-agent-common-area')
except:
puppet_agent_common_area = True
try:
debug = config.getboolean('hieragen', 'debug')
except:
debug = False
if debug:
eprint('auth_facts:'+str(auth_facts))
formated_auth_facts = []
for fact in auth_facts:
formated_auth_facts.append("%{::"+fact+"}")
auth_string = auth_facts_separator.join(formated_auth_facts)+'/'
if debug:
eprint('auth_string:'+auth_string)
if debug and hieradata_base_dir:
eprint('hieradata_base_dir: '+str(hieradata_base_dir))
hierarchy = collections.OrderedDict()
for instance in config.sections():
if instance!='hieragen':
globs = []
for (item, value) in config.items(instance):
if value!='0':
glob=item+':'+value
else:
glob=item
globs.append(glob)
hierarchy[instance]=globs
writehierayaml(write_to, auth_string, hierarchy, include_override, unauth_common_area, puppet_agent_common_area)
if hieradata_base_dir:
mkdir_gitkeep(hieradata_base_dir)
if puppet_agent_common_area and puppet_fqdn:
if not os.path.isfile(hieradata_base_dir+"/puppet-agent-config.yaml"):
puppet_agent_config = open(hieradata_base_dir+"/puppet-agent-config.yaml","w+")
puppet_agent_config.write("---\n")
puppet_agent_config.write("classes:\n")
puppet_agent_config.write(" - puppet::agent\n")
puppet_agent_config.write("puppet::agent::puppetmaster: "+puppet_fqdn+"\n")
if puppet_port:
puppet_agent_config.write("puppet::agent::puppetmasterport: "+str(puppet_port)+"\n")
puppet_agent_config.close()
generatehieradataskel(config_file, hieradata_base_dir, create_skel_auth_strings)
if __name__ == '__main__':
try:
config_file = sys.argv[1]
except IndexError:
config_file = './hieragen.config'
generatehierayaml(config_file=config_file)