|
| 1 | +""" Starts a DIRAC command inside an apptainer container. |
| 2 | +""" |
| 3 | + |
| 4 | +import os |
| 5 | +import shutil |
| 6 | +import sys |
| 7 | + |
| 8 | +import DIRAC |
| 9 | +from DIRAC import S_ERROR, gConfig, gLogger |
| 10 | +from DIRAC.Core.Base.Script import Script |
| 11 | +from DIRAC.Core.Utilities.Subprocess import systemCall |
| 12 | + |
| 13 | +CONTAINER_WRAPPER = """#!/bin/bash |
| 14 | +
|
| 15 | +echo "Starting inner container wrapper scripts (no install) at `date`." |
| 16 | +export DIRAC=%(dirac_env_var)s |
| 17 | +export DIRACOS=%(diracos_env_var)s |
| 18 | +# In any case we need to find a bashrc, and a cfg |
| 19 | +source %(rc_script)s |
| 20 | +%(command)s |
| 21 | +echo "Finishing inner container wrapper scripts at `date`." |
| 22 | +""" |
| 23 | + |
| 24 | +CONTAINER_DEFROOT = "" # Should add something like "/cvmfs/dirac.egi.eu/container/apptainer/alma9/x86_64" |
| 25 | + |
| 26 | + |
| 27 | +def getEnv(): |
| 28 | + """Gets the environment for use within the container. |
| 29 | + We blank almost everything to prevent contamination from the host system. |
| 30 | + """ |
| 31 | + |
| 32 | + payloadEnv = {k: v for k, v in os.environ.items()} |
| 33 | + payloadEnv["TMP"] = "/tmp" |
| 34 | + payloadEnv["TMPDIR"] = "/tmp" |
| 35 | + payloadEnv["X509_USER_PROXY"] = os.path.join("tmp", "proxy") |
| 36 | + payloadEnv["DIRACSYSCONFIG"] = os.path.join("tmp", "dirac.cfg") |
| 37 | + |
| 38 | + return payloadEnv |
| 39 | + |
| 40 | + |
| 41 | +@Script() |
| 42 | +def main(): |
| 43 | + Script.registerArgument(" command: Command to execute inside the container") |
| 44 | + command = Script.getPositionalArgs(group=True) |
| 45 | + |
| 46 | + user_image = None |
| 47 | + Script.registerSwitch("i:", "image=", " apptainer image to use") |
| 48 | + Script.parseCommandLine(ignoreErrors=False) |
| 49 | + for switch in Script.getUnprocessedSwitches(): |
| 50 | + if switch[0].lower() == "i" or switch[0].lower() == "image": |
| 51 | + user_image = switch[1] |
| 52 | + |
| 53 | + wrapSubs = { |
| 54 | + "dirac_env_var": os.environ.get("DIRAC", os.getcwd()), |
| 55 | + "diracos_env_var": os.environ.get("DIRACOS", os.getcwd()), |
| 56 | + } |
| 57 | + wrapSubs["rc_script"] = os.path.join(os.path.realpath(sys.base_prefix), "diracosrc") |
| 58 | + wrapSubs["command"] = command |
| 59 | + shutil.copyfile("dirac.cfg", os.path.join("tmp", "dirac.cfg")) |
| 60 | + |
| 61 | + wrapLoc = os.path.join("tmp", "dirac_container.sh") |
| 62 | + rawfd = os.open(wrapLoc, os.O_WRONLY | os.O_CREAT, 0o700) |
| 63 | + fd = os.fdopen(rawfd, "w") |
| 64 | + fd.write(CONTAINER_WRAPPER % wrapSubs) |
| 65 | + fd.close() |
| 66 | + |
| 67 | + innerCmd = os.path.join("tmp", "dirac_container.sh") |
| 68 | + cmd = ["apptainer", "exec"] |
| 69 | + cmd.extend(["--contain"]) # use minimal /dev and empty other directories (e.g. /tmp and $HOME) |
| 70 | + cmd.extend(["--ipc"]) # run container in a new IPC namespace |
| 71 | + cmd.extend(["--workdir", "/tmp"]) # working directory to be used for /tmp, /var/tmp and $HOME |
| 72 | + cmd.extend(["--home", "/tmp"]) # Avoid using small tmpfs for default $HOME and use scratch /tmp instead |
| 73 | + cmd.extend(["--bind", "{0}:{0}:ro".format(os.path.join(os.path.realpath(sys.base_prefix)))]) |
| 74 | + |
| 75 | + rootImage = user_image or gConfig.getValue("/Resources/Computing/Singularity/ContainerRoot") or CONTAINER_DEFROOT |
| 76 | + |
| 77 | + if os.path.isdir(rootImage) or os.path.isfile(rootImage): |
| 78 | + cmd.extend([rootImage, innerCmd]) |
| 79 | + else: |
| 80 | + # if we are here is because there's no image, or it is not accessible (e.g. not on CVMFS) |
| 81 | + gLogger.error("Apptainer image to exec not found: ", rootImage) |
| 82 | + return S_ERROR("Failed to find Apptainer image to exec") |
| 83 | + |
| 84 | + gLogger.debug(f"Execute Apptainer command: {cmd}") |
| 85 | + result = systemCall(0, cmd, env=getEnv()) |
| 86 | + if not result["OK"]: |
| 87 | + DIRAC.exit(1) |
| 88 | + |
| 89 | + |
| 90 | +if __name__ == "__main__": |
| 91 | + main() |
0 commit comments