src/Pure/Tools/phabricator.scala
author wenzelm
Wed, 12 Feb 2020 16:30:23 +0100
changeset 71651 760e19aa9b09
parent 71634 5d5be87330b5
child 71652 8b0b8b9ea653
permissions -rw-r--r--
afford more logging (following defaults on Ubuntu);
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
71164
79736ffe77c3 some support for Phabricator server;
wenzelm
parents:
diff changeset
     1
/*  Title:      Pure/Tools/phabricator.scala
79736ffe77c3 some support for Phabricator server;
wenzelm
parents:
diff changeset
     2
    Author:     Makarius
79736ffe77c3 some support for Phabricator server;
wenzelm
parents:
diff changeset
     3
71266
510b89906d86 discontinued somewhat pointless Isabelle options: setup implicitly assumes Ubuntu 18.04;
wenzelm
parents: 71264
diff changeset
     4
Support for Phabricator server, notably for Ubuntu 18.04 LTS.
510b89906d86 discontinued somewhat pointless Isabelle options: setup implicitly assumes Ubuntu 18.04;
wenzelm
parents: 71264
diff changeset
     5
510b89906d86 discontinued somewhat pointless Isabelle options: setup implicitly assumes Ubuntu 18.04;
wenzelm
parents: 71264
diff changeset
     6
See also:
71164
79736ffe77c3 some support for Phabricator server;
wenzelm
parents:
diff changeset
     7
  - https://www.phacility.com/phabricator
79736ffe77c3 some support for Phabricator server;
wenzelm
parents:
diff changeset
     8
  - https://secure.phabricator.com/book/phabricator
79736ffe77c3 some support for Phabricator server;
wenzelm
parents:
diff changeset
     9
*/
79736ffe77c3 some support for Phabricator server;
wenzelm
parents:
diff changeset
    10
79736ffe77c3 some support for Phabricator server;
wenzelm
parents:
diff changeset
    11
package isabelle
79736ffe77c3 some support for Phabricator server;
wenzelm
parents:
diff changeset
    12
79736ffe77c3 some support for Phabricator server;
wenzelm
parents:
diff changeset
    13
71539
836fde6f9d7e proper search with multiple "pages" of results;
wenzelm
parents: 71537
diff changeset
    14
import scala.collection.mutable
71166
a48112873f81 MySQL setup;
wenzelm
parents: 71165
diff changeset
    15
import scala.util.matching.Regex
a48112873f81 MySQL setup;
wenzelm
parents: 71165
diff changeset
    16
a48112873f81 MySQL setup;
wenzelm
parents: 71165
diff changeset
    17
71164
79736ffe77c3 some support for Phabricator server;
wenzelm
parents:
diff changeset
    18
