src/Pure/Tools/phabricator.scala
author wenzelm
Thu, 12 Dec 2019 13:55:03 +0100
changeset 71486 74cabc06cf2d
parent 71485 b4401dfd6544
child 71489 5a2033fc8f3d
permissions -rw-r--r--
proper support for multiple installations;
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
71166
a48112873f81 MySQL setup;
wenzelm
parents: 71165
diff changeset
    14
import scala.util.matching.Regex
a48112873f81 MySQL setup;
wenzelm
parents: 71165
diff changeset
    15
a48112873f81 MySQL setup;
wenzelm
parents: 71165
diff changeset
    16
71164
79736ffe77c3 some support for Phabricator server;
wenzelm
parents:
diff changeset
    17
object Phabricator
79736ffe77c3 some support for Phabricator server;
wenzelm
parents:
diff changeset
    18
{
79736ffe77c3 some support for Phabricator server;
wenzelm
parents:
diff changeset
    19
  /** defaults **/
79736ffe77c3 some support for Phabricator server;
wenzelm
parents:
diff changeset
    20
71247
f4b9dd5ab0cc more phabricator setup;
wenzelm
parents: 71245
diff changeset
    21
  /* required packages */
f4b9dd5ab0cc more phabricator setup;
wenzelm
parents: 71245
diff changeset
    22
f4b9dd5ab0cc more phabricator setup;
wenzelm
parents: 71245
diff changeset
    23
  val packages: List[String] =
71474
6ca561001244 support for pwgen;
wenzelm
parents: 71333
diff changeset
    24
    Build_Docker.packages ::: Linux.packages :::
71247
f4b9dd5ab0cc more phabricator setup;
wenzelm
parents: 71245
diff changeset
    25
    List(
f4b9dd5ab0cc more phabricator setup;
wenzelm
parents: 71245
diff changeset
    26
      // https://secure.phabricator.com/source/phabricator/browse/master/scripts/install/install_ubuntu.sh 15e6e2adea61
f4b9dd5ab0cc more phabricator setup;
wenzelm
parents: 71245
diff changeset
    27
      "git", "mysql-server", "apache2", "libapache2-mod-php", "php", "php-mysql",
f4b9dd5ab0cc more phabricator setup;
wenzelm
parents: 71245
diff changeset
    28
      "php-gd", "php-curl", "php-apcu", "php-cli", "php-json", "php-mbstring",
f4b9dd5ab0cc more phabricator setup;
wenzelm
parents: 71245
diff changeset
    29
      // more packages
71333
781b15f53098 more packages;
wenzelm
parents: 71329
diff changeset
    30
      "php-zip", "python-pygments", "ssh", "subversion", "mercurial")
71247
f4b9dd5ab0cc more phabricator setup;
wenzelm
parents: 71245
diff changeset
    31
f4b9dd5ab0cc more phabricator setup;
wenzelm
parents: 71245
diff changeset
    32
f4b9dd5ab0cc more phabricator setup;
wenzelm
parents: 71245
diff changeset
    33
  /* global system resources */
f4b9dd5ab0cc more phabricator setup;
wenzelm
parents: 71245
diff changeset
    34
71266
510b89906d86 discontinued somewhat pointless Isabelle options: setup implicitly assumes Ubuntu 18.04;
wenzelm
parents: 71264
diff changeset
    35
  val www_user = "www-data"
510b89906d86 discontinued somewhat pointless Isabelle options: setup implicitly assumes Ubuntu 18.04;
wenzelm
parents: 71264
diff changeset
    36
71247
f4b9dd5ab0cc more phabricator setup;
wenzelm
parents: 71245
diff changeset
    37
  val daemon_user = "phabricator"
f4b9dd5ab0cc more phabricator setup;
wenzelm
parents: 71245
diff changeset
    38
71308
8c1c717a830b configure SSH hosting via "isabelle phabricator_setup_ssh";
wenzelm
parents: 71302
diff changeset
    39
  val sshd_config = Path.explode("/etc/ssh/sshd_config")
71247
f4b9dd5ab0cc more phabricator setup;
wenzelm
parents: 71245
diff changeset
    40
f4b9dd5ab0cc more phabricator setup;
wenzelm
parents: 71245
diff changeset
    41
f4b9dd5ab0cc more phabricator setup;
wenzelm
parents: 71245
diff changeset
    42
  /* installation parameters */
f4b9dd5ab0cc more phabricator setup;
wenzelm
parents: 71245
diff changeset
    43
71164
79736ffe77c3 some support for Phabricator server;
wenzelm
parents:
diff changeset
    44
  val default_name = "vcs"
79736ffe77c3 some support for Phabricator server;
wenzelm
parents:
diff changeset
    45
71250
6bf53035baf0 clarified name prefixes: global config always uses "isabelle-phabricator";
wenzelm
parents: 71249
diff changeset
    46
  def phabricator_name(name: String = "", ext: String = ""): String =
6bf53035baf0 clarified name prefixes: global config always uses "isabelle-phabricator";
wenzelm
parents: 71249
diff changeset
    47
    "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
    48
6bf53035baf0 clarified name prefixes: global config always uses "isabelle-phabricator";
wenzelm
parents: 71249
diff changeset
    49
  def isabelle_phabricator_name(name: String = "", ext: String = ""): String =
6bf53035baf0 clarified name prefixes: global config always uses "isabelle-phabricator";
wenzelm
parents: 71249
diff changeset
    50
    "isabelle-" + phabricator_name(name = name, ext = ext)
71164
79736ffe77c3 some support for Phabricator server;
wenzelm
parents:
diff changeset
    51
71266
510b89906d86 discontinued somewhat pointless Isabelle options: setup implicitly assumes Ubuntu 18.04;
wenzelm
parents: 71264
diff changeset
    52
  def default_root(name: String): Path =
510b89906d86 discontinued somewhat pointless Isabelle options: setup implicitly assumes Ubuntu 18.04;
wenzelm
parents: 71264
diff changeset
    53
    Path.explode("/var/www") + Path.basic(phabricator_name(name = name))
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_repo(name: String): Path = default_root(name) + Path.basic("repo")
71164
79736ffe77c3 some support for Phabricator server;
wenzelm
parents:
diff changeset
    56
71270
wenzelm
parents: 71269
diff changeset
    57
  val default_mailers: Path = Path.explode("mailers.json")
71264
114db2b5a5f8 support for Phabricator mail configuration;
wenzelm
parents: 71256
diff changeset
    58
71308
8c1c717a830b configure SSH hosting via "isabelle phabricator_setup_ssh";
wenzelm
parents: 71302
diff changeset
    59
  val default_system_port = 22
8c1c717a830b configure SSH hosting via "isabelle phabricator_setup_ssh";
wenzelm
parents: 71302
diff changeset
    60
  val alternative_system_port = 222
8c1c717a830b configure SSH hosting via "isabelle phabricator_setup_ssh";
wenzelm
parents: 71302
diff changeset
    61
  val default_server_port = 2222
8c1c717a830b configure SSH hosting via "isabelle phabricator_setup_ssh";
wenzelm
parents: 71302
diff changeset
    62
71164
79736ffe77c3 some support for Phabricator server;
wenzelm
parents:
diff changeset
    63
79736ffe77c3 some support for Phabricator server;
wenzelm
parents:
diff changeset
    64
79736ffe77c3 some support for Phabricator server;
wenzelm
parents:
diff changeset
    65
  /** global configuration **/
79736ffe77c3 some support for Phabricator server;
wenzelm
parents:
diff changeset
    66
71250
6bf53035baf0 clarified name prefixes: global config always uses "isabelle-phabricator";
wenzelm
parents: 71249
diff changeset
    67
  val global_config = Path.explode("/etc/" + isabelle_phabricator_name(ext = "conf"))
71164
79736ffe77c3 some support for Phabricator server;
wenzelm
parents:
diff changeset
    68
71320
730090397e0d clarified signature;
wenzelm
parents: 71315
diff changeset
    69
  def global_config_script(
730090397e0d clarified signature;
wenzelm
parents: 71315
diff changeset
    70
    header: Boolean = false,
730090397e0d clarified signature;
wenzelm
parents: 71315
diff changeset
    71
    init: String = "",
730090397e0d clarified signature;
wenzelm
parents: 71315
diff changeset
    72
    body: String = "",
730090397e0d clarified signature;
wenzelm
parents: 71315
diff changeset
    73
    exit: String = ""): String =
730090397e0d clarified signature;
wenzelm
parents: 71315
diff changeset
    74
  {
730090397e0d clarified signature;
wenzelm
parents: 71315
diff changeset
    75
    (if (header) "#!/bin/bash\n" else "") +
730090397e0d clarified signature;
wenzelm
parents: 71315
diff changeset
    76
"""
730090397e0d clarified signature;
wenzelm
parents: 71315
diff changeset
    77
{""" + (if (init.nonEmpty) "\n" + Library.prefix_lines("  ", init) else "") + """
730090397e0d clarified signature;
wenzelm
parents: 71315
diff changeset
    78
  while { unset REPLY; read -r; test "$?" = 0 -o -n "$REPLY"; }
730090397e0d clarified signature;
wenzelm
parents: 71315
diff changeset
    79
  do
730090397e0d clarified signature;
wenzelm
parents: 71315
diff changeset
    80
    NAME="$(echo "$REPLY" | cut -d: -f1)"
730090397e0d clarified signature;
wenzelm
parents: 71315
diff changeset
    81
    ROOT="$(echo "$REPLY" | cut -d: -f2)"
730090397e0d clarified signature;
wenzelm
parents: 71315
diff changeset
    82
""" + Library.prefix_lines("    ", body) + """
730090397e0d clarified signature;
wenzelm
parents: 71315
diff changeset
    83
  done""" +
730090397e0d clarified signature;
wenzelm
parents: 71315
diff changeset
    84
    (if (exit.nonEmpty) "\n" + Library.prefix_lines("  ", exit) else "") + """
730090397e0d clarified signature;
wenzelm
parents: 71315
diff changeset
    85
} < """ + File.bash_path(global_config) + """
730090397e0d clarified signature;
wenzelm
parents: 71315
diff changeset
    86
"""
730090397e0d clarified signature;
wenzelm
parents: 71315
diff changeset
    87
  }
730090397e0d clarified signature;
wenzelm
parents: 71315
diff changeset
    88
71164
79736ffe77c3 some support for Phabricator server;
wenzelm
parents:
diff changeset
    89
  sealed case class Config(name: String, root: Path)
71165
46847076477c Apache setup;
wenzelm
parents: 71164
diff changeset
    90
  {
71250
6bf53035baf0 clarified name prefixes: global config always uses "isabelle-phabricator";
wenzelm
parents: 71249
diff changeset
    91
    def home: Path = root + Path.explode(phabricator_name())
71166
a48112873f81 MySQL setup;
wenzelm
parents: 71165
diff changeset
    92
a48112873f81 MySQL setup;
wenzelm
parents: 71165
diff changeset
    93
    def execute(command: String): Process_Result =
71301
wenzelm
parents: 71300
diff changeset
    94
      Isabelle_System.bash("bin/" + command, cwd = home.file, redirect = true).check
71165
46847076477c Apache setup;
wenzelm
parents: 71164
diff changeset
    95
  }
71164
79736ffe77c3 some support for Phabricator server;
wenzelm
parents:
diff changeset
    96
79736ffe77c3 some support for Phabricator server;
wenzelm
parents:
diff changeset
    97
  def read_config(): List[Config] =
79736ffe77c3 some support for Phabricator server;
wenzelm
parents:
diff changeset
    98
  {
79736ffe77c3 some support for Phabricator server;
wenzelm
parents:
diff changeset
    99
    if (global_config.is_file) {
79736ffe77c3 some support for Phabricator server;
wenzelm
parents:
diff changeset
   100
      for (entry <- Library.trim_split_lines(File.read(global_config)) if entry.nonEmpty)
79736ffe77c3 some support for Phabricator server;
wenzelm
parents:
diff changeset
   101
      yield {
79736ffe77c3 some support for Phabricator server;
wenzelm
parents:
diff changeset
   102
        space_explode(':', entry) match {
79736ffe77c3 some support for Phabricator server;
wenzelm
parents:
diff changeset
   103
          case List(name, root) => Config(name, Path.explode(root))
79736ffe77c3 some support for Phabricator server;
wenzelm
parents:
diff changeset
   104
          case _ => error("Malformed config file " + global_config + "\nentry " + quote(entry))
79736ffe77c3 some support for Phabricator server;
wenzelm
parents:
diff changeset
   105
        }
79736ffe77c3 some support for Phabricator server;
wenzelm
parents:
diff changeset
   106
      }
79736ffe77c3 some support for Phabricator server;
wenzelm
parents:
diff changeset
   107
    }
79736ffe77c3 some support for Phabricator server;
wenzelm
parents:
diff changeset
   108
    else Nil
79736ffe77c3 some support for Phabricator server;
wenzelm
parents:
diff changeset
   109
  }
79736ffe77c3 some support for Phabricator server;
wenzelm
parents:
diff changeset
   110
79736ffe77c3 some support for Phabricator server;
wenzelm
parents:
diff changeset
   111
  def write_config(configs: List[Config])
79736ffe77c3 some support for Phabricator server;
wenzelm
parents:
diff changeset
   112
  {
79736ffe77c3 some support for Phabricator server;
wenzelm
parents:
diff changeset
   113
    File.write(global_config,
79736ffe77c3 some support for Phabricator server;
wenzelm
parents:
diff changeset
   114
      configs.map(config => config.name + ":" + config.root.implode).mkString("", "\n", "\n"))
79736ffe77c3 some support for Phabricator server;
wenzelm
parents:
diff changeset
   115
  }
79736ffe77c3 some support for Phabricator server;
wenzelm
parents:
diff changeset
   116
79736ffe77c3 some support for Phabricator server;
wenzelm
parents:
diff changeset
   117
  def get_config(name: String): Config =
79736ffe77c3 some support for Phabricator server;
wenzelm
parents:
diff changeset
   118
    read_config().find(config => config.name == name) getOrElse
79736ffe77c3 some support for Phabricator server;
wenzelm
parents:
diff changeset
   119
      error("Bad Isabelle/Phabricator installation " + quote(name))
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
71296
d3ededaa77b3 added "isabelle phabricator";
wenzelm
parents: 71277
diff changeset
   123
  /** command-line tools **/
d3ededaa77b3 added "isabelle phabricator";
wenzelm
parents: 71277
diff changeset
   124
d3ededaa77b3 added "isabelle phabricator";
wenzelm
parents: 71277
diff changeset
   125
  /* Isabelle tool wrapper */
d3ededaa77b3 added "isabelle phabricator";
wenzelm
parents: 71277
diff changeset
   126
d3ededaa77b3 added "isabelle phabricator";
wenzelm
parents: 71277
diff changeset
   127
  val isabelle_tool1 =
d3ededaa77b3 added "isabelle phabricator";
wenzelm
parents: 71277
diff changeset
   128
    Isabelle_Tool("phabricator", "invoke command-line tool within Phabricator home directory", args =>
d3ededaa77b3 added "isabelle phabricator";
wenzelm
parents: 71277
diff changeset
   129
    {
71300
67c2fed5b0e9 more options;
wenzelm
parents: 71297
diff changeset
   130
      var list = false
71296
d3ededaa77b3 added "isabelle phabricator";
wenzelm
parents: 71277
diff changeset
   131
      var name = default_name
d3ededaa77b3 added "isabelle phabricator";
wenzelm
parents: 71277
diff changeset
   132
d3ededaa77b3 added "isabelle phabricator";
wenzelm
parents: 71277
diff changeset
   133
      val getopts =
d3ededaa77b3 added "isabelle phabricator";
wenzelm
parents: 71277
diff changeset
   134
        Getopts("""
d3ededaa77b3 added "isabelle phabricator";
wenzelm
parents: 71277
diff changeset
   135
Usage: isabelle phabricator [OPTIONS] COMMAND [ARGS...]
d3ededaa77b3 added "isabelle phabricator";
wenzelm
parents: 71277
diff changeset
   136
d3ededaa77b3 added "isabelle phabricator";
wenzelm
parents: 71277
diff changeset
   137
  Options are:
71300
67c2fed5b0e9 more options;
wenzelm
parents: 71297
diff changeset
   138
    -l           list available Phabricator installations
71296
d3ededaa77b3 added "isabelle phabricator";
wenzelm
parents: 71277
diff changeset
   139
    -n NAME      Phabricator installation name (default: """ + quote(default_name) + """)
d3ededaa77b3 added "isabelle phabricator";
wenzelm
parents: 71277
diff changeset
   140
71302
c073c4e79518 more documentation;
wenzelm
parents: 71301
diff changeset
   141
  Invoke a command-line tool within the home directory of the named
c073c4e79518 more documentation;
wenzelm
parents: 71301
diff changeset
   142
  Phabricator installation.
71296
d3ededaa77b3 added "isabelle phabricator";
wenzelm
parents: 71277
diff changeset
   143
""",
71300
67c2fed5b0e9 more options;
wenzelm
parents: 71297
diff changeset
   144
          "l" -> (_ => list = true),
71296
d3ededaa77b3 added "isabelle phabricator";
wenzelm
parents: 71277
diff changeset
   145
          "n:" -> (arg => name = arg))
d3ededaa77b3 added "isabelle phabricator";
wenzelm
parents: 71277
diff changeset
   146
d3ededaa77b3 added "isabelle phabricator";
wenzelm
parents: 71277
diff changeset
   147
      val more_args = getopts(args)
71300
67c2fed5b0e9 more options;
wenzelm
parents: 71297
diff changeset
   148
      if (more_args.isEmpty && !list) getopts.usage()
71296
d3ededaa77b3 added "isabelle phabricator";
wenzelm
parents: 71277
diff changeset
   149
d3ededaa77b3 added "isabelle phabricator";
wenzelm
parents: 71277
diff changeset
   150
      val progress = new Console_Progress
d3ededaa77b3 added "isabelle phabricator";
wenzelm
parents: 71277
diff changeset
   151
71300
67c2fed5b0e9 more options;
wenzelm
parents: 71297
diff changeset
   152
      if (list) {
67c2fed5b0e9 more options;
wenzelm
parents: 71297
diff changeset
   153
        for (config <- read_config()) {
71302
c073c4e79518 more documentation;
wenzelm
parents: 71301
diff changeset
   154
          progress.echo("phabricator " + quote(config.name) + " root " + config.root)
71300
67c2fed5b0e9 more options;
wenzelm
parents: 71297
diff changeset
   155
        }
67c2fed5b0e9 more options;
wenzelm
parents: 71297
diff changeset
   156
      }
71485
b4401dfd6544 clarified "isabelle phabricator -l": avoid surprise with non-existent default installation;
wenzelm
parents: 71483
diff changeset
   157
      else {
b4401dfd6544 clarified "isabelle phabricator -l": avoid surprise with non-existent default installation;
wenzelm
parents: 71483
diff changeset
   158
        val config = get_config(name)
b4401dfd6544 clarified "isabelle phabricator -l": avoid surprise with non-existent default installation;
wenzelm
parents: 71483
diff changeset
   159
        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
   160
        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
   161
      }
71296
d3ededaa77b3 added "isabelle phabricator";
wenzelm
parents: 71277
diff changeset
   162
    })
d3ededaa77b3 added "isabelle phabricator";
wenzelm
parents: 71277
diff changeset
   163
d3ededaa77b3 added "isabelle phabricator";
wenzelm
parents: 71277
diff changeset
   164
d3ededaa77b3 added "isabelle phabricator";
wenzelm
parents: 71277
diff changeset
   165
71164
79736ffe77c3 some support for Phabricator server;
wenzelm
parents:
diff changeset
   166
  /** setup **/
79736ffe77c3 some support for Phabricator server;
wenzelm
parents:
diff changeset
   167
71247
f4b9dd5ab0cc more phabricator setup;
wenzelm
parents: 71245
diff changeset
   168
  def user_setup(name: String, description: String, ssh_setup: Boolean = false)
f4b9dd5ab0cc more phabricator setup;
wenzelm
parents: 71245
diff changeset
   169
  {
f4b9dd5ab0cc more phabricator setup;
wenzelm
parents: 71245
diff changeset
   170
    if (!Linux.user_exists(name)) {
71252
b64fc38327ae prefer system user setup, e.g. avoid occurrence on login screen;
wenzelm
parents: 71251
diff changeset
   171
      Linux.user_add(name, description = description, system = true, ssh_setup = ssh_setup)
71247
f4b9dd5ab0cc more phabricator setup;
wenzelm
parents: 71245
diff changeset
   172
    }
f4b9dd5ab0cc more phabricator setup;
wenzelm
parents: 71245
diff changeset
   173
    else if (Linux.user_description(name) != description) {
f4b9dd5ab0cc more phabricator setup;
wenzelm
parents: 71245
diff changeset
   174
      error("User " + quote(name) + " already exists --" +
f4b9dd5ab0cc more phabricator setup;
wenzelm
parents: 71245
diff changeset
   175
        " for Phabricator it should have the description:\n  " + quote(description))
f4b9dd5ab0cc more phabricator setup;
wenzelm
parents: 71245
diff changeset
   176
    }
f4b9dd5ab0cc more phabricator setup;
wenzelm
parents: 71245
diff changeset
   177
  }
f4b9dd5ab0cc more phabricator setup;
wenzelm
parents: 71245
diff changeset
   178
71479
wenzelm
parents: 71478
diff changeset
   179
  def command_setup(name: String, body: String, exit: String = ""): Path =
wenzelm
parents: 71478
diff changeset
   180
  {
wenzelm
parents: 71478
diff changeset
   181
    val command = Path.explode("/usr/local/bin") + Path.basic(name)
wenzelm
parents: 71478
diff changeset
   182
    File.write(command, global_config_script(header = true, body = body, exit = exit))
wenzelm
parents: 71478
diff changeset
   183
    Isabelle_System.chmod("755", command)
wenzelm
parents: 71478
diff changeset
   184
    Isabelle_System.chown("root:root", command)
wenzelm
parents: 71478
diff changeset
   185
    command
wenzelm
parents: 71478
diff changeset
   186
  }
wenzelm
parents: 71478
diff changeset
   187
71164
79736ffe77c3 some support for Phabricator server;
wenzelm
parents:
diff changeset
   188
  def phabricator_setup(
79736ffe77c3 some support for Phabricator server;
wenzelm
parents:
diff changeset
   189
    name: String = default_name,
79736ffe77c3 some support for Phabricator server;
wenzelm
parents:
diff changeset
   190
    root: String = "",
79736ffe77c3 some support for Phabricator server;
wenzelm
parents:
diff changeset
   191
    repo: String = "",
71245
87c132cf5860 more options;
wenzelm
parents: 71167
diff changeset
   192
    package_update: Boolean = false,
71164
79736ffe77c3 some support for Phabricator server;
wenzelm
parents:
diff changeset
   193
    progress: Progress = No_Progress)
79736ffe77c3 some support for Phabricator server;
wenzelm
parents:
diff changeset
   194
  {
79736ffe77c3 some support for Phabricator server;
wenzelm
parents:
diff changeset
   195
    /* system environment */
79736ffe77c3 some support for Phabricator server;
wenzelm
parents:
diff changeset
   196
79736ffe77c3 some support for Phabricator server;
wenzelm
parents:
diff changeset
   197
    Linux.check_system_root()
79736ffe77c3 some support for Phabricator server;
wenzelm
parents:
diff changeset
   198
71277
e06852132c1d tuned messages;
wenzelm
parents: 71276
diff changeset
   199
    progress.echo("System packages ...")
e06852132c1d tuned messages;
wenzelm
parents: 71276
diff changeset
   200
71245
87c132cf5860 more options;
wenzelm
parents: 71167
diff changeset
   201
    if (package_update) {
87c132cf5860 more options;
wenzelm
parents: 71167
diff changeset
   202
      Linux.package_update(progress = progress)
87c132cf5860 more options;
wenzelm
parents: 71167
diff changeset
   203
      Linux.check_reboot_required()
87c132cf5860 more options;
wenzelm
parents: 71167
diff changeset
   204
    }
71164
79736ffe77c3 some support for Phabricator server;
wenzelm
parents:
diff changeset
   205
79736ffe77c3 some support for Phabricator server;
wenzelm
parents:
diff changeset
   206
    Linux.package_install(packages, progress = progress)
79736ffe77c3 some support for Phabricator server;
wenzelm
parents:
diff changeset
   207
    Linux.check_reboot_required()
79736ffe77c3 some support for Phabricator server;
wenzelm
parents:
diff changeset
   208
79736ffe77c3 some support for Phabricator server;
wenzelm
parents:
diff changeset
   209
71247
f4b9dd5ab0cc more phabricator setup;
wenzelm
parents: 71245
diff changeset
   210
    /* users */
f4b9dd5ab0cc more phabricator setup;
wenzelm
parents: 71245
diff changeset
   211
71323
beb781551a66 more sanity checks;
wenzelm
parents: 71322
diff changeset
   212
    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
   213
        Set("", "ssh", "phd", "dump", daemon_user).contains(name)) {
71323
beb781551a66 more sanity checks;
wenzelm
parents: 71322
diff changeset
   214
      error("Bad installation name: " + quote(name))
71247
f4b9dd5ab0cc more phabricator setup;
wenzelm
parents: 71245
diff changeset
   215
    }
f4b9dd5ab0cc more phabricator setup;
wenzelm
parents: 71245
diff changeset
   216
f4b9dd5ab0cc more phabricator setup;
wenzelm
parents: 71245
diff changeset
   217
    user_setup(daemon_user, "Phabricator Daemon User", ssh_setup = true)
f4b9dd5ab0cc more phabricator setup;
wenzelm
parents: 71245
diff changeset
   218
    user_setup(name, "Phabricator SSH User")
f4b9dd5ab0cc more phabricator setup;
wenzelm
parents: 71245
diff changeset
   219
f4b9dd5ab0cc more phabricator setup;
wenzelm
parents: 71245
diff changeset
   220
71164
79736ffe77c3 some support for Phabricator server;
wenzelm
parents:
diff changeset
   221
    /* basic installation */
79736ffe77c3 some support for Phabricator server;
wenzelm
parents:
diff changeset
   222
71277
e06852132c1d tuned messages;
wenzelm
parents: 71276
diff changeset
   223
    progress.echo("\nPhabricator installation ...")
71274
8ac137c65776 tuned messages;
wenzelm
parents: 71273
diff changeset
   224
71266
510b89906d86 discontinued somewhat pointless Isabelle options: setup implicitly assumes Ubuntu 18.04;
wenzelm
parents: 71264
diff changeset
   225
    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
   226
    val repo_path = if (repo.nonEmpty) Path.explode(repo) else default_repo(name)
71164
79736ffe77c3 some support for Phabricator server;
wenzelm
parents:
diff changeset
   227
79736ffe77c3 some support for Phabricator server;
wenzelm
parents:
diff changeset
   228
    val configs = read_config()
79736ffe77c3 some support for Phabricator server;
wenzelm
parents:
diff changeset
   229
79736ffe77c3 some support for Phabricator server;
wenzelm
parents:
diff changeset
   230
    for (config <- configs if config.name == name) {
79736ffe77c3 some support for Phabricator server;
wenzelm
parents:
diff changeset
   231
      error("Duplicate Phabricator installation " + quote(name) + " in " + config.root)
79736ffe77c3 some support for Phabricator server;
wenzelm
parents:
diff changeset
   232
    }
79736ffe77c3 some support for Phabricator server;
wenzelm
parents:
diff changeset
   233
79736ffe77c3 some support for Phabricator server;
wenzelm
parents:
diff changeset
   234
    if (!Isabelle_System.bash("mkdir -p " + File.bash_path(root_path)).ok) {
79736ffe77c3 some support for Phabricator server;
wenzelm
parents:
diff changeset
   235
      error("Failed to create root directory " + root_path)
79736ffe77c3 some support for Phabricator server;
wenzelm
parents:
diff changeset
   236
    }
79736ffe77c3 some support for Phabricator server;
wenzelm
parents:
diff changeset
   237
71315
wenzelm
parents: 71314
diff changeset
   238
    Isabelle_System.chown(Bash.string(www_user) + ":" + Bash.string(www_user), root_path)
wenzelm
parents: 71314
diff changeset
   239
    Isabelle_System.chmod("755", root_path)
wenzelm
parents: 71314
diff changeset
   240
71164
79736ffe77c3 some support for Phabricator server;
wenzelm
parents:
diff changeset
   241
    progress.bash(cwd = root_path.file, echo = true,
79736ffe77c3 some support for Phabricator server;
wenzelm
parents:
diff changeset
   242
      script = """
79736ffe77c3 some support for Phabricator server;
wenzelm
parents:
diff changeset
   243
        set -e
71324
18f87bdbc812 tuned message;
wenzelm
parents: 71323
diff changeset
   244
        echo "Cloning distribution repositories:"
71164
79736ffe77c3 some support for Phabricator server;
wenzelm
parents:
diff changeset
   245
        git clone https://github.com/phacility/libphutil.git
79736ffe77c3 some support for Phabricator server;
wenzelm
parents:
diff changeset
   246
        git clone https://github.com/phacility/arcanist.git
79736ffe77c3 some support for Phabricator server;
wenzelm
parents:
diff changeset
   247
        git clone https://github.com/phacility/phabricator.git
79736ffe77c3 some support for Phabricator server;
wenzelm
parents:
diff changeset
   248
      """).check
79736ffe77c3 some support for Phabricator server;
wenzelm
parents:
diff changeset
   249
79736ffe77c3 some support for Phabricator server;
wenzelm
parents:
diff changeset
   250
    val config = Config(name, root_path)
79736ffe77c3 some support for Phabricator server;
wenzelm
parents:
diff changeset
   251
    write_config(configs ::: List(config))
71165
46847076477c Apache setup;
wenzelm
parents: 71164
diff changeset
   252
71249
4eeff87c5072 more phabricator setup;
wenzelm
parents: 71248
diff changeset
   253
    config.execute("config set pygments.enabled true")
4eeff87c5072 more phabricator setup;
wenzelm
parents: 71248
diff changeset
   254
71165
46847076477c Apache setup;
wenzelm
parents: 71164
diff changeset
   255
71248
8198ceef0301 more phabricator setup;
wenzelm
parents: 71247
diff changeset
   256
    /* local repository directory */
8198ceef0301 more phabricator setup;
wenzelm
parents: 71247
diff changeset
   257
71308
8c1c717a830b configure SSH hosting via "isabelle phabricator_setup_ssh";
wenzelm
parents: 71302
diff changeset
   258
    progress.echo("\nRepository hosting setup ...")
8c1c717a830b configure SSH hosting via "isabelle phabricator_setup_ssh";
wenzelm
parents: 71302
diff changeset
   259
71248
8198ceef0301 more phabricator setup;
wenzelm
parents: 71247
diff changeset
   260
    if (!Isabelle_System.bash("mkdir -p " + File.bash_path(repo_path)).ok) {
8198ceef0301 more phabricator setup;
wenzelm
parents: 71247
diff changeset
   261
      error("Failed to create local repository directory " + repo_path)
8198ceef0301 more phabricator setup;
wenzelm
parents: 71247
diff changeset
   262
    }
8198ceef0301 more phabricator setup;
wenzelm
parents: 71247
diff changeset
   263
71313
6cfec8029831 clarified signature;
wenzelm
parents: 71311
diff changeset
   264
    Isabelle_System.chown(
6cfec8029831 clarified signature;
wenzelm
parents: 71311
diff changeset
   265
      "-R " + Bash.string(daemon_user) + ":" + Bash.string(daemon_user), repo_path)
6cfec8029831 clarified signature;
wenzelm
parents: 71311
diff changeset
   266
    Isabelle_System.chmod("755", repo_path)
71248
8198ceef0301 more phabricator setup;
wenzelm
parents: 71247
diff changeset
   267
8198ceef0301 more phabricator setup;
wenzelm
parents: 71247
diff changeset
   268
    config.execute("config set repository.default-local-path " + File.bash_path(repo_path))
8198ceef0301 more phabricator setup;
wenzelm
parents: 71247
diff changeset
   269
8198ceef0301 more phabricator setup;
wenzelm
parents: 71247
diff changeset
   270
71486
74cabc06cf2d proper support for multiple installations;
wenzelm
parents: 71485
diff changeset
   271
    val sudoers_file =
74cabc06cf2d proper support for multiple installations;
wenzelm
parents: 71485
diff changeset
   272
      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
   273
    File.write(sudoers_file,
8c1c717a830b configure SSH hosting via "isabelle phabricator_setup_ssh";
wenzelm
parents: 71302
diff changeset
   274
      www_user + " ALL=(" + daemon_user + ") SETENV: NOPASSWD: /usr/bin/git, /usr/bin/hg, /usr/bin/ssh, /usr/bin/id\n" +
8c1c717a830b configure SSH hosting via "isabelle phabricator_setup_ssh";
wenzelm
parents: 71302
diff changeset
   275
      name + " ALL=(" + daemon_user + ") SETENV: NOPASSWD: /usr/bin/git, /usr/bin/git-upload-pack, /usr/bin/git-receive-pack, /usr/bin/hg, /usr/bin/svnserve, /usr/bin/ssh, /usr/bin/id\n")
8c1c717a830b configure SSH hosting via "isabelle phabricator_setup_ssh";
wenzelm
parents: 71302
diff changeset
   276
71314
wenzelm
parents: 71313
diff changeset
   277
    Isabelle_System.chmod("440", sudoers_file)
71308
8c1c717a830b configure SSH hosting via "isabelle phabricator_setup_ssh";
wenzelm
parents: 71302
diff changeset
   278
8c1c717a830b configure SSH hosting via "isabelle phabricator_setup_ssh";
wenzelm
parents: 71302
diff changeset
   279
    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
   280
8c1c717a830b configure SSH hosting via "isabelle phabricator_setup_ssh";
wenzelm
parents: 71302
diff changeset
   281
71166
a48112873f81 MySQL setup;
wenzelm
parents: 71165
diff changeset
   282
    /* MySQL setup */
a48112873f81 MySQL setup;
wenzelm
parents: 71165
diff changeset
   283
71277
e06852132c1d tuned messages;
wenzelm
parents: 71276
diff changeset
   284
    progress.echo("\nMySQL setup ...")
71166
a48112873f81 MySQL setup;
wenzelm
parents: 71165
diff changeset
   285
71253
27a998cdc0f4 back to plain name, to have it accepted my mysql;
wenzelm
parents: 71252
diff changeset
   286
    File.write(Path.explode("/etc/mysql/mysql.conf.d/" + phabricator_name(ext = "cnf")),
71249
4eeff87c5072 more phabricator setup;
wenzelm
parents: 71248
diff changeset
   287
"""[mysqld]
4eeff87c5072 more phabricator setup;
wenzelm
parents: 71248
diff changeset
   288
max_allowed_packet = 32M
4eeff87c5072 more phabricator setup;
wenzelm
parents: 71248
diff changeset
   289
innodb_buffer_pool_size = 1600M
4eeff87c5072 more phabricator setup;
wenzelm
parents: 71248
diff changeset
   290
local_infile = 0
4eeff87c5072 more phabricator setup;
wenzelm
parents: 71248
diff changeset
   291
""")
4eeff87c5072 more phabricator setup;
wenzelm
parents: 71248
diff changeset
   292
4eeff87c5072 more phabricator setup;
wenzelm
parents: 71248
diff changeset
   293
    Linux.service_restart("mysql")
4eeff87c5072 more phabricator setup;
wenzelm
parents: 71248
diff changeset
   294
4eeff87c5072 more phabricator setup;
wenzelm
parents: 71248
diff changeset
   295
71475
8451c86ffa85 proper mysql user setup: avoid superuser powers in production;
wenzelm
parents: 71474
diff changeset
   296
    def mysql_conf(R: Regex, which: String): String =
8451c86ffa85 proper mysql user setup: avoid superuser powers in production;
wenzelm
parents: 71474
diff changeset
   297
    {
8451c86ffa85 proper mysql user setup: avoid superuser powers in production;
wenzelm
parents: 71474
diff changeset
   298
      val conf = Path.explode("/etc/mysql/debian.cnf")
8451c86ffa85 proper mysql user setup: avoid superuser powers in production;
wenzelm
parents: 71474
diff changeset
   299
      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
   300
        case Some(res) => res
8451c86ffa85 proper mysql user setup: avoid superuser powers in production;
wenzelm
parents: 71474
diff changeset
   301
        case None => error("Cannot determine " + which + " from " + conf)
8451c86ffa85 proper mysql user setup: avoid superuser powers in production;
wenzelm
parents: 71474
diff changeset
   302
      }
71166
a48112873f81 MySQL setup;
wenzelm
parents: 71165
diff changeset
   303
    }
