src/Pure/Admin/build_docker.scala
author wenzelm
Sat Jan 14 20:33:55 2017 +0100 (2017-01-14)
changeset 64890 d8ccbd5305bf
child 64893 07a93485d22b
permissions -rw-r--r--
build docker image from Isabelle application bundle for Linux;
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@64890
    14
  def build_docker(progress: Progress,
wenzelm@64890
    15
    app_archive: Path,
wenzelm@64890
    16
    logic: String = default_logic,
wenzelm@64890
    17
    output: Option[Path] = None,
wenzelm@64890
    18
    tag: String = "",
wenzelm@64890
    19
    verbose: Boolean = false)
wenzelm@64890
    20
  {
wenzelm@64890
    21
    val distname =
wenzelm@64890
    22
    {
wenzelm@64890
    23
      val Name = "^(Isabelle[^/]*)/?.*$".r
wenzelm@64890
    24
      Isabelle_System.bash("tar tzf " + File.bash_path(app_archive)).check.out_lines match {
wenzelm@64890
    25
        case Name(name) :: _ => name
wenzelm@64890
    26
        case _ => error("Cannot determine Isabelle distribution name from " + app_archive)
wenzelm@64890
    27
      }
wenzelm@64890
    28
    }
wenzelm@64890
    29
wenzelm@64890
    30
    val dockerfile =
wenzelm@64890
    31
      """## Dockerfile for """ + distname + """
wenzelm@64890
    32
wenzelm@64890
    33
FROM ubuntu
wenzelm@64890
    34
SHELL ["/bin/bash", "-c"]
wenzelm@64890
    35
wenzelm@64890
    36
# packages
wenzelm@64890
    37
RUN apt-get -y update && \
wenzelm@64890
    38
  apt-get install -y less lib32stdc++6 libwww-perl rlwrap unzip && \
wenzelm@64890
    39
  apt-get clean
wenzelm@64890
    40
wenzelm@64890
    41
# user
wenzelm@64890
    42
RUN useradd -m isabelle && (echo isabelle:isabelle | chpasswd)
wenzelm@64890
    43
USER isabelle
wenzelm@64890
    44
wenzelm@64890
    45
# Isabelle
wenzelm@64890
    46
WORKDIR /home/isabelle
wenzelm@64890
    47
COPY Isabelle.tar.gz .
wenzelm@64890
    48
RUN tar xzf Isabelle.tar.gz && \
wenzelm@64890
    49
  mv """ + distname + """ Isabelle && \
wenzelm@64890
    50
  rm -rf Isabelle.tar.gz Isabelle/contrib/jdk/x86-linux && \
wenzelm@64890
    51
  perl -pi -e 's,ISABELLE_HOME_USER=.*,ISABELLE_HOME_USER="\$USER_HOME/.isabelle",g;' Isabelle/etc/settings && \
wenzelm@64890
    52
  perl -pi -e 's,ISABELLE_LOGIC=.*,ISABELLE_LOGIC=""" + logic + """,g;' Isabelle/etc/settings && \
wenzelm@64890
    53
  Isabelle/bin/isabelle build -s -b """ + logic + """
wenzelm@64890
    54
wenzelm@64890
    55
ENTRYPOINT ["Isabelle/bin/isabelle"]
wenzelm@64890
    56
"""
wenzelm@64890
    57
wenzelm@64890
    58
    output.foreach(File.write(_, dockerfile))
wenzelm@64890
    59
wenzelm@64890
    60
    Isabelle_System.with_tmp_dir("docker")(tmp_dir =>
wenzelm@64890
    61
      {
wenzelm@64890
    62
        File.write(tmp_dir + Path.explode("Dockerfile"), dockerfile)
wenzelm@64890
    63
        File.copy(app_archive, tmp_dir + Path.explode("Isabelle.tar.gz"))
wenzelm@64890
    64
wenzelm@64890
    65
        val quiet_option = if (verbose) "" else " -q"
wenzelm@64890
    66
        val tag_option = if (tag == "") "" else " -t " + Bash.string(tag)
wenzelm@64890
    67
        progress.bash("docker build" + quiet_option + tag_option + " " + File.bash_path(tmp_dir),
wenzelm@64890
    68
          echo = true).check
wenzelm@64890
    69
      })
wenzelm@64890
    70
  }
wenzelm@64890
    71
wenzelm@64890
    72
wenzelm@64890
    73
  /* Isabelle tool wrapper */
wenzelm@64890
    74
wenzelm@64890
    75
  val isabelle_tool =
wenzelm@64890
    76
    Isabelle_Tool("build_docker", "build Isabelle docker image", args =>
wenzelm@64890
    77
    {
wenzelm@64890
    78
      var logic = default_logic
wenzelm@64890
    79
      var output: Option[Path] = None
wenzelm@64890
    80
      var verbose = false
wenzelm@64890
    81
      var tag = ""
wenzelm@64890
    82
wenzelm@64890
    83
      val getopts =
wenzelm@64890
    84
        Getopts("""
wenzelm@64890
    85
Usage: isabelle build_docker [OPTIONS] APP_ARCHIVE
wenzelm@64890
    86
wenzelm@64890
    87
  Options are:
wenzelm@64890
    88
    -l NAME      default logic (default ISABELLE_LOGIC=""" + quote(default_logic) + """)
wenzelm@64890
    89
    -o FILE      output generated Dockerfile
wenzelm@64890
    90
    -t TAG       docker build tag
wenzelm@64890
    91
    -v           verbose
wenzelm@64890
    92
wenzelm@64890
    93
  Build Isabelle docker image with default logic image, using a standard
wenzelm@64890
    94
  Isabelle application archive for Linux.
wenzelm@64890
    95
wenzelm@64890
    96
  The remaining DOCKER_ARGS are passed directly to "docker build".
wenzelm@64890
    97
""",
wenzelm@64890
    98
          "l:" -> (arg => logic = arg),
wenzelm@64890
    99
          "o:" -> (arg => output = Some(Path.explode(arg))),
wenzelm@64890
   100
          "t:" -> (arg => tag = arg),
wenzelm@64890
   101
          "v" -> (_ => verbose = true))
wenzelm@64890
   102
wenzelm@64890
   103
      val more_args = getopts(args)
wenzelm@64890
   104
      val app_archive =
wenzelm@64890
   105
        more_args match {
wenzelm@64890
   106
          case List(arg) => Path.explode(arg)
wenzelm@64890
   107
          case _ => getopts.usage()
wenzelm@64890
   108
        }
wenzelm@64890
   109
wenzelm@64890
   110
      build_docker(new Console_Progress(), app_archive, logic, output, tag, verbose)
wenzelm@64890
   111
    }, admin = true)
wenzelm@64890
   112
}