From 80f92405d99954d10a65a86f89dfaa4c9402779e Mon Sep 17 00:00:00 2001 From: Derek Maggio Date: Tue, 27 Jun 2023 14:57:39 +0000 Subject: [PATCH] feat: Add extra mounts logic --- .../config_file_settings.py | 9 ++++++++ .../service_builders/abstract_service.py | 21 ++++++++++++++++--- .../input/configuration_file.py | 7 ++++++- 3 files changed, 33 insertions(+), 4 deletions(-) diff --git a/emulation_system/emulation_system/compose_file_creator/config_file_settings.py b/emulation_system/emulation_system/compose_file_creator/config_file_settings.py index cd14662f..5507be9b 100644 --- a/emulation_system/emulation_system/compose_file_creator/config_file_settings.py +++ b/emulation_system/emulation_system/compose_file_creator/config_file_settings.py @@ -2,6 +2,7 @@ from enum import Enum from typing import TYPE_CHECKING, List, Optional, Union +import pydantic from pydantic import DirectoryPath, Field, FilePath from typing_extensions import Literal @@ -231,3 +232,11 @@ class FileMount(Mount): type: Literal[MountTypes.FILE] source_path: FilePath + + +class ExtraMount(OpentronsBaseModel): + """Extra bind mounts for a container.""" + + container_names: List[str] + host_path: DirectoryPath | FilePath + container_path: str diff --git a/emulation_system/emulation_system/compose_file_creator/conversion/service_builders/abstract_service.py b/emulation_system/emulation_system/compose_file_creator/conversion/service_builders/abstract_service.py index 8f4bf8cc..0aaf20a9 100644 --- a/emulation_system/emulation_system/compose_file_creator/conversion/service_builders/abstract_service.py +++ b/emulation_system/emulation_system/compose_file_creator/conversion/service_builders/abstract_service.py @@ -4,7 +4,7 @@ import pathlib from abc import ABC, abstractmethod -from typing import Optional, Type, cast +from typing import List, Optional, Type, cast from emulation_system import SystemConfigurationModel from emulation_system.compose_file_creator import BuildItem, Service @@ -199,12 +199,27 @@ def build_service(self) -> Service: """Method calling all generate* methods to build Service object.""" intermediate_healthcheck = self.generate_healthcheck() + container_name = self.generate_container_name() + mounts = self.generate_volumes() + extra_mounts = self._config_model.extra_mounts + + if len(extra_mounts) > 0: + mounts_to_add: List[str] = [] + + for mount in extra_mounts: + if container_name in mount.container_names: + mounts_to_add.append(f"{mount.host_path}:{mount.container_path}") + if mounts is not None: + mounts.extend(mounts_to_add) + else: + mounts = mounts_to_add + return Service( - container_name=cast(ServiceContainerName, self.generate_container_name()), + container_name=cast(ServiceContainerName, container_name), image=cast(ServiceImage, self.generate_image()), build=cast(ServiceBuild, self.generate_build()), tty=cast(ServiceTTY, self.is_tty()), - volumes=cast(ServiceVolumes, self.generate_volumes()), + volumes=cast(ServiceVolumes, mounts), ports=cast(ServicePorts, self.generate_ports()), environment=cast(ServiceEnvironment, self.generate_env_vars()), networks=self.generate_networks(), diff --git a/emulation_system/emulation_system/compose_file_creator/input/configuration_file.py b/emulation_system/emulation_system/compose_file_creator/input/configuration_file.py index 41f841cc..d5a741de 100644 --- a/emulation_system/emulation_system/compose_file_creator/input/configuration_file.py +++ b/emulation_system/emulation_system/compose_file_creator/input/configuration_file.py @@ -11,7 +11,11 @@ from opentrons_pydantic_base_model import OpentronsBaseModel from ...source import MonorepoSource, OpentronsModulesSource, OT3FirmwareSource -from ..config_file_settings import EmulationLevels, Hardware +from ..config_file_settings import ( + EmulationLevels, + ExtraMount, + Hardware, +) from ..errors import DuplicateHardwareNameError from ..types.input_types import Containers, Modules, Robots from ..types.intermediate_types import IntermediateNetworks @@ -43,6 +47,7 @@ class SystemConfigurationModel(OpentronsBaseModel): opentrons_modules_source: OpentronsModulesSource = Field( default=OpentronsModulesSource(source_location="latest") ) + extra_mounts: List[ExtraMount] = Field(default=[]) @root_validator(pre=True) def validate_names(cls, values) -> Dict[str, Dict[str, Containers]]: # noqa: ANN001