src/Pure/Admin/build_docker.scala
author wenzelm
Mon Jan 16 16:12:29 2017 +0100 (2017-01-16)
changeset 64906 49549acbf025
parent 64905 5e2eb9b14bbe
child 64941 730bc1bcf27c
permissions -rw-r--r--
added option -n, e.g. useful to generate Dockerfile only;
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@64905
    14
  private val Isabelle_Name = """^.*?(Isabelle[^/\\:]+)_(?:app|linux)\.tar\.gz$""".r
wenzelm@64905
    15
wenzelm@64899
    16
  val packages: List[String] =
wenzelm@64903
    17
    List("curl", "less", "lib32stdc++6", "libgomp1", "libwww-perl", "rlwrap", "unzip")
wenzelm@64894
    18
wenzelm@64899
    19
  val package_collections: Map[String, List[String]] =
wenzelm@64895
    20
    Map("X11" -> List("libx11-6", "libxext6", "libxrender1", "libxtst6", "libxi6"),
wenzelm@64895
    21
      "latex" -> List("texlive-fonts-extra", "texlive-latex-extra", "texlive-math-extra"))
wenzelm@64894
    22
wenzelm@64890
    23
  def build_docker(progress: Progress,
wenzelm@64905
    24
    app_archive: String,
wenzelm@64890
    25
    logic: String = default_logic,
wenzelm@64906
    26
    no_build: Boolean = false,
wenzelm@64890
    27
    output: Option[Path] = None,
wenzelm@64899
    28
    more_packages: List[String] = Nil,
wenzelm@64890
    29
    tag: String = "",
wenzelm@64890
    30
    verbose: Boolean = false)
wenzelm@64890
    31
  {
wenzelm@64905
    32
    val isabelle_name =
wenzelm@64905
    33
      app_archive match {
wenzelm@64905
    34
        case Isabelle_Name(name) => name
wenzelm@64890
    35
        case _ => error("Cannot determine Isabelle distribution name from " + app_archive)
wenzelm@64890
    36
      }
wenzelm@64905
    37
    val is_remote = Url.is_wellformed(app_archive)
wenzelm@64890
    38
wenzelm@64890
    39
    val dockerfile =
wenzelm@64905
    40
      """## Dockerfile for """ + isabelle_name + """
wenzelm@64890
    41
wenzelm@64890
    42
FROM ubuntu
wenzelm@64890
    43
SHELL ["/bin/bash", "-c"]
wenzelm@64890
    44
wenzelm@64890
    45
# packages
wenzelm@64890
    46
RUN apt-get -y update && \
wenzelm@64905
    47
  apt-get install -y """ + (packages ::: more_packages).map(Bash.string(_)).mkString(" ") + """ && \
wenzelm@64890
    48
  apt-get clean
wenzelm@64890
    49
wenzelm@64890
    50
# user
wenzelm@64890
    51
RUN useradd -m isabelle && (echo isabelle:isabelle | chpasswd)
wenzelm@64890
    52
USER isabelle
wenzelm@64890
    53
wenzelm@64890
    54
# Isabelle
wenzelm@64890
    55
WORKDIR /home/isabelle
wenzelm@64905
    56
""" +
wenzelm@64905
    57
 (if (is_remote)
wenzelm@64905
    58
   "RUN curl --fail --silent " + Bash.string(app_archive) + " > Isabelle.tar.gz"
wenzelm@64905
    59
  else "COPY Isabelle.tar.gz .") +
wenzelm@64905
    60
"""
wenzelm@64890
    61
RUN tar xzf Isabelle.tar.gz && \
wenzelm@64905
    62
  mv """ + isabelle_name + """ Isabelle && \
wenzelm@64890
    63
  rm -rf Isabelle.tar.gz Isabelle/contrib/jdk/x86-linux && \
wenzelm@64890
    64
  perl -pi -e 's,ISABELLE_HOME_USER=.*,ISABELLE_HOME_USER="\$USER_HOME/.isabelle",g;' Isabelle/etc/settings && \
wenzelm@64890
    65
  perl -pi -e 's,ISABELLE_LOGIC=.*,ISABELLE_LOGIC=""" + logic + """,g;' Isabelle/etc/settings && \
wenzelm@64890
    66
  Isabelle/bin/isabelle build -s -b """ + logic + """
wenzelm@64890
    67
wenzelm@64890
    68
ENTRYPOINT ["Isabelle/bin/isabelle"]
wenzelm@64890
    69
"""
wenzelm@64890
    70
wenzelm@64890
    71
    output.foreach(File.write(_, dockerfile))
wenzelm@64890
    72
wenzelm@64906
    73
    if (!no_build) {
wenzelm@64906
    74
      Isabelle_System.with_tmp_dir("docker")(tmp_dir =>
wenzelm@64906
    75
        {
wenzelm@64906
    76
          File.write(tmp_dir + Path.explode("Dockerfile"), dockerfile)
wenzelm@64905
    77
wenzelm@64906
    78
          if (is_remote) {
wenzelm@64906
    79
            if (!Url.is_readable(app_archive))
wenzelm@64906
    80
              error("Cannot access remote archive " + app_archive)
wenzelm@64906
    81
          }
wenzelm@64906
    82
          else File.copy(Path.explode(app_archive), tmp_dir + Path.explode("Isabelle.tar.gz"))
wenzelm@64890
    83
wenzelm@64906
    84
          val quiet_option = if (verbose) "" else " -q"
wenzelm@64906
    85
          val tag_option = if (tag == "") "" else " -t " + Bash.string(tag)
wenzelm@64906
    86
          progress.bash("docker build" + quiet_option + tag_option + " " + File.bash_path(tmp_dir),
wenzelm@64906
    87
            echo = true).check
wenzelm@64906
    88
        })
wenzelm@64906
    89
    }
wenzelm@64890
    90
  }