a48112873f81 MySQL setup;
wenzelm
parents: 71165
diff changeset
   304
71475
8451c86ffa85 proper mysql user setup: avoid superuser powers in production;
wenzelm
parents: 71474
diff changeset
   305
    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
   306
    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
   307
8451c86ffa85 proper mysql user setup: avoid superuser powers in production;
wenzelm
parents: 71474
diff changeset
   308
    val mysql_name = phabricator_name(name = name).replace("-", "_")
8451c86ffa85 proper mysql user setup: avoid superuser powers in production;
wenzelm
parents: 71474
diff changeset
   309
    val mysql_user_string = SQL.string(mysql_name) + "@'localhost'"
8451c86ffa85 proper mysql user setup: avoid superuser powers in production;
wenzelm
parents: 71474
diff changeset
   310
    val mysql_password = Linux.generate_password()
71166
a48112873f81 MySQL setup;
wenzelm
parents: 71165
diff changeset
   311
71475
8451c86ffa85 proper mysql user setup: avoid superuser powers in production;
wenzelm
parents: 71474
diff changeset
   312
    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
   313
      " --password=" + Bash.string(mysql_root_password) + " --execute=" +
8451c86ffa85 proper mysql user setup: avoid superuser powers in production;
wenzelm
parents: 71474
diff changeset
   314
      Bash.string(
71483
5212ca49598a more robust;
wenzelm
parents: 71482
diff changeset
   315
        """DROP USER IF EXISTS """ + mysql_user_string + "; " +
5212ca49598a more robust;
wenzelm
parents: 71482
diff changeset
   316
        """CREATE USER """ + mysql_user_string +
71475
8451c86ffa85 proper mysql user setup: avoid superuser powers in production;
wenzelm
parents: 71474
diff changeset
   317
        """ IDENTIFIED BY """ + SQL.string(mysql_password) + """ PASSWORD EXPIRE NEVER; """ +
8451c86ffa85 proper mysql user setup: avoid superuser powers in production;
wenzelm
parents: 71474
diff changeset
   318
        """GRANT ALL ON `""" + (mysql_name + "_%").replace("_", "\\_") +
8451c86ffa85 proper mysql user setup: avoid superuser powers in production;
wenzelm
parents: 71474
diff changeset
   319
        """`.* TO """ + mysql_user_string + ";")).check
