Commit 006f0c86 authored by Romain Reuillon's avatar Romain Reuillon

Fix env variables

parent 84c81aaf
......@@ -3,11 +3,12 @@ package container
import java.io.PrintStream
import org.apache.commons.exec.PumpStreamHandler
import org.apache.commons.exec.ShutdownHookProcessDestroyer
import collection.JavaConverters._
object ProcessUtil {
val processDestroyer = new ShutdownHookProcessDestroyer
def execute(cmd: Seq[String], out: PrintStream = System.out, err: PrintStream = System.err, env: Seq[String] = Seq.empty) = {
def execute(cmd: Seq[String], out: PrintStream = System.out, err: PrintStream = System.err, env: Seq[String] = environmentVariables) = {
val runtime = Runtime.getRuntime
val process = runtime.synchronized {
runtime.exec(
......@@ -41,5 +42,7 @@ object ProcessUtil {
} finally processDestroyer.remove(process)
process.exitValue
}
def environmentVariables = System.getenv().asScala.map { case (c, v) => s"$c=$v" }.toSeq
}
......@@ -24,7 +24,7 @@ import container.ImageBuilder.checkImageFile
import container.OCI._
import container.Status._
import better.files._
import collection.JavaConverters._
import scala.sys.process._
object Proot {
......@@ -183,8 +183,7 @@ object Proot {
write("function " + functionName + " {")
maybeArgs match {
case Some(args) => {
for (arg <- args)
write("\texport " + addQuoteToRightSideOfEquality(arg))
for (arg <- args) write("\texport " + addQuoteToRightSideOfEquality(arg))
}
case _ => write("\t:")
}
......@@ -321,6 +320,8 @@ object Proot {
val script = (tmpDirectory.toScala / launchScriptName).toJava
val workDirectoryValue = workDirectory.orElse(image.workDirectory)
val additionalVariables = if (!environmentVariables.exists(_._1 == "HOME")) Seq("HOME" -> "/root") else Seq()
generatePRootScript(
script,
proot = proot,
......@@ -328,7 +329,7 @@ object Proot {
workDirectory = workDirectoryValue,
bind = bind,
containerEnvironmentVariables = image.env,
environmentVariables = environmentVariables,
environmentVariables = environmentVariables ++ additionalVariables,
commandLines = commandLines,
noSeccomp = noSeccomp,
kernel = kernel)
......
......@@ -111,22 +111,26 @@ object Singularity {
buildDirectory.createDirectoryIfNotExists(createParents = true)
try {
val cmd = if (commands.isEmpty) image.command.toSeq else commands
val runFile = "_run_commands.sh"
(buildDirectory / runFile).writeText(cmd.mkString("\n"))
(buildDirectory / runFile).toJava.setExecutable(true)
def variables =
image.env.getOrElse(Seq.empty).map { e =>
val name = e.takeWhile(_ != '=')
val value = e.dropWhile(_ != '=').drop(1)
(s"SINGULARITY_$name", value)
(s"$name", value)
} ++ environmentVariables.map { e =>
(s"SINGULARITY_${e._1}", e._2)
(s"{e._1}", e._2)
}
val cmd =
s"""
|${variables.map { case (n, v) => s"export $n=$v" }.mkString("\n")}
|${(if (commands.isEmpty) image.command.toSeq else commands).mkString("\n")}
""".stripMargin
val runFile = "_run_commands.sh"
(buildDirectory / runFile).writeText(cmd)
(buildDirectory / runFile).toJava.setExecutable(true)
def pwd = workDirectory.map(w => Seq("--pwd", w)).getOrElse(Seq.empty)
val absoluteRootFS = (image.file.toScala / FlatImage.rootfsName).toJava.getAbsolutePath
......@@ -137,6 +141,7 @@ object Singularity {
singularityCommand,
"--silent",
"exec",
"--cleanenv",
"-w") ++
pwd ++
Seq(
......@@ -146,8 +151,7 @@ object Singularity {
Seq("-B", s"${(buildDirectory / runFile).toJava.getAbsolutePath}:/$runFile") ++
Seq(absoluteRootFS, "sh", s"/$runFile"),
output,
error,
variables.map { case (n, v) => s"$n=$v" })
error)
// TODO copy new directories at the root in the sandbox back to rootfs ?
} finally buildDirectory.delete()
......
......@@ -29,7 +29,7 @@ object Test extends App {
//File("/tmp/docker-repo").delete(swallowIOExceptions = true)
File("/tmp/container").delete(swallowIOExceptions = true)
val saved = ImageDownloader.downloadContainerImage(RegistryImage("debian"), File("/tmp/docker-repo/").toJava, 1 minutes, executor = ImageDownloader.Executor.parallel)
val saved = ImageDownloader.downloadContainerImage(RegistryImage("python"), File("/tmp/docker-repo/").toJava, 1 minutes, executor = ImageDownloader.Executor.parallel)
//val saved = ImageBuilder.extractImage(File("/tmp/viablab3.tar").toJava, File("/tmp/extract").toJava)
......@@ -42,16 +42,21 @@ object Test extends App {
// workDirectory = Some("/tmp"),
// bind = Seq("/tmp/test" -> "/tmp/test"))
Singularity.executeFlatImage(
flattenedImage,
File("/tmp/").toJava,
Seq("pip3 install matplotlib", """ python -c "import matplotlib.pyplot as plt" """))
// Docker.executeFlatImage(
// flattenedImage,
// File("/tmp/dock").toJava,
// Seq("/bin/ls", "/bin/ls -l"))
Singularity.executeFlatImage(
flattenedImage,
File("/tmp/sing").toJava,
Seq("touch /test", "/bin/ls", "/bin/ls -l"),
workDirectory = Some("/"))
// Singularity.executeFlatImage(
// flattenedImage,
// File("/tmp/sing").toJava,
// Seq("touch /test", "/bin/ls", "/bin/ls -l"),
// workDirectory = Some("/"))
/*,
bind = Vector("/tmp/youpi" -> "/home/youpi"),
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment