-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.py
67 lines (45 loc) · 1.38 KB
/
script.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
import os
cfgs = {}
def is_fluent_bit_conf(key, idcs):
if len(idcs) < 2:
return False
if key[:idcs[1]] == 'FLUENT_BIT_CFG':
return True
else:
return False
def get_config_name_val(key, val, idcs):
formatted_name = ''
for x in range(3, len(idcs)):
end_i = idcs[x + 1] if len(idcs) > x + 1 else len(key)
if (x > 3):
formatted_name += '_'
formatted_name += key[idcs[x] + 1]
for j in range(idcs[x] + 2, end_i):
formatted_name += key[j].lower()
return { formatted_name: val }
def merge_or_add(_id, _with, header):
if _id not in cfgs:
cfgs[_id] = { }
if header not in cfgs[_id]:
cfgs[_id] |= { header: {} }
cfgs[_id][header] |= _with
def render():
_str = ""
for cfg_id, cfg_val in cfgs.items():
for header, settings in cfg_val.items():
_str += "[" + header + "]\n"
for set_k, set_v in settings.items():
_str += set_k + " " + set_v + "\n"
return _str
for key, value in os.environ.items():
idcs = [pos for pos, char in enumerate(key) if char == '_']
if is_fluent_bit_conf(key, idcs):
header = key[idcs[1] + 1:idcs[2]]
_id = key[idcs[2] + 1:idcs[3]]
config_val = get_config_name_val(key, value, idcs)
merge_or_add(_id, config_val, header)
rendered = render()
cfg_file = open("/fluent-bit/etc/fluent-bit.conf", "w+")
cfg_file.write(rendered)
cfg_file.close()
print(rendered)