71166
a48112873f81 MySQL setup;
wenzelm
parents: 71165
diff changeset
   320
71475
8451c86ffa85 proper mysql user setup: avoid superuser powers in production;
wenzelm
parents: 71474
diff changeset
   321
    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
   322
    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
   323
8451c86ffa85 proper mysql user setup: avoid superuser powers in production;
wenzelm
parents: 71474
diff changeset
   324
    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
   325
    config.execute("config set storage.default-namespace " + Bash.string(mysql_name))
71249
4eeff87c5072 more phabricator setup;
wenzelm
parents: 71248
diff changeset
   326
    config.execute("config set storage.mysql-engine.max-size 8388608")
4eeff87c5072 more phabricator setup;
wenzelm
parents: 71248
diff changeset
   327
71301
wenzelm
parents: 71300
diff changeset
   328
    progress.bash("bin/storage upgrade --force", cwd = config.home.file, echo = true).check
71166
a48112873f81 MySQL setup;
wenzelm
parents: 71165
diff changeset
   329
a48112873f81 MySQL setup;
wenzelm
parents: 71165
diff changeset
   330
71478
7df5c110a43c support database dump;
wenzelm
parents: 71477
diff changeset
   331
    /* database dump */
7df5c110a43c support database dump;
wenzelm
parents: 71477
diff changeset
   332
