src/Pure/Admin/isabelle_cronjob.scala
author wenzelm
Mon May 08 12:04:58 2017 +0200 (2017-05-08)
changeset 65770 fb8a7962f2ae
parent 65768 b8da621a3297
child 65771 688a7dd22cbb
permissions -rw-r--r--
clarified modules;
wenzelm@64148
     1
/*  Title:      Pure/Admin/isabelle_cronjob.scala
wenzelm@64148
     2
    Author:     Makarius
wenzelm@64148
     3
wenzelm@64148
     4
Main entry point for administrative cronjob at TUM.
wenzelm@64148
     5
*/
wenzelm@64148
     6
wenzelm@64148
     7
package isabelle
wenzelm@64148
     8
wenzelm@64148
     9
wenzelm@64170
    10
import scala.annotation.tailrec
wenzelm@64170
    11
import scala.collection.mutable
wenzelm@64170
    12
wenzelm@64170
    13
wenzelm@64148
    14
object Isabelle_Cronjob
wenzelm@64148
    15
{
wenzelm@64194
    16
  /* file-system state: owned by main cronjob */
wenzelm@64153
    17
wenzelm@64153
    18
  val main_dir = Path.explode("~/cronjob")
wenzelm@64194
    19
  val main_state_file = main_dir + Path.explode("run/main.state")
wenzelm@64219
    20
  val current_log = main_dir + Path.explode("run/main.log")  // owned by log service
wenzelm@64219
    21
  val cumulative_log = main_dir + Path.explode("log/main.log")  // owned by log service
wenzelm@64153
    22
wenzelm@64231
    23
  val isabelle_repos = main_dir + Path.explode("isabelle")
wenzelm@64231
    24
  val isabelle_repos_test = main_dir + Path.explode("isabelle-test")
wenzelm@64231
    25
  val afp_repos = main_dir + Path.explode("AFP")
wenzelm@64153
    26
wenzelm@64236
    27
  val isabelle_dev_source = "http://isabelle.in.tum.de/repos/isabelle"
wenzelm@64236
    28
  val isabelle_release_source = "http://bitbucket.org/isabelle_project/isabelle-release"
wenzelm@64236
    29
  val afp_source = "https://bitbucket.org/isa-afp/afp-devel"
wenzelm@64236
    30
wenzelm@65736
    31
  val jenkins_jobs = "identify" :: Jenkins.build_log_jobs
wenzelm@65666
    32
wenzelm@64153
    33
wenzelm@64192
    34
wenzelm@64192
    35
  /** particular tasks **/
wenzelm@64192
    36
wenzelm@64405
    37
  /* identify Isabelle + AFP repository snapshots and build release */
wenzelm@64192
    38
wenzelm@64405
    39
  private val build_release =
wenzelm@64405
    40
    Logger_Task("build_release", logger =>
wenzelm@64405
    41
        {
wenzelm@64405
    42
          val rev = Mercurial.repository(isabelle_repos).id()
wenzelm@64405
    43
          val afp_rev = Mercurial.setup_repository(afp_source, afp_repos).id()
wenzelm@64192
    44
wenzelm@64405
    45
          File.write(logger.log_dir + Build_Log.log_filename("isabelle_identify", logger.start_date),
wenzelm@65674
    46
            Build_Log.Identify.content(logger.start_date, Some(rev), Some(afp_rev)))
wenzelm@64405
    47
wenzelm@65770
    48
          Isabelle_Devel.release_snapshot(rev = rev, afp_rev = afp_rev,
wenzelm@65770
    49
            parallel_jobs = 4, remote_mac = "macbroy31")
wenzelm@65770
    50
        })
wenzelm@64192
    51
wenzelm@64192
    52
wenzelm@64194
    53
  /* integrity test of build_history vs. build_history_base */
wenzelm@64193
    54
wenzelm@64194
    55
  private val build_history_base =
wenzelm@64193
    56
    Logger_Task("build_history_base", logger =>
wenzelm@64193
    57
      {
wenzelm@64251
    58
        val hg =
wenzelm@64251
    59
          Mercurial.setup_repository(
wenzelm@64251
    60
            File.standard_path(isabelle_repos), isabelle_repos_test)
wenzelm@64194
    61
        for {
wenzelm@64194
    62
          (result, log_path) <-
wenzelm@64251
    63
            Build_History.build_history(
wenzelm@64251
    64
              hg, rev = "build_history_base", fresh = true, build_args = List("HOL"))
wenzelm@64194
    65
        } {
wenzelm@64194
    66
          result.check
wenzelm@64482
    67
          File.move(log_path, logger.log_dir + log_path.base)
wenzelm@64194
    68
        }
wenzelm@64193
    69
      })
wenzelm@64193
    70
wenzelm@64193
    71
wenzelm@64231
    72
  /* remote build_history */
wenzelm@64231
    73
wenzelm@64409
    74
  sealed case class Remote_Build(
wenzelm@65764
    75
    description: String,
wenzelm@64231
    76
    host: String,
wenzelm@64231
    77
    user: String = "",
wenzelm@65594
    78
    port: Int = 0,
wenzelm@64294
    79
    shared_home: Boolean = true,
wenzelm@64231
    80
    options: String = "",
wenzelm@65732
    81
    args: String = "",
wenzelm@65732
    82
    detect: SQL.Source = "")
wenzelm@65732
    83
  {
wenzelm@65747
    84
    def profile: Build_Status.Profile =
wenzelm@65747
    85
    {
wenzelm@65747
    86
      val sql =
wenzelm@65747
    87
        Build_Log.Prop.build_engine + " = " + SQL.string(Build_History.engine) + " AND " +
wenzelm@65747
    88
        Build_Log.Prop.build_host + " = " + SQL.string(host) +
wenzelm@65747
    89
        (if (detect == "") "" else " AND " + SQL.enclose(detect))
wenzelm@65764
    90
      Build_Status.Profile(description, sql)
wenzelm@65747
    91
    }
wenzelm@65732
    92
  }
wenzelm@64231
    93
wenzelm@65747
    94
  private val remote_builds: List[List[Remote_Build]] =
wenzelm@65732
    95
  {
wenzelm@64231
    96
    List(
wenzelm@65764
    97
      List(Remote_Build("Poly/ML test", "lxbroy8",
wenzelm@65063
    98
        options = "-m32 -B -M1x2,2 -t polyml-test -e 'init_component /home/isabelle/contrib/polyml-5.7-20170217'",
wenzelm@65732
    99
        args = "-N -g timing",
wenzelm@65732
   100
        detect = Build_Log.Prop.build_tags + " = " + SQL.string("polyml-test"))),
wenzelm@65764
   101
      List(Remote_Build("Linux A", "lxbroy9",
wenzelm@65732
   102
        options = "-m32 -B -M1x2,2", args = "-N -g timing")),
wenzelm@65764
   103
      List(Remote_Build("Linux B", "lxbroy10",
wenzelm@65732
   104
        options = "-m32 -B -M1x4,2,4,6", args = "-N -g timing")),
wenzelm@64351
   105
      List(
wenzelm@65768
   106
        Remote_Build("Mac OS X 10.9 Mavericks", "macbroy2", options = "-m32 -M8", args = "-a",
wenzelm@65732
   107
          detect = Build_Log.Prop.build_tags + " IS NULL"),
wenzelm@65768
   108
        Remote_Build("Mac OS X 10.9 Mavericks, quick_and_dirty", "macbroy2",
wenzelm@65732
   109
          options = "-m32 -M8 -t quick_and_dirty", args = "-a -o quick_and_dirty",
wenzelm@65732
   110
          detect = Build_Log.Prop.build_tags + " = " + SQL.string("quick_and_dirty")),
wenzelm@65768
   111
        Remote_Build("Mac OS X 10.9 Mavericks, skip_proofs", "macbroy2",
wenzelm@65732
   112
          options = "-m32 -M8 -t skip_proofs", args = "-a -o skip_proofs",
wenzelm@65732
   113
          detect = Build_Log.Prop.build_tags + " = " + SQL.string("skip_proofs"))),
wenzelm@65768
   114
      List(
wenzelm@65768
   115
        Remote_Build("Mac OS X 10.12 Sierra", "macbroy30", options = "-m32 -M2", args = "-a",
wenzelm@65768
   116
          detect = Build_Log.Prop.build_start + " > date '2017-03-03'")),
wenzelm@65768
   117
      List(Remote_Build("Mac OS X 10.10 Yosemite", "macbroy31", options = "-m32 -M2", args = "-a")),
wenzelm@64351
   118
      List(
wenzelm@65764
   119
        Remote_Build("Windows", "vmnipkow9", shared_home = false,
wenzelm@65732
   120
          options = "-m32 -M4", args = "-a",
wenzelm@65732
   121
          detect = Build_Log.Settings.ML_PLATFORM + " = " + SQL.string("x86-windows")),
wenzelm@65764
   122
        Remote_Build("Windows", "vmnipkow9", shared_home = false,
wenzelm@65732
   123
          options = "-m64 -M4", args = "-a",
wenzelm@65732
   124
          detect = Build_Log.Settings.ML_PLATFORM + " = " + SQL.string("x86_64-windows"))))
wenzelm@65732
   125
  }
wenzelm@64231
   126
wenzelm@65768
   127
  private val remote_builds_old: List[Remote_Build] =
wenzelm@65768
   128
    List(
wenzelm@65768
   129
      Remote_Build("Mac OS X 10.8 Mountain Lion", "macbroy30", options = "-m32 -M2", args = "-a",
wenzelm@65768
   130
        detect = Build_Log.Prop.build_start + " < date '2017-03-03'"))
wenzelm@65768
   131
wenzelm@64231
   132
  private def remote_build_history(rev: String, r: Remote_Build): Logger_Task =
wenzelm@64294
   133
  {
wenzelm@64294
   134
    val task_name = "build_history-" + r.host
wenzelm@64294
   135
    Logger_Task(task_name, logger =>
wenzelm@64231
   136
      {
wenzelm@64231
   137
        using(logger.ssh_context.open_session(host = r.host, user = r.user, port = r.port))(
wenzelm@64259
   138
          ssh =>
wenzelm@64231
   139
            {
wenzelm@64348
   140
              val self_update = !r.shared_home
wenzelm@64348
   141
              val push_isabelle_home = self_update && Mercurial.is_repository(Path.explode("~~"))
wenzelm@64348
   142
wenzelm@64468
   143
              val (results, _) =
wenzelm@64468
   144
                Build_History.remote_build_history(ssh,
wenzelm@64468
   145
                  isabelle_repos,
wenzelm@64468
   146
                  isabelle_repos.ext(r.host),
wenzelm@64538
   147
                  isabelle_repos_source = isabelle_dev_source,
wenzelm@64468
   148
                  self_update = self_update,
wenzelm@64468
   149
                  push_isabelle_home = push_isabelle_home,
wenzelm@64468
   150
                  options =
wenzelm@64479
   151
                    "-r " + Bash.string(rev) + " -N " + Bash.string(task_name) + " -f " + r.options,
wenzelm@64468
   152
                  args = "-o timeout=10800 " + r.args)
wenzelm@64346
   153
wenzelm@64481
   154
              for ((log_name, bytes) <- results) {
wenzelm@64481
   155
                logger.log(Date.now(), log_name)
wenzelm@64468
   156
                Bytes.write(logger.log_dir + Path.explode(log_name), bytes)
wenzelm@64481
   157
              }
wenzelm@64231
   158
            })
wenzelm@64231
   159
      })
wenzelm@64294
   160
  }
wenzelm@64231
   161
wenzelm@65747
   162
  val build_status_profiles: List[Build_Status.Profile] =
wenzelm@65768
   163
    (remote_builds_old :: remote_builds).flatten.map(_.profile)
wenzelm@65747
   164
wenzelm@65746
   165
wenzelm@64192
   166
wenzelm@64192
   167
  /** task logging **/
wenzelm@64171
   168
wenzelm@64193
   169
  sealed case class Logger_Task(name: String = "", body: Logger => Unit)
wenzelm@64154
   170
wenzelm@64257
   171
  class Log_Service private[Isabelle_Cronjob](progress: Progress, val ssh_context: SSH.Context)
wenzelm@64171
   172
  {
wenzelm@64219
   173
    current_log.file.delete
wenzelm@64219
   174
wenzelm@64171
   175
    private val thread: Consumer_Thread[String] =
wenzelm@64171
   176
      Consumer_Thread.fork("cronjob: logger", daemon = true)(
wenzelm@64171
   177
        consume = (text: String) =>
wenzelm@64219
   178
          { // critical
wenzelm@64219
   179
            File.append(current_log, text + "\n")
wenzelm@64219
   180
            File.append(cumulative_log, text + "\n")
wenzelm@64171
   181
            progress.echo(text)
wenzelm@64171
   182
            true
wenzelm@64171
   183
          })
wenzelm@64171
   184
wenzelm@64171
   185
    def shutdown() { thread.shutdown() }
wenzelm@64171
   186
wenzelm@64171
   187
    val hostname = Isabelle_System.hostname()
wenzelm@64171
   188
wenzelm@64171
   189
    def log(date: Date, task_name: String, msg: String): Unit =
wenzelm@64193
   190
      if (task_name != "")
wenzelm@64193
   191
        thread.send(
wenzelm@64193
   192
          "[" + Build_Log.print_date(date) + ", " + hostname + ", " + task_name + "]: " + msg)
wenzelm@64171
   193
wenzelm@64171
   194
    def start_logger(start_date: Date, task_name: String): Logger =
wenzelm@64171
   195
      new Logger(this, start_date, task_name)
wenzelm@64154
   196
wenzelm@64171
   197
    def run_task(start_date: Date, task: Logger_Task)
wenzelm@64171
   198
    {
wenzelm@64171
   199
      val logger = start_logger(start_date, task.name)
wenzelm@64171
   200
      val res = Exn.capture { task.body(logger) }
wenzelm@64171
   201
      val end_date = Date.now()
wenzelm@64171
   202
      val err =
wenzelm@64171
   203
        res match {
wenzelm@64171
   204
          case Exn.Res(_) => None
wenzelm@64295
   205
          case Exn.Exn(exn) =>
wenzelm@64410
   206
            System.err.println("Exception trace for " + quote(task.name) + ":")
wenzelm@64397
   207
            exn.printStackTrace()
wenzelm@64295
   208
            val first_line = Library.split_lines(Exn.message(exn)).headOption getOrElse "exception"
wenzelm@64295
   209
            Some(first_line)
wenzelm@64171
   210
        }
wenzelm@64171
   211
      logger.log_end(end_date, err)
wenzelm@64171
   212
    }
wenzelm@64171
   213
wenzelm@64171
   214
    def fork_task(start_date: Date, task: Logger_Task): Task =
wenzelm@64171
   215
      new Task(task.name, run_task(start_date, task))
wenzelm@64171
   216
  }
wenzelm@64171
   217
wenzelm@64171
   218
  class Logger private[Isabelle_Cronjob](
wenzelm@64171
   219
    val log_service: Log_Service, val start_date: Date, val task_name: String)
wenzelm@64162
   220
  {
wenzelm@64257
   221
    def ssh_context: SSH.Context = log_service.ssh_context
wenzelm@65640
   222
    def options: Options = ssh_context.options
wenzelm@64231
   223
wenzelm@64171
   224
    def log(date: Date, msg: String): Unit = log_service.log(date, task_name, msg)
wenzelm@64171
   225
wenzelm@64171
   226
    def log_end(end_date: Date, err: Option[String])
wenzelm@64171
   227
    {
wenzelm@64171
   228
      val elapsed_time = end_date.time - start_date.time
wenzelm@64171
   229
      val msg =
wenzelm@64171
   230
        (if (err.isEmpty) "finished" else "ERROR " + err.get) +
wenzelm@64197
   231
        (if (elapsed_time.seconds < 3.0) "" else " (" + elapsed_time.message_hms + " elapsed time)")
wenzelm@64171
   232
      log(end_date, msg)
wenzelm@64171
   233
    }
wenzelm@64171
   234
wenzelm@64195
   235
    val log_dir: Path = main_dir + Build_Log.log_subdir(start_date)
wenzelm@64195
   236
wenzelm@64195
   237
    Isabelle_System.mkdirs(log_dir)
wenzelm@64171
   238
    log(start_date, "started")
wenzelm@64171
   239
  }
wenzelm@64171
   240
wenzelm@64171
   241
  class Task private[Isabelle_Cronjob](name: String, body: => Unit)
wenzelm@64171
   242
  {
wenzelm@64171
   243
    private val future: Future[Unit] = Future.thread("cronjob: " + name) { body }
wenzelm@64171
   244
    def is_finished: Boolean = future.is_finished
wenzelm@64162
   245
  }
wenzelm@64153
   246
wenzelm@64170
   247
wenzelm@64170
   248
wenzelm@64153
   249
  /** cronjob **/
wenzelm@64153
   250
wenzelm@64187
   251
  def cronjob(progress: Progress, exclude_task: Set[String])
wenzelm@64153
   252
  {
wenzelm@64171
   253
    /* soft lock */
wenzelm@64153
   254
wenzelm@64153
   255
    val still_running =
wenzelm@64153
   256
      try { Some(File.read(main_state_file)) }
wenzelm@64153
   257
      catch { case ERROR(_) => None }
wenzelm@64153
   258
wenzelm@64153
   259
    still_running match {
wenzelm@64170
   260
      case None | Some("") =>
wenzelm@64153
   261
      case Some(running) =>
wenzelm@64153
   262
        error("Isabelle cronjob appears to be still running: " + running)
wenzelm@64153
   263
    }
wenzelm@64153
   264
wenzelm@64199
   265
wenzelm@64199
   266
    /* log service */
wenzelm@64199
   267
wenzelm@64257
   268
    val log_service = new Log_Service(progress, SSH.init_context(Options.init()))
wenzelm@64154
   269
wenzelm@64199
   270
    def run(start_date: Date, task: Logger_Task) { log_service.run_task(start_date, task) }
wenzelm@64199
   271
wenzelm@64199
   272
    def run_now(task: Logger_Task) { run(Date.now(), task) }
wenzelm@64154
   273
wenzelm@64154
   274
wenzelm@64199
   275
    /* structured tasks */
wenzelm@64184
   276
wenzelm@64350
   277
    def SEQ(tasks: List[Logger_Task]): Logger_Task = Logger_Task(body = _ =>
wenzelm@64193
   278
      for (task <- tasks.iterator if !exclude_task(task.name) || task.name == "")
wenzelm@64199
   279
        run_now(task))
wenzelm@64153
   280
wenzelm@64350
   281
    def PAR(tasks: List[Logger_Task]): Logger_Task = Logger_Task(body = _ =>
wenzelm@64170
   282
      {
wenzelm@64199
   283
        @tailrec def join(running: List[Task])
wenzelm@64199
   284
        {
wenzelm@64199
   285
          running.partition(_.is_finished) match {
wenzelm@64199
   286
            case (Nil, Nil) =>
wenzelm@64199
   287
            case (Nil, _ :: _) => Thread.sleep(500); join(running)
wenzelm@64199
   288
            case (_ :: _, remaining) => join(remaining)
wenzelm@64199
   289
          }
wenzelm@64170
   290
        }
wenzelm@64199
   291
        val start_date = Date.now()
wenzelm@64199
   292
        val running =
wenzelm@64350
   293
          for (task <- tasks if !exclude_task(task.name))
wenzelm@64199
   294
            yield log_service.fork_task(start_date, task)
wenzelm@64199
   295
        join(running)
wenzelm@64199
   296
      })
wenzelm@64193
   297
wenzelm@64170
   298
wenzelm@64170
   299
    /* main */
wenzelm@64170
   300
wenzelm@64199
   301
    val main_start_date = Date.now()
wenzelm@64199
   302
    File.write(main_state_file, main_start_date + " " + log_service.hostname)
wenzelm@64199
   303
wenzelm@64232
   304
    val rev = Mercurial.repository(isabelle_repos).id()
wenzelm@64231
   305
wenzelm@64193
   306
    run(main_start_date,
wenzelm@64193
   307
      Logger_Task("isabelle_cronjob", _ =>
wenzelm@64231
   308
        run_now(
wenzelm@64405
   309
          SEQ(List(build_release, build_history_base,
wenzelm@65640
   310
            PAR(remote_builds.map(seq => SEQ(seq.map(remote_build_history(rev, _))))),
wenzelm@65666
   311
            Logger_Task("jenkins_logs", _ => Jenkins.download_logs(jenkins_jobs, main_dir)),
wenzelm@65770
   312
            Logger_Task("build_log_database",
wenzelm@65770
   313
              logger => Isabelle_Devel.build_log_database(logger.options)),
wenzelm@65770
   314
            Logger_Task("build_status",
wenzelm@65770
   315
              logger => Isabelle_Devel.build_status(logger.options)))))))
wenzelm@64153
   316
wenzelm@64171
   317
    log_service.shutdown()
wenzelm@64170
   318
wenzelm@64153
   319
    main_state_file.file.delete
wenzelm@64153
   320
  }
wenzelm@64153
   321
wenzelm@64153
   322
wenzelm@64153
   323
wenzelm@64153
   324
  /** command line entry point **/
wenzelm@64153
   325
wenzelm@64148
   326
  def main(args: Array[String])
wenzelm@64148
   327
  {
wenzelm@64148
   328
    Command_Line.tool0 {
wenzelm@64148
   329
      var force = false
wenzelm@64148
   330
      var verbose = false
wenzelm@64187
   331
      var exclude_task = Set.empty[String]
wenzelm@64148
   332
wenzelm@64148
   333
      val getopts = Getopts("""
wenzelm@64148
   334
Usage: Admin/cronjob/main [OPTIONS]
wenzelm@64148
   335
wenzelm@64148
   336
  Options are:
wenzelm@64148
   337
    -f           apply force to do anything
wenzelm@64148
   338
    -v           verbose
wenzelm@64187
   339
    -x NAME      exclude tasks with this name
wenzelm@64148
   340
""",
wenzelm@64148
   341
        "f" -> (_ => force = true),
wenzelm@64187
   342
        "v" -> (_ => verbose = true),
wenzelm@64187
   343
        "x:" -> (arg => exclude_task += arg))
wenzelm@64148
   344
wenzelm@64148
   345
      val more_args = getopts(args)
wenzelm@64148
   346
      if (more_args.nonEmpty) getopts.usage()
wenzelm@64148
   347
wenzelm@64909
   348
      val progress = if (verbose) new Console_Progress() else No_Progress
wenzelm@64148
   349
wenzelm@64187
   350
      if (force) cronjob(progress, exclude_task)
wenzelm@64153
   351
      else error("Need to apply force to do anything")
wenzelm@64148
   352
    }
wenzelm@64148
   353
  }
wenzelm@64148
   354
}