-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdresmon.py
62 lines (49 loc) · 1.83 KB
/
dresmon.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
from no_windows import terminate_if_windows_assumed
terminate_if_windows_assumed()
import docker
import importlib
import pkgutil
from providers import BaseProvider
from config import logger, DRESMON_PROVIDER
from hosts.manager import sync_hosts_file
client = docker.from_env()
###
# Container events data provider
###
# Dynamically load all providers from the providers directory
package_name = 'providers'
package = importlib.import_module(package_name)
for _, module_name, _ in pkgutil.iter_modules(package.__path__):
importlib.import_module(f'{package_name}.{module_name}')
provider_class = BaseProvider.PROVIDER_MAPPING.get(DRESMON_PROVIDER)
if not provider_class:
logger.error(f"Unknown provider: {DRESMON_PROVIDER}")
raise ValueError(f"Unknown provider: {DRESMON_PROVIDER}")
provider_instance = provider_class()
###
# Define algorythm
###
def process_container(container):
domains = provider_instance.get_domains(container)
if domains:
logger.debug(f"Domains for container {container.name}: {', '.join(domains)}")
sync_hosts_file(domains)
logger.debug("No domains to update /etc/hosts")
def process_existing_containers():
logger.info("Processing existing containers...")
for container in client.containers.list():
process_container(container)
def monitor_docker_events():
logger.info("Starting to monitor Docker events...")
# Continuously listen to Docker events
for event in client.events(decode=True):
if event['Type'] == 'container' and event['Action'] == 'start':
container = client.containers.get(event['id'])
logger.debug(f"New container started: {container.name}")
process_container(container)
###
# Start and listen for container events
###
if __name__ == "__main__":
process_existing_containers()
monitor_docker_events()