diff --git a/docs/CHANGELOG.rst b/docs/CHANGELOG.rst index 16982936f..d74044c94 100644 --- a/docs/CHANGELOG.rst +++ b/docs/CHANGELOG.rst @@ -21,6 +21,8 @@ Changed - Make processing container startup script ``Python`` 3.12 compatible - Fix stop null executor processing in the preparation phase - Reduce the maximal number of threads for downolad data in the init container +- Respect image mapper in ``list_docker_images`` management command +- Use image mapper in docker executor Fixed ----- diff --git a/resolwe/flow/executors/docker/run.py b/resolwe/flow/executors/docker/run.py index d1e79fd1a..c4f9bf696 100644 --- a/resolwe/flow/executors/docker/run.py +++ b/resolwe/flow/executors/docker/run.py @@ -300,6 +300,17 @@ def _processing_volumes(self) -> Dict: ] return dict([self._new_volume(*mount_point) for mount_point in mount_points]) + def _map_docker_image(self, docker_image: str) -> str: + """Transform the given Docker image according to the mapper. + + If image starts with the prefix in the mapper keys, it is replaced with the + corresponding value. If no prefix matches, the image is returned unchanged. + """ + for prefix, replacement in SETTINGS.get("FLOW_CONTAINER_IMAGE_MAP", {}).items(): + if docker_image.startswith(prefix): + return replacement + docker_image[len(prefix) :] + return docker_image + async def start(self): """Start process execution.""" memory = ( @@ -316,16 +327,20 @@ async def start(self): else: network_mode = "none" - processing_image = self.requirements.get( - "image", - SETTINGS.get( - "FLOW_DOCKER_DEFAULT_PROCESSING_CONTAINER_IMAGE", - "public.ecr.aws/s4q6j6e8/resolwe/base:ubuntu-20.04", - ), + processing_image = self._map_docker_image( + self.requirements.get( + "image", + SETTINGS.get( + "FLOW_DOCKER_DEFAULT_PROCESSING_CONTAINER_IMAGE", + "public.ecr.aws/s4q6j6e8/resolwe/base:ubuntu-20.04", + ), + ) ) - communicator_image = SETTINGS.get( - "FLOW_DOCKER_COMMUNICATOR_IMAGE", - "public.ecr.aws/s4q6j6e8/resolwe/com:latest", + communicator_image = self._map_docker_image( + SETTINGS.get( + "FLOW_DOCKER_COMMUNICATOR_IMAGE", + "public.ecr.aws/s4q6j6e8/resolwe/com:latest", + ) ) ulimits = [] if ( diff --git a/resolwe/flow/management/commands/list_docker_images.py b/resolwe/flow/management/commands/list_docker_images.py index 835612ee3..104a3ed8b 100644 --- a/resolwe/flow/management/commands/list_docker_images.py +++ b/resolwe/flow/management/commands/list_docker_images.py @@ -56,6 +56,22 @@ def add_arguments(self, parser): help="Don't fail whenever a Docker image can't be pulled", ) + def _map_image(self, docker_image: str, mapper: dict[str, str]) -> str: + """Transform the given Docker image according to the mapper. + + If image starts with the prefix in the mapper keys, it is replaced with the + corresponding value. If no prefix matches, the image is returned unchanged. + """ + for prefix, replacement in mapper.items(): + if docker_image.startswith(prefix): + return replacement + docker_image[len(prefix) :] + return docker_image + + def _map_images(self, docker_images: set[str]) -> set[str]: + """Map the docker images in the given set according to the settings.""" + mapper = getattr(settings, "FLOW_CONTAINER_IMAGE_MAP", {}) + return {self._map_image(image, mapper) for image in docker_images} + def handle(self, *args, **options): """Handle command list_docker_images.""" verbosity = int(options.get("verbosity")) @@ -83,6 +99,9 @@ def handle(self, *args, **options): ) ) + # Map the Docker images according to the settings. + unique_docker_images = self._map_images(unique_docker_images) + # Pull images if requested or just output the list in specified format if options["pull"]: # Remove set of already pulled images.