7df5c110a43c support database dump;
wenzelm
parents: 71477
diff changeset
   333
    val dump_name = isabelle_phabricator_name(name = "dump")
71479
wenzelm
parents: 71478
diff changeset
   334
    command_setup(dump_name,
71478
7df5c110a43c support database dump;
wenzelm
parents: 71477
diff changeset
   335
"""mkdir -p "$ROOT/database" && chown root:root "$ROOT/database" && chmod 700 "$ROOT/database"
7df5c110a43c support database dump;
wenzelm
parents: 71477
diff changeset
   336
[ -e "$ROOT/database/dump.sql.gz" ] && mv -f "$ROOT/database/dump.sql.gz" "$ROOT/database/dump-old.sql.gz"
7df5c110a43c support database dump;
wenzelm
parents: 71477
diff changeset
   337
echo "Creating $ROOT/database/dump.sql.gz"
71479
wenzelm
parents: 71478
diff changeset
   338
"$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' """)
71478
7df5c110a43c support database dump;
wenzelm
parents: 71477
diff changeset
   339
7df5c110a43c support database dump;
wenzelm
parents: 71477
diff changeset
   340
71249
4eeff87c5072 more phabricator setup;
wenzelm
parents: 71248
diff changeset
   341
    /* PHP setup */
4eeff87c5072 more phabricator setup;
wenzelm
parents: 71248
diff changeset
   342
4eeff87c5072 more phabricator setup;
wenzelm
parents: 71248
diff changeset
   343
    val php_version =
4eeff87c5072 more phabricator setup;
wenzelm
parents: 71248
diff changeset
   344
      Isabelle_System.bash("""php --run 'echo PHP_MAJOR_VERSION . "." . PHP_MINOR_VERSION;'""")
4eeff87c5072 more phabricator setup;
wenzelm
parents: 71248
diff changeset
   345
        .check.out
4eeff87c5072 more phabricator setup;
wenzelm
parents: 71248
diff changeset
   346
4eeff87c5072 more phabricator setup;
wenzelm
parents: 71248
diff changeset
   347
    val php_conf =
4eeff87c5072 more phabricator setup;
wenzelm
parents: 71248
diff changeset
   348
      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
   349
        Path.explode("apache2/conf.d") +
6bf53035baf0 clarified name prefixes: global config always uses "isabelle-phabricator";
wenzelm
parents: 71249
diff changeset
   350
        Path.basic(isabelle_phabricator_name(ext = "ini"))
71249
4eeff87c5072 more phabricator setup;
wenzelm
parents: 71248
diff changeset
   351
4eeff87c5072 more phabricator setup;
wenzelm
parents: 71248
diff changeset
   352
    File.write(php_conf,
4eeff87c5072 more phabricator setup;
wenzelm
parents: 71248
diff changeset
   353
      "post_max_size = 32M\n" +
4eeff87c5072 more phabricator setup;
wenzelm
parents: 71248
diff changeset
   354
      "opcache.validate_timestamps = 0\n" +
71327
557703db74c3 tuned PHP setup;
wenzelm
parents: 71326
diff changeset
   355
      "memory_limit = 512M\n" +
557703db74c3 tuned PHP setup;
wenzelm
parents: 71326
diff changeset
   356
      "max_execution_time = 120\n")
71249
4eeff87c5072 more phabricator setup;
wenzelm
parents: 71248
diff changeset
   357
4eeff87c5072 more phabricator setup;
wenzelm
parents: 71248
diff changeset
   358
71165
46847076477c Apache setup;
wenzelm
parents: 71164
diff changeset
   359
    /* Apache setup */
46847076477c Apache setup;
wenzelm
parents: 71164
diff changeset
   360
71277
e06852132c1d tuned messages;
wenzelm
parents: 71276
diff changeset
   361
    progress.echo("Apache setup ...")
71165
46847076477c Apache setup;
wenzelm
parents: 71164
diff changeset
   362
71266
510b89906d86 discontinued somewhat pointless Isabelle options: setup implicitly assumes Ubuntu 18.04;
wenzelm
parents: 71264
diff changeset
   363
    val apache_root = Path.explode("/etc/apache2")
71165
46847076477c Apache setup;
wenzelm
parents: 71164
diff changeset
   364
    val apache_sites = apache_root + Path.explode("sites-available")
46847076477c Apache setup;
wenzelm
parents: 71164
diff changeset
   365
46847076477c Apache setup;
wenzelm
parents: 71164
diff changeset
   366
    if (!apache_sites.is_dir) error("Bad Apache sites directory " + apache_sites)
46847076477c Apache setup;
wenzelm
parents: 71164
diff changeset
   367
71256
6ca9e8377613 proper names for multiple installations;
wenzelm
parents: 71255
diff changeset
   368
    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
   369
    val server_url = "http://" + server_name
6bf53035baf0 clarified name prefixes: global config always uses "isabelle-phabricator";
wenzelm
parents: 71249
diff changeset
   370
71256
6ca9e8377613 proper names for multiple installations;
wenzelm
parents: 71255
diff changeset
   371
    File.write(apache_sites + Path.basic(isabelle_phabricator_name(name = name, ext = "conf")),
71165
46847076477c Apache setup;
wenzelm
parents: 71164
diff changeset
   372
"""<VirtualHost *:80>
71250
6bf53035baf0 clarified name prefixes: global config always uses "isabelle-phabricator";
wenzelm
parents: 71249
diff changeset
   373
    ServerName """ + server_name + """
71165
46847076477c Apache setup;
wenzelm
parents: 71164
diff changeset
   374
    ServerAdmin webmaster@localhost
71166
a48112873f81 MySQL setup;
wenzelm
parents: 71165
diff changeset
   375
    DocumentRoot """ + config.home.implode + """/webroot
71165
46847076477c Apache setup;
wenzelm
parents: 71164
diff changeset
   376
46847076477c Apache setup;
wenzelm
parents: 71164
diff changeset
   377
    ErrorLog ${APACHE_LOG_DIR}/error.log
46847076477c Apache setup;
wenzelm
parents: 71164
diff changeset
   378
    RewriteEngine on
46847076477c Apache setup;
wenzelm
parents: 71164
diff changeset
   379
    RewriteRule ^(.*)$  /index.php?__path__=$1  [B,L,QSA]
46847076477c Apache setup;
wenzelm
parents: 71164
diff changeset
   380
</VirtualHost>
46847076477c Apache setup;
wenzelm
parents: 71164
diff changeset
   381
46847076477c Apache setup;
wenzelm
parents: 71164
diff changeset
   382
# vim: syntax=apache ts=4 sw=4 sts=4 sr noet
46847076477c Apache setup;
wenzelm
parents: 71164
diff changeset
   383
""")
46847076477c Apache setup;
wenzelm
parents: 71164
diff changeset
   384
71249
4eeff87c5072 more phabricator setup;
wenzelm
parents: 71248
diff changeset
   385
    Isabelle_System.bash( """
71165
46847076477c Apache setup;
wenzelm
parents: 71164
diff changeset
   386
      set -e
46847076477c Apache setup;
wenzelm
parents: 71164
diff changeset
   387
      a2enmod rewrite
71256
6ca9e8377613 proper names for multiple installations;
wenzelm
parents: 71255
diff changeset
   388
      a2ensite """ + Bash.string(isabelle_phabricator_name(name = name))).check
71249
4eeff87c5072 more phabricator setup;
wenzelm
parents: 71248
diff changeset
   389
71255
2965304143d8 more phabricator setup;
wenzelm
parents: 71254
diff changeset
   390
    config.execute("config set phabricator.base-uri " + Bash.string(server_url))
2965304143d8 more phabricator setup;
wenzelm
parents: 71254
diff changeset
   391
71249
4eeff87c5072 more phabricator setup;
wenzelm
parents: 71248
diff changeset
   392
    Linux.service_restart("apache2")
71165
46847076477c Apache setup;
wenzelm
parents: 71164
diff changeset
   393
71326
f79006c533b0 clarified errors: PHP daemon can fail under odd circumstances;
wenzelm
parents: 71325
diff changeset
   394
    progress.echo("\nWeb configuration via " + server_url)
f79006c533b0 clarified errors: PHP daemon can fail under odd circumstances;
wenzelm
parents: 71325
diff changeset
   395
71251
ba14aa0b5a5d more robust: install PHP daemon after Apache;
wenzelm
parents: 71250
diff changeset
   396
