src/Pure/Admin/build_docker.scala
author wenzelm
Sun Jan 15 15:50:04 2017 +0100 (2017-01-15)
changeset 64899 749d3a86c6a3
parent 64897 c3320dc731c7
child 64903 ba72a13eb78c
permissions -rw-r--r--
clarified signature: packages may be accessed in Isabelle/Scala;
wenzelm@64890
     1
/*  Title:      Pure/Admin/build_docker.scala
wenzelm@64890
     2
    Author:     Makarius
wenzelm@64890
     3
wenzelm@64890
     4
Build docker image from Isabelle application bundle for Linux.
wenzelm@64890
     5
*/
wenzelm@64890
     6
wenzelm@64890
     7
package isabelle
wenzelm@64890
     8
wenzelm@64890
     9
wenzelm@64890
    10
object Build_Docker
wenzelm@64890
    11
{
wenzelm@64890
    12
  private lazy val default_logic = Isabelle_System.getenv("ISABELLE_LOGIC")
wenzelm@64890
    13
wenzelm@64899
    14
  val packages: List[String] =
wenzelm@64897
    15
    List("less", "lib32stdc++6", "libgomp1", "libwww-perl", "rlwrap", "unzip")
wenzelm@64894
    16
wenzelm@64899
    17
  val package_collections: Map[String, List[String]] =
wenzelm@64895
    18
    Map("X11" -> List("libx11-6", "libxext6", "libxrender1", "libxtst6", "libxi6"),
wenzelm@64895
    19
      "latex" -> List("texlive-fonts-extra", "texlive-latex-extra", "texlive-math-extra"))
wenzelm@64894
    20
wenzelm@64890
    21
  def build_docker(progress: Progress,
wenzelm@64890
    22
    app_archive: Path,
wenzelm@64890
    23
    logic: String = default_logic,
wenzelm@64890
    24
    output: Option[Path] = None,
wenzelm@64899
    25
    more_packages: List[String] = Nil,
wenzelm@64890
    26
    tag: String = "",
wenzelm@64890
    27
    verbose: Boolean = false)
wenzelm@64890
    28
  {
wenzelm@64890
    29
    val distname =
wenzelm@64890
    30
    {
wenzelm@64890
    31
      val Name = "^(Isabelle[^/]*)/?.*$".r
wenzelm@64890
    32
      Isabelle_System.bash("tar tzf " + File.bash_path(app_archive)).check.out_lines match {
wenzelm@64890
    33
        case Name(name) :: _ => name
wenzelm@64890
    34
        case _ => error("Cannot determine Isabelle distribution name from " + app_archive)
wenzelm@64890
    35
      }
wenzelm@64890
    36
    }
wenzelm@64890
    37
wenzelm@64890
    38
    val dockerfile =
wenzelm@64890
    39
      """## Dockerfile for """ + distname + """
wenzelm@64890
    40
wenzelm@64890
    41
FROM ubuntu
wenzelm@64890
    42
SHELL ["/bin/bash", "-c"]
wenzelm@64890
    43
wenzelm@64890
    44
# packages
wenzelm@64890
    45
RUN apt-get -y update && \
wenzelm@64899
    46
  apt-get install -y  """ + (packages ::: more_packages).map(Bash.string(_)).mkString(" ") + """ && \
wenzelm@64890
    47
  apt-get clean
wenzelm@64890
    48
wenzelm@64890
    49
# user
wenzelm@64890
    50
RUN useradd -m isabelle && (echo isabelle:isabelle | chpasswd)
wenzelm@64890
    51
USER isabelle
wenzelm@64890
    52
wenzelm@64890
    53
# Isabelle
wenzelm@64890
    54
WORKDIR /home/isabelle
wenzelm@64890
    55
COPY Isabelle.tar.gz .
wenzelm@64890
    56
RUN tar xzf Isabelle.tar.gz && \
wenzelm@64890
    57
  mv """ + distname + """ Isabelle && \
wenzelm@64890
    58
  rm -rf Isabelle.tar.gz Isabelle/contrib/jdk/x86-linux && \
wenzelm@64890
    59
  perl -pi -e 's,ISABELLE_HOME_USER=.*,ISABELLE_HOME_USER="\$USER_HOME/.isabelle",g;' Isabelle/etc/settings && \
wenzelm@64890
    60
  perl -pi -e 's,ISABELLE_LOGIC=.*,ISABELLE_LOGIC=""" + logic + """,g;' Isabelle/etc/settings && \
wenzelm@64890
    61
  Isabelle/bin/isabelle build -s -b """ + logic + """
wenzelm@64890
    62
wenzelm@64890
    63
ENTRYPOINT ["Isabelle/bin/isabelle"]
wenzelm@64890
    64
"""
wenzelm@64890
    65
wenzelm@64890
    66
    output.foreach(File.write(_, dockerfile))
wenzelm@64890
    67
wenzelm@64890
    68
    Isabelle_System.with_tmp_dir("docker")(tmp_dir =>
wenzelm@64890
    69
      {
wenzelm@64890
    70
        File.write(tmp_dir + Path.explode("Dockerfile"), dockerfile)
wenzelm@64890
    71
        File.copy(app_archive, tmp_dir + Path.explode("Isabelle.tar.gz"))
wenzelm@64890
    72
wenzelm@64890
    73
        val quiet_option = if (verbose) "" else " -q"
wenzelm@64890
    74
        val tag_option = if (tag == "") "" else " -t " + Bash.string(tag)
wenzelm@64890
    75
        progress.bash("docker build" + quiet_option + tag_option + " " + File.bash_path(tmp_dir),
wenzelm@64890
    76
          echo = true).check
wenzelm@64890
    77
      })
wenzelm@64890
    78
  }
wenzelm@64890
    79
wenzelm@64890
    80
wenzelm@64890
    81
  /* Isabelle tool wrapper */
wenzelm@64890
    82
wenzelm@64890
    83
  val isabelle_tool =
wenzelm@64890
    84
    Isabelle_Tool("build_docker", "build Isabelle docker image", args =>
wenzelm@64890
    85
    {
wenzelm@64890
    86
      var logic = default_logic
wenzelm@64890
    87
      var output: Option[Path] = None
wenzelm@64899
    88
      var more_packages: List[String] = Nil
wenzelm@64890
    89
      var verbose = false
wenzelm@64890
    90
      var tag = ""
wenzelm@64890
    91
wenzelm@64890
    92
      val getopts =
wenzelm@64890
    93
        Getopts("""
wenzelm@64890
    94
Usage: isabelle build_docker [OPTIONS] APP_ARCHIVE
wenzelm@64890
    95
wenzelm@64890
    96
  Options are:
wenzelm@64894
    97
    -P NAME      additional Ubuntu package collection (""" +
wenzelm@64895
    98
          package_collections.keySet.toList.sorted.map(quote(_)).mkString(", ") + """)
wenzelm@64890
    99
    -l NAME      default logic (default ISABELLE_LOGIC=""" + quote(default_logic) + """)
wenzelm@64890
   100
    -o FILE      output generated Dockerfile
wenzelm@64894
   101
    -p NAME      additional Ubuntu package
wenzelm@64890
   102
    -t TAG       docker build tag
wenzelm@64890
   103
    -v           verbose
wenzelm@64890
   104
wenzelm@64890
   105
  Build Isabelle docker image with default logic image, using a standard
wenzelm@64890
   106
  Isabelle application archive for Linux.
wenzelm@64890
   107
wenzelm@64895
   108
  Example:
wenzelm@64895
   109
wenzelm@64896
   110
    isabelle build_docker -t test/isabelle:Isabelle2016-1 Isabelle2016-1_app.tar.gz
wenzelm@64890
   111
""",
wenzelm@64894
   112
          "P:" -> (arg =>
wenzelm@64894
   113
            package_collections.get(arg) match {
wenzelm@64899
   114
              case Some(ps) => more_packages :::= ps
wenzelm@64894
   115
              case None => error("Unknown package collection " + quote(arg))
wenzelm@64894
   116
            }),
wenzelm@64890
   117
          "l:" -> (arg => logic = arg),
wenzelm@64890
   118
          "o:" -> (arg => output = Some(Path.explode(arg))),
wenzelm@64899
   119
          "p:" -> (arg => more_packages ::= arg),
wenzelm@64890
   120
          "t:" -> (arg => tag = arg),
wenzelm@64890
   121
          "v" -> (_ => verbose = true))
wenzelm@64890
   122
wenzelm@64890
   123
      val more_args = getopts(args)
wenzelm@64890
   124
      val app_archive =
wenzelm@64890
   125
        more_args match {
wenzelm@64890
   126
          case List(arg) => Path.explode(arg)
wenzelm@64890
   127
          case _ => getopts.usage()
wenzelm@64890
   128
        }
wenzelm@64890
   129
wenzelm@64893
   130
      build_docker(new Console_Progress(), app_archive, logic = logic, output = output,
wenzelm@64899
   131
        more_packages = more_packages, tag = tag, verbose = verbose)
wenzelm@64890
   132
    }, admin = true)
wenzelm@64890
   133
}