-
Notifications
You must be signed in to change notification settings - Fork 5
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
Upgrade to BMI 2.0 #339
Closed
Closed
Upgrade to BMI 2.0 #339
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
02a8a95
Bump versions of pre-commit
sverhoeven adc25cf
Use latest grpc4bmi
sverhoeven 13eff27
Repalce basic_modeling_interface with bmipy
sverhoeven 1681801
Start to centralize way to start container
sverhoeven 367e180
Move starting of container to own module + each model declares versio…
sverhoeven 0b25c6d
Fix tests
sverhoeven 93c571e
Simplify typings
sverhoeven File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,19 +1,10 @@ | ||
""" | ||
Versions of Lisflood container images | ||
""" | ||
from pathlib import Path | ||
"""Versions of Lisflood container images.""" | ||
|
||
version_images = { | ||
from ewatercycle.container import VersionImages | ||
|
||
version_images: VersionImages = { | ||
"20.10": { | ||
"docker": "ewatercycle/lisflood-grpc4bmi:20.10", | ||
"singularity": "ewatercycle-lisflood-grpc4bmi_20.10.sif", | ||
} | ||
} | ||
|
||
|
||
def get_docker_image(version): | ||
return version_images[version]["docker"] | ||
|
||
|
||
def get_singularity_image(version, singularity_dir: Path): | ||
return singularity_dir / version_images[version]["singularity"] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,97 @@ | ||
"""Container utilities.""" | ||
from pathlib import Path | ||
from typing import Dict, Iterable, Literal, Mapping, Optional, Union | ||
|
||
from bmipy import Bmi | ||
from grpc import FutureTimeoutError | ||
from grpc4bmi.bmi_client_docker import BmiClientDocker | ||
from grpc4bmi.bmi_client_singularity import BmiClientSingularity | ||
from grpc4bmi.bmi_memoized import MemoizedBmi | ||
from grpc4bmi.bmi_optionaldest import OptionalDestBmi | ||
|
||
from ewatercycle import CFG | ||
|
||
ContainerEngines = Literal["docker", "singularity"] | ||
"""Supported container engines.""" | ||
|
||
ImageForContainerEngines = Dict[ContainerEngines, str] | ||
"""Container image name for each container engine.""" | ||
|
||
VersionImages = Mapping[str, ImageForContainerEngines] | ||
"""Dictionary of versions of a model. | ||
|
||
Each version has the image name for each container engine. | ||
""" | ||
|
||
|
||
def start_container( | ||
work_dir: Union[str, Path], | ||
image_engine: ImageForContainerEngines, | ||
input_dirs: Optional[Iterable[str]] = None, | ||
image_port=55555, | ||
timeout=None, | ||
delay=0, | ||
) -> Bmi: | ||
"""Start container with model inside. | ||
|
||
The `ewatercycle.CFG['container_engine']` value determines | ||
the engine used to start a container. | ||
|
||
Args: | ||
work_dir: Work directory | ||
version_image: Image name for each container engine. | ||
input_dirs: Additional directories to mount inside container. | ||
image_port: Docker port inside container where grpc4bmi server is running. | ||
timeout: Number of seconds to wait for grpc connection. | ||
delay: Number of seconds to wait before connecting. | ||
|
||
Raises: | ||
ValueError: When unknown container technology is requested. | ||
TimeoutError: When model inside container did not start quickly enough. | ||
|
||
Returns: | ||
_description_ | ||
""" | ||
engine: ContainerEngines = CFG["container_engine"] | ||
image = image_engine[engine] | ||
if input_dirs is None: | ||
input_dirs = [] | ||
if engine == "docker": | ||
try: | ||
bmi = BmiClientDocker( | ||
image=image, | ||
image_port=image_port, | ||
work_dir=str(work_dir), | ||
input_dirs=input_dirs, | ||
timeout=timeout, | ||
delay=delay, | ||
) | ||
except FutureTimeoutError as exc: | ||
# https://github.com/eWaterCycle/grpc4bmi/issues/95 | ||
# https://github.com/eWaterCycle/grpc4bmi/issues/100 | ||
raise TimeoutError( | ||
"Couldn't spawn container within allocated time limit " | ||
f"({timeout} seconds). You may try pulling the docker image with" | ||
f" `docker pull {image}` and then try again." | ||
) from exc | ||
elif engine == "singularity": | ||
image = str(CFG["singularity_dir"] / image) | ||
try: | ||
bmi = BmiClientSingularity( | ||
image=image, | ||
work_dir=str(work_dir), | ||
input_dirs=input_dirs, | ||
timeout=timeout, | ||
delay=delay, | ||
) | ||
except FutureTimeoutError as exc: | ||
docker_image = image_engine["docker"] | ||
raise TimeoutError( | ||
"Couldn't spawn container within allocated time limit " | ||
f"({timeout} seconds). You may try pulling the docker image with" | ||
f" `singularity build {image} " | ||
f"docker://{docker_image}` and then try again." | ||
) from exc | ||
else: | ||
raise ValueError(f"Unknown container technology: {CFG['container_engine']}") | ||
return OptionalDestBmi(MemoizedBmi(bmi)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Having this isolated from the models is great!
Would it be possible to separate the image selection from the actual starting of container? I'd like to be able to start a container by simply passing in an image (assuming that I know which container engine I'm using)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I added start_apptainer_container() and start_docker_container()