ba14aa0b5a5d more robust: install PHP daemon after Apache;
wenzelm
parents: 71250
diff changeset
   397
    /* PHP daemon */
ba14aa0b5a5d more robust: install PHP daemon after Apache;
wenzelm
parents: 71250
diff changeset
   398
71326
f79006c533b0 clarified errors: PHP daemon can fail under odd circumstances;
wenzelm
parents: 71325
diff changeset
   399
    progress.echo("\nPHP daemon setup ...")
71251
ba14aa0b5a5d more robust: install PHP daemon after Apache;
wenzelm
parents: 71250
diff changeset
   400
71482
6b8cbdc9713b more robust;
wenzelm
parents: 71479
diff changeset
   401
    val phd_log_path = Path.explode("/var/tmp/phd")
6b8cbdc9713b more robust;
wenzelm
parents: 71479
diff changeset
   402
    Isabelle_System.mkdirs(phd_log_path)
6b8cbdc9713b more robust;
wenzelm
parents: 71479
diff changeset
   403
    Isabelle_System.chown(
6b8cbdc9713b more robust;
wenzelm
parents: 71479
diff changeset
   404
      "-R " + Bash.string(daemon_user) + ":" + Bash.string(daemon_user), phd_log_path)
6b8cbdc9713b more robust;
wenzelm
parents: 71479
diff changeset
   405
    Isabelle_System.chmod("755", phd_log_path)
6b8cbdc9713b more robust;
wenzelm
parents: 71479
diff changeset
   406
71251
ba14aa0b5a5d more robust: install PHP daemon after Apache;
wenzelm
parents: 71250
diff changeset
   407
    config.execute("config set phd.user " + Bash.string(daemon_user))
71311
eed5b6188371 more support for multiple daemons;
wenzelm
parents: 71310
diff changeset
   408
    config.execute("config set phd.log-directory /var/tmp/phd/" +
eed5b6188371 more support for multiple daemons;
wenzelm
parents: 71310
diff changeset
   409
      isabelle_phabricator_name(name = name) + "/log")
71251
ba14aa0b5a5d more robust: install PHP daemon after Apache;
wenzelm
parents: 71250
diff changeset
   410
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
   411
    val phd_name = isabelle_phabricator_name(name = "phd")
71325
0ad53b5f2bb1 more robust;
wenzelm
parents: 71324
diff changeset
   412
    Linux.service_shutdown(phd_name)
71479
wenzelm
parents: 71478
diff changeset
   413
    val phd_command = command_setup(phd_name, """"$ROOT/phabricator/bin/phd" "$@" """)
71326
f79006c533b0 clarified errors: PHP daemon can fail under odd circumstances;
wenzelm
parents: 71325
diff changeset
   414
    try {
f79006c533b0 clarified errors: PHP daemon can fail under odd circumstances;
wenzelm
parents: 71325
diff changeset
   415
      Linux.service_install(phd_name,
71251
ba14aa0b5a5d more robust: install PHP daemon after Apache;
wenzelm
parents: 71250
diff changeset
   416
"""[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
   417
Description=PHP daemon manager for Isabelle/Phabricator
71251
ba14aa0b5a5d more robust: install PHP daemon after Apache;
wenzelm
parents: 71250
diff changeset
   418
After=syslog.target network.target apache2.service mysql.service
ba14aa0b5a5d more robust: install PHP daemon after Apache;
wenzelm
parents: 71250
diff changeset
   419
ba14aa0b5a5d more robust: install PHP daemon after Apache;
wenzelm
parents: 71250
diff changeset
   420
[Service]
ba14aa0b5a5d more robust: install PHP daemon after Apache;
wenzelm
parents: 71250
diff changeset
   421
Type=oneshot
ba14aa0b5a5d more robust: install PHP daemon after Apache;
wenzelm
parents: 71250
diff changeset
   422
User=""" + daemon_user + """
ba14aa0b5a5d more robust: install PHP daemon after Apache;
wenzelm
parents: 71250
diff changeset
   423
Group=""" + daemon_user + """
ba14aa0b5a5d more robust: install PHP daemon after Apache;
wenzelm
parents: 71250
diff changeset
   424
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
   425
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
   426
ExecStop=""" + phd_command.implode + """ stop
71251
ba14aa0b5a5d more robust: install PHP daemon after Apache;
wenzelm
parents: 71250
diff changeset
   427
RemainAfterExit=yes
ba14aa0b5a5d more robust: install PHP daemon after Apache;
wenzelm
parents: 71250
diff changeset
   428
ba14aa0b5a5d more robust: install PHP daemon after Apache;
wenzelm
parents: 71250
diff changeset
   429
[Install]
ba14aa0b5a5d more robust: install PHP daemon after Apache;
wenzelm
parents: 71250
diff changeset
   430
WantedBy=multi-user.target
ba14aa0b5a5d more robust: install PHP daemon after Apache;
wenzelm
parents: 71250
diff changeset
   431
""")
71326
f79006c533b0 clarified errors: PHP daemon can fail under odd circumstances;
wenzelm
parents: 71325
diff changeset
   432
    }
f79006c533b0 clarified errors: PHP daemon can fail under odd circumstances;
wenzelm
parents: 71325
diff changeset
   433
    catch {
f79006c533b0 clarified errors: PHP daemon can fail under odd circumstances;
wenzelm
parents: 71325
diff changeset
   434
      case ERROR(msg) =>
f79006c533b0 clarified errors: PHP daemon can fail under odd circumstances;
wenzelm
parents: 71325
diff changeset
   435
        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
   436
        error(msg)
f79006c533b0 clarified errors: PHP daemon can fail under odd circumstances;
wenzelm
parents: 71325
diff changeset
   437
    }
71164
79736ffe77c3 some support for Phabricator server;
wenzelm
parents:
diff changeset
   438
  }
79736ffe77c3 some support for Phabricator server;
wenzelm
parents:
diff changeset
   439
79736ffe77c3 some support for Phabricator server;
wenzelm
parents:
diff changeset
   440
79736ffe77c3 some support for Phabricator server;
wenzelm
parents:
diff changeset
   441
  /* Isabelle tool wrapper */
79736ffe77c3 some support for Phabricator server;
wenzelm
parents:
diff changeset
   442
71296
d3ededaa77b3 added "isabelle phabricator";
wenzelm
parents: 71277
diff changeset
   443
  val isabelle_tool2 =
71164
79736ffe77c3 some support for Phabricator server;
wenzelm
parents:
diff changeset
   444
    Isabelle_Tool("phabricator_setup", "setup Phabricator server on Ubuntu Linux", args =>
79736ffe77c3 some support for Phabricator server;
wenzelm
parents:
diff changeset
   445
    {
71245
87c132cf5860 more options;
wenzelm
parents: 71167
diff changeset
   446
      var repo = ""
87c132cf5860 more options;
wenzelm
parents: 71167
diff changeset
   447
      var package_update = false
71276
5bb2235d843d clarified command-line;
wenzelm
parents: 71275
diff changeset
   448
      var name = default_name
71164
79736ffe77c3 some support for Phabricator server;
wenzelm
parents:
diff changeset
   449
      var root = ""
79736ffe77c3 some support for Phabricator server;
wenzelm
parents:
diff changeset
   450
79736ffe77c3 some support for Phabricator server;
wenzelm
parents:
diff changeset
   451
      val getopts =
79736ffe77c3 some support for Phabricator server;
wenzelm
parents:
diff changeset
   452
        Getopts("""
71276
5bb2235d843d clarified command-line;
wenzelm
parents: 71275
diff changeset
   453
Usage: isabelle phabricator_setup [OPTIONS]
71164
79736ffe77c3 some support for Phabricator server;
wenzelm
parents:
diff changeset
   454
79736ffe77c3 some support for Phabricator server;
wenzelm
parents:
diff changeset
   455
  Options are:
71266
510b89906d86 discontinued somewhat pointless Isabelle options: setup implicitly assumes Ubuntu 18.04;
wenzelm
parents: 71264
diff changeset
   456
    -R DIR       repository directory (default: """ + default_repo("NAME") + """)
71245
87c132cf5860 more options;
wenzelm
parents: 71167
diff changeset
   457
    -U           full update of system packages before installation
71276
5bb2235d843d clarified command-line;
wenzelm
parents: 71275
diff changeset
   458
    -n NAME      Phabricator installation name (default: """ + quote(default_name) + """)
71266
510b89906d86 discontinued somewhat pointless Isabelle options: setup implicitly assumes Ubuntu 18.04;
wenzelm
parents: 71264
diff changeset
   459
    -r DIR       installation root directory (default: """ + default_root("NAME") + """)
71164
79736ffe77c3 some support for Phabricator server;
wenzelm
parents:
diff changeset
   460
71302
c073c4e79518 more documentation;
wenzelm
parents: 71301
diff changeset
   461
  Install Phabricator as LAMP application (Linux, Apache, MySQL, PHP).
71164
79736ffe77c3 some support for Phabricator server;
wenzelm
parents:
diff changeset
   462
71276
5bb2235d843d clarified command-line;
wenzelm
parents: 71275
diff changeset
   463
  The installation name (default: """ + quote(default_name) + """) is mapped to a regular
5bb2235d843d clarified command-line;
wenzelm
parents: 71275
diff changeset
   464
  Unix user; this is relevant for public SSH access.
71164
79736ffe77c3 some support for Phabricator server;
wenzelm
parents:
diff changeset
   465
""",
79736ffe77c3 some support for Phabricator server;
wenzelm
parents:
diff changeset
   466
          "R:" -> (arg => repo = arg),
71245
87c132cf5860 more options;
wenzelm
parents: 71167
diff changeset
   467
          "U" -> (_ => package_update = true),
71276
5bb2235d843d clarified command-line;
wenzelm
parents: 71275
diff changeset
   468
          "n:" -> (arg => name = arg),
71164
79736ffe77c3 some support for Phabricator server;
wenzelm
parents:
diff changeset
   469
          "r:" -> (arg => root = arg))
79736ffe77c3 some support for Phabricator server;
wenzelm
parents:
diff changeset
   470
79736ffe77c3 some support for Phabricator server;
wenzelm
parents:
diff changeset
   471
      val more_args = getopts(args)
71276
5bb2235d843d clarified command-line;
wenzelm
parents: 71275
diff changeset
   472
      if (more_args.nonEmpty) getopts.usage()
71164
79736ffe77c3 some support for Phabricator server;
wenzelm
parents:
diff changeset
   473
79736ffe77c3 some support for Phabricator server;
wenzelm
parents:
diff changeset
   474
      val progress = new Console_Progress
79736ffe77c3 some support for Phabricator server;
wenzelm
parents:
diff changeset
   475
71477
e2fb60756fb8 proper check of Linux version;
wenzelm
parents: 71475
diff changeset
   476
      val release = Linux.Release()
e2fb60756fb8 proper check of Linux version;
wenzelm
parents: 71475
diff changeset
   477
      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
   478
71276
5bb2235d843d clarified command-line;
wenzelm
parents: 71275
diff changeset
   479
      phabricator_setup(name = name, root = root, repo = repo,
71245
87c132cf5860 more options;
wenzelm
parents: 71167
diff changeset
   480
        package_update = package_update, progress = progress)
71164
79736ffe77c3 some support for Phabricator server;
wenzelm
parents:
diff changeset
   481
    })
79736ffe77c3 some support for Phabricator server;
wenzelm
parents:
diff changeset
   482
79736ffe77c3 some support for Phabricator server;
wenzelm
parents:
diff changeset
   483
79736ffe77c3 some support for Phabricator server;
wenzelm
parents:
diff changeset
   484
71264
114db2b5a5f8 support for Phabricator mail configuration;
wenzelm
parents: 71256
diff changeset
   485
  /** setup mail **/
71164
79736ffe77c3 some support for Phabricator server;
wenzelm
parents:
diff changeset
   486
71270
wenzelm
parents: 71269
diff changeset
   487
  val mailers_template: String =
wenzelm
parents: 71269
diff changeset
   488
