Skip to content

Commit

Permalink
Support using udocker to run nodejs
Browse files Browse the repository at this point in the history
Fixes: #153
  • Loading branch information
mr-c committed May 17, 2023
1 parent 9453f0b commit 7d2e1ef
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 4 deletions.
30 changes: 27 additions & 3 deletions cwl_utils/sandboxjs.py
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ def __del__(self) -> None:
try:
with open(cidfile[0]) as inp_stream:
p = subprocess.Popen( # nosec
["docker", "kill", inp_stream.read()],
[args[0], "kill", inp_stream.read()],
shell=False, # nosec
)
try:
Expand Down Expand Up @@ -318,6 +318,8 @@ def new_js_proc(
nodeimg = "docker.io/node:alpine"
if container_engine == "singularity":
nodeimg = f"docker://{nodeimg}"
elif container_engine == "podman":
nodeimg = "docker.io/library/node:alpine"

if not self.have_node_slim:
singularity_cache: Optional[str] = None
Expand All @@ -338,6 +340,16 @@ def new_js_proc(
)
if singularityimgs:
nodeimg = singularityimgs[0]
elif container_engine == "udocker":
matches = re.search(
re.escape(nodeimg),
subprocess.check_output( # nosec
[container_engine, "images"],
universal_newlines=True,
),
)
if matches:
dockerimgs = matches[0]
else:
raise Exception(
f"Unknown container_engine: {container_engine}."
Expand Down Expand Up @@ -367,7 +379,10 @@ def new_js_proc(
)
self.have_node_slim = True
nodejs_commands = [container_engine]
if container_engine != "singularity":
if (
container_engine != "singularity"
and "udocker" not in container_engine
):
nodejs_commands.extend(
[
"run",
Expand All @@ -379,7 +394,7 @@ def new_js_proc(
"--rm",
]
)
else:
elif "singularity" in container_engine:
nodejs_commands.extend(
[
"exec",
Expand All @@ -389,6 +404,15 @@ def new_js_proc(
"--userns" if singularity_supports_userns() else "--pid",
]
)
elif "udocker" in container_engine:
nodejs_commands.extend(
[
"run",
"--device=/dev/stdin",
"--device=/dev/stdout",
"--device=/dev/stderr",
]
)
nodejs_commands.extend(
[
nodeimg,
Expand Down
28 changes: 27 additions & 1 deletion tests/test_js_sandbox.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

from cwl_utils import expression, sandboxjs

from .util import needs_podman, needs_singularity
from .util import needs_podman, needs_singularity, needs_udocker

node_versions = [
("v0.8.26\n", False),
Expand Down Expand Up @@ -101,6 +101,32 @@ def test_value_from_two_concatenated_expressions_podman(
)


@needs_udocker
def test_value_from_two_concatenated_expressions_udocker(
tmp_path: Path, monkeypatch: pytest.MonkeyPatch
) -> None:
"""Javascript test using udocker."""
new_paths = hide_nodejs(tmp_path)
with monkeypatch.context() as m:
m.setenv("PATH", new_paths)
js_engine = sandboxjs.get_js_engine()
js_engine.have_node_slim = False # type: ignore[attr-defined]
js_engine.localdata = threading.local() # type: ignore[attr-defined]
assert (
expression.do_eval(
'$("a ")$("string")',
{},
[{"class": "InlineJavascriptRequirement"}],
None,
None,
{},
cwlVersion="v1.0",
container_engine="udocker",
)
== "a string"
)


@needs_singularity
def test_value_from_two_concatenated_expressions_singularity(
tmp_path: Path, monkeypatch: pytest.MonkeyPatch
Expand Down
5 changes: 5 additions & 0 deletions tests/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,3 +33,8 @@ def get_data(filename: str) -> str:
not bool(shutil.which("podman")),
reason="Requires the podman executable on the system path.",
)

needs_udocker = pytest.mark.skipif(
not bool(shutil.which("udocker")),
reason="Requires the udocker executable on the system path.",
)

0 comments on commit 7d2e1ef

Please sign in to comment.