mirror of
https://github.com/jupyter/docker-stacks.git
synced 2025-10-07 10:04:03 +00:00
Improve logs around running docker (#2261)
This commit is contained in:
@@ -22,11 +22,10 @@ class DockerRunner:
|
|||||||
self.docker_client: docker.DockerClient = docker_client
|
self.docker_client: docker.DockerClient = docker_client
|
||||||
|
|
||||||
def __enter__(self) -> Container:
|
def __enter__(self) -> Container:
|
||||||
LOGGER.info(f"Creating container for image {self.image_name} ...")
|
LOGGER.info(f"Creating a container for the image: {self.image_name} ...")
|
||||||
|
default_kwargs = {"detach": True, "tty": True}
|
||||||
self.container = self.docker_client.containers.run(
|
self.container = self.docker_client.containers.run(
|
||||||
image=self.image_name,
|
image=self.image_name, command=self.command, **default_kwargs
|
||||||
command=self.command,
|
|
||||||
detach=True,
|
|
||||||
)
|
)
|
||||||
LOGGER.info(f"Container {self.container.name} created")
|
LOGGER.info(f"Container {self.container.name} created")
|
||||||
return self.container
|
return self.container
|
||||||
@@ -48,6 +47,6 @@ class DockerRunner:
|
|||||||
exec_result = container.exec_run(cmd)
|
exec_result = container.exec_run(cmd)
|
||||||
output = exec_result.output.decode().rstrip()
|
output = exec_result.output.decode().rstrip()
|
||||||
assert isinstance(output, str)
|
assert isinstance(output, str)
|
||||||
LOGGER.info(f"Command output: {output}")
|
LOGGER.debug(f"Command output: {output}")
|
||||||
assert exec_result.exit_code == 0, f"Command: `{cmd}` failed"
|
assert exec_result.exit_code == 0, f"Command: `{cmd}` failed"
|
||||||
return output
|
return output
|
||||||
|
@@ -29,7 +29,7 @@ def http_client() -> requests.Session:
|
|||||||
def docker_client() -> docker.DockerClient:
|
def docker_client() -> docker.DockerClient:
|
||||||
"""Docker client configured based on the host environment"""
|
"""Docker client configured based on the host environment"""
|
||||||
client = docker.from_env()
|
client = docker.from_env()
|
||||||
LOGGER.info(f"Docker client created: {client.version()}")
|
LOGGER.debug(f"Docker client created: {client.version()}")
|
||||||
return client
|
return client
|
||||||
|
|
||||||
|
|
||||||
|
@@ -1,7 +1,7 @@
|
|||||||
[pytest]
|
[pytest]
|
||||||
addopts = -ra --color=yes
|
addopts = -ra --color=yes
|
||||||
log_cli = 1
|
log_cli = 1
|
||||||
log_cli_level = DEBUG
|
log_cli_level = INFO
|
||||||
log_cli_format = %(asctime)s [%(levelname)8s] %(message)s (%(filename)s:%(lineno)s)
|
log_cli_format = %(asctime)s [%(levelname)8s] %(message)s (%(filename)s:%(lineno)s)
|
||||||
log_cli_date_format=%Y-%m-%d %H:%M:%S
|
log_cli_date_format=%Y-%m-%d %H:%M:%S
|
||||||
markers =
|
markers =
|
||||||
|
@@ -41,8 +41,6 @@ class CondaPackageHelper:
|
|||||||
|
|
||||||
def __init__(self, container: TrackedContainer):
|
def __init__(self, container: TrackedContainer):
|
||||||
self.container = container
|
self.container = container
|
||||||
|
|
||||||
LOGGER.info(f"Starting container {self.container.image_name} ...")
|
|
||||||
self.container.run_detached(command=["bash", "-c", "sleep infinity"])
|
self.container.run_detached(command=["bash", "-c", "sleep infinity"])
|
||||||
|
|
||||||
self.requested: dict[str, set[str]] | None = None
|
self.requested: dict[str, set[str]] | None = None
|
||||||
@@ -101,7 +99,7 @@ class CondaPackageHelper:
|
|||||||
)
|
)
|
||||||
# Keeping command line output since `mamba search --outdated --json` is way too long ...
|
# Keeping command line output since `mamba search --outdated --json` is way too long ...
|
||||||
self.available = CondaPackageHelper._extract_available(
|
self.available = CondaPackageHelper._extract_available(
|
||||||
self.container.exec_cmd("mamba search --outdated --quiet")
|
self.container.exec_cmd("conda search --outdated --quiet")
|
||||||
)
|
)
|
||||||
return self.available
|
return self.available
|
||||||
|
|
||||||
|
@@ -44,12 +44,15 @@ class TrackedContainer:
|
|||||||
Keyword arguments to pass to docker.DockerClient.containers.run
|
Keyword arguments to pass to docker.DockerClient.containers.run
|
||||||
extending and/or overriding key/value pairs passed to the constructor
|
extending and/or overriding key/value pairs passed to the constructor
|
||||||
"""
|
"""
|
||||||
LOGGER.info(f"Running {self.image_name} with args {kwargs} ...")
|
LOGGER.info(
|
||||||
|
f"Creating a container for the image: {self.image_name} with args: {kwargs} ..."
|
||||||
|
)
|
||||||
default_kwargs = {"detach": True, "tty": True}
|
default_kwargs = {"detach": True, "tty": True}
|
||||||
final_kwargs = default_kwargs | kwargs
|
final_kwargs = default_kwargs | kwargs
|
||||||
self.container = self.docker_client.containers.run(
|
self.container = self.docker_client.containers.run(
|
||||||
self.image_name, **final_kwargs
|
self.image_name, **final_kwargs
|
||||||
)
|
)
|
||||||
|
LOGGER.info(f"Container {self.container.name} created")
|
||||||
|
|
||||||
def get_logs(self) -> str:
|
def get_logs(self) -> str:
|
||||||
assert self.container is not None
|
assert self.container is not None
|
||||||
@@ -65,13 +68,14 @@ class TrackedContainer:
|
|||||||
def exec_cmd(self, cmd: str, **kwargs: Any) -> str:
|
def exec_cmd(self, cmd: str, **kwargs: Any) -> str:
|
||||||
assert self.container is not None
|
assert self.container is not None
|
||||||
container = self.container
|
container = self.container
|
||||||
|
|
||||||
LOGGER.info(f"Running cmd: `{cmd}` on container: {container.name}")
|
LOGGER.info(f"Running cmd: `{cmd}` on container: {container.name}")
|
||||||
default_kwargs = {"tty": True}
|
default_kwargs = {"tty": True}
|
||||||
final_kwargs = default_kwargs | kwargs
|
final_kwargs = default_kwargs | kwargs
|
||||||
exec_result = container.exec_run(cmd, **final_kwargs)
|
exec_result = container.exec_run(cmd, **final_kwargs)
|
||||||
output = exec_result.output.decode().rstrip()
|
output = exec_result.output.decode().rstrip()
|
||||||
assert isinstance(output, str)
|
assert isinstance(output, str)
|
||||||
LOGGER.info(f"Command output: {output}")
|
LOGGER.debug(f"Command output: {output}")
|
||||||
assert exec_result.exit_code == 0, f"Command: `{cmd}` failed"
|
assert exec_result.exit_code == 0, f"Command: `{cmd}` failed"
|
||||||
return output
|
return output
|
||||||
|
|
||||||
@@ -109,7 +113,7 @@ class TrackedContainer:
|
|||||||
def remove(self) -> None:
|
def remove(self) -> None:
|
||||||
"""Kills and removes the tracked docker container."""
|
"""Kills and removes the tracked docker container."""
|
||||||
if self.container is None:
|
if self.container is None:
|
||||||
LOGGER.info("No container to remove")
|
LOGGER.debug("No container to remove")
|
||||||
else:
|
else:
|
||||||
LOGGER.info(f"Removing container {self.container.name} ...")
|
LOGGER.info(f"Removing container {self.container.name} ...")
|
||||||
self.container.remove(force=True)
|
self.container.remove(force=True)
|
||||||
|
Reference in New Issue
Block a user