"""[
wenzelm
parents: 71269
diff changeset
   489
  {
wenzelm
parents: 71269
diff changeset
   490
    "key": "example.org",
wenzelm
parents: 71269
diff changeset
   491
    "type": "smtp",
wenzelm
parents: 71269
diff changeset
   492
    "options": {
wenzelm
parents: 71269
diff changeset
   493
      "host": "mail.example.org",
wenzelm
parents: 71269
diff changeset
   494
      "port": 465,
wenzelm
parents: 71269
diff changeset
   495
      "user": "phabricator@example.org",
wenzelm
parents: 71269
diff changeset
   496
      "password": "********",
wenzelm
parents: 71269
diff changeset
   497
      "protocol": "ssl",
wenzelm
parents: 71269
diff changeset
   498
      "message-id": true
wenzelm
parents: 71269
diff changeset
   499
    }
wenzelm
parents: 71269
diff changeset
   500
  }
wenzelm
parents: 71269
diff changeset
   501
]"""
wenzelm
parents: 71269
diff changeset
   502
71264
114db2b5a5f8 support for Phabricator mail configuration;
wenzelm
parents: 71256
diff changeset
   503
  def phabricator_setup_mail(
114db2b5a5f8 support for Phabricator mail configuration;
wenzelm
parents: 71256
diff changeset
   504
    name: String = default_name,
114db2b5a5f8 support for Phabricator mail configuration;
wenzelm
parents: 71256
diff changeset
   505
    config_file: Option[Path] = None,
114db2b5a5f8 support for Phabricator mail configuration;
wenzelm
parents: 71256
diff changeset
   506
    test_user: String = "",
114db2b5a5f8 support for Phabricator mail configuration;
wenzelm
parents: 71256
diff changeset
   507
    progress: Progress = No_Progress)
71164
79736ffe77c3 some support for Phabricator server;
wenzelm
parents:
diff changeset
   508
  {
79736ffe77c3 some support for Phabricator server;
wenzelm
parents:
diff changeset
   509
    Linux.check_system_root()
79736ffe77c3 some support for Phabricator server;
wenzelm
parents:
diff changeset
   510
71264
114db2b5a5f8 support for Phabricator mail configuration;
wenzelm
parents: 71256
diff changeset
   511
    val config = get_config(name)
71271
d61fd7aade69 clarified directory;
wenzelm
parents: 71270
diff changeset
   512
    val default_config_file = config.root + default_mailers
71264
114db2b5a5f8 support for Phabricator mail configuration;
wenzelm
parents: 71256
diff changeset
   513
114db2b5a5f8 support for Phabricator mail configuration;
wenzelm
parents: 71256
diff changeset
   514
    val mail_config = config_file getOrElse default_config_file
114db2b5a5f8 support for Phabricator mail configuration;
wenzelm
parents: 71256
diff changeset
   515
114db2b5a5f8 support for Phabricator mail configuration;
wenzelm
parents: 71256
diff changeset
   516
    def setup_mail
114db2b5a5f8 support for Phabricator mail configuration;
wenzelm
parents: 71256
diff changeset
   517
    {
114db2b5a5f8 support for Phabricator mail configuration;
wenzelm
parents: 71256
diff changeset
   518
      progress.echo("Using mail configuration from " + mail_config)
114db2b5a5f8 support for Phabricator mail configuration;
wenzelm
parents: 71256
diff changeset
   519
      config.execute("config set cluster.mailers --stdin < " + File.bash_path(mail_config))
114db2b5a5f8 support for Phabricator mail configuration;
wenzelm
parents: 71256
diff changeset
   520
114db2b5a5f8 support for Phabricator mail configuration;
wenzelm
parents: 71256
diff changeset
   521
      if (test_user.nonEmpty) {
114db2b5a5f8 support for Phabricator mail configuration;
wenzelm
parents: 71256
diff changeset
   522
        progress.echo("Sending test mail to " + quote(test_user))
114db2b5a5f8 support for Phabricator mail configuration;
wenzelm
parents: 71256
diff changeset
   523
        progress.bash(cwd = config.home.file, echo = true,
71301
wenzelm
parents: 71300
diff changeset
   524
          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
   525
            Bash.string(test_user)).check
114db2b5a5f8 support for Phabricator mail configuration;
wenzelm
parents: 71256
diff changeset
   526
      }
114db2b5a5f8 support for Phabricator mail configuration;
wenzelm
parents: 71256
diff changeset
   527
    }
114db2b5a5f8 support for Phabricator mail configuration;
wenzelm
parents: 71256
diff changeset
   528
114db2b5a5f8 support for Phabricator mail configuration;
wenzelm
parents: 71256
diff changeset
   529
    if (config_file.isEmpty) {
71268
79b89278b825 clarified permissions;
wenzelm
parents: 71266
diff changeset
   530
      if (!default_config_file.is_file) {
79b89278b825 clarified permissions;
wenzelm
parents: 71266
diff changeset
   531
        File.write(default_config_file, mailers_template)
71313
6cfec8029831 clarified signature;
wenzelm
parents: 71311
diff changeset
   532
        Isabelle_System.chmod("600", default_config_file)
71268
79b89278b825 clarified permissions;
wenzelm
parents: 71266
diff changeset
   533
      }
71264
114db2b5a5f8 support for Phabricator mail configuration;
wenzelm
parents: 71256
diff changeset
   534
      if (File.read(default_config_file) == mailers_template) {
71329
1579a9160c7f misc tuning and clarification;
wenzelm
parents: 71327
diff changeset
   535
        progress.echo("Please invoke the tool again, after providing details in\n  " +
1579a9160c7f misc tuning and clarification;
wenzelm
parents: 71327
diff changeset
   536
          default_config_file.implode + "\n")
71264
114db2b5a5f8 support for Phabricator mail configuration;
wenzelm
parents: 71256
diff changeset
   537
      }
114db2b5a5f8 support for Phabricator mail configuration;
wenzelm
parents: 71256
diff changeset
   538
      else setup_mail
114db2b5a5f8 support for Phabricator mail configuration;
wenzelm
parents: 71256
diff changeset
   539
    }
114db2b5a5f8 support for Phabricator mail configuration;
wenzelm
parents: 71256
diff changeset
   540
    else setup_mail
71164
79736ffe77c3 some support for Phabricator server;
wenzelm
parents:
diff changeset
   541
  }
79736ffe77c3 some support for Phabricator server;
wenzelm
parents:
diff changeset
   542
79736ffe77c3 some support for Phabricator server;
wenzelm
parents:
diff changeset
   543
79736ffe77c3 some support for Phabricator server;
wenzelm
parents:
diff changeset
   544
  /* Isabelle tool wrapper */
79736ffe77c3 some support for Phabricator server;
wenzelm
parents:
diff changeset
   545
71296
d3ededaa77b3 added "isabelle phabricator";
wenzelm
parents: 71277
diff changeset
   546
  val isabelle_tool3 =
71264
114db2b5a5f8 support for Phabricator mail configuration;
wenzelm
parents: 71256
diff changeset
   547
    Isabelle_Tool("phabricator_setup_mail",
71308
8c1c717a830b configure SSH hosting via "isabelle phabricator_setup_ssh";
wenzelm
parents: 71302
diff changeset
   548
      "setup mail for one Phabricator installation", args =>
71164
79736ffe77c3 some support for Phabricator server;
wenzelm
parents:
diff changeset
   549
    {
71264
114db2b5a5f8 support for Phabricator mail configuration;
wenzelm
parents: 71256
diff changeset
   550
      var test_user = ""
114db2b5a5f8 support for Phabricator mail configuration;
wenzelm
parents: 71256
diff changeset
   551
      var name = default_name
114db2b5a5f8 support for Phabricator mail configuration;
wenzelm
parents: 71256
diff changeset
   552
      var config_file: Option[Path] = None
114db2b5a5f8 support for Phabricator mail configuration;
wenzelm
parents: 71256
diff changeset
   553
71164
79736ffe77c3 some support for Phabricator server;
wenzelm
parents:
diff changeset
   554
      val getopts =
79736ffe77c3 some support for Phabricator server;
wenzelm
parents:
diff changeset
   555
        Getopts("""
71264
114db2b5a5f8 support for Phabricator mail configuration;
wenzelm
parents: 71256
diff changeset
   556
Usage: isabelle phabricator_setup_mail [OPTIONS]
114db2b5a5f8 support for Phabricator mail configuration;
wenzelm
parents: 71256
diff changeset
   557
114db2b5a5f8 support for Phabricator mail configuration;
wenzelm
parents: 71256
diff changeset
   558
  Options are:
114db2b5a5f8 support for Phabricator mail configuration;
wenzelm
parents: 71256
diff changeset
   559
    -T USER      send test mail to Phabricator user
71302
c073c4e79518 more documentation;
wenzelm
parents: 71301
diff changeset
   560
    -f FILE      config file (default: """ + default_mailers + """ within Phabricator root)
71264
114db2b5a5f8 support for Phabricator mail configuration;
wenzelm
parents: 71256
diff changeset
   561
    -n NAME      Phabricator installation name (default: """ + quote(default_name) + """)
71164
79736ffe77c3 some support for Phabricator server;
wenzelm
parents:
diff changeset
   562
71275
41b6ca223500 tuned messages;
wenzelm
parents: 71274
diff changeset
   563
  Provide mail configuration for existing Phabricator installation.
71264
114db2b5a5f8 support for Phabricator mail configuration;
wenzelm
parents: 71256
diff changeset
   564
""",
114db2b5a5f8 support for Phabricator mail configuration;
wenzelm
parents: 71256
diff changeset
   565
          "T:" -> (arg => test_user = arg),
114db2b5a5f8 support for Phabricator mail configuration;
wenzelm
parents: 71256
diff changeset
   566
          "f:" -> (arg => config_file = Some(Path.explode(arg))),
114db2b5a5f8 support for Phabricator mail configuration;
wenzelm
parents: 71256
diff changeset
   567
          "n:" -> (arg => name = arg))
71164
79736ffe77c3 some support for Phabricator server;
wenzelm
parents:
diff changeset
   568
79736ffe77c3 some support for Phabricator server;
wenzelm
parents:
diff changeset
   569
      val more_args = getopts(args)
71264
114db2b5a5f8 support for Phabricator mail configuration;
wenzelm
parents: 71256
diff changeset
   570
      if (more_args.nonEmpty) getopts.usage()
71164
79736ffe77c3 some support for Phabricator server;
wenzelm
parents:
diff changeset
   571
79736ffe77c3 some support for Phabricator server;
wenzelm
parents:
diff changeset
   572
      val progress = new Console_Progress
79736ffe77c3 some support for Phabricator server;
wenzelm
parents:
diff changeset
   573
71264
114db2b5a5f8 support for Phabricator mail configuration;
wenzelm
parents: 71256
diff changeset
   574
      phabricator_setup_mail(name = name, config_file = config_file,
114db2b5a5f8 support for Phabricator mail configuration;
wenzelm
parents: 71256
diff changeset
   575
        test_user = test_user, progress = progress)
71164
79736ffe77c3 some support for Phabricator server;
wenzelm
parents:
diff changeset
   576
    })
71308
8c1c717a830b configure SSH hosting via "isabelle phabricator_setup_ssh";
wenzelm
parents: 71302
diff changeset
   577
8c1c717a830b configure SSH hosting via "isabelle phabricator_setup_ssh";
wenzelm
parents: 71302
diff changeset
   578
8c1c717a830b configure SSH hosting via "isabelle phabricator_setup_ssh";
wenzelm
parents: 71302
diff changeset
   579
8c1c717a830b configure SSH hosting via "isabelle phabricator_setup_ssh";
wenzelm
parents: 71302
diff changeset
   580
  /** setup ssh **/
8c1c717a830b configure SSH hosting via "isabelle phabricator_setup_ssh";
wenzelm
parents: 71302
diff changeset
   581
8c1c717a830b configure SSH hosting via "isabelle phabricator_setup_ssh";
wenzelm
parents: 71302
diff changeset
   582
  /* sshd config */
8c1c717a830b configure SSH hosting via "isabelle phabricator_setup_ssh";
wenzelm
parents: 71302
diff changeset
   583
8c1c717a830b configure SSH hosting via "isabelle phabricator_setup_ssh";
wenzelm
parents: 71302
diff changeset
   584
  private val Port = """^\s*Port\s+(\d+)\s*$""".r
8c1c717a830b configure SSH hosting via "isabelle phabricator_setup_ssh";
wenzelm
parents: 71302
diff changeset
   585
  private val No_Port = """^#\s*Port\b.*$""".r
