Skip to content
Snippets Groups Projects
Unverified Commit e310e1af authored by Stephane Bonnet's avatar Stephane Bonnet
Browse files

acme-copy-certs: implémenté l'ajout et la suppression de services sur...

acme-copy-certs: implémenté l'ajout et la suppression de services sur événéments start et stop des conteneurs
parent 0a8d9e00
No related branches found
No related tags found
2 merge requests!43WIP: Acme copy certs dev,!42Pica openldap dev
......@@ -12,6 +12,7 @@ Classes
import docker
import threading
import requests
from service_manager import Service
from docker_actions import RestartDockerAction
......@@ -25,32 +26,40 @@ class DockerMonitor(threading.Thread):
super().__init__()
def run(self):
print('Starting monitor')
# Identify all running containers and get dependent services
# at startup
containers = self.docker_client.containers.list()
try:
containers = self.docker_client.containers.list()
for c in containers:
if c.status == 'running':
self._add_service(c)
for c in containers:
if c.status == 'running':
self._add_service(c)
# Then, wait for docker events and identify new dependent services
# or services that exit the system. Do an update each time a new
# service is added / restarted etc. or a service is changed.
self.events = self.docker_client.events(decode = True)
for event in self.events:
if 'status' not in event:
continue
print(event['status'])
if event['status'] == 'stop':
pass
elif event['status'] == 'start':
print('start event !')
# Then, wait for docker events and identify new dependent services
# or services that exit the system. Do an update each time a new
# service is added / restarted etc. or a service is changed.
self.events = self.docker_client.events(decode = True)
for event in self.events:
if 'status' not in event:
continue
try:
c = self.docker_client.containers.get(event['id'])
if event['status'] == 'stop':
self._remove_service(c)
elif event['status'] == 'start':
self._add_service(c)
except docker.errors.NotFound:
pass
except docker.errors.APIError as error:
print('Docker error while looking up container {0}: {1} '.format(event.id, error.strerror))
except requests.exceptions.ConnectionError as error:
print('Connection to docker socket refused.')
print('Stopping monitor')
print('No more events to handle: stopping monitor')
def stop(self):
if self.events is not None:
self.events.close()
......@@ -83,9 +92,9 @@ class DockerMonitor(threading.Thread):
def _add_service(self, container):
if 'acme_copy_certs.enable' in container.labels:
if container.labels['acme_copy_certs'] == 'enable':
if container.labels['acme_copy_certs.enable'] == 'true':
host = self._get_host_from_traefik_rule(container)
elif container.labels['acme_copy_certs'] == 'disable':
elif container.labels['acme_copy_certs.enable'] == 'false':
pass
else:
print('Invalid acme_copy_certs value for {0}'
......@@ -98,4 +107,10 @@ class DockerMonitor(threading.Thread):
else:
s = Service(container.id, host, None)
self.services_mgr.add(s)
\ No newline at end of file
print('Handling container {0}({1})'.format(container.name, container.id))
self.services_mgr.add(s)
else:
print('Not handling container {0}({1})'.format(container.name, container.id))
def _remove_service(self, container):
self.services_mgr.remove(container.id)
\ No newline at end of file
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment