Skip to content

Commit

Permalink
Merge pull request #228 from 2i2c-org/host_config
Browse files Browse the repository at this point in the history
Allow setting arbitrary host config and env variables on launched containers
  • Loading branch information
yuvipanda authored Jan 27, 2025
2 parents 05228d3 + bd68510 commit 4feb779
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 7 deletions.
35 changes: 28 additions & 7 deletions frx_challenges/web/management/commands/evaluator.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import asyncio
import collections.abc
import json
import logging
import os
Expand All @@ -11,6 +12,7 @@
import fsspec
from django.conf import settings
from django.core.management.base import BaseCommand
from web.utils import recursive_update

from ...models import Evaluation

Expand Down Expand Up @@ -80,13 +82,32 @@ async def start_evaluation(self, input_uri):
)
for c in settings.EVALUATOR_DOCKER_CMD
]
container = await self.docker.containers.create(
config={
"Image": self.image,
"Cmd": cmd,
"HostConfig": host_config,
}
)

if settings.EVALUATOR_DOCKER_EXTRA_HOST_CONFIG:
recursive_update(host_config, settings.EVALUATOR_DOCKER_EXTRA_HOST_CONFIG)

config = {
"Image": self.image,
"Cmd": cmd,
"HostConfig": host_config,
}

if settings.EVALUATOR_DOCKER_CONTAINER_ENV:
if isinstance(
settings.EVALUATOR_DOCKER_CONTAINER_ENV, collections.abc.Mapping
):
config["Env"] = [
f"{k}={v}"
for k, v in settings.EVALUATOR_DOCKER_CONTAINER_ENV.items()
]
elif isinstance(settings.EVALUATOR_DOCKER_CONTAINER_ENV, list):
config["Env"] = settings.EVALUATOR_DOCKER_CONTAINER_ENV
else:
raise RuntimeError(
f"settings.EVALUATOR_DOCKER_CONTAINER_ENV should be a dict or list, found {type( settings.EVALUATOR_DOCKER_CONTAINER_ENV)}"
)

container = await self.docker.containers.create(config)

logger.debug(f"Container created with ID: {container.id}")
try:
Expand Down
18 changes: 18 additions & 0 deletions frx_challenges/web/utils.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# From https://github.com/jupyter-server/jupyter_server/blob/fc0ac3236fdd92778ea765db6e8982212c8389ee/jupyter_server/config_manager.py#L14
def recursive_update(target: dict, new: dict) -> None:
"""
Recursively update one dictionary in-place using another.
None values will delete their keys.
"""
for k, v in new.items():
if isinstance(v, dict):
if k not in target:
target[k] = {}
recursive_update(target[k], v)

elif v is None:
target.pop(k, None)

else:
target[k] = v

0 comments on commit 4feb779

Please sign in to comment.