8c1c717a830b configure SSH hosting via "isabelle phabricator_setup_ssh";
wenzelm
parents: 71302
diff changeset
   586
  private val Any_Port = """^#?\s*Port\b.*$""".r
8c1c717a830b configure SSH hosting via "isabelle phabricator_setup_ssh";
wenzelm
parents: 71302
diff changeset
   587
8c1c717a830b configure SSH hosting via "isabelle phabricator_setup_ssh";
wenzelm
parents: 71302
diff changeset
   588
  def conf_ssh_port(port: Int): String =
8c1c717a830b configure SSH hosting via "isabelle phabricator_setup_ssh";
wenzelm
parents: 71302
diff changeset
   589
    if (port == 22) "#Port 22" else "Port " + port
8c1c717a830b configure SSH hosting via "isabelle phabricator_setup_ssh";
wenzelm
parents: 71302
diff changeset
   590
8c1c717a830b configure SSH hosting via "isabelle phabricator_setup_ssh";
wenzelm
parents: 71302
diff changeset
   591
  def read_ssh_port(conf: Path): Int =
8c1c717a830b configure SSH hosting via "isabelle phabricator_setup_ssh";
wenzelm
parents: 71302
diff changeset
   592
  {
8c1c717a830b configure SSH hosting via "isabelle phabricator_setup_ssh";
wenzelm
parents: 71302
diff changeset
   593
    val lines = split_lines(File.read(conf))
8c1c717a830b configure SSH hosting via "isabelle phabricator_setup_ssh";
wenzelm
parents: 71302
diff changeset
   594
    val ports =
8c1c717a830b configure SSH hosting via "isabelle phabricator_setup_ssh";
wenzelm
parents: 71302
diff changeset
   595
      lines.flatMap({
8c1c717a830b configure SSH hosting via "isabelle phabricator_setup_ssh";
wenzelm
parents: 71302
diff changeset
   596
        case Port(Value.Int(p)) => Some(p)
8c1c717a830b configure SSH hosting via "isabelle phabricator_setup_ssh";
wenzelm
parents: 71302
diff changeset
   597
        case No_Port() => Some(22)
8c1c717a830b configure SSH hosting via "isabelle phabricator_setup_ssh";
wenzelm
parents: 71302
diff changeset
   598
        case _ => None
8c1c717a830b configure SSH hosting via "isabelle phabricator_setup_ssh";
wenzelm
parents: 71302
diff changeset
   599
      })
8c1c717a830b configure SSH hosting via "isabelle phabricator_setup_ssh";
wenzelm
parents: 71302
diff changeset
   600
    ports match {
8c1c717a830b configure SSH hosting via "isabelle phabricator_setup_ssh";
wenzelm
parents: 71302
diff changeset
   601
      case List(port) => port
8c1c717a830b configure SSH hosting via "isabelle phabricator_setup_ssh";
wenzelm
parents: 71302
diff changeset
   602
      case Nil => error("Missing Port specification in " + conf)
8c1c717a830b configure SSH hosting via "isabelle phabricator_setup_ssh";
wenzelm
parents: 71302
diff changeset
   603
      case _ => error("Multiple Port specifications in " + conf)
8c1c717a830b configure SSH hosting via "isabelle phabricator_setup_ssh";
wenzelm
parents: 71302
diff changeset
   604
    }
8c1c717a830b configure SSH hosting via "isabelle phabricator_setup_ssh";
wenzelm
parents: 71302
diff changeset
   605
  }
8c1c717a830b configure SSH hosting via "isabelle phabricator_setup_ssh";
wenzelm
parents: 71302
diff changeset
   606
8c1c717a830b configure SSH hosting via "isabelle phabricator_setup_ssh";
wenzelm
parents: 71302
diff changeset
   607
  def write_ssh_port(conf: Path, port: Int): Boolean =
8c1c717a830b configure SSH hosting via "isabelle phabricator_setup_ssh";
wenzelm
parents: 71302
diff changeset
   608
  {
8c1c717a830b configure SSH hosting via "isabelle phabricator_setup_ssh";
wenzelm
parents: 71302
diff changeset
   609
    val old_port = read_ssh_port(conf)
8c1c717a830b configure SSH hosting via "isabelle phabricator_setup_ssh";
wenzelm
parents: 71302
diff changeset
   610
    if (old_port == port) false
8c1c717a830b configure SSH hosting via "isabelle phabricator_setup_ssh";
wenzelm
parents: 71302
diff changeset
   611
    else {
8c1c717a830b configure SSH hosting via "isabelle phabricator_setup_ssh";
wenzelm
parents: 71302
diff changeset
   612
      val lines = split_lines(File.read(conf))
8c1c717a830b configure SSH hosting via "isabelle phabricator_setup_ssh";
wenzelm
parents: 71302
diff changeset
   613
      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
   614
      File.write(conf, cat_lines(lines1))
8c1c717a830b configure SSH hosting via "isabelle phabricator_setup_ssh";
wenzelm
parents: 71302
diff changeset
   615
      true
8c1c717a830b configure SSH hosting via "isabelle phabricator_setup_ssh";
wenzelm
parents: 71302
diff changeset
   616
    }
8c1c717a830b configure SSH hosting via "isabelle phabricator_setup_ssh";
wenzelm
parents: 71302
diff changeset
   617
  }
8c1c717a830b configure SSH hosting via "isabelle phabricator_setup_ssh";
wenzelm
parents: 71302
diff changeset
   618
8c1c717a830b configure SSH hosting via "isabelle phabricator_setup_ssh";
wenzelm
parents: 71302
diff changeset
   619
8c1c717a830b configure SSH hosting via "isabelle phabricator_setup_ssh";
wenzelm
parents: 71302
diff changeset
   620
  /* phabricator_setup_ssh */
8c1c717a830b configure SSH hosting via "isabelle phabricator_setup_ssh";
wenzelm
parents: 71302
diff changeset
   621
8c1c717a830b configure SSH hosting via "isabelle phabricator_setup_ssh";
wenzelm
parents: 71302
diff changeset
   622
  def phabricator_setup_ssh(
8c1c717a830b configure SSH hosting via "isabelle phabricator_setup_ssh";
wenzelm
parents: 71302
diff changeset
   623
    server_port: Int = default_server_port,
8c1c717a830b configure SSH hosting via "isabelle phabricator_setup_ssh";
wenzelm
parents: 71302
diff changeset
   624
    system_port: Int = default_system_port,
8c1c717a830b configure SSH hosting via "isabelle phabricator_setup_ssh";
wenzelm
parents: 71302
diff changeset
   625
    test_server: Boolean = false,
8c1c717a830b configure SSH hosting via "isabelle phabricator_setup_ssh";
wenzelm
parents: 71302
diff changeset
   626
    progress: Progress = No_Progress)
