Skip to content

Commit

Permalink
rename singularity image
Browse files Browse the repository at this point in the history
  • Loading branch information
Brandon Walker committed Dec 11, 2023
1 parent 71e765b commit c12cb2b
Showing 1 changed file with 44 additions and 24 deletions.
68 changes: 44 additions & 24 deletions cwltool/singularity.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import logging
import os
import os.path
from pathlib import Path
import re
import shutil
import sys
Expand Down Expand Up @@ -171,29 +172,42 @@ def get_image(
cache_folder = create_tmp_dir(tmp_outdir_prefix)

absolute_path = os.path.abspath(cache_folder)
dockerfile_path = os.path.join(absolute_path, "Dockerfile")
singularityfile_path = dockerfile_path + ".def"
# if you do not set APPTAINER_TMPDIR will crash
# WARNING: 'nodev' mount option set on /tmp, it could be a
# source of failure during build process
# FATAL: Unable to create build: 'noexec' mount option set on
# /tmp, temporary root filesystem won't be usable at this location
with open(dockerfile_path, "w") as dfile:
dfile.write(dockerRequirement["dockerFile"])

singularityfile = SingularityWriter(DockerParser(dockerfile_path).parse()).convert()
with open(singularityfile_path, "w") as file:
file.write(singularityfile)

os.environ["APPTAINER_TMPDIR"] = absolute_path
singularity_options = ["--fakeroot"] if not shutil.which("proot") else []
Client.build(
recipe=singularityfile_path,
build_folder=absolute_path,
sudo=False,
options=singularity_options,
)
found = True
if "dockerImageId" in dockerRequirement:
image_name = dockerRequirement["dockerImageId"]
image_path = os.path.join(absolute_path, image_name)

Check warning on line 177 in cwltool/singularity.py

View check run for this annotation

Codecov / codecov/patch

cwltool/singularity.py#L176-L177

Added lines #L176 - L177 were not covered by tests
if os.path.exists(image_path):
found = True

Check warning on line 179 in cwltool/singularity.py

View check run for this annotation

Codecov / codecov/patch

cwltool/singularity.py#L179

Added line #L179 was not covered by tests
if found is False:
dockerfile_path = os.path.join(absolute_path, "Dockerfile")
singularityfile_path = dockerfile_path + ".def"

Check warning on line 182 in cwltool/singularity.py

View check run for this annotation

Codecov / codecov/patch

cwltool/singularity.py#L181-L182

Added lines #L181 - L182 were not covered by tests
# if you do not set APPTAINER_TMPDIR will crash
# WARNING: 'nodev' mount option set on /tmp, it could be a
# source of failure during build process
# FATAL: Unable to create build: 'noexec' mount option set on
# /tmp, temporary root filesystem won't be usable at this location
with open(dockerfile_path, "w") as dfile:
dfile.write(dockerRequirement["dockerFile"])

Check warning on line 189 in cwltool/singularity.py

View check run for this annotation

Codecov / codecov/patch

cwltool/singularity.py#L189

Added line #L189 was not covered by tests

singularityfile = SingularityWriter(DockerParser(dockerfile_path).parse()).convert()

Check warning on line 191 in cwltool/singularity.py

View check run for this annotation

Codecov / codecov/patch

cwltool/singularity.py#L191

Added line #L191 was not covered by tests
with open(singularityfile_path, "w") as file:
file.write(singularityfile)

Check warning on line 193 in cwltool/singularity.py

View check run for this annotation

Codecov / codecov/patch

cwltool/singularity.py#L193

Added line #L193 was not covered by tests

os.environ["APPTAINER_TMPDIR"] = absolute_path
singularity_options = ["--fakeroot"] if not shutil.which("proot") else []
Client.build(

Check warning on line 197 in cwltool/singularity.py

View check run for this annotation

Codecov / codecov/patch

cwltool/singularity.py#L195-L197

Added lines #L195 - L197 were not covered by tests
recipe=singularityfile_path,
build_folder=absolute_path,
sudo=False,
options=singularity_options,
)
if "dockerImageId" in dockerRequirement:
image_name = dockerRequirement["dockerImageId"]
children = sorted(Path(absolute_path).glob('*.sif'))
image_path = children[0]
desired_image_path = os.path.join(absolute_path, image_name)
os.rename(image_path, desired_image_path)

Check warning on line 208 in cwltool/singularity.py

View check run for this annotation

Codecov / codecov/patch

cwltool/singularity.py#L204-L208

Added lines #L204 - L208 were not covered by tests

found = True

Check warning on line 210 in cwltool/singularity.py

View check run for this annotation

Codecov / codecov/patch

cwltool/singularity.py#L210

Added line #L210 was not covered by tests
elif "dockerImageId" not in dockerRequirement and "dockerPull" in dockerRequirement:
match = re.search(pattern=r"([a-z]*://)", string=dockerRequirement["dockerPull"])
img_name = _normalize_image_id(dockerRequirement["dockerPull"])
Expand Down Expand Up @@ -326,7 +340,13 @@ def get_from_requirements(
if not self.get_image(cast(Dict[str, str], r), pull_image, tmp_outdir_prefix, force_pull):
raise WorkflowException("Container image {} not found".format(r["dockerImageId"]))

return os.path.abspath(cast(str, r["dockerImageId"]))
if "CWL_SINGULARITY_CACHE" in os.environ:
cache_folder = os.environ["CWL_SINGULARITY_CACHE"]
img_path = os.path.join(cache_folder, cast(str, r["dockerImageId"]))

Check warning on line 345 in cwltool/singularity.py

View check run for this annotation

Codecov / codecov/patch

cwltool/singularity.py#L344-L345

Added lines #L344 - L345 were not covered by tests
else:
img_path = cast(str, r["dockerImageId"])

Check warning on line 347 in cwltool/singularity.py

View check run for this annotation

Codecov / codecov/patch

cwltool/singularity.py#L347

Added line #L347 was not covered by tests

return os.path.abspath(img_path)

Check warning on line 349 in cwltool/singularity.py

View check run for this annotation

Codecov / codecov/patch

cwltool/singularity.py#L349

Added line #L349 was not covered by tests

@staticmethod
def append_volume(runtime: List[str], source: str, target: str, writable: bool = False) -> None:
Expand Down

0 comments on commit c12cb2b

Please sign in to comment.