object Phabricator
79736ffe77c3 some support for Phabricator server;
wenzelm
parents:
diff changeset
    19
{
79736ffe77c3 some support for Phabricator server;
wenzelm
parents:
diff changeset
    20
  /** defaults **/
79736ffe77c3 some support for Phabricator server;
wenzelm
parents:
diff changeset
    21
71247
f4b9dd5ab0cc more phabricator setup;
wenzelm
parents: 71245
diff changeset
    22
  /* required packages */
f4b9dd5ab0cc more phabricator setup;
wenzelm
parents: 71245
diff changeset
    23
f4b9dd5ab0cc more phabricator setup;
wenzelm
parents: 71245
diff changeset
    24
  val packages: List[String] =
71474
6ca561001244 support for pwgen;
wenzelm
parents: 71333
diff changeset
    25
    Build_Docker.packages ::: Linux.packages :::
71247
f4b9dd5ab0cc more phabricator setup;
wenzelm
parents: 71245
diff changeset
    26
    List(
f4b9dd5ab0cc more phabricator setup;
wenzelm
parents: 71245
diff changeset
    27
      // https://secure.phabricator.com/source/phabricator/browse/master/scripts/install/install_ubuntu.sh 15e6e2adea61
f4b9dd5ab0cc more phabricator setup;
wenzelm
parents: 71245
diff changeset
    28
      "git", "mysql-server", "apache2", "libapache2-mod-php", "php", "php-mysql",
f4b9dd5ab0cc more phabricator setup;
wenzelm
parents: 71245
diff changeset
    29
      "php-gd", "php-curl", "php-apcu", "php-cli", "php-json", "php-mbstring",
f4b9dd5ab0cc more phabricator setup;
wenzelm
parents: 71245
diff changeset
    30
      // more packages
71497
26a40fc962e8 more packages: required for svn;
wenzelm
parents: 71496
diff changeset
    31
      "php-xml", "php-zip", "python-pygments", "ssh", "subversion",
71489
5a2033fc8f3d avoid odd (harmless) problem with Mercurial 4.5.3 provided by Ubuntu 18.04 on first push: "couldn't write revision branch cache names";
wenzelm
parents: 71486
diff changeset
    32
      // mercurial build packages
5a2033fc8f3d avoid odd (harmless) problem with Mercurial 4.5.3 provided by Ubuntu 18.04 on first push: "couldn't write revision branch cache names";
wenzelm
parents: 71486
diff changeset
    33
      "make", "gcc", "python", "python-dev", "python-docutils", "python-pygments", "python-openssl")
71247
f4b9dd5ab0cc more phabricator setup;
wenzelm
parents: 71245
diff changeset
    34
f4b9dd5ab0cc more phabricator setup;
wenzelm
parents: 71245
diff changeset
    35
f4b9dd5ab0cc more phabricator setup;
wenzelm
parents: 71245
diff changeset
    36
  /* global system resources */
f4b9dd5ab0cc more phabricator setup;
wenzelm
parents: 71245
diff changeset
    37
71266
510b89906d86 discontinued somewhat pointless Isabelle options: setup implicitly assumes Ubuntu 18.04;
wenzelm
parents: 71264
diff changeset
    38
  val www_user = "www-data"
510b89906d86 discontinued somewhat pointless Isabelle options: setup implicitly assumes Ubuntu 18.04;
wenzelm
parents: 71264
diff changeset
    39
71247
f4b9dd5ab0cc more phabricator setup;
wenzelm
parents: 71245
diff changeset
    40
  val daemon_user = "phabricator"
f4b9dd5ab0cc more phabricator setup;
wenzelm
parents: 71245
diff changeset
    41
71308
8c1c717a830b configure SSH hosting via "isabelle phabricator_setup_ssh";
wenzelm
parents: 71302
diff changeset
    42
  val sshd_config = Path.explode("/etc/ssh/sshd_config")
71247
f4b9dd5ab0cc more phabricator setup;
wenzelm
parents: 71245
diff changeset
    43
f4b9dd5ab0cc more phabricator setup;
wenzelm
parents: 71245
diff changeset
    44
f4b9dd5ab0cc more phabricator setup;
wenzelm
parents: 71245
diff changeset
    45
  /* installation parameters */
f4b9dd5ab0cc more phabricator setup;
wenzelm
parents: 71245
diff changeset
    46
71164
79736ffe77c3 some support for Phabricator server;
wenzelm
parents:
diff changeset
    47
  val default_name = "vcs"
79736ffe77c3 some support for Phabricator server;
wenzelm
parents:
diff changeset
    48
71250
6bf53035baf0 clarified name prefixes: global config always uses "isabelle-phabricator";
wenzelm
parents: 71249
diff changeset
    49
  def phabricator_name(name: String = "", ext: String = ""): String =
6bf53035baf0 clarified name prefixes: global config always uses "isabelle-phabricator";
wenzelm
parents: 71249
diff changeset
    50
    "phabricator" + (if (name.isEmpty) "" else "-" + name) + (if (ext.isEmpty) "" else "." + ext)
6bf53035baf0 clarified name prefixes: global config always uses "isabelle-phabricator";
wenzelm
parents: 71249
diff changeset
    51
6bf53035baf0 clarified name prefixes: global config always uses "isabelle-phabricator";
wenzelm
parents: 71249
diff changeset
    52
  def isabelle_phabricator_name(name: String = "", ext: String = ""): String =
6bf53035baf0 clarified name prefixes: global config always uses "isabelle-phabricator";
wenzelm
parents: 71249
diff changeset
    53
    "isabelle-" + phabricator_name(name = name, ext = ext)
71164
79736ffe77c3 some support for Phabricator server;
wenzelm
parents:
diff changeset
    54
71266
510b89906d86 discontinued somewhat pointless Isabelle options: setup implicitly assumes Ubuntu 18.04;
wenzelm
parents: 71264
diff changeset
    55
  def default_root(name: String): Path =
510b89906d86 discontinued somewhat pointless Isabelle options: setup implicitly assumes Ubuntu 18.04;
wenzelm
parents: 71264
diff changeset
    56
    Path.explode("/var/www") + Path.basic(phabricator_name(name = name))
71164
79736ffe77c3 some support for Phabricator server;
wenzelm
parents:
diff changeset
    57
71266
510b89906d86 discontinued somewhat pointless Isabelle options: setup implicitly assumes Ubuntu 18.04;
wenzelm
parents: 71264
diff changeset
    58
  def default_repo(name: String): Path = default_root(name) + Path.basic("repo")
71164
79736ffe77c3 some support for Phabricator server;
wenzelm
parents:
diff changeset
    59
71270
wenzelm
parents: 71269
diff changeset
    60
  val default_mailers: Path = Path.explode("mailers.json")
71264
114db2b5a5f8 support for Phabricator mail configuration;
wenzelm
parents: 71256
diff changeset
    61
71308
8c1c717a830b configure SSH hosting via "isabelle phabricator_setup_ssh";
wenzelm
parents: 71302
diff changeset
    62
  val default_system_port = 22
8c1c717a830b configure SSH hosting via "isabelle phabricator_setup_ssh";
wenzelm
parents: 71302
diff changeset
    63
  val alternative_system_port = 222
8c1c717a830b configure SSH hosting via "isabelle phabricator_setup_ssh";
wenzelm
parents: 71302
diff changeset
    64
  val default_server_port = 2222
8c1c717a830b configure SSH hosting via "isabelle phabricator_setup_ssh";
wenzelm
parents: 71302
diff changeset
    65
71571
597059a44d6f prefer mercurial-2.8.2 for more robustness (i.e. the version used by the hosting service https://admin.phacility.com);
wenzelm
parents: 71541
diff changeset
    66
  val standard_mercurial_source = "https://www.mercurial-scm.org/release/mercurial-2.8.2.tar.gz"
71489
5a2033fc8f3d avoid odd (harmless) problem with Mercurial 4.5.3 provided by Ubuntu 18.04 on first push: "couldn't write revision branch cache names";
wenzelm
parents: 71486
diff changeset
    67
71164
79736ffe77c3 some support for Phabricator server;
wenzelm
parents:
diff changeset
    68
79736ffe77c3 some support for Phabricator server;
wenzelm
parents:
diff changeset
    69
79736ffe77c3 some support for Phabricator server;
wenzelm
parents:
diff changeset
    70
  /** global configuration **/
79736ffe77c3 some support for Phabricator server;
wenzelm
parents:
diff changeset
    71
71250
6bf53035baf0 clarified name prefixes: global config always uses "isabelle-phabricator";
wenzelm
parents: 71249
diff changeset
    72
  val global_config = Path.explode("/etc/" + isabelle_phabricator_name(ext = "conf"))
71164
79736ffe77c3 some support for Phabricator server;
wenzelm
parents:
diff changeset
    73
71320
730090397e0d clarified signature;
wenzelm
parents: 71315
diff changeset
    74
  def global_config_script(
730090397e0d clarified signature;
wenzelm
parents: 71315
diff changeset
    75
    init: String = "",
730090397e0d clarified signature;
wenzelm
parents: 71315
diff changeset
    76
    body: String = "",
730090397e0d clarified signature;
wenzelm
parents: 71315
diff changeset
    77
    exit: String = ""): String =
730090397e0d clarified signature;
wenzelm
parents: 71315
diff changeset
    78
  {
71491
de59dd86760f tuned signature;
wenzelm
parents: 71490
diff changeset
    79
"""#!/bin/bash
71493
49bc17bf4384 more robust: avoid accidental use of stdin;
wenzelm
parents: 71492
diff changeset
    80
""" + (if (init.nonEmpty) "\n" + init else "") + """
49bc17bf4384 more robust: avoid accidental use of stdin;
wenzelm
parents: 71492
diff changeset
    81
{
71320
730090397e0d clarified signature;
wenzelm
parents: 71315
diff changeset
    82
  while { unset REPLY; read -r; test "$?" = 0 -o -n "$REPLY"; }
730090397e0d clarified signature;
wenzelm
parents: 71315
diff changeset
    83
  do
730090397e0d clarified signature;
wenzelm
parents: 71315
diff changeset
    84
    NAME="$(echo "$REPLY" | cut -d: -f1)"
730090397e0d clarified signature;
wenzelm
parents: 71315
diff changeset
    85
    ROOT="$(echo "$REPLY" | cut -d: -f2)"
71493
49bc17bf4384 more robust: avoid accidental use of stdin;
wenzelm
parents: 71492
diff changeset
    86
    {
49bc17bf4384 more robust: avoid accidental use of stdin;
wenzelm
parents: 71492
diff changeset
    87
""" + Library.prefix_lines("      ", body) + """
49bc17bf4384 more robust: avoid accidental use of stdin;
wenzelm
parents: 71492
diff changeset
    88
    } < /dev/null
49bc17bf4384 more robust: avoid accidental use of stdin;
wenzelm
parents: 71492
diff changeset
    89
  done
49bc17bf4384 more robust: avoid accidental use of stdin;
wenzelm
parents: 71492
diff changeset
    90
} < """ + File.bash_path(global_config) + "\n" +
49bc17bf4384 more robust: avoid accidental use of stdin;
wenzelm
parents: 71492
diff changeset
    91
    (if (exit.nonEmpty) "\n" + exit + "\n" else "")
71320
730090397e0d clarified signature;
wenzelm
parents: 71315
diff changeset
    92
  }
730090397e0d clarified signature;
wenzelm
parents: 71315
diff changeset
    93
71164
79736ffe77c3 some support for Phabricator server;
wenzelm
parents:
diff changeset
    94
  sealed case class Config(name: String, root: Path)
71165
46847076477c Apache setup;
wenzelm
parents: 71164
diff changeset
    95
  {
71250
6bf53035baf0 clarified name prefixes: global config always uses "isabelle-phabricator";
wenzelm
parents: 71249
diff changeset
    96
    def home: Path = root + Path.explode(phabricator_name())
71166
a48112873f81 MySQL setup;
wenzelm
parents: 71165
diff changeset
    97
a48112873f81 MySQL setup;
wenzelm
parents: 71165
diff changeset
    98
    def execute(command: String): Process_Result =
71301
wenzelm
parents: 71300
diff changeset
    99
      Isabelle_System.bash("bin/" + command, cwd = home.file, redirect = true).check
71165
46847076477c Apache setup;
wenzelm
parents: 71164
diff changeset
   100
  }
71164
79736ffe77c3 some support for Phabricator server;
wenzelm
parents:
diff changeset
   101
79736ffe77c3 some support for Phabricator server;
wenzelm
parents:
diff changeset
   102
  def read_config(): List[Config] =
79736ffe77c3 some support for Phabricator server;
wenzelm
parents:
diff changeset
   103
  {
79736ffe77c3 some support for Phabricator server;
wenzelm
parents:
diff changeset
   104
    if (global_config.is_file) {
79736ffe77c3 some support for Phabricator server;
wenzelm
parents:
diff changeset
   105
      for (entry <- Library.trim_split_lines(File.read(global_config)) if entry.nonEmpty)
79736ffe77c3 some support for Phabricator server;
wenzelm
parents:
diff changeset
   106
      yield {
79736ffe77c3 some support for Phabricator server;
wenzelm
parents:
diff changeset
   107
        space_explode(':', entry) match {
79736ffe77c3 some support for Phabricator server;
wenzelm
parents:
diff changeset
   108
          case List(name, root) => Config(name, Path.explode(root))
79736ffe77c3 some support for Phabricator server;
wenzelm
parents:
diff changeset
   109
          case _ => error("Malformed config file " + global_config + "\nentry " + quote(entry))
79736ffe77c3 some support for Phabricator server;
wenzelm
parents:
diff changeset
   110
        }
79736ffe77c3 some support for Phabricator server;
wenzelm
parents:
diff changeset
   111
      }
79736ffe77c3 some support for Phabricator server;
wenzelm
parents:
diff changeset
   112
    }
79736ffe77c3 some support for Phabricator server;
wenzelm
parents:
diff changeset
   113
    else Nil
79736ffe77c3 some support for Phabricator server;
wenzelm
parents:
diff changeset
   114
  }
79736ffe77c3 some support for Phabricator server;
wenzelm
parents:
diff changeset
   115
79736ffe77c3 some support for Phabricator server;
wenzelm
parents:
diff changeset
   116
  def write_config(configs: List[Config])
79736ffe77c3 some support for Phabricator server;
wenzelm
parents:
diff changeset
   117
  {
79736ffe77c3 some support for Phabricator server;
wenzelm
parents:
diff changeset
   118
    File.write(global_config,
79736ffe77c3 some support for Phabricator server;
wenzelm
parents:
diff changeset
   119
      configs.map(config => config.name + ":" + config.root.implode).mkString("", "\n", "\n"))
79736ffe77c3 some support for Phabricator server;
wenzelm
parents:
diff changeset
   120
  }
79736ffe77c3 some support for Phabricator server;
wenzelm
parents:
diff changeset
   121
79736ffe77c3 some support for Phabricator server;
wenzelm
parents:
diff changeset
   122
  def get_config(name: String): Config =
79736ffe77c3 some support for Phabricator server;
wenzelm
parents:
diff changeset
   123
    read_config().find(config => config.name == name) getOrElse
79736ffe77c3 some support for Phabricator server;
wenzelm
parents:
diff changeset
   124
      error("Bad Isabelle/Phabricator installation " + quote(name))
79736ffe77c3 some support for Phabricator server;
wenzelm
parents:
diff changeset
   125
79736ffe77c3 some support for Phabricator server;
wenzelm
parents:
diff changeset
   126
79736ffe77c3 some support for Phabricator server;
wenzelm
parents:
diff changeset
   127
71508
51c19a44cfed support for conduit API;
wenzelm
parents: 71504
diff changeset
   128
  /** administrative tools **/
71296
d3ededaa77b3 added "isabelle phabricator";
wenzelm
parents: 71277
diff changeset
   129
d3ededaa77b3 added "isabelle phabricator";
wenzelm
parents: 71277
diff changeset
   130
  /* Isabelle tool wrapper */
d3ededaa77b3 added "isabelle phabricator";
wenzelm
parents: 71277
diff changeset
   131
d3ededaa77b3 added "isabelle phabricator";
wenzelm
parents: 71277
diff changeset
   132
  val isabelle_tool1 =
d3ededaa77b3 added "isabelle phabricator";
wenzelm
parents: 71277
diff changeset
   133
    Isabelle_Tool("phabricator", "invoke command-line tool within Phabricator home directory", args =>
d3ededaa77b3 added "isabelle phabricator";
wenzelm
parents: 71277
diff changeset
   134
    {
71300
67c2fed5b0e9 more options;
wenzelm
parents: 71297
diff changeset
   135
      var list = false
71296
d3ededaa77b3 added "isabelle phabricator";
wenzelm
parents: 71277
diff changeset
   136
      var name = default_name
d3ededaa77b3 added "isabelle phabricator";
wenzelm
parents: 71277
diff changeset
   137
d3ededaa77b3 added "isabelle phabricator";
wenzelm
parents: 71277
diff changeset
   138
      val getopts =
d3ededaa77b3 added "isabelle phabricator";
wenzelm
parents: 71277
diff changeset
   139
        Getopts("""
d3ededaa77b3 added "isabelle phabricator";
wenzelm
parents: 71277
diff changeset
   140
Usage: isabelle phabricator [OPTIONS] COMMAND [ARGS...]
d3ededaa77b3 added "isabelle phabricator";
wenzelm
parents: 71277
diff changeset
   141
d3ededaa77b3 added "isabelle phabricator";
wenzelm
parents: 71277
diff changeset
   142
  Options are:
71300
67c2fed5b0e9 more options;
wenzelm
parents: 71297
diff changeset
   143
    -l           list available Phabricator installations
71296
d3ededaa77b3 added "isabelle phabricator";
wenzelm
parents: 71277
diff changeset
   144
    -n NAME      Phabricator installation name (default: """ + quote(default_name) + """)
d3ededaa77b3 added "isabelle phabricator";
wenzelm
parents: 71277
diff changeset
   145
71302
c073c4e79518 more documentation;
wenzelm
parents: 71301
diff changeset
   146
  Invoke a command-line tool within the home directory of the named
c073c4e79518 more documentation;
wenzelm
parents: 71301
diff changeset
   147
  Phabricator installation.
71296
d3ededaa77b3 added "isabelle phabricator";
wenzelm
parents: 71277
diff changeset
   148
""",
71300
67c2fed5b0e9 more options;
wenzelm
parents: 71297
diff changeset
   149
          "l" -> (_ => list = true),
71296
d3ededaa77b3 added "isabelle phabricator";
wenzelm
parents: 71277
diff changeset
   150
          "n:" -> (arg => name = arg))
d3ededaa77b3 added "isabelle phabricator";
wenzelm
parents: 71277
diff changeset
   151
d3ededaa77b3 added "isabelle phabricator";
wenzelm
parents: 71277
diff changeset
   152
      val more_args = getopts(args)
71300
67c2fed5b0e9 more options;
wenzelm
parents: 71297
diff changeset
   153
      if (more_args.isEmpty && !list) getopts.usage()
71296
d3ededaa77b3 added "isabelle phabricator";
wenzelm
parents: 71277
diff changeset
   154
d3ededaa77b3 added "isabelle phabricator";
wenzelm
parents: 71277
diff changeset
   155
      val progress = new Console_Progress
d3ededaa77b3 added "isabelle phabricator";
wenzelm
parents: 71277
diff changeset
   156
71300
67c2fed5b0e9 more options;
wenzelm
parents: 71297
diff changeset
   157
      if (list) {
67c2fed5b0e9 more options;
wenzelm
parents: 71297
diff changeset
   158
        for (config <- read_config()) {
71302
c073c4e79518 more documentation;
wenzelm
parents: 71301
diff changeset
   159
          progress.echo("phabricator " + quote(config.name) + " root " + config.root)
71300
67c2fed5b0e9 more options;
wenzelm
parents: 71297
diff changeset
   160
        }
67c2fed5b0e9 more options;
wenzelm
parents: 71297
diff changeset
   161
      }
71485
b4401dfd6544 clarified "isabelle phabricator -l": avoid surprise with non-existent default installation;
wenzelm
parents: 71483
diff changeset
   162
      else {
b4401dfd6544 clarified "isabelle phabricator -l": avoid surprise with non-existent default installation;
wenzelm
parents: 71483
diff changeset
   163
        val config = get_config(name)
b4401dfd6544 clarified "isabelle phabricator -l": avoid surprise with non-existent default installation;
wenzelm
parents: 71483
diff changeset
   164
        val result = progress.bash(Bash.strings(more_args), cwd = config.home.file, echo = true)
b4401dfd6544 clarified "isabelle phabricator -l": avoid surprise with non-existent default installation;
wenzelm
parents: 71483
diff changeset
   165
        if (!result.ok) error("Return code: " + result.rc.toString)
b4401dfd6544 clarified "isabelle phabricator -l": avoid surprise with non-existent default installation;
wenzelm
parents: 71483
diff changeset
   166
      }
71296
d3ededaa77b3 added "isabelle phabricator";
wenzelm
parents: 71277
diff changeset
   167
    })
d3ededaa77b3 added "isabelle phabricator";
wenzelm
parents: 71277
diff changeset
   168
d3ededaa77b3 added "isabelle phabricator";
wenzelm
parents: 71277
diff changeset
   169
d3ededaa77b3 added "isabelle phabricator";
wenzelm
parents: 71277
diff changeset
   170
71164
79736ffe77c3 some support for Phabricator server;
wenzelm
parents:
diff changeset
   171
  /** setup **/
79736ffe77c3 some support for Phabricator server;
wenzelm
parents:
diff changeset
   172
71247
f4b9dd5ab0cc more phabricator setup;
wenzelm
parents: 71245
diff changeset
   173
  def user_setup(name: String, description: String, ssh_setup: Boolean = false)
f4b9dd5ab0cc more phabricator setup;
wenzelm
parents: 71245
diff changeset
   174
  {
f4b9dd5ab0cc more phabricator setup;
wenzelm
parents: 71245
diff changeset
   175
    if (!Linux.user_exists(name)) {
71252
b64fc38327ae prefer system user setup, e.g. avoid occurrence on login screen;
wenzelm
parents: 71251
diff changeset
   176
      Linux.user_add(name, description = description, system = true, ssh_setup = ssh_setup)
71247
f4b9dd5ab0cc more phabricator setup;
wenzelm
parents: 71245
diff changeset
   177
    }
f4b9dd5ab0cc more phabricator setup;
wenzelm
parents: 71245
diff changeset
   178
    else if (Linux.user_description(name) != description) {
f4b9dd5ab0cc more phabricator setup;
wenzelm
parents: 71245
diff changeset
   179
      error("User " + quote(name) + " already exists --" +
f4b9dd5ab0cc more phabricator setup;
wenzelm
parents: 71245
diff changeset
   180
        " for Phabricator it should have the description:\n  " + quote(description))
f4b9dd5ab0cc more phabricator setup;
wenzelm
parents: 71245
diff changeset
   181
    }
f4b9dd5ab0cc more phabricator setup;
wenzelm
parents: 71245
diff changeset
   182
  }
f4b9dd5ab0cc more phabricator setup;
wenzelm
parents: 71245
diff changeset
   183
71491
de59dd86760f tuned signature;
wenzelm
parents: 71490
diff changeset
   184
  def command_setup(name: String,
de59dd86760f tuned signature;
wenzelm
parents: 71490
diff changeset
   185
    init: String = "",
de59dd86760f tuned signature;
wenzelm
parents: 71490
diff changeset
   186
    body: String = "",
de59dd86760f tuned signature;
wenzelm
parents: 71490
diff changeset
   187
    exit: String = ""): Path =
71479
wenzelm
parents: 71478
diff changeset
   188
  {
wenzelm
parents: 71478
diff changeset
   189
    val command = Path.explode("/usr/local/bin") + Path.basic(name)
71491
de59dd86760f tuned signature;
wenzelm
parents: 71490
diff changeset
   190
    File.write(command, global_config_script(init = init, body = body, exit = exit))
71479
wenzelm
parents: 71478
diff changeset
   191
    Isabelle_System.chmod("755", command)
wenzelm
parents: 71478
diff changeset
   192
    Isabelle_System.chown("root:root", command)
wenzelm
parents: 71478
diff changeset
   193
    command
wenzelm
parents: 71478
diff changeset
   194
  }
wenzelm
parents: 71478
diff changeset
   195
71489
5a2033fc8f3d avoid odd (harmless) problem with Mercurial 4.5.3 provided by Ubuntu 18.04 on first push: "couldn't write revision branch cache names";
wenzelm
parents: 71486
diff changeset
   196
  def mercurial_setup(mercurial_source: String, progress: Progress = No_Progress)
5a2033fc8f3d avoid odd (harmless) problem with Mercurial 4.5.3 provided by Ubuntu 18.04 on first push: "couldn't write revision branch cache names";
wenzelm
parents: 71486
diff changeset
   197
  {
71490
5b3a813853bb tuned message;
wenzelm
parents: 71489
diff changeset
   198
    progress.echo("\nMercurial installation from source " + quote(mercurial_source) + " ...")
71489
5a2033fc8f3d avoid odd (harmless) problem with Mercurial 4.5.3 provided by Ubuntu 18.04 on first push: "couldn't write revision branch cache names";
wenzelm
parents: 71486
diff changeset
   199
    Isabelle_System.with_tmp_dir("mercurial")(tmp_dir =>
5a2033fc8f3d avoid odd (harmless) problem with Mercurial 4.5.3 provided by Ubuntu 18.04 on first push: "couldn't write revision branch cache names";
wenzelm
parents: 71486
diff changeset
   200
    {
5a2033fc8f3d avoid odd (harmless) problem with Mercurial 4.5.3 provided by Ubuntu 18.04 on first push: "couldn't write revision branch cache names";
wenzelm
parents: 71486
diff changeset
   201
      val archive =
5a2033fc8f3d avoid odd (harmless) problem with Mercurial 4.5.3 provided by Ubuntu 18.04 on first push: "couldn't write revision branch cache names";
wenzelm
parents: 71486
diff changeset
   202
        if (Url.is_wellformed(mercurial_source)) {
5a2033fc8f3d avoid odd (harmless) problem with Mercurial 4.5.3 provided by Ubuntu 18.04 on first push: "couldn't write revision branch cache names";
wenzelm
parents: 71486
diff changeset
   203
          val archive = tmp_dir + Path.basic("mercurial.tar.gz")
5a2033fc8f3d avoid odd (harmless) problem with Mercurial 4.5.3 provided by Ubuntu 18.04 on first push: "couldn't write revision branch cache names";
wenzelm
parents: 71486
diff changeset
   204
          Bytes.write(archive, Url.read_bytes(Url(mercurial_source)))
5a2033fc8f3d avoid odd (harmless) problem with Mercurial 4.5.3 provided by Ubuntu 18.04 on first push: "couldn't write revision branch cache names";
wenzelm
parents: 71486
diff changeset
   205
          archive
5a2033fc8f3d avoid odd (harmless) problem with Mercurial 4.5.3 provided by Ubuntu 18.04 on first push: "couldn't write revision branch cache names";
wenzelm
parents: 71486
diff changeset
   206
        }
5a2033fc8f3d avoid odd (harmless) problem with Mercurial 4.5.3 provided by Ubuntu 18.04 on first push: "couldn't write revision branch cache names";
wenzelm
parents: 71486
diff changeset
   207
        else Path.explode(mercurial_source)
5a2033fc8f3d avoid odd (harmless) problem with Mercurial 4.5.3 provided by Ubuntu 18.04 on first push: "couldn't write revision branch cache names";
wenzelm
parents: 71486
diff changeset
   208
5a2033fc8f3d avoid odd (harmless) problem with Mercurial 4.5.3 provided by Ubuntu 18.04 on first push: "couldn't write revision branch cache names";
wenzelm
parents: 71486
diff changeset
   209
      Isabelle_System.gnutar("-xzf " + File.bash_path(archive), dir = tmp_dir).check
5a2033fc8f3d avoid odd (harmless) problem with Mercurial 4.5.3 provided by Ubuntu 18.04 on first push: "couldn't write revision branch cache names";
wenzelm
parents: 71486
diff changeset
   210
5a2033fc8f3d avoid odd (harmless) problem with Mercurial 4.5.3 provided by Ubuntu 18.04 on first push: "couldn't write revision branch cache names";
wenzelm
parents: 71486
diff changeset
   211
      File.read_dir(tmp_dir).filter(name => (tmp_dir + Path.basic(name)).is_dir) match {
5a2033fc8f3d avoid odd (harmless) problem with Mercurial 4.5.3 provided by Ubuntu 18.04 on first push: "couldn't write revision branch cache names";
wenzelm
parents: 71486
diff changeset
   212
        case List(dir) =>
5a2033fc8f3d avoid odd (harmless) problem with Mercurial 4.5.3 provided by Ubuntu 18.04 on first push: "couldn't write revision branch cache names";
wenzelm
parents: 71486
diff changeset
   213
          val build_dir = tmp_dir + Path.basic(dir)
5a2033fc8f3d avoid odd (harmless) problem with Mercurial 4.5.3 provided by Ubuntu 18.04 on first push: "couldn't write revision branch cache names";
wenzelm
parents: 71486
diff changeset
   214
          progress.bash("make all && make install", cwd = build_dir.file, echo = true).check
5a2033fc8f3d avoid odd (harmless) problem with Mercurial 4.5.3 provided by Ubuntu 18.04 on first push: "couldn't write revision branch cache names";
wenzelm
parents: 71486
diff changeset
   215
        case dirs =>
5a2033fc8f3d avoid odd (harmless) problem with Mercurial 4.5.3 provided by Ubuntu 18.04 on first push: "couldn't write revision branch cache names";
wenzelm
parents: 71486
diff changeset
   216
          error("Bad archive " + archive +
5a2033fc8f3d avoid odd (harmless) problem with Mercurial 4.5.3 provided by Ubuntu 18.04 on first push: "couldn't write revision branch cache names";
wenzelm
parents: 71486
diff changeset
   217
            (if (dirs.isEmpty) "" else "\nmultiple directory entries " + commas_quote(dirs)))
5a2033fc8f3d avoid odd (harmless) problem with Mercurial 4.5.3 provided by Ubuntu 18.04 on first push: "couldn't write revision branch cache names";
wenzelm
parents: 71486
diff changeset
   218
      }
5a2033fc8f3d avoid odd (harmless) problem with Mercurial 4.5.3 provided by Ubuntu 18.04 on first push: "couldn't write revision branch cache names";
wenzelm
parents: 71486
diff changeset
   219
    })
5a2033fc8f3d avoid odd (harmless) problem with Mercurial 4.5.3 provided by Ubuntu 18.04 on first push: "couldn't write revision branch cache names";
wenzelm
parents: 71486
diff changeset
   220
  }
5a2033fc8f3d avoid odd (harmless) problem with Mercurial 4.5.3 provided by Ubuntu 18.04 on first push: "couldn't write revision branch cache names";
wenzelm
parents: 71486
diff changeset
   221
71164
79736ffe77c3 some support for Phabricator server;
wenzelm
parents:
diff changeset
   222
  def phabricator_setup(
71634
5d5be87330b5 allow to override repository versions at runtime;
wenzelm
parents: 71633
diff changeset
   223
    options: Options,
71164
79736ffe77c3 some support for Phabricator server;
wenzelm
parents:
diff changeset
   224
    name: String = default_name,
79736ffe77c3 some support for Phabricator server;
wenzelm
parents:
diff changeset
   225
    root: String = "",
79736ffe77c3 some support for Phabricator server;
wenzelm
parents:
diff changeset
   226
    repo: String = "",
71245
87c132cf5860 more options;
wenzelm
parents: 71167
diff changeset
   227
    package_update: Boolean = false,
71489
5a2033fc8f3d avoid odd (harmless) problem with Mercurial 4.5.3 provided by Ubuntu 18.04 on first push: "couldn't write revision branch cache names";
wenzelm
parents: 71486
diff changeset
   228
    mercurial_source: String = "",
71164
79736ffe77c3 some support for Phabricator server;
wenzelm
parents:
diff changeset
   229
    progress: Progress = No_Progress)
79736ffe77c3 some support for Phabricator server;
wenzelm
parents:
diff changeset
   230
  {
79736ffe77c3 some support for Phabricator server;
wenzelm
parents:
diff changeset
   231
    /* system environment */
79736ffe77c3 some support for Phabricator server;
wenzelm
parents:
diff changeset
   232
79736ffe77c3 some support for Phabricator server;
wenzelm
parents:
diff changeset
   233
    Linux.check_system_root()
79736ffe77c3 some support for Phabricator server;
wenzelm
parents:
diff changeset
   234
71277
e06852132c1d tuned messages;
wenzelm
parents: 71276
diff changeset
   235
    progress.echo("System packages ...")
e06852132c1d tuned messages;
wenzelm
parents: 71276
diff changeset
   236
71245
87c132cf5860 more options;
wenzelm
parents: 71167
diff changeset
   237
    if (package_update) {
87c132cf5860 more options;
wenzelm
parents: 71167
diff changeset
   238
      Linux.package_update(progress = progress)
87c132cf5860 more options;
wenzelm
parents: 71167
diff changeset
   239
      Linux.check_reboot_required()
87c132cf5860 more options;
wenzelm
parents: 71167
diff changeset
   240
    }
71164
79736ffe77c3 some support for Phabricator server;
wenzelm
parents:
diff changeset
   241
79736ffe77c3 some support for Phabricator server;
wenzelm
parents:
diff changeset
   242
    Linux.package_install(packages, progress = progress)
79736ffe77c3 some support for Phabricator server;
wenzelm
parents:
diff changeset
   243
    Linux.check_reboot_required()
79736ffe77c3 some support for Phabricator server;
wenzelm
parents:
diff changeset
   244
79736ffe77c3 some support for Phabricator server;
wenzelm
parents:
diff changeset
   245
71489
5a2033fc8f3d avoid odd (harmless) problem with Mercurial 4.5.3 provided by Ubuntu 18.04 on first push: "couldn't write revision branch cache names";
wenzelm
parents: 71486
diff changeset
   246
    if (mercurial_source.nonEmpty) {
5a2033fc8f3d avoid odd (harmless) problem with Mercurial 4.5.3 provided by Ubuntu 18.04 on first push: "couldn't write revision branch cache names";
wenzelm
parents: 71486
diff changeset
   247
      for { name <- List("mercurial", "mercurial-common") if Linux.package_installed(name) } {
71535
d85258458623 tuned message;
wenzelm
parents: 71523
diff changeset
   248
        error("Cannot install Mercurial from source:\n" +
71489
5a2033fc8f3d avoid odd (harmless) problem with Mercurial 4.5.3 provided by Ubuntu 18.04 on first push: "couldn't write revision branch cache names";
wenzelm
parents: 71486
diff changeset
   249
          "package package " + quote(name) + " already installed")
5a2033fc8f3d avoid odd (harmless) problem with Mercurial 4.5.3 provided by Ubuntu 18.04 on first push: "couldn't write revision branch cache names";
wenzelm
parents: 71486
diff changeset
   250
      }
5a2033fc8f3d avoid odd (harmless) problem with Mercurial 4.5.3 provided by Ubuntu 18.04 on first push: "couldn't write revision branch cache names";
wenzelm
parents: 71486
diff changeset
   251
      mercurial_setup(mercurial_source, progress = progress)
5a2033fc8f3d avoid odd (harmless) problem with Mercurial 4.5.3 provided by Ubuntu 18.04 on first push: "couldn't write revision branch cache names";
wenzelm
parents: 71486
diff changeset
   252
    }
5a2033fc8f3d avoid odd (harmless) problem with Mercurial 4.5.3 provided by Ubuntu 18.04 on first push: "couldn't write revision branch cache names";
wenzelm
parents: 71486
diff changeset
   253
5a2033fc8f3d avoid odd (harmless) problem with Mercurial 4.5.3 provided by Ubuntu 18.04 on first push: "couldn't write revision branch cache names";
wenzelm
parents: 71486
diff changeset
   254
71247
f4b9dd5ab0cc more phabricator setup;
wenzelm
parents: 71245
diff changeset
   255
    /* users */
f4b9dd5ab0cc more phabricator setup;
wenzelm
parents: 71245
diff changeset
   256
71323
beb781551a66 more sanity checks;
wenzelm
parents: 71322
diff changeset
   257
    if (name.contains((c: Char) => !(Symbol.is_ascii_letter(c) || Symbol.is_ascii_digit(c))) ||
71478
7df5c110a43c support database dump;
wenzelm
parents: 71477
diff changeset
   258
        Set("", "ssh", "phd", "dump", daemon_user).contains(name)) {
71323
beb781551a66 more sanity checks;
wenzelm
parents: 71322
diff changeset
   259
      error("Bad installation name: " + quote(name))
71247
f4b9dd5ab0cc more phabricator setup;
wenzelm
parents: 71245
diff changeset
   260
    }
f4b9dd5ab0cc more phabricator setup;
wenzelm
parents: 71245
diff changeset
   261
f4b9dd5ab0cc more phabricator setup;
wenzelm
parents: 71245
diff changeset
   262
    user_setup(daemon_user, "Phabricator Daemon User", ssh_setup = true)
f4b9dd5ab0cc more phabricator setup;
wenzelm
parents: 71245
diff changeset
   263
    user_setup(name, "Phabricator SSH User")
f4b9dd5ab0cc more phabricator setup;
wenzelm
parents: 71245
diff changeset
   264
f4b9dd5ab0cc more phabricator setup;
wenzelm
parents: 71245
diff changeset
   265
71164
79736ffe77c3 some support for Phabricator server;
wenzelm
parents:
diff changeset
   266
    /* basic installation */
79736ffe77c3 some support for Phabricator server;
wenzelm
parents:
diff changeset
   267
71277
e06852132c1d tuned messages;
wenzelm
parents: 71276
diff changeset
   268
    progress.echo("\nPhabricator installation ...")
71274
8ac137c65776 tuned messages;
wenzelm
parents: 71273
diff changeset
   269
71266
510b89906d86 discontinued somewhat pointless Isabelle options: setup implicitly assumes Ubuntu 18.04;
wenzelm
parents: 71264
diff changeset
   270
    val root_path = if (root.nonEmpty) Path.explode(root) else default_root(name)
510b89906d86 discontinued somewhat pointless Isabelle options: setup implicitly assumes Ubuntu 18.04;
wenzelm
parents: 71264
diff changeset
   271
    val repo_path = if (repo.nonEmpty) Path.explode(repo) else default_repo(name)
71164
79736ffe77c3 some support for Phabricator server;
wenzelm
parents:
diff changeset
   272
79736ffe77c3 some support for Phabricator server;
wenzelm
parents:
diff changeset
   273
    val configs = read_config()
79736ffe77c3 some support for Phabricator server;
wenzelm
parents:
diff changeset
   274
79736ffe77c3 some support for Phabricator server;
wenzelm
parents:
diff changeset
   275
    for (config <- configs if config.name == name) {
79736ffe77c3 some support for Phabricator server;
wenzelm
parents:
diff changeset
   276
      error("Duplicate Phabricator installation " + quote(name) + " in " + config.root)
79736ffe77c3 some support for Phabricator server;
wenzelm
parents:
diff changeset
   277
    }
79736ffe77c3 some support for Phabricator server;
wenzelm
parents:
diff changeset
   278
79736ffe77c3 some support for Phabricator server;
wenzelm
parents:
diff changeset
   279
    if (!Isabelle_System.bash("mkdir -p " + File.bash_path(root_path)).ok) {
79736ffe77c3 some support for Phabricator server;
wenzelm
parents:
diff changeset
   280
      error("Failed to create root directory " + root_path)
79736ffe77c3 some support for Phabricator server;
wenzelm
parents:
diff changeset
   281
    }
79736ffe77c3 some support for Phabricator server;
wenzelm
parents:
diff changeset
   282
71315
wenzelm
parents: 71314
diff changeset
   283
    Isabelle_System.chown(Bash.string(www_user) + ":" + Bash.string(www_user), root_path)
wenzelm
parents: 71314
diff changeset
   284
    Isabelle_System.chmod("755", root_path)
wenzelm
parents: 71314
diff changeset
   285
71164
79736ffe77c3 some support for Phabricator server;
wenzelm
parents:
diff changeset
   286
    progress.bash(cwd = root_path.file, echo = true,
79736ffe77c3 some support for Phabricator server;
wenzelm
parents:
diff changeset
   287
      script = """
79736ffe77c3 some support for Phabricator server;
wenzelm
parents:
diff changeset
   288
        set -e
71324
18f87bdbc812 tuned message;
wenzelm
parents: 71323
diff changeset
   289
        echo "Cloning distribution repositories:"
71496
71fd25a7bbe2 more robust setup: avoid blind shot at "the latest" version;
wenzelm
parents: 71494
diff changeset
   290
71634
5d5be87330b5 allow to override repository versions at runtime;
wenzelm
parents: 71633
diff changeset
   291
        git clone --branch stable https://github.com/phacility/arcanist.git
5d5be87330b5 allow to override repository versions at runtime;
wenzelm
parents: 71633
diff changeset
   292
        git -C arcanist reset --hard """ +
5d5be87330b5 allow to override repository versions at runtime;
wenzelm
parents: 71633
diff changeset
   293
          Bash.string(options.string("phabricator_version_arcanist")) + """
71496
71fd25a7bbe2 more robust setup: avoid blind shot at "the latest" version;
wenzelm
parents: 71494
diff changeset
   294
71634
5d5be87330b5 allow to override repository versions at runtime;
wenzelm
parents: 71633
diff changeset
   295
        git clone --branch stable https://github.com/phacility/libphutil.git
5d5be87330b5 allow to override repository versions at runtime;
wenzelm
parents: 71633
diff changeset
   296
        git -C libphutil reset --hard """ +
5d5be87330b5 allow to override repository versions at runtime;
wenzelm
parents: 71633
diff changeset
   297
          Bash.string(options.string("phabricator_version_libphutil")) + """
71496
71fd25a7bbe2 more robust setup: avoid blind shot at "the latest" version;
wenzelm
parents: 71494
diff changeset
   298
71492
cfcc1a2233ca support for Phabricator upgrade;
wenzelm
parents: 71491
diff changeset
   299
        git clone --branch stable https://github.com/phacility/phabricator.git
71634
5d5be87330b5 allow to override repository versions at runtime;
wenzelm
parents: 71633
diff changeset
   300
        git -C phabricator reset --hard """ +
5d5be87330b5 allow to override repository versions at runtime;
wenzelm
parents: 71633
diff changeset
   301
          Bash.string(options.string("phabricator_version_phabricator")) + """
71164
79736ffe77c3 some support for Phabricator server;
wenzelm
parents:
diff changeset
   302
      """).check
79736ffe77c3 some support for Phabricator server;
wenzelm
parents:
diff changeset
   303
79736ffe77c3 some support for Phabricator server;
wenzelm
parents:
diff changeset
   304
    val config = Config(name, root_path)
79736ffe77c3 some support for Phabricator server;
wenzelm
parents:
diff changeset
   305
    write_config(configs ::: List(config))
71165
46847076477c Apache setup;
wenzelm
parents: 71164
diff changeset
   306
71249
4eeff87c5072 more phabricator setup;
wenzelm
parents: 71248
diff changeset
   307
    config.execute("config set pygments.enabled true")
4eeff87c5072 more phabricator setup;
wenzelm
parents: 71248
diff changeset
   308
71165
46847076477c Apache setup;
wenzelm
parents: 71164
diff changeset
   309
71248
8198ceef0301 more phabricator setup;
wenzelm
parents: 71247
diff changeset
   310
    /* local repository directory */
8198ceef0301 more phabricator setup;
wenzelm
parents: 71247
diff changeset
   311
71308
8c1c717a830b configure SSH hosting via "isabelle phabricator_setup_ssh";
wenzelm
parents: 71302
diff changeset
   312
    progress.echo("\nRepository hosting setup ...")
8c1c717a830b configure SSH hosting via "isabelle phabricator_setup_ssh";
wenzelm
parents: 71302
diff changeset
   313
71248
8198ceef0301 more phabricator setup;
wenzelm
parents: 71247
diff changeset
   314
    if (!Isabelle_System.bash("mkdir -p " + File.bash_path(repo_path)).ok) {
8198ceef0301 more phabricator setup;
wenzelm
parents: 71247
diff changeset
   315
      error("Failed to create local repository directory " + repo_path)
8198ceef0301 more phabricator setup;
wenzelm
parents: 71247
diff changeset
   316
    }
8198ceef0301 more phabricator setup;
wenzelm
parents: 71247
diff changeset
   317
71313
6cfec8029831 clarified signature;
wenzelm
parents: 71311
diff changeset
   318
    Isabelle_System.chown(
6cfec8029831 clarified signature;
wenzelm
parents: 71311
diff changeset
   319
      "-R " + Bash.string(daemon_user) + ":" + Bash.string(daemon_user), repo_path)
6cfec8029831 clarified signature;
wenzelm
parents: 71311
diff changeset
   320
    Isabelle_System.chmod("755", repo_path)
71248
8198ceef0301 more phabricator setup;
wenzelm
parents: 71247
diff changeset
   321
8198ceef0301 more phabricator setup;
wenzelm
parents: 71247
diff changeset
   322
    config.execute("config set repository.default-local-path " + File.bash_path(repo_path))
8198ceef0301 more phabricator setup;
wenzelm
parents: 71247
diff changeset
   323
8198ceef0301 more phabricator setup;
wenzelm
parents: 71247
diff changeset
   324
71486
74cabc06cf2d proper support for multiple installations;
wenzelm
parents: 71485
diff changeset
   325
    val sudoers_file =
74cabc06cf2d proper support for multiple installations;
wenzelm
parents: 71485
diff changeset
   326
      Path.explode("/etc/sudoers.d") + Path.basic(isabelle_phabricator_name(name = name))
71308
8c1c717a830b configure SSH hosting via "isabelle phabricator_setup_ssh";
wenzelm
parents: 71302
diff changeset
   327
    File.write(sudoers_file,
71489
5a2033fc8f3d avoid odd (harmless) problem with Mercurial 4.5.3 provided by Ubuntu 18.04 on first push: "couldn't write revision branch cache names";
wenzelm
parents: 71486
diff changeset
   328
      www_user + " ALL=(" + daemon_user + ") SETENV: NOPASSWD: /usr/bin/git, /usr/local/bin/hg, /usr/bin/hg, /usr/bin/ssh, /usr/bin/id\n" +
5a2033fc8f3d avoid odd (harmless) problem with Mercurial 4.5.3 provided by Ubuntu 18.04 on first push: "couldn't write revision branch cache names";
wenzelm
parents: 71486
diff changeset
   329
      name + " ALL=(" + daemon_user + ") SETENV: NOPASSWD: /usr/bin/git, /usr/bin/git-upload-pack, /usr/bin/git-receive-pack, /usr/local/bin/hg, /usr/bin/hg, /usr/bin/svnserve, /usr/bin/ssh, /usr/bin/id\n")
71308
8c1c717a830b configure SSH hosting via "isabelle phabricator_setup_ssh";
wenzelm
parents: 71302
diff changeset
   330
71314
wenzelm
parents: 71313
diff changeset
   331
    Isabelle_System.chmod("440", sudoers_file)
71308
8c1c717a830b configure SSH hosting via "isabelle phabricator_setup_ssh";
wenzelm
parents: 71302
diff changeset
   332
8c1c717a830b configure SSH hosting via "isabelle phabricator_setup_ssh";
wenzelm
parents: 71302
diff changeset
   333
    config.execute("config set diffusion.ssh-user " + Bash.string(config.name))
8c1c717a830b configure SSH hosting via "isabelle phabricator_setup_ssh";
wenzelm
parents: 71302
diff changeset
   334
8c1c717a830b configure SSH hosting via "isabelle phabricator_setup_ssh";
wenzelm
parents: 71302
diff changeset
   335
71166
a48112873f81 MySQL setup;
wenzelm
parents: 71165
diff changeset
   336
    /* MySQL setup */
a48112873f81 MySQL setup;
wenzelm
parents: 71165
diff changeset
   337
71277
e06852132c1d tuned messages;
wenzelm
parents: 71276
diff changeset
   338
    progress.echo("\nMySQL setup ...")
71166
a48112873f81 MySQL setup;
wenzelm
parents: 71165
diff changeset
   339
71253
27a998cdc0f4 back to plain name, to have it accepted my mysql;
wenzelm
parents: 71252
diff changeset
   340
    File.write(Path.explode("/etc/mysql/mysql.conf.d/" + phabricator_name(ext = "cnf")),
71249
4eeff87c5072 more phabricator setup;
wenzelm
parents: 71248
diff changeset
   341
"""[mysqld]
4eeff87c5072 more phabricator setup;
wenzelm
parents: 71248
diff changeset
   342
max_allowed_packet = 32M
4eeff87c5072 more phabricator setup;
wenzelm
parents: 71248
diff changeset
   343
innodb_buffer_pool_size = 1600M
4eeff87c5072 more phabricator setup;
wenzelm
parents: 71248
diff changeset
   344
local_infile = 0
4eeff87c5072 more phabricator setup;
wenzelm
parents: 71248
diff changeset
   345
""")
4eeff87c5072 more phabricator setup;
wenzelm
parents: 71248
diff changeset
   346
4eeff87c5072 more phabricator setup;
wenzelm
parents: 71248
diff changeset
   347
    Linux.service_restart("mysql")
4eeff87c5072 more phabricator setup;
wenzelm
parents: 71248
diff changeset
   348
4eeff87c5072 more phabricator setup;
wenzelm
parents: 71248
diff changeset
   349
71475
8451c86ffa85 proper mysql user setup: avoid superuser powers in production;
wenzelm
parents: 71474
diff changeset
   350
    def mysql_conf(R: Regex, which: String): String =
8451c86ffa85 proper mysql user setup: avoid superuser powers in production;
wenzelm
parents: 71474
diff changeset
   351
    {
8451c86ffa85 proper mysql user setup: avoid superuser powers in production;
wenzelm
parents: 71474
diff changeset
   352
      val conf = Path.explode("/etc/mysql/debian.cnf")
8451c86ffa85 proper mysql user setup: avoid superuser powers in production;
wenzelm
parents: 71474
diff changeset
   353
      split_lines(File.read(conf)).collectFirst({ case R(a) => a }) match {
8451c86ffa85 proper mysql user setup: avoid superuser powers in production;
wenzelm
parents: 71474
diff changeset
   354
        case Some(res) => res
8451c86ffa85 proper mysql user setup: avoid superuser powers in production;
wenzelm
parents: 71474
diff changeset
   355
        case None => error("Cannot determine " + which + " from " + conf)
8451c86ffa85 proper mysql user setup: avoid superuser powers in production;
wenzelm
parents: 71474
diff changeset
   356
      }
71166
a48112873f81 MySQL setup;
wenzelm
parents: 71165
diff changeset
   357
    }
a48112873f81 MySQL setup;
wenzelm
parents: 71165
diff changeset
   358
71475
8451c86ffa85 proper mysql user setup: avoid superuser powers in production;
wenzelm
parents: 71474
diff changeset
   359
    val mysql_root_user = mysql_conf("""^user\s*=\s*(\S*)\s*$""".r, "superuser name")
8451c86ffa85 proper mysql user setup: avoid superuser powers in production;
wenzelm
parents: 71474
diff changeset
   360
    val mysql_root_password = mysql_conf("""^password\s*=\s*(\S*)\s*$""".r, "superuser password")
8451c86ffa85 proper mysql user setup: avoid superuser powers in production;
wenzelm
parents: 71474
diff changeset
   361
8451c86ffa85 proper mysql user setup: avoid superuser powers in production;
wenzelm
parents: 71474
diff changeset
   362
    val mysql_name = phabricator_name(name = name).replace("-", "_")
8451c86ffa85 proper mysql user setup: avoid superuser powers in production;
wenzelm
parents: 71474
diff changeset
   363
    val mysql_user_string = SQL.string(mysql_name) + "@'localhost'"
8451c86ffa85 proper mysql user setup: avoid superuser powers in production;
wenzelm
parents: 71474
diff changeset
   364
    val mysql_password = Linux.generate_password()
71166
a48112873f81 MySQL setup;
wenzelm
parents: 71165
diff changeset
   365
71475
8451c86ffa85 proper mysql user setup: avoid superuser powers in production;
wenzelm
parents: 71474
diff changeset
   366
    Isabelle_System.bash("mysql --user=" + Bash.string(mysql_root_user) +
8451c86ffa85 proper mysql user setup: avoid superuser powers in production;
wenzelm
parents: 71474
diff changeset
   367
      " --password=" + Bash.string(mysql_root_password) + " --execute=" +
8451c86ffa85 proper mysql user setup: avoid superuser powers in production;
wenzelm
parents: 71474
diff changeset
   368
      Bash.string(
71483
5212ca49598a more robust;
wenzelm
parents: 71482
diff changeset
   369
        """DROP USER IF EXISTS """ + mysql_user_string + "; " +
5212ca49598a more robust;
wenzelm
parents: 71482
diff changeset
   370
        """CREATE USER """ + mysql_user_string +
71475
8451c86ffa85 proper mysql user setup: avoid superuser powers in production;
wenzelm
parents: 71474
diff changeset
   371
        """ IDENTIFIED BY """ + SQL.string(mysql_password) + """ PASSWORD EXPIRE NEVER; """ +
8451c86ffa85 proper mysql user setup: avoid superuser powers in production;
wenzelm
parents: 71474
diff changeset
   372
        """GRANT ALL ON `""" + (mysql_name + "_%").replace("_", "\\_") +
8451c86ffa85 proper mysql user setup: avoid superuser powers in production;
wenzelm
parents: 71474
diff changeset
   373
        """`.* TO """ + mysql_user_string + ";")).check
71166
a48112873f81 MySQL setup;
wenzelm
parents: 71165
diff changeset
   374
71475
8451c86ffa85 proper mysql user setup: avoid superuser powers in production;
wenzelm
parents: 71474
diff changeset
   375
    config.execute("config set mysql.user " + Bash.string(mysql_name))
8451c86ffa85 proper mysql user setup: avoid superuser powers in production;
wenzelm
parents: 71474
diff changeset
   376
    config.execute("config set mysql.pass " + Bash.string(mysql_password))
8451c86ffa85 proper mysql user setup: avoid superuser powers in production;
wenzelm
parents: 71474
diff changeset
   377
8451c86ffa85 proper mysql user setup: avoid superuser powers in production;
wenzelm
parents: 71474
diff changeset
   378
    config.execute("config set phabricator.cache-namespace " + Bash.string(mysql_name))
8451c86ffa85 proper mysql user setup: avoid superuser powers in production;
wenzelm
parents: 71474
diff changeset
   379
    config.execute("config set storage.default-namespace " + Bash.string(mysql_name))
71249
4eeff87c5072 more phabricator setup;
wenzelm
parents: 71248
diff changeset
   380
    config.execute("config set storage.mysql-engine.max-size 8388608")
4eeff87c5072 more phabricator setup;
wenzelm
parents: 71248
diff changeset
   381
71301
wenzelm
parents: 71300
diff changeset
   382
    progress.bash("bin/storage upgrade --force", cwd = config.home.file, echo = true).check
71166
a48112873f81 MySQL setup;
wenzelm
parents: 71165
diff changeset
   383
a48112873f81 MySQL setup;
wenzelm
parents: 71165
diff changeset
   384
71478
7df5c110a43c support database dump;
wenzelm
parents: 71477
diff changeset
   385
    /* database dump */
7df5c110a43c support database dump;
wenzelm
parents: 71477
diff changeset
   386
7df5c110a43c support database dump;
wenzelm
parents: 71477
diff changeset
   387
    val dump_name = isabelle_phabricator_name(name = "dump")
71491
de59dd86760f tuned signature;
wenzelm
parents: 71490
diff changeset
   388
    command_setup(dump_name, body =
71478
7df5c110a43c support database dump;
wenzelm
parents: 71477
diff changeset
   389
"""mkdir -p "$ROOT/database" && chown root:root "$ROOT/database" && chmod 700 "$ROOT/database"
7df5c110a43c support database dump;
wenzelm
parents: 71477
diff changeset
   390
[ -e "$ROOT/database/dump.sql.gz" ] && mv -f "$ROOT/database/dump.sql.gz" "$ROOT/database/dump-old.sql.gz"
71537
4642a81f5913 tuned messages;
wenzelm
parents: 71535
diff changeset
   391
echo -n "Creating $ROOT/database/dump.sql.gz ..."
4642a81f5913 tuned messages;
wenzelm
parents: 71535
diff changeset
   392
"$ROOT/phabricator/bin/storage" dump --compress --output "$ROOT/database/dump.sql.gz" 2>&1 | fgrep -v '[Warning] Using a password on the command line interface can be insecure'
4642a81f5913 tuned messages;
wenzelm
parents: 71535
diff changeset
   393
echo " $(ls -hs "$ROOT/database/dump.sql.gz" | cut -d" " -f1)" """)
71478
7df5c110a43c support database dump;
wenzelm
parents: 71477
diff changeset
   394
7df5c110a43c support database dump;
wenzelm
parents: 71477
diff changeset
   395
71492
cfcc1a2233ca support for Phabricator upgrade;
wenzelm
parents: 71491
diff changeset
   396
    /* Phabricator upgrade */
cfcc1a2233ca support for Phabricator upgrade;
wenzelm
parents: 71491
diff changeset
   397
cfcc1a2233ca support for Phabricator upgrade;
wenzelm
parents: 71491
diff changeset
   398
    command_setup(isabelle_phabricator_name(name = "upgrade"),
cfcc1a2233ca support for Phabricator upgrade;
wenzelm
parents: 71491
diff changeset
   399
      init =
71494
8cd05f7b3b4a proper default;
wenzelm
parents: 71493
diff changeset
   400
"""BRANCH="${1:-stable}"
71492
cfcc1a2233ca support for Phabricator upgrade;
wenzelm
parents: 71491
diff changeset
   401
if [ "$BRANCH" != "master" -a "$BRANCH" != "stable" ]
cfcc1a2233ca support for Phabricator upgrade;
wenzelm
parents: 71491
diff changeset
   402
then
cfcc1a2233ca support for Phabricator upgrade;
wenzelm
parents: 71491
diff changeset
   403
  echo "Bad branch: \"$BRANCH\""
cfcc1a2233ca support for Phabricator upgrade;
wenzelm
parents: 71491
diff changeset
   404
  exit 1
cfcc1a2233ca support for Phabricator upgrade;
wenzelm
parents: 71491
diff changeset
   405
fi
cfcc1a2233ca support for Phabricator upgrade;
wenzelm
parents: 71491
diff changeset
   406
cfcc1a2233ca support for Phabricator upgrade;
wenzelm
parents: 71491
diff changeset
   407
systemctl stop isabelle-phabricator-phd
cfcc1a2233ca support for Phabricator upgrade;
wenzelm
parents: 71491
diff changeset
   408
systemctl stop apache2
cfcc1a2233ca support for Phabricator upgrade;
wenzelm
parents: 71491
diff changeset
   409
""",
cfcc1a2233ca support for Phabricator upgrade;
wenzelm
parents: 71491
diff changeset
   410
      body =
cfcc1a2233ca support for Phabricator upgrade;
wenzelm
parents: 71491
diff changeset
   411
"""echo -e "\nUpgrading phabricator \"$NAME\" root \"$ROOT\" ..."
cfcc1a2233ca support for Phabricator upgrade;
wenzelm
parents: 71491
diff changeset
   412
for REPO in libphutil arcanist phabricator
cfcc1a2233ca support for Phabricator upgrade;
wenzelm
parents: 71491
diff changeset
   413
do
cfcc1a2233ca support for Phabricator upgrade;
wenzelm
parents: 71491
diff changeset
   414
  cd "$ROOT/$REPO"
cfcc1a2233ca support for Phabricator upgrade;
wenzelm
parents: 71491
diff changeset
   415
  echo -e "\nUpdating \"$REPO\" ..."
cfcc1a2233ca support for Phabricator upgrade;
wenzelm
parents: 71491
diff changeset
   416
  git checkout "$BRANCH"
cfcc1a2233ca support for Phabricator upgrade;
wenzelm
parents: 71491
diff changeset
   417
  git pull
cfcc1a2233ca support for Phabricator upgrade;
wenzelm
parents: 71491
diff changeset
   418
done
cfcc1a2233ca support for Phabricator upgrade;
wenzelm
parents: 71491
diff changeset
   419
echo -e "\nUpgrading storage ..."
cfcc1a2233ca support for Phabricator upgrade;
wenzelm
parents: 71491
diff changeset
   420
"$ROOT/phabricator/bin/storage" upgrade --force
cfcc1a2233ca support for Phabricator upgrade;
wenzelm
parents: 71491
diff changeset
   421
""",
cfcc1a2233ca support for Phabricator upgrade;
wenzelm
parents: 71491
diff changeset
   422
      exit =
cfcc1a2233ca support for Phabricator upgrade;
wenzelm
parents: 71491
diff changeset
   423
"""systemctl start apache2
cfcc1a2233ca support for Phabricator upgrade;
wenzelm
parents: 71491
diff changeset
   424
systemctl start isabelle-phabricator-phd""")
cfcc1a2233ca support for Phabricator upgrade;
wenzelm
parents: 71491
diff changeset
   425
cfcc1a2233ca support for Phabricator upgrade;
wenzelm
parents: 71491
diff changeset
   426
71249
4eeff87c5072 more phabricator setup;
wenzelm
parents: 71248
diff changeset
   427
    /* PHP setup */
4eeff87c5072 more phabricator setup;
wenzelm
parents: 71248
diff changeset
   428
4eeff87c5072 more phabricator setup;
wenzelm
parents: 71248
diff changeset
   429
    val php_version =
4eeff87c5072 more phabricator setup;
wenzelm
parents: 71248
diff changeset
   430
      Isabelle_System.bash("""php --run 'echo PHP_MAJOR_VERSION . "." . PHP_MINOR_VERSION;'""")
4eeff87c5072 more phabricator setup;
wenzelm
parents: 71248
diff changeset
   431
        .check.out
4eeff87c5072 more phabricator setup;
wenzelm
parents: 71248
diff changeset
   432
4eeff87c5072 more phabricator setup;
wenzelm
parents: 71248
diff changeset
   433
    val php_conf =
4eeff87c5072 more phabricator setup;
wenzelm
parents: 71248
diff changeset
   434
      Path.explode("/etc/php") + Path.basic(php_version) +  // educated guess
71250
6bf53035baf0 clarified name prefixes: global config always uses "isabelle-phabricator";
wenzelm
parents: 71249
diff changeset
   435
        Path.explode("apache2/conf.d") +
6bf53035baf0 clarified name prefixes: global config always uses "isabelle-phabricator";
wenzelm
parents: 71249
diff changeset
   436
        Path.basic(isabelle_phabricator_name(ext = "ini"))
71249
4eeff87c5072 more phabricator setup;
wenzelm
parents: 71248
diff changeset
   437
4eeff87c5072 more phabricator setup;
wenzelm
parents: 71248
diff changeset
   438
    File.write(php_conf,
4eeff87c5072 more phabricator setup;
wenzelm
parents: 71248
diff changeset
   439
      "post_max_size = 32M\n" +
4eeff87c5072 more phabricator setup;
wenzelm
parents: 71248
diff changeset
   440
      "opcache.validate_timestamps = 0\n" +
71327
557703db74c3 tuned PHP setup;
wenzelm
parents: 71326
diff changeset
   441
      "memory_limit = 512M\n" +
557703db74c3 tuned PHP setup;
wenzelm
parents: 71326
diff changeset
   442
      "max_execution_time = 120\n")
71249
4eeff87c5072 more phabricator setup;
wenzelm
parents: 71248
diff changeset
   443
4eeff87c5072 more phabricator setup;
wenzelm
parents: 71248
diff changeset
   444
71165
46847076477c Apache setup;
wenzelm
parents: 71164
diff changeset
   445
    /* Apache setup */
46847076477c Apache setup;
wenzelm
parents: 71164
diff changeset
   446
71277
e06852132c1d tuned messages;
wenzelm
parents: 71276
diff changeset
   447
    progress.echo("Apache setup ...")
71165
46847076477c Apache setup;
wenzelm
parents: 71164
diff changeset
   448
71266
510b89906d86 discontinued somewhat pointless Isabelle options: setup implicitly assumes Ubuntu 18.04;
wenzelm
parents: 71264
diff changeset
   449
    val apache_root = Path.explode("/etc/apache2")
71165
46847076477c Apache setup;
wenzelm
parents: 71164
diff changeset
   450
    val apache_sites = apache_root + Path.explode("sites-available")
46847076477c Apache setup;
wenzelm
parents: 71164
diff changeset
   451
46847076477c Apache setup;
wenzelm
parents: 71164
diff changeset
   452
    if (!apache_sites.is_dir) error("Bad Apache sites directory " + apache_sites)
46847076477c Apache setup;
wenzelm
parents: 71164
diff changeset
   453
71256
6ca9e8377613 proper names for multiple installations;
wenzelm
parents: 71255
diff changeset
   454
    val server_name = phabricator_name(name = name, ext = "lvh.me")  // alias for "localhost" for testing
71250
6bf53035baf0 clarified name prefixes: global config always uses "isabelle-phabricator";
wenzelm
parents: 71249
diff changeset
   455
    val server_url = "http://" + server_name
6bf53035baf0 clarified name prefixes: global config always uses "isabelle-phabricator";
wenzelm
parents: 71249
diff changeset
   456
71256
6ca9e8377613 proper names for multiple installations;
wenzelm
parents: 71255
diff changeset
   457
    File.write(apache_sites + Path.basic(isabelle_phabricator_name(name = name, ext = "conf")),
71165
46847076477c Apache setup;
wenzelm
parents: 71164
diff changeset
   458
"""<VirtualHost *:80>
71250
6bf53035baf0 clarified name prefixes: global config always uses "isabelle-phabricator";
wenzelm
parents: 71249
diff changeset
   459
    ServerName """ + server_name + """
71165
46847076477c Apache setup;
wenzelm
parents: 71164
diff changeset
   460
    ServerAdmin webmaster@localhost
71166
a48112873f81 MySQL setup;
wenzelm
parents: 71165
diff changeset
   461
    DocumentRoot """ + config.home.implode + """/webroot
71165
46847076477c Apache setup;
wenzelm
parents: 71164
diff changeset
   462
46847076477c Apache setup;
wenzelm
parents: 71164
diff changeset
   463
    ErrorLog ${APACHE_LOG_DIR}/error.log
71651
760e19aa9b09 afford more logging (following defaults on Ubuntu);
wenzelm
parents: 71634
diff changeset
   464
    CustomLog ${APACHE_LOG_DIR}/access.log combined
760e19aa9b09 afford more logging (following defaults on Ubuntu);
wenzelm
parents: 71634
diff changeset
   465
71165
46847076477c Apache setup;
wenzelm
parents: 71164
diff changeset
   466
    RewriteEngine on
46847076477c Apache setup;
wenzelm
parents: 71164
diff changeset
   467
    RewriteRule ^(.*)$  /index.php?__path__=$1  [B,L,QSA]
46847076477c Apache setup;
wenzelm
parents: 71164
diff changeset
   468
</VirtualHost>
46847076477c Apache setup;
wenzelm
parents: 71164
diff changeset
   469
46847076477c Apache setup;
wenzelm
parents: 71164
diff changeset
   470
# vim: syntax=apache ts=4 sw=4 sts=4 sr noet
46847076477c Apache setup;
wenzelm
parents: 71164
diff changeset
   471
""")
46847076477c Apache setup;
wenzelm
parents: 71164
diff changeset
   472
71249
4eeff87c5072 more phabricator setup;
wenzelm
parents: 71248
diff changeset
   473
    Isabelle_System.bash( """
71165
46847076477c Apache setup;
wenzelm
parents: 71164
diff changeset
   474
      set -e
46847076477c Apache setup;
wenzelm
parents: 71164
diff changeset
   475
      a2enmod rewrite
71256
6ca9e8377613 proper names for multiple installations;
wenzelm
parents: 71255
diff changeset
   476
      a2ensite """ + Bash.string(isabelle_phabricator_name(name = name))).check
71249
4eeff87c5072 more phabricator setup;
wenzelm
parents: 71248
diff changeset
   477
71255
2965304143d8 more phabricator setup;
wenzelm
parents: 71254
diff changeset
   478
    config.execute("config set phabricator.base-uri " + Bash.string(server_url))
2965304143d8 more phabricator setup;
wenzelm
parents: 71254
diff changeset
   479
71249
4eeff87c5072 more phabricator setup;
wenzelm
parents: 71248
diff changeset
   480
    Linux.service_restart("apache2")
71165
46847076477c Apache setup;
wenzelm
parents: 71164
diff changeset
   481
71537
4642a81f5913 tuned messages;
wenzelm
parents: 71535
diff changeset
   482
    progress.echo("\nFurther manual configuration via " + server_url)
71326
f79006c533b0 clarified errors: PHP daemon can fail under odd circumstances;
wenzelm
parents: 71325
diff changeset
   483
71251
ba14aa0b5a5d more robust: install PHP daemon after Apache;
wenzelm
parents: 71250
diff changeset
   484
ba14aa0b5a5d more robust: install PHP daemon after Apache;
wenzelm
parents: 71250
diff changeset
   485
    /* PHP daemon */
ba14aa0b5a5d more robust: install PHP daemon after Apache;
wenzelm
parents: 71250
diff changeset
   486
71326
f79006c533b0 clarified errors: PHP daemon can fail under odd circumstances;
wenzelm
parents: 71325
diff changeset
   487
    progress.echo("\nPHP daemon setup ...")
71251
ba14aa0b5a5d more robust: install PHP daemon after Apache;
wenzelm
parents: 71250
diff changeset
   488
71482
6b8cbdc9713b more robust;
wenzelm
parents: 71479
diff changeset
   489
    val phd_log_path = Path.explode("/var/tmp/phd")
6b8cbdc9713b more robust;
wenzelm
parents: 71479
diff changeset
   490
    Isabelle_System.mkdirs(phd_log_path)
6b8cbdc9713b more robust;
wenzelm
parents: 71479
diff changeset
   491
    Isabelle_System.chown(
6b8cbdc9713b more robust;
wenzelm
parents: 71479
diff changeset
   492
      "-R " + Bash.string(daemon_user) + ":" + Bash.string(daemon_user), phd_log_path)
6b8cbdc9713b more robust;
wenzelm
parents: 71479
diff changeset
   493
    Isabelle_System.chmod("755", phd_log_path)
6b8cbdc9713b more robust;
wenzelm
parents: 71479
diff changeset
   494
71251
ba14aa0b5a5d more robust: install PHP daemon after Apache;
wenzelm
parents: 71250
diff changeset
   495
    config.execute("config set phd.user " + Bash.string(daemon_user))
71311
eed5b6188371 more support for multiple daemons;
wenzelm
parents: 71310
diff changeset
   496
    config.execute("config set phd.log-directory /var/tmp/phd/" +
eed5b6188371 more support for multiple daemons;
wenzelm
parents: 71310
diff changeset
   497
      isabelle_phabricator_name(name = name) + "/log")
71251
ba14aa0b5a5d more robust: install PHP daemon after Apache;
wenzelm
parents: 71250
diff changeset
   498
71322
7dbadecdc118 just one isabelle-phabricator-phd service, which manages all processes uniformly (NB: "bin/phd stop" affects all installations);
wenzelm
parents: 71320
diff changeset
   499
    val phd_name = isabelle_phabricator_name(name = "phd")
71325
0ad53b5f2bb1 more robust;
wenzelm
parents: 71324
diff changeset
   500
    Linux.service_shutdown(phd_name)
71491
de59dd86760f tuned signature;
wenzelm
parents: 71490
diff changeset
   501
    val phd_command = command_setup(phd_name, body = """"$ROOT/phabricator/bin/phd" "$@" """)
71326
f79006c533b0 clarified errors: PHP daemon can fail under odd circumstances;
wenzelm
parents: 71325
diff changeset
   502
    try {
f79006c533b0 clarified errors: PHP daemon can fail under odd circumstances;
wenzelm
parents: 71325
diff changeset
   503
      Linux.service_install(phd_name,
71251
ba14aa0b5a5d more robust: install PHP daemon after Apache;
wenzelm
parents: 71250
diff changeset
   504
"""[Unit]
71322
7dbadecdc118 just one isabelle-phabricator-phd service, which manages all processes uniformly (NB: "bin/phd stop" affects all installations);
wenzelm
parents: 71320
diff changeset
   505
Description=PHP daemon manager for Isabelle/Phabricator
71251
ba14aa0b5a5d more robust: install PHP daemon after Apache;
wenzelm
parents: 71250
diff changeset
   506
After=syslog.target network.target apache2.service mysql.service
ba14aa0b5a5d more robust: install PHP daemon after Apache;
wenzelm
parents: 71250
diff changeset
   507
ba14aa0b5a5d more robust: install PHP daemon after Apache;
wenzelm
parents: 71250
diff changeset
   508
[Service]
ba14aa0b5a5d more robust: install PHP daemon after Apache;
wenzelm
parents: 71250
diff changeset
   509
Type=oneshot
ba14aa0b5a5d more robust: install PHP daemon after Apache;
wenzelm
parents: 71250
diff changeset
   510
User=""" + daemon_user + """
ba14aa0b5a5d more robust: install PHP daemon after Apache;
wenzelm
parents: 71250
diff changeset
   511
Group=""" + daemon_user + """
ba14aa0b5a5d more robust: install PHP daemon after Apache;
wenzelm
parents: 71250
diff changeset
   512
Environment=PATH=/sbin:/usr/sbin:/usr/local/sbin:/usr/local/bin:/usr/bin:/bin
71322
7dbadecdc118 just one isabelle-phabricator-phd service, which manages all processes uniformly (NB: "bin/phd stop" affects all installations);
wenzelm
parents: 71320
diff changeset
   513
ExecStart=""" + phd_command.implode + """ start --force
7dbadecdc118 just one isabelle-phabricator-phd service, which manages all processes uniformly (NB: "bin/phd stop" affects all installations);
wenzelm
parents: 71320
diff changeset
   514
ExecStop=""" + phd_command.implode + """ stop
71251
ba14aa0b5a5d more robust: install PHP daemon after Apache;
wenzelm
parents: 71250
diff changeset
   515
RemainAfterExit=yes
ba14aa0b5a5d more robust: install PHP daemon after Apache;
wenzelm
parents: 71250
diff changeset
   516
ba14aa0b5a5d more robust: install PHP daemon after Apache;
wenzelm
parents: 71250
diff changeset
   517
[Install]
ba14aa0b5a5d more robust: install PHP daemon after Apache;
wenzelm
parents: 71250
diff changeset
   518
WantedBy=multi-user.target
ba14aa0b5a5d more robust: install PHP daemon after Apache;
wenzelm
parents: 71250
diff changeset
   519
""")
71326
f79006c533b0 clarified errors: PHP daemon can fail under odd circumstances;
wenzelm
parents: 71325
diff changeset
   520
    }
f79006c533b0 clarified errors: PHP daemon can fail under odd circumstances;
wenzelm
parents: 71325
diff changeset
   521
    catch {
f79006c533b0 clarified errors: PHP daemon can fail under odd circumstances;
wenzelm
parents: 71325
diff changeset
   522
      case ERROR(msg) =>
f79006c533b0 clarified errors: PHP daemon can fail under odd circumstances;
wenzelm
parents: 71325
diff changeset
   523
        progress.bash("bin/phd status", cwd = config.home.file, echo = true).check
f79006c533b0 clarified errors: PHP daemon can fail under odd circumstances;
wenzelm
parents: 71325
diff changeset
   524
        error(msg)
f79006c533b0 clarified errors: PHP daemon can fail under odd circumstances;
wenzelm
parents: 71325
diff changeset
   525
    }
71164
79736ffe77c3 some support for Phabricator server;
wenzelm
parents:
diff changeset
   526
  }
79736ffe77c3 some support for Phabricator server;
wenzelm
parents:
diff changeset
   527
79736ffe77c3 some support for Phabricator server;
wenzelm
parents:
diff changeset
   528
79736ffe77c3 some support for Phabricator server;
wenzelm
parents:
diff changeset
   529
  /* Isabelle tool wrapper */
79736ffe77c3 some support for Phabricator server;
wenzelm
parents:
diff changeset
   530
71296
d3ededaa77b3 added "isabelle phabricator";
wenzelm
parents: 71277
diff changeset
   531
  val isabelle_tool2 =
71164
79736ffe77c3 some support for Phabricator server;
wenzelm
parents:
diff changeset
   532
    Isabelle_Tool("phabricator_setup", "setup Phabricator server on Ubuntu Linux", args =>
79736ffe77c3 some support for Phabricator server;
wenzelm
parents:
diff changeset
   533
    {
71489
5a2033fc8f3d avoid odd (harmless) problem with Mercurial 4.5.3 provided by Ubuntu 18.04 on first push: "couldn't write revision branch cache names";
wenzelm
parents: 71486
diff changeset
   534
      var mercurial_source = ""
71245
87c132cf5860 more options;
wenzelm
parents: 71167
diff changeset
   535
      var repo = ""
87c132cf5860 more options;
wenzelm
parents: 71167
diff changeset
   536
      var package_update = false
71276
5bb2235d843d clarified command-line;
wenzelm
parents: 71275
diff changeset
   537
      var name = default_name
71634
5d5be87330b5 allow to override repository versions at runtime;
wenzelm
parents: 71633
diff changeset
   538
      var options = Options.init()
71164
79736ffe77c3 some support for Phabricator server;
wenzelm
parents:
diff changeset
   539
      var root = ""
79736ffe77c3 some support for Phabricator server;
wenzelm
parents:
diff changeset
   540
79736ffe77c3 some support for Phabricator server;
wenzelm
parents:
diff changeset
   541
      val getopts =
79736ffe77c3 some support for Phabricator server;
wenzelm
parents:
diff changeset
   542
        Getopts("""
71276
5bb2235d843d clarified command-line;
wenzelm
parents: 71275
diff changeset
   543
Usage: isabelle phabricator_setup [OPTIONS]
71164
79736ffe77c3 some support for Phabricator server;
wenzelm
parents:
diff changeset
   544
79736ffe77c3 some support for Phabricator server;
wenzelm
parents:
diff changeset
   545
  Options are:
71489
5a2033fc8f3d avoid odd (harmless) problem with Mercurial 4.5.3 provided by Ubuntu 18.04 on first push: "couldn't write revision branch cache names";
wenzelm
parents: 71486
diff changeset
   546
    -M SOURCE    install Mercurial from source: local PATH, or URL, or ":" for
5a2033fc8f3d avoid odd (harmless) problem with Mercurial 4.5.3 provided by Ubuntu 18.04 on first push: "couldn't write revision branch cache names";
wenzelm
parents: 71486
diff changeset
   547
                 """ + standard_mercurial_source + """
71266
510b89906d86 discontinued somewhat pointless Isabelle options: setup implicitly assumes Ubuntu 18.04;
wenzelm
parents: 71264
diff changeset
   548
    -R DIR       repository directory (default: """ + default_repo("NAME") + """)
71245
87c132cf5860 more options;
wenzelm
parents: 71167
diff changeset
   549
    -U           full update of system packages before installation
71276
5bb2235d843d clarified command-line;
wenzelm
parents: 71275
diff changeset
   550
    -n NAME      Phabricator installation name (default: """ + quote(default_name) + """)
71634
5d5be87330b5 allow to override repository versions at runtime;
wenzelm
parents: 71633
diff changeset
   551
    -o OPTION    override Isabelle system OPTION (via NAME=VAL or NAME)
71266
510b89906d86 discontinued somewhat pointless Isabelle options: setup implicitly assumes Ubuntu 18.04;
wenzelm
parents: 71264
diff changeset
   552
    -r DIR       installation root directory (default: """ + default_root("NAME") + """)
71164
79736ffe77c3 some support for Phabricator server;
wenzelm
parents:
diff changeset
   553
71302
c073c4e79518 more documentation;
wenzelm
parents: 71301
diff changeset
   554
  Install Phabricator as LAMP application (Linux, Apache, MySQL, PHP).
71164
79736ffe77c3 some support for Phabricator server;
wenzelm
parents:
diff changeset
   555
71276
5bb2235d843d clarified command-line;
wenzelm
parents: 71275
diff changeset
   556
  The installation name (default: """ + quote(default_name) + """) is mapped to a regular
5bb2235d843d clarified command-line;
wenzelm
parents: 71275
diff changeset
   557
  Unix user; this is relevant for public SSH access.
71164
79736ffe77c3 some support for Phabricator server;
wenzelm
parents:
diff changeset
   558
""",
71489
5a2033fc8f3d avoid odd (harmless) problem with Mercurial 4.5.3 provided by Ubuntu 18.04 on first push: "couldn't write revision branch cache names";
wenzelm
parents: 71486
diff changeset
   559
          "M:" -> (arg => mercurial_source = (if (arg == ":") standard_mercurial_source else arg)),
71164
79736ffe77c3 some support for Phabricator server;
wenzelm
parents:
diff changeset
   560
          "R:" -> (arg => repo = arg),
71245
87c132cf5860 more options;
wenzelm
parents: 71167
diff changeset
   561
          "U" -> (_ => package_update = true),
71276
5bb2235d843d clarified command-line;
wenzelm
parents: 71275
diff changeset
   562
          "n:" -> (arg => name = arg),
71634
5d5be87330b5 allow to override repository versions at runtime;
wenzelm
parents: 71633
diff changeset
   563
          "o:" -> (arg => options = options + arg),
71164
79736ffe77c3 some support for Phabricator server;
wenzelm
parents:
diff changeset
   564
          "r:" -> (arg => root = arg))
79736ffe77c3 some support for Phabricator server;
wenzelm
parents:
diff changeset
   565
79736ffe77c3 some support for Phabricator server;
wenzelm
parents:
diff changeset
   566
      val more_args = getopts(args)
71276
5bb2235d843d clarified command-line;
wenzelm
parents: 71275
diff changeset
   567
      if (more_args.nonEmpty) getopts.usage()
71164
79736ffe77c3 some support for Phabricator server;
wenzelm
parents:
diff changeset
   568
79736ffe77c3 some support for Phabricator server;
wenzelm
parents:
diff changeset
   569
      val progress = new Console_Progress
79736ffe77c3 some support for Phabricator server;
wenzelm
parents:
diff changeset
   570
71477
e2fb60756fb8 proper check of Linux version;
wenzelm
parents: 71475
diff changeset
   571
      val release = Linux.Release()
e2fb60756fb8 proper check of Linux version;
wenzelm
parents: 71475
diff changeset
   572
      if (!release.is_ubuntu_18_04) error("Bad Linux version: Ubuntu 18.04 LTS required")
e2fb60756fb8 proper check of Linux version;
wenzelm
parents: 71475
diff changeset
   573
71634
5d5be87330b5 allow to override repository versions at runtime;
wenzelm
parents: 71633
diff changeset
   574
      phabricator_setup(options, name = name, root = root, repo = repo,
71489
5a2033fc8f3d avoid odd (harmless) problem with Mercurial 4.5.3 provided by Ubuntu 18.04 on first push: "couldn't write revision branch cache names";
wenzelm
parents: 71486
diff changeset
   575
        package_update = package_update, mercurial_source = mercurial_source, progress = progress)
71164
79736ffe77c3 some support for Phabricator server;
wenzelm
parents:
diff changeset
   576
    })
79736ffe77c3 some support for Phabricator server;
wenzelm
parents:
diff changeset
   577
79736ffe77c3 some support for Phabricator server;
wenzelm
parents:
diff changeset
   578
79736ffe77c3 some support for Phabricator server;
wenzelm
parents:
diff changeset
   579
71264
114db2b5a5f8 support for Phabricator mail configuration;
wenzelm
parents: 71256
diff changeset
   580
  /** setup mail **/
71164
79736ffe77c3 some support for Phabricator server;
wenzelm
parents:
diff changeset
   581
71270
wenzelm
parents: 71269
diff changeset
   582
  val mailers_template: String =
wenzelm
parents: 71269
diff changeset
   583
"""[
wenzelm
parents: 71269
diff changeset
   584
  {
wenzelm
parents: 71269
diff changeset
   585
    "key": "example.org",
wenzelm
parents: 71269
diff changeset
   586
    "type": "smtp",
wenzelm
parents: 71269
diff changeset
   587
    "options": {
wenzelm
parents: 71269
diff changeset
   588
      "host": "mail.example.org",
wenzelm
parents: 71269
diff changeset
   589
      "port": 465,
wenzelm
parents: 71269
diff changeset
   590
      "user": "phabricator@example.org",
wenzelm
parents: 71269
diff changeset
   591
      "password": "********",
wenzelm
parents: 71269
diff changeset
   592
      "protocol": "ssl",
wenzelm
parents: 71269
diff changeset
   593
      "message-id": true
wenzelm
parents: 71269
diff changeset
   594
    }
wenzelm
parents: 71269
diff changeset
   595
  }
wenzelm
parents: 71269
diff changeset
   596
]"""
wenzelm
parents: 71269
diff changeset
   597
71264
114db2b5a5f8 support for Phabricator mail configuration;
wenzelm
parents: 71256
diff changeset
   598
  def phabricator_setup_mail(
114db2b5a5f8 support for Phabricator mail configuration;
wenzelm
parents: 71256
diff changeset
   599
    name: String = default_name,
114db2b5a5f8 support for Phabricator mail configuration;
wenzelm
parents: 71256
diff changeset
   600
    config_file: Option[Path] = None,
114db2b5a5f8 support for Phabricator mail configuration;
wenzelm
parents: 71256
diff changeset
   601
    test_user: String = "",
114db2b5a5f8 support for Phabricator mail configuration;
wenzelm
parents: 71256
diff changeset
   602
    progress: Progress = No_Progress)
71164
79736ffe77c3 some support for Phabricator server;
wenzelm
parents:
diff changeset
   603
  {
79736ffe77c3 some support for Phabricator server;
wenzelm
parents:
diff changeset
   604
    Linux.check_system_root()
79736ffe77c3 some support for Phabricator server;
wenzelm
parents:
diff changeset
   605
71264
114db2b5a5f8 support for Phabricator mail configuration;
wenzelm
parents: 71256
diff changeset
   606
    val config = get_config(name)
71271
d61fd7aade69 clarified directory;
wenzelm
parents: 71270
diff changeset
   607
    val default_config_file = config.root + default_mailers
71264
114db2b5a5f8 support for Phabricator mail configuration;
wenzelm
parents: 71256
diff changeset
   608
114db2b5a5f8 support for Phabricator mail configuration;
wenzelm
parents: 71256
diff changeset
   609
    val mail_config = config_file getOrElse default_config_file
114db2b5a5f8 support for Phabricator mail configuration;
wenzelm
parents: 71256
diff changeset
   610
114db2b5a5f8 support for Phabricator mail configuration;
wenzelm
parents: 71256
diff changeset
   611
    def setup_mail
114db2b5a5f8 support for Phabricator mail configuration;
wenzelm
parents: 71256
diff changeset
   612
    {
114db2b5a5f8 support for Phabricator mail configuration;
wenzelm
parents: 71256
diff changeset
   613
      progress.echo("Using mail configuration from " + mail_config)
114db2b5a5f8 support for Phabricator mail configuration;
wenzelm
parents: 71256
diff changeset
   614
      config.execute("config set cluster.mailers --stdin < " + File.bash_path(mail_config))
114db2b5a5f8 support for Phabricator mail configuration;
wenzelm
parents: 71256
diff changeset
   615
114db2b5a5f8 support for Phabricator mail configuration;
wenzelm
parents: 71256
diff changeset
   616
      if (test_user.nonEmpty) {
114db2b5a5f8 support for Phabricator mail configuration;
wenzelm
parents: 71256
diff changeset
   617
        progress.echo("Sending test mail to " + quote(test_user))
114db2b5a5f8 support for Phabricator mail configuration;
wenzelm
parents: 71256
diff changeset
   618
        progress.bash(cwd = config.home.file, echo = true,
71301
wenzelm
parents: 71300
diff changeset
   619
          script = """echo "Test from Phabricator ($(date))" | bin/mail send-test --subject "Test" --to """ +
71264
114db2b5a5f8 support for Phabricator mail configuration;
wenzelm
parents: 71256
diff changeset
   620
            Bash.string(test_user)).check
114db2b5a5f8 support for Phabricator mail configuration;
wenzelm
parents: 71256
diff changeset
   621
      }
114db2b5a5f8 support for Phabricator mail configuration;
wenzelm
parents: 71256
diff changeset
   622
    }
114db2b5a5f8 support for Phabricator mail configuration;
wenzelm
parents: 71256
diff changeset
   623
114db2b5a5f8 support for Phabricator mail configuration;
wenzelm
parents: 71256
diff changeset
   624
    if (config_file.isEmpty) {
71268
79b89278b825 clarified permissions;
wenzelm
parents: 71266
diff changeset
   625
      if (!default_config_file.is_file) {
79b89278b825 clarified permissions;
wenzelm
parents: 71266
diff changeset
   626
        File.write(default_config_file, mailers_template)
71313
6cfec8029831 clarified signature;
wenzelm
parents: 71311
diff changeset
   627
        Isabelle_System.chmod("600", default_config_file)
71268
79b89278b825 clarified permissions;
wenzelm
parents: 71266
diff changeset
   628
      }
71264
114db2b5a5f8 support for Phabricator mail configuration;
wenzelm
parents: 71256
diff changeset
   629
      if (File.read(default_config_file) == mailers_template) {
71329
1579a9160c7f misc tuning and clarification;
wenzelm
parents: 71327
diff changeset
   630
        progress.echo("Please invoke the tool again, after providing details in\n  " +
1579a9160c7f misc tuning and clarification;
wenzelm
parents: 71327
diff changeset
   631
          default_config_file.implode + "\n")
71264
114db2b5a5f8 support for Phabricator mail configuration;
wenzelm
parents: 71256
diff changeset
   632
      }
114db2b5a5f8 support for Phabricator mail configuration;
wenzelm
parents: 71256
diff changeset
   633
      else setup_mail
114db2b5a5f8 support for Phabricator mail configuration;
wenzelm
parents: 71256
diff changeset
   634
    }
114db2b5a5f8 support for Phabricator mail configuration;
wenzelm
parents: 71256
diff changeset
   635
    else setup_mail
71164
79736ffe77c3 some support for Phabricator server;
wenzelm
parents:
diff changeset
   636
  }
79736ffe77c3 some support for Phabricator server;
wenzelm
parents:
diff changeset
   637
79736ffe77c3 some support for Phabricator server;
wenzelm
parents:
diff changeset
   638
79736ffe77c3 some support for Phabricator server;
wenzelm
parents:
diff changeset
   639
  /* Isabelle tool wrapper */
79736ffe77c3 some support for Phabricator server;
wenzelm
parents:
diff changeset
   640
71296
d3ededaa77b3 added "isabelle phabricator";
wenzelm
parents: 71277
diff changeset
   641
  val isabelle_tool3 =
71264
114db2b5a5f8 support for Phabricator mail configuration;
wenzelm
parents: 71256
diff changeset
   642
    Isabelle_Tool("phabricator_setup_mail",
71308
8c1c717a830b configure SSH hosting via "isabelle phabricator_setup_ssh";
wenzelm
parents: 71302
diff changeset
   643
      "setup mail for one Phabricator installation", args =>
71164
79736ffe77c3 some support for Phabricator server;
wenzelm
parents:
diff changeset
   644
    {
71264
114db2b5a5f8 support for Phabricator mail configuration;
wenzelm
parents: 71256
diff changeset
   645
      var test_user = ""
114db2b5a5f8 support for Phabricator mail configuration;
wenzelm
parents: 71256
diff changeset
   646
      var name = default_name
114db2b5a5f8 support for Phabricator mail configuration;
wenzelm
parents: 71256
diff changeset
   647
      var config_file: Option[Path] = None
114db2b5a5f8 support for Phabricator mail configuration;
wenzelm
parents: 71256
diff changeset
   648
71164
79736ffe77c3 some support for Phabricator server;
wenzelm
parents:
diff changeset
   649
      val getopts =
79736ffe77c3 some support for Phabricator server;
wenzelm
parents:
diff changeset
   650
        Getopts("""
71264
114db2b5a5f8 support for Phabricator mail configuration;
wenzelm
parents: 71256
diff changeset
   651
Usage: isabelle phabricator_setup_mail [OPTIONS]
114db2b5a5f8 support for Phabricator mail configuration;
wenzelm
parents: 71256
diff changeset
   652
114db2b5a5f8 support for Phabricator mail configuration;
wenzelm
parents: 71256
diff changeset
   653
  Options are:
114db2b5a5f8 support for Phabricator mail configuration;
wenzelm
parents: 71256
diff changeset
   654
    -T USER      send test mail to Phabricator user
71302
c073c4e79518 more documentation;
wenzelm
parents: 71301
diff changeset
   655
    -f FILE      config file (default: """ + default_mailers + """ within Phabricator root)
71264
114db2b5a5f8 support for Phabricator mail configuration;
wenzelm
parents: 71256
diff changeset
   656
    -n NAME      Phabricator installation name (default: """ + quote(default_name) + """)
71164
79736ffe77c3 some support for Phabricator server;
wenzelm
parents:
diff changeset
   657
71275
41b6ca223500 tuned messages;
wenzelm
parents: 71274
diff changeset
   658
  Provide mail configuration for existing Phabricator installation.
71264
114db2b5a5f8 support for Phabricator mail configuration;
wenzelm
parents: 71256
diff changeset
   659
""",
114db2b5a5f8 support for Phabricator mail configuration;
wenzelm
parents: 71256
diff changeset
   660
          "T:" -> (arg => test_user = arg),
114db2b5a5f8 support for Phabricator mail configuration;
wenzelm
parents: 71256
diff changeset
   661
          "f:" -> (arg => config_file = Some(Path.explode(arg))),
114db2b5a5f8 support for Phabricator mail configuration;
wenzelm
parents: 71256
diff changeset
   662
          "n:" -> (arg => name = arg))
71164
79736ffe77c3 some support for Phabricator server;
wenzelm
parents:
diff changeset
   663
79736ffe77c3 some support for Phabricator server;
wenzelm
parents:
diff changeset
   664
      val more_args = getopts(args)
71264
114db2b5a5f8 support for Phabricator mail configuration;
wenzelm
parents: 71256
diff changeset
   665
      if (more_args.nonEmpty) getopts.usage()
71164
79736ffe77c3 some support for Phabricator server;
wenzelm
parents:
diff changeset
   666
79736ffe77c3 some support for Phabricator server;
wenzelm
parents:
diff changeset
   667
      val progress = new Console_Progress
79736ffe77c3 some support for Phabricator server;
wenzelm
parents:
diff changeset
   668
71264
114db2b5a5f8 support for Phabricator mail configuration;
wenzelm
parents: 71256
diff changeset
   669
      phabricator_setup_mail(name = name, config_file = config_file,
114db2b5a5f8 support for Phabricator mail configuration;
wenzelm
parents: 71256
diff changeset
   670
        test_user = test_user, progress = progress)
71164
79736ffe77c3 some support for Phabricator server;
wenzelm
parents:
diff changeset
   671
    })
71308
8c1c717a830b configure SSH hosting via "isabelle phabricator_setup_ssh";
wenzelm
parents: 71302
diff changeset
   672
8c1c717a830b configure SSH hosting via "isabelle phabricator_setup_ssh";
wenzelm
parents: 71302
diff changeset
   673
8c1c717a830b configure SSH hosting via "isabelle phabricator_setup_ssh";
wenzelm
parents: 71302
diff changeset
   674
8c1c717a830b configure SSH hosting via "isabelle phabricator_setup_ssh";
wenzelm
parents: 71302
diff changeset
   675
  /** setup ssh **/
8c1c717a830b configure SSH hosting via "isabelle phabricator_setup_ssh";
wenzelm
parents: 71302
diff changeset
   676
8c1c717a830b configure SSH hosting via "isabelle phabricator_setup_ssh";
wenzelm
parents: 71302
diff changeset
   677
  /* sshd config */
8c1c717a830b configure SSH hosting via "isabelle phabricator_setup_ssh";
wenzelm
parents: 71302
diff changeset
   678
8c1c717a830b configure SSH hosting via "isabelle phabricator_setup_ssh";
wenzelm
parents: 71302
diff changeset
   679
  private val Port = """^\s*Port\s+(\d+)\s*$""".r
8c1c717a830b configure SSH hosting via "isabelle phabricator_setup_ssh";
wenzelm
parents: 71302
diff changeset
   680
  private val No_Port = """^#\s*Port\b.*$""".r
8c1c717a830b configure SSH hosting via "isabelle phabricator_setup_ssh";
wenzelm
parents: 71302
diff changeset
   681
  private val Any_Port = """^#?\s*Port\b.*$""".r
8c1c717a830b configure SSH hosting via "isabelle phabricator_setup_ssh";
wenzelm
parents: 71302
diff changeset
   682
8c1c717a830b configure SSH hosting via "isabelle phabricator_setup_ssh";
wenzelm
parents: 71302
diff changeset
   683
  def conf_ssh_port(port: Int): String =
8c1c717a830b configure SSH hosting via "isabelle phabricator_setup_ssh";
wenzelm
parents: 71302
diff changeset
   684
    if (port == 22) "#Port 22" else "Port " + port
8c1c717a830b configure SSH hosting via "isabelle phabricator_setup_ssh";
wenzelm
parents: 71302
diff changeset
   685
8c1c717a830b configure SSH hosting via "isabelle phabricator_setup_ssh";
wenzelm
parents: 71302
diff changeset
   686
  def read_ssh_port(conf: Path): Int =
8c1c717a830b configure SSH hosting via "isabelle phabricator_setup_ssh";
wenzelm
parents: 71302
diff changeset
   687
  {
8c1c717a830b configure SSH hosting via "isabelle phabricator_setup_ssh";
wenzelm
parents: 71302
diff changeset
   688
    val lines = split_lines(File.read(conf))
8c1c717a830b configure SSH hosting via "isabelle phabricator_setup_ssh";
wenzelm
parents: 71302
diff changeset
   689
    val ports =
8c1c717a830b configure SSH hosting via "isabelle phabricator_setup_ssh";
wenzelm
parents: 71302
diff changeset
   690
      lines.flatMap({
8c1c717a830b configure SSH hosting via "isabelle phabricator_setup_ssh";
wenzelm
parents: 71302
diff changeset
   691
        case Port(Value.Int(p)) => Some(p)
8c1c717a830b configure SSH hosting via "isabelle phabricator_setup_ssh";
wenzelm
parents: 71302
diff changeset
   692
        case No_Port() => Some(22)
8c1c717a830b configure SSH hosting via "isabelle phabricator_setup_ssh";
wenzelm
parents: 71302
diff changeset
   693
        case _ => None
8c1c717a830b configure SSH hosting via "isabelle phabricator_setup_ssh";
wenzelm
parents: 71302
diff changeset
   694
      })
8c1c717a830b configure SSH hosting via "isabelle phabricator_setup_ssh";
wenzelm
parents: 71302
diff changeset
   695
    ports match {
8c1c717a830b configure SSH hosting via "isabelle phabricator_setup_ssh";
wenzelm
parents: 71302
diff changeset
   696
      case List(port) => port
8c1c717a830b configure SSH hosting via "isabelle phabricator_setup_ssh";
wenzelm
parents: 71302
diff changeset
   697
      case Nil => error("Missing Port specification in " + conf)
8c1c717a830b configure SSH hosting via "isabelle phabricator_setup_ssh";
wenzelm
parents: 71302
diff changeset
   698
      case _ => error("Multiple Port specifications in " + conf)
8c1c717a830b configure SSH hosting via "isabelle phabricator_setup_ssh";
wenzelm
parents: 71302
diff changeset
   699
    }
8c1c717a830b configure SSH hosting via "isabelle phabricator_setup_ssh";
wenzelm
parents: 71302
diff changeset
   700
  }
8c1c717a830b configure SSH hosting via "isabelle phabricator_setup_ssh";
wenzelm
parents: 71302
diff changeset
   701
8c1c717a830b configure SSH hosting via "isabelle phabricator_setup_ssh";
wenzelm
parents: 71302
diff changeset
   702
  def write_ssh_port(conf: Path, port: Int): Boolean =
8c1c717a830b configure SSH hosting via "isabelle phabricator_setup_ssh";
wenzelm
parents: 71302
diff changeset
   703
  {
8c1c717a830b configure SSH hosting via "isabelle phabricator_setup_ssh";
wenzelm
parents: 71302
diff changeset
   704
    val old_port = read_ssh_port(conf)
8c1c717a830b configure SSH hosting via "isabelle phabricator_setup_ssh";
wenzelm
parents: 71302
diff changeset
   705
    if (old_port == port) false
8c1c717a830b configure SSH hosting via "isabelle phabricator_setup_ssh";
wenzelm
parents: 71302
diff changeset
   706
    else {
8c1c717a830b configure SSH hosting via "isabelle phabricator_setup_ssh";
wenzelm
parents: 71302
diff changeset
   707
      val lines = split_lines(File.read(conf))
8c1c717a830b configure SSH hosting via "isabelle phabricator_setup_ssh";
wenzelm
parents: 71302
diff changeset
   708
      val lines1 = lines.map({ case Any_Port() => conf_ssh_port(port) case line => line })
8c1c717a830b configure SSH hosting via "isabelle phabricator_setup_ssh";
wenzelm
parents: 71302
diff changeset
   709
      File.write(conf, cat_lines(lines1))
8c1c717a830b configure SSH hosting via "isabelle phabricator_setup_ssh";
wenzelm
parents: 71302
diff changeset
   710
      true
8c1c717a830b configure SSH hosting via "isabelle phabricator_setup_ssh";
wenzelm
parents: 71302
diff changeset
   711
    }
8c1c717a830b configure SSH hosting via "isabelle phabricator_setup_ssh";
wenzelm
parents: 71302
diff changeset
   712
  }
8c1c717a830b configure SSH hosting via "isabelle phabricator_setup_ssh";
wenzelm
parents: 71302
diff changeset
   713
8c1c717a830b configure SSH hosting via "isabelle phabricator_setup_ssh";
wenzelm
parents: 71302
diff changeset
   714
8c1c717a830b configure SSH hosting via "isabelle phabricator_setup_ssh";
wenzelm
parents: 71302
diff changeset
   715
  /* phabricator_setup_ssh */
8c1c717a830b configure SSH hosting via "isabelle phabricator_setup_ssh";
wenzelm
parents: 71302
diff changeset
   716
8c1c717a830b configure SSH hosting via "isabelle phabricator_setup_ssh";
wenzelm
parents: 71302
diff changeset
   717
  def phabricator_setup_ssh(
8c1c717a830b configure SSH hosting via "isabelle phabricator_setup_ssh";
wenzelm
parents: 71302
diff changeset
   718
    server_port: Int = default_server_port,
8c1c717a830b configure SSH hosting via "isabelle phabricator_setup_ssh";
wenzelm
parents: 71302
diff changeset
   719
    system_port: Int = default_system_port,
8c1c717a830b configure SSH hosting via "isabelle phabricator_setup_ssh";
wenzelm
parents: 71302
diff changeset
   720
    progress: Progress = No_Progress)
8c1c717a830b configure SSH hosting via "isabelle phabricator_setup_ssh";
wenzelm
parents: 71302
diff changeset
   721
  {
8c1c717a830b configure SSH hosting via "isabelle phabricator_setup_ssh";
wenzelm
parents: 71302
diff changeset
   722
    Linux.check_system_root()
8c1c717a830b configure SSH hosting via "isabelle phabricator_setup_ssh";
wenzelm
parents: 71302
diff changeset
   723
8c1c717a830b configure SSH hosting via "isabelle phabricator_setup_ssh";
wenzelm
parents: 71302
diff changeset
   724
    val configs = read_config()
8c1c717a830b configure SSH hosting via "isabelle phabricator_setup_ssh";
wenzelm
parents: 71302
diff changeset
   725
8c1c717a830b configure SSH hosting via "isabelle phabricator_setup_ssh";
wenzelm
parents: 71302
diff changeset
   726
    if (server_port == system_port) {
8c1c717a830b configure SSH hosting via "isabelle phabricator_setup_ssh";
wenzelm
parents: 71302
diff changeset
   727
      error("Port for Phabricator sshd coincides with system port: " + system_port)
8c1c717a830b configure SSH hosting via "isabelle phabricator_setup_ssh";
wenzelm
parents: 71302
diff changeset
   728
    }
8c1c717a830b configure SSH hosting via "isabelle phabricator_setup_ssh";
wenzelm
parents: 71302
diff changeset
   729
8c1c717a830b configure SSH hosting via "isabelle phabricator_setup_ssh";
wenzelm
parents: 71302
diff changeset
   730
    val sshd_conf_system = Path.explode("/etc/ssh/sshd_config")
8c1c717a830b configure SSH hosting via "isabelle phabricator_setup_ssh";
wenzelm
parents: 71302
diff changeset
   731
    val sshd_conf_server = sshd_conf_system.ext(isabelle_phabricator_name())
8c1c717a830b configure SSH hosting via "isabelle phabricator_setup_ssh";
wenzelm
parents: 71302
diff changeset
   732
8c1c717a830b configure SSH hosting via "isabelle phabricator_setup_ssh";
wenzelm
parents: 71302
diff changeset
   733
    val ssh_name = isabelle_phabricator_name(name = "ssh")
71310
cd166c3904dd more robust: system ssh service is required for Phabricator ssh service;
wenzelm
parents: 71308
diff changeset
   734
    Linux.service_shutdown(ssh_name)
cd166c3904dd more robust: system ssh service is required for Phabricator ssh service;
wenzelm
parents: 71308
diff changeset
   735
71308
8c1c717a830b configure SSH hosting via "isabelle phabricator_setup_ssh";
wenzelm
parents: 71302
diff changeset
   736
    val old_system_port = read_ssh_port(sshd_conf_system)
8c1c717a830b configure SSH hosting via "isabelle phabricator_setup_ssh";
wenzelm
parents: 71302
diff changeset
   737
    if (old_system_port != system_port) {
8c1c717a830b configure SSH hosting via "isabelle phabricator_setup_ssh";
wenzelm
parents: 71302
diff changeset
   738
      progress.echo("Reconfigurig system ssh service")
71310
cd166c3904dd more robust: system ssh service is required for Phabricator ssh service;
wenzelm
parents: 71308
diff changeset
   739
      Linux.service_shutdown("ssh")
71308
8c1c717a830b configure SSH hosting via "isabelle phabricator_setup_ssh";
wenzelm
parents: 71302
diff changeset
   740
      write_ssh_port(sshd_conf_system, system_port)
71310
cd166c3904dd more robust: system ssh service is required for Phabricator ssh service;
wenzelm
parents: 71308
diff changeset
   741
      Linux.service_start("ssh")
71308
8c1c717a830b configure SSH hosting via "isabelle phabricator_setup_ssh";
wenzelm
parents: 71302
diff changeset
   742
    }
8c1c717a830b configure SSH hosting via "isabelle phabricator_setup_ssh";
wenzelm
parents: 71302
diff changeset
   743
8c1c717a830b configure SSH hosting via "isabelle phabricator_setup_ssh";
wenzelm
parents: 71302
diff changeset
   744
    progress.echo("Configuring " + ssh_name + " service")
8c1c717a830b configure SSH hosting via "isabelle phabricator_setup_ssh";
wenzelm
parents: 71302
diff changeset
   745
71491
de59dd86760f tuned signature;
wenzelm
parents: 71490
diff changeset
   746
    val ssh_command = command_setup(ssh_name, body =
71320
730090397e0d clarified signature;
wenzelm
parents: 71315
diff changeset
   747
"""if [ "$1" = "$NAME" ]
730090397e0d clarified signature;
wenzelm
parents: 71315
diff changeset
   748
then
730090397e0d clarified signature;
wenzelm
parents: 71315
diff changeset
   749
  exec "$ROOT/phabricator/bin/ssh-auth" "$@"
71479
wenzelm
parents: 71478
diff changeset
   750
fi""", exit = "exit 1")
71308
8c1c717a830b configure SSH hosting via "isabelle phabricator_setup_ssh";
wenzelm
parents: 71302
diff changeset
   751
8c1c717a830b configure SSH hosting via "isabelle phabricator_setup_ssh";
wenzelm
parents: 71302
diff changeset
   752
    File.write(sshd_conf_server,
8c1c717a830b configure SSH hosting via "isabelle phabricator_setup_ssh";
wenzelm
parents: 71302
diff changeset
   753
"""# OpenBSD Secure Shell server for Isabelle/Phabricator
8c1c717a830b configure SSH hosting via "isabelle phabricator_setup_ssh";
wenzelm
parents: 71302
diff changeset
   754
AuthorizedKeysCommand """ + ssh_command.implode + """
8c1c717a830b configure SSH hosting via "isabelle phabricator_setup_ssh";
wenzelm
parents: 71302
diff changeset
   755
AuthorizedKeysCommandUser """ + daemon_user + """
8c1c717a830b configure SSH hosting via "isabelle phabricator_setup_ssh";
wenzelm
parents: 71302
diff changeset
   756
AuthorizedKeysFile none
8c1c717a830b configure SSH hosting via "isabelle phabricator_setup_ssh";
wenzelm
parents: 71302
diff changeset
   757
AllowUsers """ + configs.map(_.name).mkString(" ") + """
8c1c717a830b configure SSH hosting via "isabelle phabricator_setup_ssh";
wenzelm
parents: 71302
diff changeset
   758
Port """ + server_port + """
8c1c717a830b configure SSH hosting via "isabelle phabricator_setup_ssh";
wenzelm
parents: 71302
diff changeset
   759
Protocol 2
8c1c717a830b configure SSH hosting via "isabelle phabricator_setup_ssh";
wenzelm
parents: 71302
diff changeset
   760
PermitRootLogin no
8c1c717a830b configure SSH hosting via "isabelle phabricator_setup_ssh";
wenzelm
parents: 71302
diff changeset
   761
AllowAgentForwarding no
8c1c717a830b configure SSH hosting via "isabelle phabricator_setup_ssh";
wenzelm
parents: 71302
diff changeset
   762
AllowTcpForwarding no
8c1c717a830b configure SSH hosting via "isabelle phabricator_setup_ssh";
wenzelm
parents: 71302
diff changeset
   763
PrintMotd no
8c1c717a830b configure SSH hosting via "isabelle phabricator_setup_ssh";
wenzelm
parents: 71302
diff changeset
   764
PrintLastLog no
8c1c717a830b configure SSH hosting via "isabelle phabricator_setup_ssh";
wenzelm
parents: 71302
diff changeset
   765
PasswordAuthentication no
8c1c717a830b configure SSH hosting via "isabelle phabricator_setup_ssh";
wenzelm
parents: 71302
diff changeset
   766
ChallengeResponseAuthentication no
8c1c717a830b configure SSH hosting via "isabelle phabricator_setup_ssh";
wenzelm
parents: 71302
diff changeset
   767
PidFile /var/run/""" + ssh_name + """.pid
8c1c717a830b configure SSH hosting via "isabelle phabricator_setup_ssh";
wenzelm
parents: 71302
diff changeset
   768
""")
8c1c717a830b configure SSH hosting via "isabelle phabricator_setup_ssh";
wenzelm
parents: 71302
diff changeset
   769
8c1c717a830b configure SSH hosting via "isabelle phabricator_setup_ssh";
wenzelm
parents: 71302
diff changeset
   770
    Linux.service_install(ssh_name,
8c1c717a830b configure SSH hosting via "isabelle phabricator_setup_ssh";
wenzelm
parents: 71302
diff changeset
   771
"""[Unit]
8c1c717a830b configure SSH hosting via "isabelle phabricator_setup_ssh";
wenzelm
parents: 71302
diff changeset
   772
Description=OpenBSD Secure Shell server for Isabelle/Phabricator
8c1c717a830b configure SSH hosting via "isabelle phabricator_setup_ssh";
wenzelm
parents: 71302
diff changeset
   773
After=network.target auditd.service
8c1c717a830b configure SSH hosting via "isabelle phabricator_setup_ssh";
wenzelm
parents: 71302
diff changeset
   774
ConditionPathExists=!/etc/ssh/sshd_not_to_be_run
8c1c717a830b configure SSH hosting via "isabelle phabricator_setup_ssh";
wenzelm
parents: 71302
diff changeset
   775
8c1c717a830b configure SSH hosting via "isabelle phabricator_setup_ssh";
wenzelm
parents: 71302
diff changeset
   776
[Service]
8c1c717a830b configure SSH hosting via "isabelle phabricator_setup_ssh";
wenzelm
parents: 71302
diff changeset
   777
EnvironmentFile=-/etc/default/ssh
8c1c717a830b configure SSH hosting via "isabelle phabricator_setup_ssh";
wenzelm
parents: 71302
diff changeset
   778
ExecStartPre=/usr/sbin/sshd -f """ + sshd_conf_server.implode + """ -t
8c1c717a830b configure SSH hosting via "isabelle phabricator_setup_ssh";
wenzelm
parents: 71302
diff changeset
   779
ExecStart=/usr/sbin/sshd -f """ + sshd_conf_server.implode + """ -D $SSHD_OPTS
8c1c717a830b configure SSH hosting via "isabelle phabricator_setup_ssh";
wenzelm
parents: 71302
diff changeset
   780
ExecReload=/usr/sbin/sshd -f """ + sshd_conf_server.implode + """ -t
8c1c717a830b configure SSH hosting via "isabelle phabricator_setup_ssh";
wenzelm
parents: 71302
diff changeset
   781
ExecReload=/bin/kill -HUP $MAINPID
8c1c717a830b configure SSH hosting via "isabelle phabricator_setup_ssh";
wenzelm
parents: 71302
diff changeset
   782
KillMode=process
8c1c717a830b configure SSH hosting via "isabelle phabricator_setup_ssh";
wenzelm
parents: 71302
diff changeset
   783
Restart=on-failure
8c1c717a830b configure SSH hosting via "isabelle phabricator_setup_ssh";
wenzelm
parents: 71302
diff changeset
   784
RestartPreventExitStatus=255
8c1c717a830b configure SSH hosting via "isabelle phabricator_setup_ssh";
wenzelm
parents: 71302
diff changeset
   785
Type=notify
8c1c717a830b configure SSH hosting via "isabelle phabricator_setup_ssh";
wenzelm
parents: 71302
diff changeset
   786
RuntimeDirectory=sshd-phabricator
8c1c717a830b configure SSH hosting via "isabelle phabricator_setup_ssh";
wenzelm
parents: 71302
diff changeset
   787
RuntimeDirectoryMode=0755
8c1c717a830b configure SSH hosting via "isabelle phabricator_setup_ssh";
wenzelm
parents: 71302
diff changeset
   788
8c1c717a830b configure SSH hosting via "isabelle phabricator_setup_ssh";
wenzelm
parents: 71302
diff changeset
   789
[Install]
8c1c717a830b configure SSH hosting via "isabelle phabricator_setup_ssh";
wenzelm
parents: 71302
diff changeset
   790
WantedBy=multi-user.target
8c1c717a830b configure SSH hosting via "isabelle phabricator_setup_ssh";
wenzelm
parents: 71302
diff changeset
   791
Alias=""" + ssh_name + """.service
8c1c717a830b configure SSH hosting via "isabelle phabricator_setup_ssh";
wenzelm
parents: 71302
diff changeset
   792
""")
8c1c717a830b configure SSH hosting via "isabelle phabricator_setup_ssh";
wenzelm
parents: 71302
diff changeset
   793
8c1c717a830b configure SSH hosting via "isabelle phabricator_setup_ssh";
wenzelm
parents: 71302
diff changeset
   794
    for (config <- configs) {
8c1c717a830b configure SSH hosting via "isabelle phabricator_setup_ssh";
wenzelm
parents: 71302
diff changeset
   795
      progress.echo("phabricator " + quote(config.name) + " port " +  server_port)
8c1c717a830b configure SSH hosting via "isabelle phabricator_setup_ssh";
wenzelm
parents: 71302
diff changeset
   796
      config.execute("config set diffusion.ssh-port " + Bash.string(server_port.toString))
71501
8b745b4d71b5 more rebust setup for Subversion -- assuming that diffusion.ssh-port is only set in phabricator/conf/local/local.json, not in the database;
wenzelm
parents: 71497
diff changeset
   797
      if (server_port == 22) config.execute("config delete diffusion.ssh-port")
71308
8c1c717a830b configure SSH hosting via "isabelle phabricator_setup_ssh";
wenzelm
parents: 71302
diff changeset
   798
    }
8c1c717a830b configure SSH hosting via "isabelle phabricator_setup_ssh";
wenzelm
parents: 71302
diff changeset
   799
  }
8c1c717a830b configure SSH hosting via "isabelle phabricator_setup_ssh";
wenzelm
parents: 71302
diff changeset
   800
8c1c717a830b configure SSH hosting via "isabelle phabricator_setup_ssh";
wenzelm
parents: 71302
diff changeset
   801
8c1c717a830b configure SSH hosting via "isabelle phabricator_setup_ssh";
wenzelm
parents: 71302
diff changeset
   802
  /* Isabelle tool wrapper */
8c1c717a830b configure SSH hosting via "isabelle phabricator_setup_ssh";
wenzelm
parents: 71302
diff changeset
   803
8c1c717a830b configure SSH hosting via "isabelle phabricator_setup_ssh";
wenzelm
parents: 71302
diff changeset
   804
  val isabelle_tool4 =
8c1c717a830b configure SSH hosting via "isabelle phabricator_setup_ssh";
wenzelm
parents: 71302
diff changeset
   805
    Isabelle_Tool("phabricator_setup_ssh",
8c1c717a830b configure SSH hosting via "isabelle phabricator_setup_ssh";
wenzelm
parents: 71302
diff changeset
   806
      "setup ssh service for all Phabricator installations", args =>
8c1c717a830b configure SSH hosting via "isabelle phabricator_setup_ssh";
wenzelm
parents: 71302
diff changeset
   807
    {
8c1c717a830b configure SSH hosting via "isabelle phabricator_setup_ssh";
wenzelm
parents: 71302
diff changeset
   808
      var server_port = default_server_port
8c1c717a830b configure SSH hosting via "isabelle phabricator_setup_ssh";
wenzelm
parents: 71302
diff changeset
   809
      var system_port = default_system_port
8c1c717a830b configure SSH hosting via "isabelle phabricator_setup_ssh";
wenzelm
parents: 71302
diff changeset
   810
8c1c717a830b configure SSH hosting via "isabelle phabricator_setup_ssh";
wenzelm
parents: 71302
diff changeset
   811
      val getopts =
8c1c717a830b configure SSH hosting via "isabelle phabricator_setup_ssh";
wenzelm
parents: 71302
diff changeset
   812
        Getopts("""
8c1c717a830b configure SSH hosting via "isabelle phabricator_setup_ssh";
wenzelm
parents: 71302
diff changeset
   813
Usage: isabelle phabricator_setup_ssh [OPTIONS]
8c1c717a830b configure SSH hosting via "isabelle phabricator_setup_ssh";
wenzelm
parents: 71302
diff changeset
   814
8c1c717a830b configure SSH hosting via "isabelle phabricator_setup_ssh";
wenzelm
parents: 71302
diff changeset
   815
  Options are:
8c1c717a830b configure SSH hosting via "isabelle phabricator_setup_ssh";
wenzelm
parents: 71302
diff changeset
   816
    -p PORT      sshd port for Phabricator servers (default: """ + default_server_port + """)
8c1c717a830b configure SSH hosting via "isabelle phabricator_setup_ssh";
wenzelm
parents: 71302
diff changeset
   817
    -q PORT      sshd port for the operating system (default: """ + default_system_port + """)
8c1c717a830b configure SSH hosting via "isabelle phabricator_setup_ssh";
wenzelm
parents: 71302
diff changeset
   818
8c1c717a830b configure SSH hosting via "isabelle phabricator_setup_ssh";
wenzelm
parents: 71302
diff changeset
   819
  Configure ssh service for all Phabricator installations: a separate sshd
8c1c717a830b configure SSH hosting via "isabelle phabricator_setup_ssh";
wenzelm
parents: 71302
diff changeset
   820
  is run in addition to the one of the operating system, and ports need to
8c1c717a830b configure SSH hosting via "isabelle phabricator_setup_ssh";
wenzelm
parents: 71302
diff changeset
   821
  be distinct.
8c1c717a830b configure SSH hosting via "isabelle phabricator_setup_ssh";
wenzelm
parents: 71302
diff changeset
   822
8c1c717a830b configure SSH hosting via "isabelle phabricator_setup_ssh";
wenzelm
parents: 71302
diff changeset
   823
  A particular Phabricator installation is addressed by using its
8c1c717a830b configure SSH hosting via "isabelle phabricator_setup_ssh";
wenzelm
parents: 71302
diff changeset
   824
  name as the ssh user; the actual Phabricator user is determined via
8c1c717a830b configure SSH hosting via "isabelle phabricator_setup_ssh";
wenzelm
parents: 71302
diff changeset
   825
  stored ssh keys.
8c1c717a830b configure SSH hosting via "isabelle phabricator_setup_ssh";
wenzelm
parents: 71302
diff changeset
   826
""",
8c1c717a830b configure SSH hosting via "isabelle phabricator_setup_ssh";
wenzelm
parents: 71302
diff changeset
   827
          "p:" -> (arg => server_port = Value.Int.parse(arg)),
71504
6aadbd650280 eliminated pointless option -T: it merely tests ssh config of root, which is not required later;
wenzelm
parents: 71501
diff changeset
   828
          "q:" -> (arg => system_port = Value.Int.parse(arg)))
71308
8c1c717a830b configure SSH hosting via "isabelle phabricator_setup_ssh";
wenzelm
parents: 71302
diff changeset
   829
8c1c717a830b configure SSH hosting via "isabelle phabricator_setup_ssh";
wenzelm
parents: 71302
diff changeset
   830
      val more_args = getopts(args)
8c1c717a830b configure SSH hosting via "isabelle phabricator_setup_ssh";
wenzelm
parents: 71302
diff changeset
   831
      if (more_args.nonEmpty) getopts.usage()
8c1c717a830b configure SSH hosting via "isabelle phabricator_setup_ssh";
wenzelm
parents: 71302
diff changeset
   832
8c1c717a830b configure SSH hosting via "isabelle phabricator_setup_ssh";
wenzelm
parents: 71302
diff changeset
   833
      val progress = new Console_Progress
8c1c717a830b configure SSH hosting via "isabelle phabricator_setup_ssh";
wenzelm
parents: 71302
diff changeset
   834
8c1c717a830b configure SSH hosting via "isabelle phabricator_setup_ssh";
wenzelm
parents: 71302
diff changeset
   835
      phabricator_setup_ssh(
71504
6aadbd650280 eliminated pointless option -T: it merely tests ssh config of root, which is not required later;
wenzelm
parents: 71501
diff changeset
   836
        server_port = server_port, system_port = system_port, progress = progress)
71308
8c1c717a830b configure SSH hosting via "isabelle phabricator_setup_ssh";
wenzelm
parents: 71302
diff changeset
   837
    })
71508
51c19a44cfed support for conduit API;
wenzelm
parents: 71504
diff changeset
   838
51c19a44cfed support for conduit API;
wenzelm
parents: 71504
diff changeset
   839
51c19a44cfed support for conduit API;
wenzelm
parents: 71504
diff changeset
   840
51c19a44cfed support for conduit API;
wenzelm
parents: 71504
diff changeset
   841
  /** conduit API **/
51c19a44cfed support for conduit API;
wenzelm
parents: 71504
diff changeset
   842
71523
5b68cc73f8b1 clarified signature;
wenzelm
parents: 71520
diff changeset
   843
  object API
71508
51c19a44cfed support for conduit API;
wenzelm
parents: 71504
diff changeset
   844
  {
71541
277ee690cb14 clarified signature -- more operations;
wenzelm
parents: 71540
diff changeset
   845
    /* user information */
277ee690cb14 clarified signature -- more operations;
wenzelm
parents: 71540
diff changeset
   846
277ee690cb14 clarified signature -- more operations;
wenzelm
parents: 71540
diff changeset
   847
    sealed case class User(
277ee690cb14 clarified signature -- more operations;
wenzelm
parents: 71540
diff changeset
   848
      id: Long,
277ee690cb14 clarified signature -- more operations;
wenzelm
parents: 71540
diff changeset
   849
      phid: String,
277ee690cb14 clarified signature -- more operations;
wenzelm
parents: 71540
diff changeset
   850
      name: String,
277ee690cb14 clarified signature -- more operations;
wenzelm
parents: 71540
diff changeset
   851
      real_name: String,
277ee690cb14 clarified signature -- more operations;
wenzelm
parents: 71540
diff changeset
   852
      roles: List[String])
277ee690cb14 clarified signature -- more operations;
wenzelm
parents: 71540
diff changeset
   853
    {
277ee690cb14 clarified signature -- more operations;
wenzelm
parents: 71540
diff changeset
   854
      def is_valid: Boolean =
277ee690cb14 clarified signature -- more operations;
wenzelm
parents: 71540
diff changeset
   855
        roles.contains("verified") &&
277ee690cb14 clarified signature -- more operations;
wenzelm
parents: 71540
diff changeset
   856
        roles.contains("approved") &&
277ee690cb14 clarified signature -- more operations;
wenzelm
parents: 71540
diff changeset
   857
        roles.contains("activated")
277ee690cb14 clarified signature -- more operations;
wenzelm
parents: 71540
diff changeset
   858
      def is_admin: Boolean = roles.contains("admin")
277ee690cb14 clarified signature -- more operations;
wenzelm
parents: 71540
diff changeset
   859
      def is_regular: Boolean = !(roles.contains("bot") || roles.contains("list"))
277ee690cb14 clarified signature -- more operations;
wenzelm
parents: 71540
diff changeset
   860
    }
277ee690cb14 clarified signature -- more operations;
wenzelm
parents: 71540
diff changeset
   861
277ee690cb14 clarified signature -- more operations;
wenzelm
parents: 71540
diff changeset
   862
71523
5b68cc73f8b1 clarified signature;
wenzelm
parents: 71520
diff changeset
   863
    /* repository information */
5b68cc73f8b1 clarified signature;
wenzelm
parents: 71520
diff changeset
   864
5b68cc73f8b1 clarified signature;
wenzelm
parents: 71520
diff changeset
   865
    sealed case class Repository(
5b68cc73f8b1 clarified signature;
wenzelm
parents: 71520
diff changeset
   866
      vcs: VCS.Value,
5b68cc73f8b1 clarified signature;
wenzelm
parents: 71520
diff changeset
   867
      id: Long,
5b68cc73f8b1 clarified signature;
wenzelm
parents: 71520
diff changeset
   868
      phid: String,
5b68cc73f8b1 clarified signature;
wenzelm
parents: 71520
diff changeset
   869
      name: String,
5b68cc73f8b1 clarified signature;
wenzelm
parents: 71520
diff changeset
   870
      callsign: String,
5b68cc73f8b1 clarified signature;
wenzelm
parents: 71520
diff changeset
   871
      short_name: String,
5b68cc73f8b1 clarified signature;
wenzelm
parents: 71520
diff changeset
   872
      importing: Boolean,
5b68cc73f8b1 clarified signature;
wenzelm
parents: 71520
diff changeset
   873
      ssh_url: String)
5b68cc73f8b1 clarified signature;
wenzelm
parents: 71520
diff changeset
   874
    {
5b68cc73f8b1 clarified signature;
wenzelm
parents: 71520
diff changeset
   875
      def is_hg: Boolean = vcs == VCS.hg
5b68cc73f8b1 clarified signature;
wenzelm
parents: 71520
diff changeset
   876
    }
5b68cc73f8b1 clarified signature;
wenzelm
parents: 71520
diff changeset
   877
5b68cc73f8b1 clarified signature;
wenzelm
parents: 71520
diff changeset
   878
    object VCS extends Enumeration
5b68cc73f8b1 clarified signature;
wenzelm
parents: 71520
diff changeset
   879
    {
5b68cc73f8b1 clarified signature;
wenzelm
parents: 71520
diff changeset
   880
      val hg, git, svn = Value
5b68cc73f8b1 clarified signature;
wenzelm
parents: 71520
diff changeset
   881
      def read(s: String): Value =
5b68cc73f8b1 clarified signature;
wenzelm
parents: 71520
diff changeset
   882
        try { withName(s) }
5b68cc73f8b1 clarified signature;
wenzelm
parents: 71520
diff changeset
   883
        catch { case _: java.util.NoSuchElementException  => error("Unknown vcs type " + quote(s)) }
5b68cc73f8b1 clarified signature;
wenzelm
parents: 71520
diff changeset
   884
    }
5b68cc73f8b1 clarified signature;
wenzelm
parents: 71520
diff changeset
   885
5b68cc73f8b1 clarified signature;
wenzelm
parents: 71520
diff changeset
   886
    def edits(typ: String, value: JSON.T): List[JSON.Object.T] =
5b68cc73f8b1 clarified signature;
wenzelm
parents: 71520
diff changeset
   887
      List(JSON.Object("type" -> typ, "value" -> value))
5b68cc73f8b1 clarified signature;
wenzelm
parents: 71520
diff changeset
   888
5b68cc73f8b1 clarified signature;
wenzelm
parents: 71520
diff changeset
   889
    def opt_edits(typ: String, value: Option[JSON.T]): List[JSON.Object.T] =
5b68cc73f8b1 clarified signature;
wenzelm
parents: 71520
diff changeset
   890
      value.toList.flatMap(edits(typ, _))
5b68cc73f8b1 clarified signature;
wenzelm
parents: 71520
diff changeset
   891
5b68cc73f8b1 clarified signature;
wenzelm
parents: 71520
diff changeset
   892
5b68cc73f8b1 clarified signature;
wenzelm
parents: 71520
diff changeset
   893
    /* result with optional error */
5b68cc73f8b1 clarified signature;
wenzelm
parents: 71520
diff changeset
   894
5b68cc73f8b1 clarified signature;
wenzelm
parents: 71520
diff changeset
   895
    sealed case class Result(result: JSON.T, error: Option[String])
5b68cc73f8b1 clarified signature;
wenzelm
parents: 71520
diff changeset
   896
    {
5b68cc73f8b1 clarified signature;
wenzelm
parents: 71520
diff changeset
   897
      def ok: Boolean = error.isEmpty
5b68cc73f8b1 clarified signature;
wenzelm
parents: 71520
diff changeset
   898
      def get: JSON.T = if (ok) result else Exn.error(error.get)
5b68cc73f8b1 clarified signature;
wenzelm
parents: 71520
diff changeset
   899
5b68cc73f8b1 clarified signature;
wenzelm
parents: 71520
diff changeset
   900
      def get_value[A](unapply: JSON.T => Option[A]): A =
5b68cc73f8b1 clarified signature;
wenzelm
parents: 71520
diff changeset
   901
        unapply(get) getOrElse Exn.error("Bad JSON result: " + JSON.Format(result))
5b68cc73f8b1 clarified signature;
wenzelm
parents: 71520
diff changeset
   902
5b68cc73f8b1 clarified signature;
wenzelm
parents: 71520
diff changeset
   903
      def get_string: String = get_value(JSON.Value.String.unapply)
5b68cc73f8b1 clarified signature;
wenzelm
parents: 71520
diff changeset
   904
    }
5b68cc73f8b1 clarified signature;
wenzelm
parents: 71520
diff changeset
   905
5b68cc73f8b1 clarified signature;
wenzelm
parents: 71520
diff changeset
   906
    def make_result(json: JSON.T): Result =
5b68cc73f8b1 clarified signature;
wenzelm
parents: 71520
diff changeset
   907
    {
5b68cc73f8b1 clarified signature;
wenzelm
parents: 71520
diff changeset
   908
      val result = JSON.value(json, "result").getOrElse(JSON.Object.empty)
5b68cc73f8b1 clarified signature;
wenzelm
parents: 71520
diff changeset
   909
      val error_info = JSON.string(json, "error_info")
5b68cc73f8b1 clarified signature;
wenzelm
parents: 71520
diff changeset
   910
      val error_code = JSON.string(json, "error_code")
5b68cc73f8b1 clarified signature;
wenzelm
parents: 71520
diff changeset
   911
      Result(result, error_info orElse error_code)
5b68cc73f8b1 clarified signature;
wenzelm
parents: 71520
diff changeset
   912
    }
5b68cc73f8b1 clarified signature;
wenzelm
parents: 71520
diff changeset
   913
5b68cc73f8b1 clarified signature;
wenzelm
parents: 71520
diff changeset
   914
5b68cc73f8b1 clarified signature;
wenzelm
parents: 71520
diff changeset
   915
    /* context for operations */
5b68cc73f8b1 clarified signature;
wenzelm
parents: 71520
diff changeset
   916
5b68cc73f8b1 clarified signature;
wenzelm
parents: 71520
diff changeset
   917
    def apply(user: String, host: String, port: Int = 22): API =
5b68cc73f8b1 clarified signature;
wenzelm
parents: 71520
diff changeset
   918
      new API(user, host, port)
71508
51c19a44cfed support for conduit API;
wenzelm
parents: 71504
diff changeset
   919
  }
51c19a44cfed support for conduit API;
wenzelm
parents: 71504
diff changeset
   920
71523
5b68cc73f8b1 clarified signature;
wenzelm
parents: 71520
diff changeset
   921
  final class API private(ssh_user: String, ssh_host: String, ssh_port: Int)
71508
51c19a44cfed support for conduit API;
wenzelm
parents: 71504
diff changeset
   922
  {
51c19a44cfed support for conduit API;
wenzelm
parents: 71504
diff changeset
   923
    /* connection */
51c19a44cfed support for conduit API;
wenzelm
parents: 71504
diff changeset
   924
51c19a44cfed support for conduit API;
wenzelm
parents: 71504
diff changeset
   925
    require(ssh_host.nonEmpty && ssh_port >= 0)
51c19a44cfed support for conduit API;
wenzelm
parents: 71504
diff changeset
   926
51c19a44cfed support for conduit API;
wenzelm
parents: 71504
diff changeset
   927
    private def ssh_user_prefix: String = if (ssh_user.isEmpty) "" else ssh_user + "@"
51c19a44cfed support for conduit API;
wenzelm
parents: 71504
diff changeset
   928
    private def ssh_port_suffix: String = if (ssh_port == 22) "" else ":" + ssh_port
51c19a44cfed support for conduit API;
wenzelm
parents: 71504
diff changeset
   929
51c19a44cfed support for conduit API;
wenzelm
parents: 71504
diff changeset
   930
    override def toString: String = ssh_user_prefix + ssh_host + ssh_port_suffix
71517
384755399fa8 clarified signature;
wenzelm
parents: 71515
diff changeset
   931
    def hg_url: String = "ssh://" + ssh_user_prefix + ssh_host + ssh_port_suffix
71508
51c19a44cfed support for conduit API;
wenzelm
parents: 71504
diff changeset
   932
51c19a44cfed support for conduit API;
wenzelm
parents: 71504
diff changeset
   933
51c19a44cfed support for conduit API;
wenzelm
parents: 71504
diff changeset
   934
    /* execute methods */
51c19a44cfed support for conduit API;
wenzelm
parents: 71504
diff changeset
   935
71509
ca794da3bb1d tuned signature;
wenzelm
parents: 71508
diff changeset
   936
    def execute_raw(method: String, params: JSON.T = JSON.Object.empty): JSON.T =
71508
51c19a44cfed support for conduit API;
wenzelm
parents: 71504
diff changeset
   937
    {
71509
ca794da3bb1d tuned signature;
wenzelm
parents: 71508
diff changeset
   938
      Isabelle_System.with_tmp_file("params", "json")(params_file =>
71508
51c19a44cfed support for conduit API;
wenzelm
parents: 71504
diff changeset
   939
      {
71509
ca794da3bb1d tuned signature;
wenzelm
parents: 71508
diff changeset
   940
        File.write(params_file, JSON.Format(JSON.Object("params" -> JSON.Format(params))))
71508
51c19a44cfed support for conduit API;
wenzelm
parents: 71504
diff changeset
   941
        val result =
51c19a44cfed support for conduit API;
wenzelm
parents: 71504
diff changeset
   942
          Isabelle_System.bash(
51c19a44cfed support for conduit API;
wenzelm
parents: 71504
diff changeset
   943
            "ssh -p " + ssh_port + " " + Bash.string(ssh_user_prefix + ssh_host) +
71509
ca794da3bb1d tuned signature;
wenzelm
parents: 71508
diff changeset
   944
            " conduit " + Bash.string(method) + " < " + File.bash_path(params_file)).check
71508
51c19a44cfed support for conduit API;
wenzelm
parents: 71504
diff changeset
   945
        JSON.parse(result.out, strict = false)
51c19a44cfed support for conduit API;
wenzelm
parents: 71504
diff changeset
   946
      })
51c19a44cfed support for conduit API;
wenzelm
parents: 71504
diff changeset
   947
    }
51c19a44cfed support for conduit API;
wenzelm
parents: 71504
diff changeset
   948
71509
ca794da3bb1d tuned signature;
wenzelm
parents: 71508
diff changeset
   949
    def execute(method: String, params: JSON.T = JSON.Object.empty): API.Result =
ca794da3bb1d tuned signature;
wenzelm
parents: 71508
diff changeset
   950
      API.make_result(execute_raw(method, params = params))
71508
51c19a44cfed support for conduit API;
wenzelm
parents: 71504
diff changeset
   951
71539
836fde6f9d7e proper search with multiple "pages" of results;
wenzelm
parents: 71537
diff changeset
   952
    def execute_search[A](
836fde6f9d7e proper search with multiple "pages" of results;
wenzelm
parents: 71537
diff changeset
   953
      method: String, params: JSON.Object.T, unapply: JSON.T => Option[A]): List[A] =
836fde6f9d7e proper search with multiple "pages" of results;
wenzelm
parents: 71537
diff changeset
   954
    {
836fde6f9d7e proper search with multiple "pages" of results;
wenzelm
parents: 71537
diff changeset
   955
      val results = new mutable.ListBuffer[A]
836fde6f9d7e proper search with multiple "pages" of results;
wenzelm
parents: 71537
diff changeset
   956
      var after = ""
836fde6f9d7e proper search with multiple "pages" of results;
wenzelm
parents: 71537
diff changeset
   957
836fde6f9d7e proper search with multiple "pages" of results;
wenzelm
parents: 71537
diff changeset
   958
      do {
836fde6f9d7e proper search with multiple "pages" of results;
wenzelm
parents: 71537
diff changeset
   959
        val result =
836fde6f9d7e proper search with multiple "pages" of results;
wenzelm
parents: 71537
diff changeset
   960
          execute(method, params = params ++ JSON.optional("after" -> proper_string(after)))
836fde6f9d7e proper search with multiple "pages" of results;
wenzelm
parents: 71537
diff changeset
   961
        results ++= result.get_value(JSON.list(_, "data", unapply))
836fde6f9d7e proper search with multiple "pages" of results;
wenzelm
parents: 71537
diff changeset
   962
        after = result.get_value(JSON.value(_, "cursor", JSON.string0(_, "after")))
836fde6f9d7e proper search with multiple "pages" of results;
wenzelm
parents: 71537
diff changeset
   963
      } while (after.nonEmpty)
836fde6f9d7e proper search with multiple "pages" of results;
wenzelm
parents: 71537
diff changeset
   964
836fde6f9d7e proper search with multiple "pages" of results;
wenzelm
parents: 71537
diff changeset
   965
      results.toList
836fde6f9d7e proper search with multiple "pages" of results;
wenzelm
parents: 71537
diff changeset
   966
    }
836fde6f9d7e proper search with multiple "pages" of results;
wenzelm
parents: 71537
diff changeset
   967
71541
277ee690cb14 clarified signature -- more operations;
wenzelm
parents: 71540
diff changeset
   968
    def ping(): String = execute("conduit.ping").get_string
71508
51c19a44cfed support for conduit API;
wenzelm
parents: 71504
diff changeset
   969
51c19a44cfed support for conduit API;
wenzelm
parents: 71504
diff changeset
   970
71541
277ee690cb14 clarified signature -- more operations;
wenzelm
parents: 71540
diff changeset
   971
    /* users */
71508
51c19a44cfed support for conduit API;
wenzelm
parents: 71504
diff changeset
   972
71509
ca794da3bb1d tuned signature;
wenzelm
parents: 71508
diff changeset
   973
    lazy val user_phid: String = execute("user.whoami").get_value(JSON.string(_, "phid"))
ca794da3bb1d tuned signature;
wenzelm
parents: 71508
diff changeset
   974
    lazy val user_name: String = execute("user.whoami").get_value(JSON.string(_, "userName"))
71510
3fdd0b93fa4b clarified signature;
wenzelm
parents: 71509
diff changeset
   975
71541
277ee690cb14 clarified signature -- more operations;
wenzelm
parents: 71540
diff changeset
   976
    def get_users(
277ee690cb14 clarified signature -- more operations;
wenzelm
parents: 71540
diff changeset
   977
      all: Boolean = false,
277ee690cb14 clarified signature -- more operations;
wenzelm
parents: 71540
diff changeset
   978
      phid: String = "",
277ee690cb14 clarified signature -- more operations;
wenzelm
parents: 71540
diff changeset
   979
      name: String = ""): List[API.User] =
277ee690cb14 clarified signature -- more operations;
wenzelm
parents: 71540
diff changeset
   980
    {
277ee690cb14 clarified signature -- more operations;
wenzelm
parents: 71540
diff changeset
   981
      val constraints: JSON.Object.T =
277ee690cb14 clarified signature -- more operations;
wenzelm
parents: 71540
diff changeset
   982
        (for { (key, value) <- List("phids" -> phid, "usernames" -> name) if value.nonEmpty }
277ee690cb14 clarified signature -- more operations;
wenzelm
parents: 71540
diff changeset
   983
          yield (key, List(value))).toMap
277ee690cb14 clarified signature -- more operations;
wenzelm
parents: 71540
diff changeset
   984
277ee690cb14 clarified signature -- more operations;
wenzelm
parents: 71540
diff changeset
   985
      execute_search("user.search",
277ee690cb14 clarified signature -- more operations;
wenzelm
parents: 71540
diff changeset
   986
          JSON.Object("queryKey" -> (if (all) "all" else "active"), "constraints" -> constraints),
277ee690cb14 clarified signature -- more operations;
wenzelm
parents: 71540
diff changeset
   987
            data => JSON.value(data, "fields", fields =>
277ee690cb14 clarified signature -- more operations;
wenzelm
parents: 71540
diff changeset
   988
              for {
277ee690cb14 clarified signature -- more operations;
wenzelm
parents: 71540
diff changeset
   989
                id <- JSON.long(data, "id")
277ee690cb14 clarified signature -- more operations;
wenzelm
parents: 71540
diff changeset
   990
                phid <- JSON.string(data, "phid")
277ee690cb14 clarified signature -- more operations;
wenzelm
parents: 71540
diff changeset
   991
                name <- JSON.string(fields, "username")
277ee690cb14 clarified signature -- more operations;
wenzelm
parents: 71540
diff changeset
   992
                real_name <- JSON.string0(fields, "realName")
277ee690cb14 clarified signature -- more operations;
wenzelm
parents: 71540
diff changeset
   993
                roles <- JSON.strings(fields, "roles")
277ee690cb14 clarified signature -- more operations;
wenzelm
parents: 71540
diff changeset
   994
              } yield API.User(id, phid, name, real_name, roles)))
277ee690cb14 clarified signature -- more operations;
wenzelm
parents: 71540
diff changeset
   995
    }
277ee690cb14 clarified signature -- more operations;
wenzelm
parents: 71540
diff changeset
   996
277ee690cb14 clarified signature -- more operations;
wenzelm
parents: 71540
diff changeset
   997
    def the_user(phid: String): API.User =
277ee690cb14 clarified signature -- more operations;
wenzelm
parents: 71540
diff changeset
   998
      get_users(phid = phid) match {
277ee690cb14 clarified signature -- more operations;
wenzelm
parents: 71540
diff changeset
   999
        case List(user) => user
277ee690cb14 clarified signature -- more operations;
wenzelm
parents: 71540
diff changeset
  1000
        case _ => error("Bad user PHID " + quote(phid))
277ee690cb14 clarified signature -- more operations;
wenzelm
parents: 71540
diff changeset
  1001
      }
277ee690cb14 clarified signature -- more operations;
wenzelm
parents: 71540
diff changeset
  1002
277ee690cb14 clarified signature -- more operations;
wenzelm
parents: 71540
diff changeset
  1003
277ee690cb14 clarified signature -- more operations;
wenzelm
parents: 71540
diff changeset
  1004
    /* repositories */
277ee690cb14 clarified signature -- more operations;
wenzelm
parents: 71540
diff changeset
  1005
71515
113779776ee4 more repository information;
wenzelm
parents: 71512
diff changeset
  1006
    def get_repositories(
71540
79232f138382 clarified signature;
wenzelm
parents: 71539
diff changeset
  1007
      all: Boolean = false,
79232f138382 clarified signature;
wenzelm
parents: 71539
diff changeset
  1008
      phid: String = "",
79232f138382 clarified signature;
wenzelm
parents: 71539
diff changeset
  1009
      callsign: String = "",
79232f138382 clarified signature;
wenzelm
parents: 71539
diff changeset
  1010
      short_name: String = ""): List[API.Repository] =
71515
113779776ee4 more repository information;
wenzelm
parents: 71512
diff changeset
  1011
    {
113779776ee4 more repository information;
wenzelm
parents: 71512
diff changeset
  1012
      val constraints: JSON.Object.T =
113779776ee4 more repository information;
wenzelm
parents: 71512
diff changeset
  1013
        (for {
113779776ee4 more repository information;
wenzelm
parents: 71512
diff changeset
  1014
          (key, value) <- List("phids" -> phid, "callsigns" -> callsign, "shortNames" -> short_name)
113779776ee4 more repository information;
wenzelm
parents: 71512
diff changeset
  1015
          if value.nonEmpty
113779776ee4 more repository information;
wenzelm
parents: 71512
diff changeset
  1016
        } yield (key, List(value))).toMap
113779776ee4 more repository information;
wenzelm
parents: 71512
diff changeset
  1017
71539
836fde6f9d7e proper search with multiple "pages" of results;
wenzelm
parents: 71537
diff changeset
  1018
      execute_search("diffusion.repository.search",
71540
79232f138382 clarified signature;
wenzelm
parents: 71539
diff changeset
  1019
          JSON.Object("queryKey" -> (if (all) "all" else "active"), "constraints" -> constraints),
71539
836fde6f9d7e proper search with multiple "pages" of results;
wenzelm
parents: 71537
diff changeset
  1020
            data => JSON.value(data, "fields", fields =>
836fde6f9d7e proper search with multiple "pages" of results;
wenzelm
parents: 71537
diff changeset
  1021
              for {
836fde6f9d7e proper search with multiple "pages" of results;
wenzelm
parents: 71537
diff changeset
  1022
                vcs_name <- JSON.string(fields, "vcs")
836fde6f9d7e proper search with multiple "pages" of results;
wenzelm
parents: 71537
diff changeset
  1023
                id <- JSON.long(data, "id")
836fde6f9d7e proper search with multiple "pages" of results;
wenzelm
parents: 71537
diff changeset
  1024
                phid <- JSON.string(data, "phid")
836fde6f9d7e proper search with multiple "pages" of results;
wenzelm
parents: 71537
diff changeset
  1025
                name <- JSON.string(fields, "name")
836fde6f9d7e proper search with multiple "pages" of results;
wenzelm
parents: 71537
diff changeset
  1026
                callsign <- JSON.string0(fields, "callsign")
836fde6f9d7e proper search with multiple "pages" of results;
wenzelm
parents: 71537
diff changeset
  1027
                short_name <- JSON.string0(fields, "shortName")
836fde6f9d7e proper search with multiple "pages" of results;
wenzelm
parents: 71537
diff changeset
  1028
                importing <- JSON.bool(fields, "isImporting")
71515
113779776ee4 more repository information;
wenzelm
parents: 71512
diff changeset
  1029
              }
71539
836fde6f9d7e proper search with multiple "pages" of results;
wenzelm
parents: 71537
diff changeset
  1030
              yield {
836fde6f9d7e proper search with multiple "pages" of results;
wenzelm
parents: 71537
diff changeset
  1031
                val vcs = API.VCS.read(vcs_name)
836fde6f9d7e proper search with multiple "pages" of results;
wenzelm
parents: 71537
diff changeset
  1032
                val url_path =
836fde6f9d7e proper search with multiple "pages" of results;
wenzelm
parents: 71537
diff changeset
  1033
                  if (short_name.isEmpty) "/diffusion/" + id else "/source/" + short_name
836fde6f9d7e proper search with multiple "pages" of results;
wenzelm
parents: 71537
diff changeset
  1034
                val ssh_url =
836fde6f9d7e proper search with multiple "pages" of results;
wenzelm
parents: 71537
diff changeset
  1035
                  vcs match {
836fde6f9d7e proper search with multiple "pages" of results;
wenzelm
parents: 71537
diff changeset
  1036
                    case API.VCS.hg => hg_url + url_path
836fde6f9d7e proper search with multiple "pages" of results;
wenzelm
parents: 71537
diff changeset
  1037
                    case API.VCS.git => hg_url + url_path + ".git"
836fde6f9d7e proper search with multiple "pages" of results;
wenzelm
parents: 71537
diff changeset
  1038
                    case API.VCS.svn => ""
836fde6f9d7e proper search with multiple "pages" of results;
wenzelm
parents: 71537
diff changeset
  1039
                  }
836fde6f9d7e proper search with multiple "pages" of results;
wenzelm
parents: 71537
diff changeset
  1040
                API.Repository(vcs, id, phid, name, callsign, short_name, importing, ssh_url)
836fde6f9d7e proper search with multiple "pages" of results;
wenzelm
parents: 71537
diff changeset
  1041
              }))
71515
113779776ee4 more repository information;
wenzelm
parents: 71512
diff changeset
  1042
    }
71518
wenzelm
parents: 71517
diff changeset
  1043
71520
e169a04e4d3b clarified signature;
wenzelm
parents: 71519
diff changeset
  1044
    def the_repository(phid: String): API.Repository =
e169a04e4d3b clarified signature;
wenzelm
parents: 71519
diff changeset
  1045
      get_repositories(phid = phid) match {
e169a04e4d3b clarified signature;
wenzelm
parents: 71519
diff changeset
  1046
        case List(repo) => repo
71523
5b68cc73f8b1 clarified signature;
wenzelm
parents: 71520
diff changeset
  1047
        case _ => error("Bad repository PHID " + quote(phid))
71520
e169a04e4d3b clarified signature;
wenzelm
parents: 71519
diff changeset
  1048
      }
e169a04e4d3b clarified signature;
wenzelm
parents: 71519
diff changeset
  1049
71518
wenzelm
parents: 71517
diff changeset
  1050
    def create_repository(
wenzelm
parents: 71517
diff changeset
  1051
      name: String,
wenzelm
parents: 71517
diff changeset
  1052
      callsign: String = "",    // unique name, UPPERCASE
wenzelm
parents: 71517
diff changeset
  1053
      short_name: String = "",  // unique name
wenzelm
parents: 71517
diff changeset
  1054
      description: String = "",
wenzelm
parents: 71517
diff changeset
  1055
      public: Boolean = false,
71519
fd644fb7871b clarified signature;
wenzelm
parents: 71518
diff changeset
  1056
      vcs: API.VCS.Value = API.VCS.hg): API.Repository =
71518
wenzelm
parents: 71517
diff changeset
  1057
    {
wenzelm
parents: 71517
diff changeset
  1058
      require(name.nonEmpty)
wenzelm
parents: 71517
diff changeset
  1059
wenzelm
parents: 71517
diff changeset
  1060
      val transactions =
wenzelm
parents: 71517
diff changeset
  1061
        API.edits("vcs", vcs.toString) :::
wenzelm
parents: 71517
diff changeset
  1062
        API.edits("name", name) :::
wenzelm
parents: 71517
diff changeset
  1063
        API.opt_edits("callsign", proper_string(callsign)) :::
wenzelm
parents: 71517
diff changeset
  1064
        API.opt_edits("shortName", proper_string(short_name)) :::
wenzelm
parents: 71517
diff changeset
  1065
        API.opt_edits("description", proper_string(description)) :::
wenzelm
parents: 71517
diff changeset
  1066
        (if (public) Nil
wenzelm
parents: 71517
diff changeset
  1067
         else API.edits("view", user_phid) ::: API.edits("policy.push", user_phid)) :::
wenzelm
parents: 71517
diff changeset
  1068
        API.edits("status", "active")
wenzelm
parents: 71517
diff changeset
  1069
71519
fd644fb7871b clarified signature;
wenzelm
parents: 71518
diff changeset
  1070
      val phid =
71518
wenzelm
parents: 71517
diff changeset
  1071
        execute("diffusion.repository.edit", params = JSON.Object("transactions" -> transactions))
wenzelm
parents: 71517
diff changeset
  1072
          .get_value(JSON.value(_, "object", JSON.string(_, "phid")))
wenzelm
parents: 71517
diff changeset
  1073
71519
fd644fb7871b clarified signature;
wenzelm
parents: 71518
diff changeset
  1074
      execute("diffusion.looksoon", params = JSON.Object("repositories" -> List(phid))).get
71518
wenzelm
parents: 71517
diff changeset
  1075
71520
e169a04e4d3b clarified signature;
wenzelm
parents: 71519
diff changeset
  1076
      the_repository(phid)
71518
wenzelm
parents: 71517
diff changeset
  1077
    }
71508
51c19a44cfed support for conduit API;
wenzelm
parents: 71504
diff changeset
  1078
  }
71164
79736ffe77c3 some support for Phabricator server;
wenzelm
parents:
diff changeset
  1079
}