-
Notifications
You must be signed in to change notification settings - Fork 9
/
main.py
166 lines (131 loc) · 4.56 KB
/
main.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
import docker
import re
import os
from netaddr import valid_ipv4
LABEL = 'ru.grachevko.dhu'
MARKER = '#### DOCKER HOSTS UPDATER ####'
HOSTS_PATH = '/opt/hosts'
CONTAINER_HOSTNAME_DISABLED = bool(os.getenv('CONTAINER_HOSTNAME_DISABLED', False))
CONTAINER_NAME_DISABLED = bool(os.getenv('CONTAINER_NAME_DISABLED', False))
def listen():
for event in docker.events(decode=True):
if 'container' == event.get('Type') and event.get('Action') in ["start", "stop", "die"]:
handle()
def scan():
containers = []
for container in docker.containers.list():
label = container.attrs.get('Config').get('Labels').get(LABEL)
if not label:
continue
for string in label.split(';'):
priority = 0
lb = container
ip = False
if ':' in string:
parts = string.split(':')
string = parts[0]
priority = int(parts[1]) if len(parts) >= 2 else priority
if len(parts) == 3:
lbString = parts[2]
if valid_ipv4(lbString):
ip = lbString
else:
lb = docker.containers.get(lbString)
if ip == False:
ip = next(iter(lb.attrs.get('NetworkSettings').get('Networks').values())).get('IPAddress')
hosts = string_to_array(string)
if not CONTAINER_HOSTNAME_DISABLED:
hosts.append(container.attrs.get('Config').get('Hostname'))
if not CONTAINER_NAME_DISABLED:
hosts.append(container.name)
if ip:
containers.append({
'ip': ip,
'priority': priority,
'hosts': hosts,
'createdAt': container.attrs.get('Created'),
})
return containers
def string_to_array(input_string):
dd = [(rec.group().replace("{", "").replace("}", "").split(","), rec.span()) for rec in
re.finditer("{[^}]*}", input_string)]
texts = []
if len(dd) != 0:
for i in range(len(dd)):
if i == 0:
if dd[0][1][0] == 0:
texts.append("")
else:
texts.append(input_string[0:dd[0][1][0]])
else:
texts.append(input_string[dd[i - 1][1][1]:dd[i][1][0]])
if i == len(dd) - 1:
texts.append(input_string[dd[-1][1][1]:])
else:
texts = [input_string]
if len(dd) > 0:
idxs = [0] * len(dd)
summary = []
while idxs[0] != len(dd[0][0]):
summary_string = ""
for i in range(len(idxs)):
summary_string += texts[i] + dd[i][0][idxs[i]]
summary_string += texts[-1]
summary.append(summary_string)
for j in range(len(idxs) - 1, -1, -1):
if j == len(idxs) - 1:
idxs[j] += 1
if j > 0 and idxs[j] == len(dd[j][0]):
idxs[j] = 0
idxs[j - 1] += 1
else:
summary = texts
return summary
def update(items):
f = open(HOSTS_PATH, 'r+')
lines = []
skip_lines = False
for line in f.read().split('\n'):
if line == MARKER:
skip_lines = not skip_lines
continue
if not skip_lines:
lines.append(line)
if items:
lines.append(MARKER)
for ip, value in items.items():
line = '{} {}'.format(ip, ' '.join(value))
lines.append(line)
print(line)
lines.append(MARKER)
summary = '\n'.join(lines)
f.seek(0)
f.truncate()
f.write(summary)
f.close()
def handle():
print('Recompiling...')
items = scan()
map_dict = {}
for item in items:
for host in item.get('hosts'):
if host in map_dict:
priority_left = map_dict[host].get('priority')
priority_right = item.get('priority')
if priority_left > priority_right:
continue
if priority_left == priority_right and map_dict[host].get('createdAt') < item.get('createdAt'):
continue
map_dict[host] = item
summary = {}
for item in items:
ip = item.get('ip')
for host in item.get('hosts'):
if map_dict[host].get('ip') == ip:
if ip not in summary:
summary[ip] = []
summary[ip].append(host)
update(summary)
docker = docker.from_env()
handle()
listen()