wenzelm@64890
    91
wenzelm@64890
    92
wenzelm@64890
    93
  /* Isabelle tool wrapper */
wenzelm@64890
    94
wenzelm@64890
    95
  val isabelle_tool =
wenzelm@64890
    96
    Isabelle_Tool("build_docker", "build Isabelle docker image", args =>
wenzelm@64890
    97
    {
wenzelm@64890
    98
      var logic = default_logic
wenzelm@64906
    99
      var no_build = false
wenzelm@64890
   100
      var output: Option[Path] = None
wenzelm@64899
   101
      var more_packages: List[String] = Nil
wenzelm@64890
   102
      var verbose = false
wenzelm@64890
   103
      var tag = ""
wenzelm@64890
   104
wenzelm@64890
   105
      val getopts =
wenzelm@64890
   106
        Getopts("""
wenzelm@64890
   107
Usage: isabelle build_docker [OPTIONS] APP_ARCHIVE
wenzelm@64890
   108
wenzelm@64890
   109
  Options are:
wenzelm@64894
   110
    -P NAME      additional Ubuntu package collection (""" +
wenzelm@64895
   111
          package_collections.keySet.toList.sorted.map(quote(_)).mkString(", ") + """)
wenzelm@64890
   112
    -l NAME      default logic (default ISABELLE_LOGIC=""" + quote(default_logic) + """)
wenzelm@64906
   113
    -n           no docker build
wenzelm@64890
   114
    -o FILE      output generated Dockerfile
wenzelm@64894
   115
    -p NAME      additional Ubuntu package
wenzelm@64890
   116
    -t TAG       docker build tag
wenzelm@64890
   117
    -v           verbose
wenzelm@64890
   118
wenzelm@64890
   119
  Build Isabelle docker image with default logic image, using a standard
wenzelm@64905
   120
  Isabelle application archive for Linux (local file or remote URL).
wenzelm@64890
   121
wenzelm@64905
   122
  Examples:
wenzelm@64895
   123
wenzelm@64896
   124
    isabelle build_docker -t test/isabelle:Isabelle2016-1 Isabelle2016-1_app.tar.gz
wenzelm@64905
   125
wenzelm@64906
   126
    isabelle build_docker -n -o Dockerfile http://isabelle.in.tum.de/dist/Isabelle2016-1_app.tar.gz
wenzelm@64905
   127
wenzelm@64890
   128
""",
wenzelm@64894
   129
          "P:" -> (arg =>
wenzelm@64894
   130
            package_collections.get(arg) match {
wenzelm@64899
   131
              case Some(ps) => more_packages :::= ps
wenzelm@64894
   132
              case None => error("Unknown package collection " + quote(arg))
wenzelm@64894
   133
            }),
wenzelm@64890
   134
          "l:" -> (arg => logic = arg),
wenzelm@64906
   135
          "n" -> (_ => no_build = true),
wenzelm@64890
   136
          "o:" -> (arg => output = Some(Path.explode(arg))),
wenzelm@64899
   137
          "p:" -> (arg => more_packages ::= arg),
wenzelm@64890
   138
          "t:" -> (arg => tag = arg),
wenzelm@64890
   139
          "v" -> (_ => verbose = true))
wenzelm@64890
   140
wenzelm@64890
   141
      val more_args = getopts(args)
wenzelm@64890
   142
      val app_archive =
wenzelm@64890
   143
        more_args match {
wenzelm@64905
   144
          case List(arg) => arg
wenzelm@64890
   145
          case _ => getopts.usage()
wenzelm@64890
   146
        }
wenzelm@64890
   147
wenzelm@64906
   148
      build_docker(new Console_Progress(), app_archive, logic = logic, no_build = no_build,
wenzelm@64906
   149
        output = output, more_packages = more_packages, tag = tag, verbose = verbose)
wenzelm@64890
   150
    }, admin = true)
wenzelm@64890
   151
}