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