reworked service mapping

This commit is contained in:
John Lancaster
2024-05-27 18:33:41 -05:00
parent f1ab1ce6d0
commit fc15a24ab6

View File

@@ -12,26 +12,30 @@ def manage_containers(project: str, services: list[str]):
def wrapper(*args, **kwargs): def wrapper(*args, **kwargs):
client = docker.from_env() client = docker.from_env()
containers: list[Container] = [ try:
project_containers = (
c c
for c in client.containers.list() for c in client.containers.list()
if c.labels['com.docker.compose.project'] == project if c.labels['com.docker.compose.project'] == project
and c.labels['com.docker.compose.service'] in services )
] service_dict = {
try: c.labels['com.docker.compose.service']: c for c in project_containers
}
containers: list[Container] = [service_dict[s] for s in services]
except Exception as e:
raise e
else:
# Start the containers # Start the containers
for container in containers: for container in containers:
container.stop() container.stop()
logger.info(f'Stopped container [blue]{container.name}[/]') logger.info(f'Stopped container [blue]{container.name}[/]')
try:
# Execute the wrapped function # Execute the wrapped function
result = func(*args, **kwargs) return func(*args, **kwargs)
except Exception as e: except Exception as e:
# Handle exceptions and ensure containers are stopped # Handle exceptions and ensure containers are stopped
raise e raise e
else:
return result
finally: finally:
# Stop the containers # Stop the containers
for container in containers: for container in containers: