-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconfigure.py
More file actions
executable file
·314 lines (214 loc) · 9.41 KB
/
configure.py
File metadata and controls
executable file
·314 lines (214 loc) · 9.41 KB
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
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
#!/usr/bin/env python3
'''
Read from a config file or a directory of config files and use
values parsed from the config file to render data into a single
Jinja template or a directory of Jinja templates
'''
import os
import argparse
from configparser import ConfigParser, ExtendedInterpolation
import jinja2
import json
###############################################################################
###############################################################################
##
## Arguments - allows user to call from command line
##
###############################################################################
###############################################################################
class Args(object):
parser = argparse.ArgumentParser(description='Pipeline deployment utility')
parser.add_argument(
'--config', '--config-file', '-c',
dest='config',
nargs=1,
help='Path to a the config file or directory containing multiple config files'
)
def print_help(self):
self.parser.print_help()
exit(1)
return
def parse(self):
args = self.parser.parse_args()
return args
###########################################################################
###########################################################################
##
## lambda handler
##
###########################################################################
###########################################################################
class ConfigBuilder(object):
def __init__(self, config=None):
self.exit_code = 0
if config is not None:
self.config = config
##########################################################################
##########################################################################
##
## Parse
##
##########################################################################
##########################################################################
def parse_config(self):
path = self.config
config_list = []
if os.path.isfile(path):
print('[+] Using config file {}'.format(path))
config_list.append(path)
elif os.path.isdir(path):
print('[+] Using config file path {}'.format(path))
for root, dirs, files in os.walk(path, topdown=False):
for name in files:
print('[+] Found config file {}'.format(os.path.join(path, name)))
##
## handle multi-config here
##
config_list.append(os.path.join(path, name))
for c in sorted(config_list):
self.build(config=c)
return
##########################################################################
##########################################################################
##
## Build
##
##########################################################################
##########################################################################
def build(self, config=None):
if config is not None:
##
## option names of interest found in config file
##
paths_section_name = 'CONFIG_PATHS'
params_section_name = 'CONFIG_PARAMS'
##
## option names within CONFIG_PATHS
##
param_path_option_name = 'ParameterPath'
output_option_name = 'OutputPath'
##
## start parsing config
##
conf = ConfigParser(interpolation=ExtendedInterpolation())
conf.optionxform = str
conf.read(config)
working_dir = os.getcwd()
out_path = None
if paths_section_name in conf.sections():
if param_path_option_name in conf[paths_section_name]:
param_file_list = []
try:
s = conf[paths_section_name][param_path_option_name]
print(s)
path_list = json.loads(s)
except Exception as e:
print('[-] ParameterPath config option must be a list: {}'.format(e))
exit(1)
##
## set output path if it's in the config ...
## otherwise skip it
##
if output_option_name in conf[paths_section_name]:
out_path = conf[paths_section_name][output_option_name]
for path in path_list:
path = os.path.join(working_dir, path)
if os.path.isfile(path):
print('[+] Using param file {}'.format(path))
param_file_list.append(path)
elif os.path.isdir(path):
print('[+] Using param file path {}'.format(path))
for root, dirs, files in os.walk(path, topdown=False):
for name in files:
print('[+] Found config file {}'.format(os.path.join(path, name)))
##
## handle multi-config here
##
param_file_list.append(os.path.join(path, name))
for p in param_file_list:
print('[+] Using parameter file {}'.format(p))
##
## now loop through all of the params in the config
##
kwargs = {}
try:
for o in conf[params_section_name]:
##
## strip leading and traling quotes
##
s = conf[params_section_name][o].lstrip('\"')
s = s.rstrip('\"')
kwargs[o] = s
with open(p) as file_:
print('[+] Reading jinja template {}'.format(p))
template = jinja2.Template(file_.read())
print('[+] Rendering jinja template with **kwargs\n{}'.format(kwargs))
template_data = template.render(**kwargs)
if out_path is not None:
if not os.path.exists(out_path):
os.makedirs(out_path)
basename = os.path.basename(p)
param_out_file = os.path.join(out_path, basename)
else:
param_out_file = p
print('[+] Writing template output to {}'.format(param_out_file))
with open(param_out_file, 'wb+') as f:
f.write(b'%b'%template_data.encode())
except Exception as e:
print('[-] Jinja2 render exception: {}'.format(e))
###########################################################################
###########################################################################
##
## lambda handler
##
###########################################################################
###########################################################################
def lambda_handler(event, context):
##
## Lambda handler
##
path = event['config']
builder = ConfigBuilder(config=event['config'])
builder.parse_config()
return
###########################################################################
###########################################################################
##
## MAIN - install boto3 and AWS CLI to test locally
##
###########################################################################
###########################################################################
if __name__ == '__main__':
##########################################################################
##########################################################################
##
## parse arguments
##
##########################################################################
##########################################################################
a = Args()
args = a.parse()
config = args.config[0] if args.config else None
##########################################################################
##########################################################################
##
## exit on missing flags
##
##########################################################################
##########################################################################
if config is None:
print('[-] Requires a config file via --config argument')
a.print_help()
exit(1)
##########################################################################
##########################################################################
##
## call the handler
##
##########################################################################
##########################################################################
context = None
event = {
'config' : config
}
lambda_handler(event, context)