Skip to content

Commit

Permalink
Merge pull request #74 from aCLImatise/auto-docker-image
Browse files Browse the repository at this point in the history
Automatically store docker images produced by docker executor
  • Loading branch information
multimeric committed Jan 28, 2021
2 parents b3fc52e + f154bef commit cd682da
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 1 deletion.
21 changes: 20 additions & 1 deletion aclimatise/execution/docker.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
from docker.utils.socket import consume_socket_output, demux_adaptor, frames_iter

from aclimatise.execution.help import CliHelpExecutor
from aclimatise.model import Command


def read_socket(sock, timeout: int = None) -> Tuple[bytes, bytes]:
Expand Down Expand Up @@ -40,9 +41,27 @@ class DockerExecutor(CliHelpExecutor):
An executor that runs the commands on an already-running docker Container (not an Image!)
"""

def __init__(self, container: "docker.models.containers.Container", **kwargs):
def __init__(
self, container: "docker.models.containers.Container", save_image=True, **kwargs
):
"""
:param container: The object from the Docker API that represents the running container to run inside
:param save_image: If true (default), save the image name on the command, meaning that the resulting tool
definitions also use this Docker image
"""
super().__init__(**kwargs)
self.container = container
self.save_image = save_image

def convert(
self,
cmd: List[str],
) -> Command:
# Use the existing function, but patch in the docker image
cmd = super().convert(cmd)
if self.save_image:
cmd.docker_image = self.container.image.tags[0]
return cmd

def execute(self, command: List[str]) -> str:
_, sock = self.container.exec_run(
Expand Down
1 change: 1 addition & 0 deletions docs/changes.rst
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ Breaking Changes
New Features
************
* Add ``docker_image`` field to ``Command``
* The ``DockerExecutor`` has been updated to automatically set this field based on the Docker image being used by it
* Add `Janis <https://janis.readthedocs.io/en/latest/index.html>`_ converter (`#73 <https://github.com/aCLImatise/CliHelpParser/issues/73>`_)

Bug Fixes
Expand Down
14 changes: 14 additions & 0 deletions test/executors/test_docker.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,20 @@
from aclimatise.execution.docker import DockerExecutor


@pytest.mark.timeout(360)
def test_docker_image_saved(bwamem_help):
client = docker.from_env()
container = client.containers.run(
"biocontainers/bwa:v0.7.17_cv1",
entrypoint=["sleep", "999999999"],
detach=True,
)

exec = DockerExecutor(container)
cmd = exec.convert(["bwa", "mem"])
assert cmd.docker_image == "biocontainers/bwa:v0.7.17_cv1"


def test_docker(bwamem_help):
client = docker.from_env()
container = client.containers.run(
Expand Down

0 comments on commit cd682da

Please sign in to comment.