-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
284 lines (242 loc) · 10.4 KB
/
Copy pathmain.py
File metadata and controls
284 lines (242 loc) · 10.4 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
import os
import docker
from datetime import datetime
from notifiers import technitium_dns, service_tracker_dashboard
import threading
import time
from logging_setup import get_logger
import interpreter_loader
logger = get_logger("main")
# === Settings ===
logger.debug("main.py is running")
STD_REFRESH_SECONDS = int(os.environ.get("STD_REFRESH_SECONDS", "60")) # Default to 60 seconds
def _parse_bool_env(name, default=False):
raw = os.environ.get(name)
if raw is None:
return default
normalized = raw.strip().lower()
if normalized in ("true", "1", "yes"):
return True
if normalized in ("false", "0", "no", ""):
return False
logger.warning(
f"Unrecognized boolean value for {name}={raw!r}; treating as off"
)
return False
# When True, the STD notifier fires for every running container on this
# host regardless of whether the container has the
# `dockernotifier.notifiers=service-tracker-dashboard` opt-in label.
# Scope is intentionally limited to STD — other notifiers (DNS) still
# require explicit per-container opt-in.
STD_REPORT_ALL_CONTAINERS = _parse_bool_env("STD_REPORT_ALL_CONTAINERS")
if STD_REPORT_ALL_CONTAINERS:
logger.info(
"STD_REPORT_ALL_CONTAINERS is on — every running container on this host "
"will be reported to STD regardless of opt-in label"
)
# Whether the STD notifier has the env vars it needs. Checked once at
# startup so STD dispatch and the periodic refresh loop (which exists
# only to re-report containers to STD) can be skipped entirely when STD
# is not configured, instead of logging a "not enabled" line on every
# event. DNS-only deployments are the common case for this.
STD_CONFIGURED = service_tracker_dashboard.is_configured()
if not STD_CONFIGURED:
logger.info(
"STD_URL or STD_API_TOKEN is missing — disabling STD integration "
"(no STD reporting, no periodic refresh loop)"
)
# Debug-only: re-load interpreters on every event instead of once at
# startup. Not for production use; intended for iterating on YAML
# files without bouncing the notifier.
INTERPRETER_RELOAD_ON_EACH_EVENT = _parse_bool_env("INTERPRETER_RELOAD_ON_EACH_EVENT")
# Loaded once at startup. See `interpreter_loader.py`.
INTERPRETER_LOAD_RESULT = interpreter_loader.load_interpreters()
# Real Docker events the notifier subscribes to.
WATCHED_DOCKER_ACTIONS = frozenset({
"start", "stop", "die", "pause", "unpause",
"destroy", "kill", "update",
})
# Synthetic actions the notifier injects (not from Docker).
SYNTHETIC_ACTIONS = frozenset({"boot", "refresh"})
# Per-notifier action sets. Each notifier declares the actions it wants
# to be invoked for, drawn from WATCHED_DOCKER_ACTIONS and SYNTHETIC_ACTIONS.
NOTIFIER_TRIGGERS = {
"dns": {"boot", "start"},
"service-tracker-dashboard": WATCHED_DOCKER_ACTIONS | SYNTHETIC_ACTIONS,
}
def is_trigger_enabled(notifier, action):
return action in NOTIFIER_TRIGGERS.get(notifier, {"start"})
def periodic_update_loop(docker_host):
client = docker.from_env()
while True:
logger.debug(f"STD refresh loop — every {STD_REFRESH_SECONDS} sec")
for container in client.containers.list():
try:
handle_container_event(container, docker_host, action="refresh")
except Exception as e:
logger.error(f"Refresh failed for {container.name}: {e}")
time.sleep(STD_REFRESH_SECONDS)
def get_host_name():
override = os.environ.get("HOST_NAME_OVERRIDE")
if override and override.strip():
return override.strip()
try:
with open("/etc/host_hostname", "r") as f:
return f.read().strip()
except Exception:
return os.uname()[1]
def _extract_networks(container_attrs):
network_settings = container_attrs.get("NetworkSettings") or {}
networks_raw = network_settings.get("Networks") or {}
return [
{"name": name, "aliases": (data.get("Aliases") or []) if isinstance(data, dict) else []}
for name, data in networks_raw.items()
]
def _extract_exposed_ports(container_attrs):
config = container_attrs.get("Config") or {}
exposed = config.get("ExposedPorts") or {}
return list(exposed.keys())
def _extract_published_ports(container_attrs):
network_settings = container_attrs.get("NetworkSettings") or {}
ports_raw = network_settings.get("Ports") or {}
out = []
for port_key, bindings in ports_raw.items():
if not bindings:
continue
try:
container_port_str, protocol = port_key.split("/", 1)
container_port = int(container_port_str)
except (ValueError, AttributeError):
logger.debug(f"Skipping malformed port key {port_key!r}")
continue
for binding in bindings:
if not isinstance(binding, dict):
continue
host_port_raw = binding.get("HostPort")
try:
host_port = int(host_port_raw)
except (TypeError, ValueError):
logger.debug(
f"Skipping binding with non-integer HostPort={host_port_raw!r} for {port_key}"
)
continue
out.append({
"container_port": container_port,
"protocol": protocol,
"host_ip": binding.get("HostIp", "") or "",
"host_port": host_port,
})
return out
def handle_container_event(container, docker_host, action):
labels = container.attrs["Config"]["Labels"] or {}
notifier_list_raw = labels.get("dockernotifier.notifiers", "").strip()
notifier_list = [n.strip() for n in notifier_list_raw.split(",") if n.strip()]
std_via_label = "service-tracker-dashboard" in notifier_list
std_via_env = STD_REPORT_ALL_CONTAINERS
std_should_fire = (
STD_CONFIGURED
and (std_via_label or std_via_env)
and action in NOTIFIER_TRIGGERS["service-tracker-dashboard"]
)
dns_should_fire = (
"dns" in notifier_list and action in NOTIFIER_TRIGGERS["dns"]
)
if not std_should_fire and not dns_should_fire:
return
base_kwargs = {
"container_name": container.name,
"container_id": container.id,
"docker_host": docker_host,
"docker_status": container.attrs["State"]["Status"],
"image_name": container.attrs["Config"]["Image"],
"stack_name": labels.get("com.docker.compose.project"),
"started_at": container.attrs["State"]["StartedAt"],
"action": action,
"networks": _extract_networks(container.attrs),
"exposed_ports": _extract_exposed_ports(container.attrs),
"published_ports": _extract_published_ports(container.attrs),
}
logger.info(f"[MATCH] Container {action.upper()}: {container.name}")
if dns_should_fire:
container_hostname = labels.get("dockernotifier.dns.containerhostname")
zone_label = labels.get("dockernotifier.dns.containerzone")
docker_domain = labels.get("dockernotifier.dns.dockerdomain")
container_fqdn = (
f"{container_hostname}.{zone_label}"
if container_hostname and zone_label
else None
)
if container_fqdn and docker_domain and zone_label:
logger.info(f"DNS notifier triggered for {container.name} on {action}")
technitium_dns.register(
**base_kwargs,
container_fqdn=container_fqdn,
zone=zone_label,
value=f"{docker_host}.{docker_domain}",
)
else:
logger.warning(f"Missing DNS label info for {container.name}, skipping DNS registration")
if std_should_fire:
if std_via_env and not std_via_label:
logger.debug(
f"STD notifier firing for {container.name} via "
f"STD_REPORT_ALL_CONTAINERS (no opt-in label)"
)
logger.info(f"STD notifier triggered for {container.name} on {action}")
std_extras = {
key.replace("dockernotifier.std.", ""): value
for key, value in labels.items()
if key.startswith("dockernotifier.std.")
}
std_extras["exposure_observations"] = _run_interpreters(labels)
service_tracker_dashboard.register(**base_kwargs, **std_extras)
def _run_interpreters(labels):
"""
Run all loaded interpreters against a container's labels.
Returns:
- a list of ExposureObservation dicts (possibly empty) if any
interpreters were loaded successfully;
- None if no interpreters are loaded — STD treats null as
"no update; preserve existing exposure rows."
"""
global INTERPRETER_LOAD_RESULT
if INTERPRETER_RELOAD_ON_EACH_EVENT:
INTERPRETER_LOAD_RESULT = interpreter_loader.load_interpreters()
if not INTERPRETER_LOAD_RESULT.any_loaded:
return None
return interpreter_loader.evaluate(INTERPRETER_LOAD_RESULT.interpreters, labels)
def main():
client = docker.from_env()
docker_host = get_host_name()
logger.info(f"Starting Docker API Notifier on host: {docker_host}")
logger.info("Running boot-time scan of existing containers...")
for container in client.containers.list():
try:
handle_container_event(container, docker_host, action="boot")
except Exception as e:
logger.error(f"Failed to process container {container.name} on boot: {e}")
# The periodic loop re-reports containers to STD; it does nothing for
# DNS (which only triggers on boot/start). Skip it entirely when STD
# is not configured.
if STD_CONFIGURED:
threading.Thread(target=periodic_update_loop, args=(docker_host,), daemon=True).start()
else:
logger.info("Periodic refresh loop not started — STD integration disabled")
for event in client.events(decode=True):
if event.get("Type") != "container":
continue
action = event.get("Action")
if action not in WATCHED_DOCKER_ACTIONS:
continue
container_id = event.get("Actor", {}).get("ID") or event.get("id")
if not container_id:
continue
try:
container = client.containers.get(container_id)
handle_container_event(container, docker_host, action=action)
except docker.errors.NotFound:
pass
except Exception as e:
logger.error(f"Failed to handle {action} event for {container_id}: {e}")
if __name__ == "__main__":
main()