8c1c717a830b configure SSH hosting via "isabelle phabricator_setup_ssh";
wenzelm
parents: 71302
diff changeset
   627
  {
8c1c717a830b configure SSH hosting via "isabelle phabricator_setup_ssh";
wenzelm
parents: 71302
diff changeset
   628
    Linux.check_system_root()
8c1c717a830b configure SSH hosting via "isabelle phabricator_setup_ssh";
wenzelm
parents: 71302
diff changeset
   629
8c1c717a830b configure SSH hosting via "isabelle phabricator_setup_ssh";
wenzelm
parents: 71302
diff changeset
   630
    val configs = read_config()
8c1c717a830b configure SSH hosting via "isabelle phabricator_setup_ssh";
wenzelm
parents: 71302
diff changeset
   631
8c1c717a830b configure SSH hosting via "isabelle phabricator_setup_ssh";
wenzelm
parents: 71302
diff changeset
   632
    if (server_port == system_port) {
8c1c717a830b configure SSH hosting via "isabelle phabricator_setup_ssh";
wenzelm
parents: 71302
diff changeset
   633
      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
   634
    }
8c1c717a830b configure SSH hosting via "isabelle phabricator_setup_ssh";
wenzelm
parents: 71302
diff changeset
   635
8c1c717a830b configure SSH hosting via "isabelle phabricator_setup_ssh";
wenzelm
parents: 71302
diff changeset
   636
    val sshd_conf_system = Path.explode("/etc/ssh/sshd_config")
8c1c717a830b configure SSH hosting via "isabelle phabricator_setup_ssh";
wenzelm
parents: 71302
diff changeset
   637
    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
   638
8c1c717a830b configure SSH hosting via "isabelle phabricator_setup_ssh";
wenzelm
parents: 71302
diff changeset
   639
    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
   640
    Linux.service_shutdown(ssh_name)
cd166c3904dd more robust: system ssh service is required for Phabricator ssh service;
wenzelm
parents: 71308
diff changeset
   641
71308
8c1c717a830b configure SSH hosting via "isabelle phabricator_setup_ssh";
wenzelm
parents: 71302
diff changeset
   642
    val old_system_port = read_ssh_port(sshd_conf_system)
8c1c717a830b configure SSH hosting via "isabelle phabricator_setup_ssh";
wenzelm
parents: 71302
diff changeset
   643
    if (old_system_port != system_port) {
8c1c717a830b configure SSH hosting via "isabelle phabricator_setup_ssh";
wenzelm
parents: 71302
diff changeset
   644
      progress.echo("Reconfigurig system ssh service")
71310
cd166c3904dd more robust: system ssh service is required for Phabricator ssh service;
wenzelm
parents: 71308
diff changeset
   645
      Linux.service_shutdown("ssh")
71308
8c1c717a830b configure SSH hosting via "isabelle phabricator_setup_ssh";
wenzelm
parents: 71302
diff changeset
   646
      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
   647
      Linux.service_start("ssh")
71308
8c1c717a830b configure SSH hosting via "isabelle phabricator_setup_ssh";
wenzelm
parents: 71302
diff changeset
   648
    }
8c1c717a830b configure SSH hosting via "isabelle phabricator_setup_ssh";
wenzelm
parents: 71302
diff changeset
   649
8c1c717a830b configure SSH hosting via "isabelle phabricator_setup_ssh";
wenzelm
parents: 71302
diff changeset
   650
    progress.echo("Configuring " + ssh_name + " service")
8c1c717a830b configure SSH hosting via "isabelle phabricator_setup_ssh";
wenzelm
parents: 71302
diff changeset
   651
71479
wenzelm
parents: 71478
diff changeset
   652
    val ssh_command = command_setup(ssh_name,
71320
730090397e0d clarified signature;
wenzelm
parents: 71315
diff changeset
   653
"""if [ "$1" = "$NAME" ]
730090397e0d clarified signature;
wenzelm
parents: 71315
diff changeset
   654
then
730090397e0d clarified signature;
wenzelm
parents: 71315
diff changeset
   655
  exec "$ROOT/phabricator/bin/ssh-auth" "$@"
71479
wenzelm
parents: 71478
diff changeset
   656
fi""", exit = "exit 1")
71308
8c1c717a830b configure SSH hosting via "isabelle phabricator_setup_ssh";
wenzelm
parents: 71302
diff changeset
   657
8c1c717a830b configure SSH hosting via "isabelle phabricator_setup_ssh";
wenzelm
parents: 71302
diff changeset
   658
    File.write(sshd_conf_server,
8c1c717a830b configure SSH hosting via "isabelle phabricator_setup_ssh";
wenzelm
parents: 71302
diff changeset
   659
"""# OpenBSD Secure Shell server for Isabelle/Phabricator
8c1c717a830b configure SSH hosting via "isabelle phabricator_setup_ssh";
wenzelm
parents: 71302
diff changeset
   660
AuthorizedKeysCommand """ + ssh_command.implode + """
8c1c717a830b configure SSH hosting via "isabelle phabricator_setup_ssh";
wenzelm
parents: 71302
diff changeset
   661
AuthorizedKeysCommandUser """ + daemon_user + """
8c1c717a830b configure SSH hosting via "isabelle phabricator_setup_ssh";
wenzelm
parents: 71302
diff changeset
   662
AuthorizedKeysFile none
8c1c717a830b configure SSH hosting via "isabelle phabricator_setup_ssh";
wenzelm
parents: 71302
diff changeset
   663
AllowUsers """ + configs.map(_.name).mkString(" ") + """
8c1c717a830b configure SSH hosting via "isabelle phabricator_setup_ssh";
wenzelm
parents: 71302
diff changeset
   664
Port """ + server_port + """
8c1c717a830b configure SSH hosting via "isabelle phabricator_setup_ssh";
wenzelm
parents: 71302
diff changeset
   665
Protocol 2
8c1c717a830b configure SSH hosting via "isabelle phabricator_setup_ssh";
wenzelm
parents: 71302
diff changeset
   666
PermitRootLogin no
8c1c717a830b configure SSH hosting via "isabelle phabricator_setup_ssh";
wenzelm
parents: 71302
diff changeset
   667
AllowAgentForwarding no
8c1c717a830b configure SSH hosting via "isabelle phabricator_setup_ssh";
wenzelm
parents: 71302
diff changeset
   668
AllowTcpForwarding no
8c1c717a830b configure SSH hosting via "isabelle phabricator_setup_ssh";
wenzelm
parents: 71302
diff changeset
   669
PrintMotd no
8c1c717a830b configure SSH hosting via "isabelle phabricator_setup_ssh";
wenzelm
parents: 71302
diff changeset
   670
PrintLastLog no
8c1c717a830b configure SSH hosting via "isabelle phabricator_setup_ssh";
wenzelm
parents: 71302
diff changeset
   671
PasswordAuthentication no
8c1c717a830b configure SSH hosting via "isabelle phabricator_setup_ssh";
wenzelm
parents: 71302
diff changeset
   672
ChallengeResponseAuthentication no
8c1c717a830b configure SSH hosting via "isabelle phabricator_setup_ssh";
wenzelm
parents: 71302
diff changeset
   673
PidFile /var/run/""" + ssh_name + """.pid
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
8c1c717a830b configure SSH hosting via "isabelle phabricator_setup_ssh";
wenzelm
parents: 71302
diff changeset
   676
    Linux.service_install(ssh_name,
8c1c717a830b configure SSH hosting via "isabelle phabricator_setup_ssh";
wenzelm
parents: 71302
diff changeset
   677
"""[Unit]
8c1c717a830b configure SSH hosting via "isabelle phabricator_setup_ssh";
wenzelm
parents: 71302
diff changeset
   678
Description=OpenBSD Secure Shell server for Isabelle/Phabricator
8c1c717a830b configure SSH hosting via "isabelle phabricator_setup_ssh";
wenzelm
parents: 71302
diff changeset
   679
After=network.target auditd.service
8c1c717a830b configure SSH hosting via "isabelle phabricator_setup_ssh";
wenzelm
parents: 71302
diff changeset
   680
ConditionPathExists=!/etc/ssh/sshd_not_to_be_run
8c1c717a830b configure SSH hosting via "isabelle phabricator_setup_ssh";
wenzelm
parents: 71302
diff changeset
   681
8c1c717a830b configure SSH hosting via "isabelle phabricator_setup_ssh";
wenzelm
parents: 71302
diff changeset
   682
[Service]
8c1c717a830b configure SSH hosting via "isabelle phabricator_setup_ssh";
wenzelm
parents: 71302
diff changeset
   683
EnvironmentFile=-/etc/default/ssh
8c1c717a830b configure SSH hosting via "isabelle phabricator_setup_ssh";
wenzelm
parents: 71302
diff changeset
   684
ExecStartPre=/usr/sbin/sshd -f """ + sshd_conf_server.implode + """ -t
8c1c717a830b configure SSH hosting via "isabelle phabricator_setup_ssh";
wenzelm
parents: 71302
diff changeset
   685
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
   686
ExecReload=/usr/sbin/sshd -f """ + sshd_conf_server.implode + """ -t
8c1c717a830b configure SSH hosting via "isabelle phabricator_setup_ssh";
wenzelm
parents: 71302
diff changeset
   687
ExecReload=/bin/kill -HUP $MAINPID
8c1c717a830b configure SSH hosting via "isabelle phabricator_setup_ssh";
wenzelm
parents: 71302
diff changeset
   688
KillMode=process
8c1c717a830b configure SSH hosting via "isabelle phabricator_setup_ssh";
wenzelm
parents: 71302
diff changeset
   689
Restart=on-failure
8c1c717a830b configure SSH hosting via "isabelle phabricator_setup_ssh";
wenzelm
parents: 71302
diff changeset
   690
RestartPreventExitStatus=255
8c1c717a830b configure SSH hosting via "isabelle phabricator_setup_ssh";
wenzelm
parents: 71302
diff changeset
   691
Type=notify
8c1c717a830b configure SSH hosting via "isabelle phabricator_setup_ssh";
wenzelm
parents: 71302
diff changeset
   692
RuntimeDirectory=sshd-phabricator
8c1c717a830b configure SSH hosting via "isabelle phabricator_setup_ssh";
wenzelm
parents: 71302
diff changeset
   693
RuntimeDirectoryMode=0755
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
[Install]
8c1c717a830b configure SSH hosting via "isabelle phabricator_setup_ssh";
wenzelm
parents: 71302
diff changeset
   696
WantedBy=multi-user.target
8c1c717a830b configure SSH hosting via "isabelle phabricator_setup_ssh";
wenzelm
parents: 71302
diff changeset
   697
Alias=""" + ssh_name + """.service
8c1c717a830b configure SSH hosting via "isabelle phabricator_setup_ssh";
wenzelm
parents: 71302
diff changeset
   698
""")
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
    for (config <- configs) {
8c1c717a830b configure SSH hosting via "isabelle phabricator_setup_ssh";
wenzelm
parents: 71302
diff changeset
   701
      progress.echo("phabricator " + quote(config.name) + " port " +  server_port)
8c1c717a830b configure SSH hosting via "isabelle phabricator_setup_ssh";
wenzelm
parents: 71302
diff changeset
   702
      config.execute("config set diffusion.ssh-port " + Bash.string(server_port.toString))
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
      if (test_server) {
8c1c717a830b configure SSH hosting via "isabelle phabricator_setup_ssh";
wenzelm
parents: 71302
diff changeset
   705
        progress.bash(
8c1c717a830b configure SSH hosting via "isabelle phabricator_setup_ssh";
wenzelm
parents: 71302
diff changeset
   706
          """unset DISPLAY
8c1c717a830b configure SSH hosting via "isabelle phabricator_setup_ssh";
wenzelm
parents: 71302
diff changeset
   707
          echo "{}" | ssh -p """ + Bash.string(server_port.toString) +
8c1c717a830b configure SSH hosting via "isabelle phabricator_setup_ssh";
wenzelm
parents: 71302
diff changeset
   708
          " -o StrictHostKeyChecking=false " +
8c1c717a830b configure SSH hosting via "isabelle phabricator_setup_ssh";
wenzelm
parents: 71302
diff changeset
   709
          Bash.string(config.name) + """@localhost conduit conduit.ping""").print
8c1c717a830b configure SSH hosting via "isabelle phabricator_setup_ssh";
wenzelm
parents: 71302
diff changeset
   710
      }
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
  /* Isabelle tool wrapper */
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
  val isabelle_tool4 =
8c1c717a830b configure SSH hosting via "isabelle phabricator_setup_ssh";
wenzelm
parents: 71302
diff changeset
   718
    Isabelle_Tool("phabricator_setup_ssh",
8c1c717a830b configure SSH hosting via "isabelle phabricator_setup_ssh";
wenzelm
parents: 71302
diff changeset
   719
      "setup ssh service for all Phabricator installations", args =>
8c1c717a830b configure SSH hosting via "isabelle phabricator_setup_ssh";
wenzelm
parents: 71302
diff changeset
   720
    {
8c1c717a830b configure SSH hosting via "isabelle phabricator_setup_ssh";
wenzelm
parents: 71302
diff changeset
   721
      var server_port = default_server_port
8c1c717a830b configure SSH hosting via "isabelle phabricator_setup_ssh";
wenzelm
parents: 71302
diff changeset
   722
      var system_port = default_system_port
8c1c717a830b configure SSH hosting via "isabelle phabricator_setup_ssh";
wenzelm
parents: 71302
diff changeset
   723
      var test_server = false
8c1c717a830b configure SSH hosting via "isabelle phabricator_setup_ssh";
wenzelm
parents: 71302
diff changeset
   724
8c1c717a830b configure SSH hosting via "isabelle phabricator_setup_ssh";
wenzelm
parents: 71302
diff changeset
   725
      val getopts =
8c1c717a830b configure SSH hosting via "isabelle phabricator_setup_ssh";
wenzelm
parents: 71302
diff changeset
   726
        Getopts("""
8c1c717a830b configure SSH hosting via "isabelle phabricator_setup_ssh";
wenzelm
parents: 71302
diff changeset
   727
Usage: isabelle phabricator_setup_ssh [OPTIONS]
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
  Options are:
8c1c717a830b configure SSH hosting via "isabelle phabricator_setup_ssh";
wenzelm
parents: 71302
diff changeset
   730
    -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
   731
    -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
   732
    -T           test the ssh service for each Phabricator installation
8c1c717a830b configure SSH hosting via "isabelle phabricator_setup_ssh";
wenzelm
parents: 71302
diff changeset
   733
8c1c717a830b configure SSH hosting via "isabelle phabricator_setup_ssh";
wenzelm
parents: 71302
diff changeset
   734
  Configure ssh service for all Phabricator installations: a separate sshd
8c1c717a830b configure SSH hosting via "isabelle phabricator_setup_ssh";
wenzelm
parents: 71302
diff changeset
   735
  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
   736
  be distinct.
8c1c717a830b configure SSH hosting via "isabelle phabricator_setup_ssh";
wenzelm
parents: 71302
diff changeset
   737
8c1c717a830b configure SSH hosting via "isabelle phabricator_setup_ssh";
wenzelm
parents: 71302
diff changeset
   738
  A particular Phabricator installation is addressed by using its
8c1c717a830b configure SSH hosting via "isabelle phabricator_setup_ssh";
wenzelm
parents: 71302
diff changeset
   739
  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
   740
  stored ssh keys.
8c1c717a830b configure SSH hosting via "isabelle phabricator_setup_ssh";
wenzelm
parents: 71302
diff changeset
   741
""",
8c1c717a830b configure SSH hosting via "isabelle phabricator_setup_ssh";
wenzelm
parents: 71302
diff changeset
   742
          "p:" -> (arg => server_port = Value.Int.parse(arg)),
8c1c717a830b configure SSH hosting via "isabelle phabricator_setup_ssh";
wenzelm
parents: 71302
diff changeset
   743
          "q:" -> (arg => system_port = Value.Int.parse(arg)),
8c1c717a830b configure SSH hosting via "isabelle phabricator_setup_ssh";
wenzelm
parents: 71302
diff changeset
   744
          "T" -> (_ => test_server = true))
8c1c717a830b configure SSH hosting via "isabelle phabricator_setup_ssh";
wenzelm
parents: 71302
diff changeset
   745
8c1c717a830b configure SSH hosting via "isabelle phabricator_setup_ssh";
wenzelm
parents: 71302
diff changeset
   746
      val more_args = getopts(args)
8c1c717a830b configure SSH hosting via "isabelle phabricator_setup_ssh";
wenzelm
parents: 71302
diff changeset
   747
      if (more_args.nonEmpty) getopts.usage()
8c1c717a830b configure SSH hosting via "isabelle phabricator_setup_ssh";
wenzelm
parents: 71302
diff changeset
   748
8c1c717a830b configure SSH hosting via "isabelle phabricator_setup_ssh";
wenzelm
parents: 71302
diff changeset
   749
      val progress = new Console_Progress
8c1c717a830b configure SSH hosting via "isabelle phabricator_setup_ssh";
wenzelm
parents: 71302
diff changeset
   750
8c1c717a830b configure SSH hosting via "isabelle phabricator_setup_ssh";
wenzelm
parents: 71302
diff changeset
   751
      phabricator_setup_ssh(
8c1c717a830b configure SSH hosting via "isabelle phabricator_setup_ssh";
wenzelm
parents: 71302
diff changeset
   752
        server_port = server_port, system_port = system_port, test_server = test_server,
8c1c717a830b configure SSH hosting via "isabelle phabricator_setup_ssh";
wenzelm
parents: 71302
diff changeset
   753
        progress = progress)
8c1c717a830b configure SSH hosting via "isabelle phabricator_setup_ssh";
wenzelm
parents: 71302
diff changeset
   754
    })
71164
79736ffe77c3 some support for Phabricator server;
wenzelm
parents:
diff changeset
   755
}