Skip to content

Commit

Permalink
refactor configuration options for udocker & singularity CLI invocations
Browse files Browse the repository at this point in the history
  • Loading branch information
mlin committed Dec 4, 2021
1 parent 1b156d9 commit 0463f5a
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 14 deletions.
5 changes: 4 additions & 1 deletion WDL/runtime/backend/cli_subprocess.py
Original file line number Diff line number Diff line change
Expand Up @@ -129,11 +129,14 @@ def _run(self, logger: logging.Logger, terminating: Callable[[], bool], command:
def cli_name(self) -> str:
pass

def cli_exe(self) -> List[str]:
return [self.cli_name]

def _pull_invocation(self, logger: logging.Logger, cleanup: ExitStack) -> Tuple[str, List[str]]:
image = self.runtime_values.get(
"docker", self.cfg.get_dict("task_runtime", "defaults")["docker"]
)
return (image, [self.cli_name, "pull", image])
return (image, self.cli_exe + ["pull", image])

@abstractmethod
def _run_invocation(self, logger: logging.Logger, cleanup: ExitStack, image: str) -> List[str]:
Expand Down
15 changes: 10 additions & 5 deletions WDL/runtime/backend/singularity.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,17 +18,18 @@ class SingularityContainer(SubprocessBase):

@classmethod
def global_init(cls, cfg: config.Loader, logger: logging.Logger) -> None:
cmd = cfg.get_list("singularity", "exe") + ["--version"]
try:
singularity_version = subprocess.run(
["singularity", "--version"],
cmd,
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
check=True,
universal_newlines=True,
)
except:
raise RuntimeError(
"Unable to check `singularity --version`; verify Singularity installation"
f"Unable to check `{' '.join(cmd)}`; verify Singularity installation"
)
logger.notice( # pyre-ignore
_(
Expand All @@ -41,29 +42,33 @@ def global_init(cls, cfg: config.Loader, logger: logging.Logger) -> None:
def cli_name(self) -> str:
return "singularity"

@property
def cli_exe(self) -> List[str]:
return self.cfg.get_list("singularity", "exe")

def _pull_invocation(self, logger: logging.Logger, cleanup: ExitStack) -> Tuple[str, List[str]]:
image, invocation = super()._pull_invocation(logger, cleanup)
docker_uri = "docker://" + image

# The docker image layers are cached in SINGULARITY_CACHEDIR, so we don't need to keep the
# *.sif
pulldir = cleanup.enter_context(tempfile.TemporaryDirectory(prefix="miniwdl_sif_"))
return (docker_uri, ["singularity", "pull", "--dir", pulldir, docker_uri])
return (docker_uri, self.cli_exe + ["pull", "--dir", pulldir, docker_uri])

def _run_invocation(self, logger: logging.Logger, cleanup: ExitStack, image: str) -> List[str]:
"""
Formulate `singularity run` command-line invocation
"""

ans = ["singularity"]
ans = self.cli_exe
if logger.isEnabledFor(logging.DEBUG):
ans.append("--verbose")
ans += [
"run",
"--pwd",
os.path.join(self.container_dir, "work"),
]
ans += self.cfg.get_list("singularity", "cli_options")
ans += self.cfg.get_list("singularity", "run_options")

mounts = self.prepare_mounts()
# Also create a scratch directory and mount to /tmp and /var/tmp
Expand Down
14 changes: 9 additions & 5 deletions WDL/runtime/backend/udocker.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,16 +17,17 @@ class UdockerContainer(SubprocessBase):

@classmethod
def global_init(cls, cfg: config.Loader, logger: logging.Logger) -> None:
cmd = cfg.get_list("udocker", "exe") + ["--version"]
try:
udocker_version = subprocess.run(
["udocker", "--version"],
cmd,
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
check=True,
universal_newlines=True,
)
except:
raise RuntimeError("Unable to check `udocker --version`; verify udocker installation")
raise RuntimeError(f"Unable to check `{' '.join(cmd)}`; verify udocker installation")
logger.notice( # pyre-ignore
_(
"udocker runtime initialized (BETA)",
Expand All @@ -38,17 +39,20 @@ def global_init(cls, cfg: config.Loader, logger: logging.Logger) -> None:
def cli_name(self) -> str:
return "udocker"

@property
def cli_exe(self) -> List[str]:
return self.cfg.get_list("udocker", "exe")

def _run_invocation(self, logger: logging.Logger, cleanup: ExitStack, image: str) -> List[str]:
"""
Formulate `udocker run` command-line invocation
"""
ans = [
"udocker",
ans = self.cli_exe + [
"run",
"--workdir",
os.path.join(self.container_dir, "work"),
]
ans += self.cfg.get_list("udocker", "cli_options")
ans += self.cfg.get_list("udocker", "run_options")

mounts = self.prepare_mounts()
logger.info(
Expand Down
9 changes: 6 additions & 3 deletions WDL/runtime/config_templates/default.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -183,18 +183,21 @@ disable_patterns = ["miniwdl_task_omnibus_example:*"]
# Singularity task runtime -- with `singularity` CLI already set up, set
# [task_scheduler] container_backend = singularity
#
# singularity executable and any desired global options
exe = ["singularity"]
# Configuration options to pass to `singularity run` invocations. Defaults for docker-like
# isolation from the host.
cli_options = [
run_options = [
"--containall",
"--no-mount", "hostfs",
"--fakeroot"
]


[udocker]
# udocker task runtime -- with `udocker` CLI already set up, set
# [task_scheduler] container_backend = udocker
#
# udocker executable and any desired global options
exe = ["udocker", "--allow-root"]
# Configuration options to pass to `udocker run` invocations
cli_options = ["--nobanner", "--rm"]
run_options = ["--nobanner", "--rm"]

0 comments on commit 0463f5a

Please sign in to comment.