Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix bug with ersilia fetch from github inside a docker container #1193

Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
53 changes: 26 additions & 27 deletions ersilia/utils/docker.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
from .identifiers.long import LongIdentifier
from .terminal import run_command, run_command_check_output

from .. import logger
from ..default import DEFAULT_DOCKER_PLATFORM, DEFAULT_UDOCKER_USERNAME
from ..utils.system import SystemChecker

Expand Down Expand Up @@ -49,7 +50,6 @@ def is_udocker_installed():
class SimpleDocker(object):
def __init__(self, use_udocker=None):
self.identifier = LongIdentifier()
self.client = docker.from_env()
if use_udocker is None:
self._with_udocker = self._use_udocker()
else:
Expand Down Expand Up @@ -250,17 +250,17 @@ def exec(self, cmd, org, img, tag, name):
self.exec_container(name, cmd)
self.kill(name)

def get_container(self):
def _get_containers(self):
"""
This function will get the Docker container running Ersilia Models.
it wil return a message if a container is not running.
This function will get the Docker container running an ersilia model
"""

try:
containers = self.client.containers.list()
client = docker.from_env()
containers = client.containers.list()

if not containers:
return ["No running containers found"]
logger.debug("No containers found")
return containers

except docker.errors.APIError as e:
Expand All @@ -272,42 +272,41 @@ def get_container(self):

def container_memory(self):
"""
This function will get the total memory usage of the Docker container running Ersilia Models.
This function will get the total memory usage of the Docker container running an ersilia model.
"""

containers = self.get_container()
if isinstance(containers, list) and isinstance(containers[0], str):
return containers[0]

result = []
for container in containers:
stats = container.stats(stream=False)
mem_usage = stats["memory_stats"]["usage"] / (1024 * 1024)
if len(containers) > 0:
result = []
for container in containers:
stats = container.stats(stream=False)
mem_usage = stats["memory_stats"]["usage"] / (1024 * 1024)

return (
f"Total memory consumed by container '{container.name}': {mem_usage:.2f}MiB",
)
return (
f"Total memory consumed by container '{container.name}': {mem_usage:.2f}MiB",
)
return

def container_cpu(self):
"""
This function will get the CPU time of the Docker container running Ersilia Models.
"""

containers = self.get_container()
if isinstance(containers, list) and isinstance(containers[0], str):
return containers[0]

for container in containers:
stats = container.stats(stream=False)
cpu_stats = stats["cpu_stats"]
total_cpu_time = cpu_stats["cpu_usage"]["total_usage"] / 1e9
if len(containers) > 0:
for container in containers:
stats = container.stats(stream=False)
cpu_stats = stats["cpu_stats"]
total_cpu_time = cpu_stats["cpu_usage"]["total_usage"] / 1e9

minutes = total_cpu_time // 60
seconds = total_cpu_time % 60
minutes = total_cpu_time // 60
seconds = total_cpu_time % 60

return (
f"Total CPU time used by container '{container.name}': {int(minutes)} minutes {seconds:.2f} seconds",
)
return (
f"Total CPU time used by container '{container.name}': {int(minutes)} minutes {seconds:.2f} seconds",
)

def container_peak(self):
"""
Expand Down