| author | wenzelm | 
| Tue, 17 Dec 2019 13:35:03 +0100 | |
| changeset 71295 | 6aadbd650280 | 
| parent 71292 | 8b745b4d71b5 | 
| child 71299 | 51c19a44cfed | 
| 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 | ||
| 70969 | 14 | import scala.util.matching.Regex | 
| 15 | ||
| 16 | ||
| 70967 | 17 | object Phabricator | 
| 18 | {
 | |
| 19 | /** defaults **/ | |
| 20 | ||
| 71049 | 21 | /* required packages */ | 
| 22 | ||
| 23 | val packages: List[String] = | |
| 71265 | 24 | Build_Docker.packages ::: Linux.packages ::: | 
| 71049 | 25 | List( | 
| 26 | // https://secure.phabricator.com/source/phabricator/browse/master/scripts/install/install_ubuntu.sh 15e6e2adea61 | |
| 27 | "git", "mysql-server", "apache2", "libapache2-mod-php", "php", "php-mysql", | |
| 28 | "php-gd", "php-curl", "php-apcu", "php-cli", "php-json", "php-mbstring", | |
| 29 | // more packages | |
| 71288 | 30 | "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 | 31 | // 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 | 32 | "make", "gcc", "python", "python-dev", "python-docutils", "python-pygments", "python-openssl") | 
| 71049 | 33 | |
| 34 | ||
| 35 | /* global system resources */ | |
| 36 | ||
| 71068 
510b89906d86
discontinued somewhat pointless Isabelle options: setup implicitly assumes Ubuntu 18.04;
 wenzelm parents: 
71066diff
changeset | 37 | val www_user = "www-data" | 
| 
510b89906d86
discontinued somewhat pointless Isabelle options: setup implicitly assumes Ubuntu 18.04;
 wenzelm parents: 
71066diff
changeset | 38 | |
| 71049 | 39 | val daemon_user = "phabricator" | 
| 40 | ||
| 71109 
8c1c717a830b
configure SSH hosting via "isabelle phabricator_setup_ssh";
 wenzelm parents: 
71103diff
changeset | 41 |   val sshd_config = Path.explode("/etc/ssh/sshd_config")
 | 
| 71049 | 42 | |
| 43 | ||
| 44 | /* installation parameters */ | |
| 45 | ||
| 70967 | 46 | val default_name = "vcs" | 
| 47 | ||
| 71052 
6bf53035baf0
clarified name prefixes: global config always uses "isabelle-phabricator";
 wenzelm parents: 
71051diff
changeset | 48 | def phabricator_name(name: String = "", ext: String = ""): String = | 
| 
6bf53035baf0
clarified name prefixes: global config always uses "isabelle-phabricator";
 wenzelm parents: 
71051diff
changeset | 49 | "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 | 50 | |
| 
6bf53035baf0
clarified name prefixes: global config always uses "isabelle-phabricator";
 wenzelm parents: 
71051diff
changeset | 51 | def isabelle_phabricator_name(name: String = "", ext: String = ""): String = | 
| 
6bf53035baf0
clarified name prefixes: global config always uses "isabelle-phabricator";
 wenzelm parents: 
71051diff
changeset | 52 | "isabelle-" + phabricator_name(name = name, ext = ext) | 
| 70967 | 53 | |
| 71068 
510b89906d86
discontinued somewhat pointless Isabelle options: setup implicitly assumes Ubuntu 18.04;
 wenzelm parents: 
71066diff
changeset | 54 | def default_root(name: String): Path = | 
| 
510b89906d86
discontinued somewhat pointless Isabelle options: setup implicitly assumes Ubuntu 18.04;
 wenzelm parents: 
71066diff
changeset | 55 |     Path.explode("/var/www") + Path.basic(phabricator_name(name = name))
 | 
| 70967 | 56 | |
| 71068 
510b89906d86
discontinued somewhat pointless Isabelle options: setup implicitly assumes Ubuntu 18.04;
 wenzelm parents: 
71066diff
changeset | 57 |   def default_repo(name: String): Path = default_root(name) + Path.basic("repo")
 | 
| 70967 | 58 | |
| 71072 | 59 |   val default_mailers: Path = Path.explode("mailers.json")
 | 
| 71066 | 60 | |
| 71109 
8c1c717a830b
configure SSH hosting via "isabelle phabricator_setup_ssh";
 wenzelm parents: 
71103diff
changeset | 61 | val default_system_port = 22 | 
| 
8c1c717a830b
configure SSH hosting via "isabelle phabricator_setup_ssh";
 wenzelm parents: 
71103diff
changeset | 62 | val alternative_system_port = 222 | 
| 
8c1c717a830b
configure SSH hosting via "isabelle phabricator_setup_ssh";
 wenzelm parents: 
71103diff
changeset | 63 | val default_server_port = 2222 | 
| 
8c1c717a830b
configure SSH hosting via "isabelle phabricator_setup_ssh";
 wenzelm parents: 
71103diff
changeset | 64 | |
| 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 | 65 | val standard_mercurial_source = "https://www.mercurial-scm.org/release/mercurial-5.2.1.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 | 66 | |
| 70967 | 67 | |
| 68 | ||
| 69 | /** global configuration **/ | |
| 70 | ||
| 71052 
6bf53035baf0
clarified name prefixes: global config always uses "isabelle-phabricator";
 wenzelm parents: 
71051diff
changeset | 71 |   val global_config = Path.explode("/etc/" + isabelle_phabricator_name(ext = "conf"))
 | 
| 70967 | 72 | |
| 71122 | 73 | def global_config_script( | 
| 74 | init: String = "", | |
| 75 | body: String = "", | |
| 76 | exit: String = ""): String = | |
| 77 |   {
 | |
| 71282 | 78 | """#!/bin/bash | 
| 71284 | 79 | """ + (if (init.nonEmpty) "\n" + init else "") + """ | 
| 80 | {
 | |
| 71122 | 81 |   while { unset REPLY; read -r; test "$?" = 0 -o -n "$REPLY"; }
 | 
| 82 | do | |
| 83 | NAME="$(echo "$REPLY" | cut -d: -f1)" | |
| 84 | ROOT="$(echo "$REPLY" | cut -d: -f2)" | |
| 71284 | 85 |     {
 | 
| 86 | """ + Library.prefix_lines("      ", body) + """
 | |
| 87 | } < /dev/null | |
| 88 | done | |
| 89 | } < """ + File.bash_path(global_config) + "\n" + | |
| 90 | (if (exit.nonEmpty) "\n" + exit + "\n" else "") | |
| 71122 | 91 | } | 
| 92 | ||
| 70967 | 93 | sealed case class Config(name: String, root: Path) | 
| 70968 | 94 |   {
 | 
| 71052 
6bf53035baf0
clarified name prefixes: global config always uses "isabelle-phabricator";
 wenzelm parents: 
71051diff
changeset | 95 | def home: Path = root + Path.explode(phabricator_name()) | 
| 70969 | 96 | |
| 97 | def execute(command: String): Process_Result = | |
| 71102 | 98 |       Isabelle_System.bash("bin/" + command, cwd = home.file, redirect = true).check
 | 
| 70968 | 99 | } | 
| 70967 | 100 | |
| 101 | def read_config(): List[Config] = | |
| 102 |   {
 | |
| 103 |     if (global_config.is_file) {
 | |
| 104 | for (entry <- Library.trim_split_lines(File.read(global_config)) if entry.nonEmpty) | |
| 105 |       yield {
 | |
| 106 |         space_explode(':', entry) match {
 | |
| 107 | case List(name, root) => Config(name, Path.explode(root)) | |
| 108 |           case _ => error("Malformed config file " + global_config + "\nentry " + quote(entry))
 | |
| 109 | } | |
| 110 | } | |
| 111 | } | |
| 112 | else Nil | |
| 113 | } | |
| 114 | ||
| 115 | def write_config(configs: List[Config]) | |
| 116 |   {
 | |
| 117 | File.write(global_config, | |
| 118 |       configs.map(config => config.name + ":" + config.root.implode).mkString("", "\n", "\n"))
 | |
| 119 | } | |
| 120 | ||
| 121 | def get_config(name: String): Config = | |
| 122 | read_config().find(config => config.name == name) getOrElse | |
| 123 |       error("Bad Isabelle/Phabricator installation " + quote(name))
 | |
| 124 | ||
| 125 | ||
| 126 | ||
| 71097 | 127 | /** command-line tools **/ | 
| 128 | ||
| 129 | /* Isabelle tool wrapper */ | |
| 130 | ||
| 131 | val isabelle_tool1 = | |
| 132 |     Isabelle_Tool("phabricator", "invoke command-line tool within Phabricator home directory", args =>
 | |
| 133 |     {
 | |
| 71101 | 134 | var list = false | 
| 71097 | 135 | var name = default_name | 
| 136 | ||
| 137 | val getopts = | |
| 138 |         Getopts("""
 | |
| 139 | Usage: isabelle phabricator [OPTIONS] COMMAND [ARGS...] | |
| 140 | ||
| 141 | Options are: | |
| 71101 | 142 | -l list available Phabricator installations | 
| 71097 | 143 | -n NAME Phabricator installation name (default: """ + quote(default_name) + """) | 
| 144 | ||
| 71103 | 145 | Invoke a command-line tool within the home directory of the named | 
| 146 | Phabricator installation. | |
| 71097 | 147 | """, | 
| 71101 | 148 | "l" -> (_ => list = true), | 
| 71097 | 149 | "n:" -> (arg => name = arg)) | 
| 150 | ||
| 151 | val more_args = getopts(args) | |
| 71101 | 152 | if (more_args.isEmpty && !list) getopts.usage() | 
| 71097 | 153 | |
| 154 | val progress = new Console_Progress | |
| 155 | ||
| 71101 | 156 |       if (list) {
 | 
| 157 |         for (config <- read_config()) {
 | |
| 71103 | 158 |           progress.echo("phabricator " + quote(config.name) + " root " + config.root)
 | 
| 71101 | 159 | } | 
| 160 | } | |
| 71276 
b4401dfd6544
clarified "isabelle phabricator -l": avoid surprise with non-existent default installation;
 wenzelm parents: 
71274diff
changeset | 161 |       else {
 | 
| 
b4401dfd6544
clarified "isabelle phabricator -l": avoid surprise with non-existent default installation;
 wenzelm parents: 
71274diff
changeset | 162 | val config = get_config(name) | 
| 
b4401dfd6544
clarified "isabelle phabricator -l": avoid surprise with non-existent default installation;
 wenzelm parents: 
71274diff
changeset | 163 | 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: 
71274diff
changeset | 164 |         if (!result.ok) error("Return code: " + result.rc.toString)
 | 
| 
b4401dfd6544
clarified "isabelle phabricator -l": avoid surprise with non-existent default installation;
 wenzelm parents: 
71274diff
changeset | 165 | } | 
| 71097 | 166 | }) | 
| 167 | ||
| 168 | ||
| 169 | ||
| 70967 | 170 | /** setup **/ | 
| 171 | ||
| 71049 | 172 | def user_setup(name: String, description: String, ssh_setup: Boolean = false) | 
| 173 |   {
 | |
| 174 |     if (!Linux.user_exists(name)) {
 | |
| 71054 
b64fc38327ae
prefer system user setup, e.g. avoid occurrence on login screen;
 wenzelm parents: 
71053diff
changeset | 175 | Linux.user_add(name, description = description, system = true, ssh_setup = ssh_setup) | 
| 71049 | 176 | } | 
| 177 |     else if (Linux.user_description(name) != description) {
 | |
| 178 |       error("User " + quote(name) + " already exists --" +
 | |
| 179 | " for Phabricator it should have the description:\n " + quote(description)) | |
| 180 | } | |
| 181 | } | |
| 182 | ||
| 71282 | 183 | def command_setup(name: String, | 
| 184 | init: String = "", | |
| 185 | body: String = "", | |
| 186 | exit: String = ""): Path = | |
| 71270 | 187 |   {
 | 
| 188 |     val command = Path.explode("/usr/local/bin") + Path.basic(name)
 | |
| 71282 | 189 | File.write(command, global_config_script(init = init, body = body, exit = exit)) | 
| 71270 | 190 |     Isabelle_System.chmod("755", command)
 | 
| 191 |     Isabelle_System.chown("root:root", command)
 | |
| 192 | command | |
| 193 | } | |
| 194 | ||
| 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 | 195 | def mercurial_setup(mercurial_source: String, progress: Progress = No_Progress) | 
| 
5a2033fc8f3d
avoid odd (harmless) problem with Mercurial 4.5.3 provided by Ubuntu 18.04 on first push: "couldn't write revision branch cache names";
 wenzelm parents: 
71277diff
changeset | 196 |   {
 | 
| 71281 | 197 |     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 | 198 |     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 | 199 |     {
 | 
| 
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 | 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 | 201 |         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 | 202 |           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 | 203 | 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 | 204 | 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 | 205 | } | 
| 
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 | 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 | 207 | |
| 
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 |       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 | 209 | |
| 
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 |       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 | 211 | 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 | 212 | 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 | 213 |           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 | 214 | 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 | 215 |           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 | 216 | (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 | 217 | } | 
| 
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 | |
| 70967 | 221 | def phabricator_setup( | 
| 222 | name: String = default_name, | |
| 223 | root: String = "", | |
| 224 | repo: String = "", | |
| 71047 | 225 | 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 | 226 | mercurial_source: String = "", | 
| 70967 | 227 | progress: Progress = No_Progress) | 
| 228 |   {
 | |
| 229 | /* system environment */ | |
| 230 | ||
| 231 | Linux.check_system_root() | |
| 232 | ||
| 71079 | 233 |     progress.echo("System packages ...")
 | 
| 234 | ||
| 71047 | 235 |     if (package_update) {
 | 
| 236 | Linux.package_update(progress = progress) | |
| 237 | Linux.check_reboot_required() | |
| 238 | } | |
| 70967 | 239 | |
| 240 | Linux.package_install(packages, progress = progress) | |
| 241 | Linux.check_reboot_required() | |
| 242 | ||
| 243 | ||
| 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 | 244 |     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 | 245 |       for { name <- List("mercurial", "mercurial-common") if Linux.package_installed(name) } {
 | 
| 
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 |         error("Cannot install Mercurial from 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 | 247 | "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 | 248 | } | 
| 
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 | 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 | 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 | |
| 
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 | |
| 71049 | 253 | /* users */ | 
| 254 | ||
| 71125 | 255 | if (name.contains((c: Char) => !(Symbol.is_ascii_letter(c) || Symbol.is_ascii_digit(c))) || | 
| 71269 | 256 |         Set("", "ssh", "phd", "dump", daemon_user).contains(name)) {
 | 
| 71125 | 257 |       error("Bad installation name: " + quote(name))
 | 
| 71049 | 258 | } | 
| 259 | ||
| 260 | user_setup(daemon_user, "Phabricator Daemon User", ssh_setup = true) | |
| 261 | user_setup(name, "Phabricator SSH User") | |
| 262 | ||
| 263 | ||
| 70967 | 264 | /* basic installation */ | 
| 265 | ||
| 71079 | 266 |     progress.echo("\nPhabricator installation ...")
 | 
| 71076 | 267 | |
| 71068 
510b89906d86
discontinued somewhat pointless Isabelle options: setup implicitly assumes Ubuntu 18.04;
 wenzelm parents: 
71066diff
changeset | 268 | 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 | 269 | val repo_path = if (repo.nonEmpty) Path.explode(repo) else default_repo(name) | 
| 70967 | 270 | |
| 271 | val configs = read_config() | |
| 272 | ||
| 273 |     for (config <- configs if config.name == name) {
 | |
| 274 |       error("Duplicate Phabricator installation " + quote(name) + " in " + config.root)
 | |
| 275 | } | |
| 276 | ||
| 277 |     if (!Isabelle_System.bash("mkdir -p " + File.bash_path(root_path)).ok) {
 | |
| 278 |       error("Failed to create root directory " + root_path)
 | |
| 279 | } | |
| 280 | ||
| 71116 | 281 | Isabelle_System.chown(Bash.string(www_user) + ":" + Bash.string(www_user), root_path) | 
| 282 |     Isabelle_System.chmod("755", root_path)
 | |
| 283 | ||
| 70967 | 284 | progress.bash(cwd = root_path.file, echo = true, | 
| 285 | script = """ | |
| 286 | set -e | |
| 71126 | 287 | echo "Cloning distribution repositories:" | 
| 71287 
71fd25a7bbe2
more robust setup: avoid blind shot at "the latest" version;
 wenzelm parents: 
71285diff
changeset | 288 | |
| 71283 | 289 | git clone --branch stable https://github.com/phacility/libphutil.git | 
| 71287 
71fd25a7bbe2
more robust setup: avoid blind shot at "the latest" version;
 wenzelm parents: 
71285diff
changeset | 290 | git -C libphutil reset --hard 1750586fdc50a6cd98adba4aa2f5a7649bd91dbe | 
| 
71fd25a7bbe2
more robust setup: avoid blind shot at "the latest" version;
 wenzelm parents: 
71285diff
changeset | 291 | |
| 71283 | 292 | git clone --branch stable https://github.com/phacility/arcanist.git | 
| 71287 
71fd25a7bbe2
more robust setup: avoid blind shot at "the latest" version;
 wenzelm parents: 
71285diff
changeset | 293 | git -C arcanist reset --hard bac2028421a4be6e34e08764bbbda49e68b3a604 | 
| 
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 | 
| 71287 
71fd25a7bbe2
more robust setup: avoid blind shot at "the latest" version;
 wenzelm parents: 
71285diff
changeset | 296 | git -C phabricator reset --hard c4b4a53cad7722f031b725f8b41511e9d341d033 | 
| 70967 | 297 | """).check | 
| 298 | ||
| 299 | val config = Config(name, root_path) | |
| 300 | write_config(configs ::: List(config)) | |
| 70968 | 301 | |
| 71051 | 302 |     config.execute("config set pygments.enabled true")
 | 
| 303 | ||
| 70968 | 304 | |
| 71050 | 305 | /* local repository directory */ | 
| 306 | ||
| 71109 
8c1c717a830b
configure SSH hosting via "isabelle phabricator_setup_ssh";
 wenzelm parents: 
71103diff
changeset | 307 |     progress.echo("\nRepository hosting setup ...")
 | 
| 
8c1c717a830b
configure SSH hosting via "isabelle phabricator_setup_ssh";
 wenzelm parents: 
71103diff
changeset | 308 | |
| 71050 | 309 |     if (!Isabelle_System.bash("mkdir -p " + File.bash_path(repo_path)).ok) {
 | 
| 310 |       error("Failed to create local repository directory " + repo_path)
 | |
| 311 | } | |
| 312 | ||
| 71114 | 313 | Isabelle_System.chown( | 
| 314 | "-R " + Bash.string(daemon_user) + ":" + Bash.string(daemon_user), repo_path) | |
| 315 |     Isabelle_System.chmod("755", repo_path)
 | |
| 71050 | 316 | |
| 317 |     config.execute("config set repository.default-local-path " + File.bash_path(repo_path))
 | |
| 318 | ||
| 319 | ||
| 71277 | 320 | val sudoers_file = | 
| 321 |       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 | 322 | 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 | 323 |       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 | 324 |       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 | 325 | |
| 71115 | 326 |     Isabelle_System.chmod("440", sudoers_file)
 | 
| 71109 
8c1c717a830b
configure SSH hosting via "isabelle phabricator_setup_ssh";
 wenzelm parents: 
71103diff
changeset | 327 | |
| 
8c1c717a830b
configure SSH hosting via "isabelle phabricator_setup_ssh";
 wenzelm parents: 
71103diff
changeset | 328 |     config.execute("config set diffusion.ssh-user " + Bash.string(config.name))
 | 
| 
8c1c717a830b
configure SSH hosting via "isabelle phabricator_setup_ssh";
 wenzelm parents: 
71103diff
changeset | 329 | |
| 
8c1c717a830b
configure SSH hosting via "isabelle phabricator_setup_ssh";
 wenzelm parents: 
71103diff
changeset | 330 | |
| 70969 | 331 | /* MySQL setup */ | 
| 332 | ||
| 71079 | 333 |     progress.echo("\nMySQL setup ...")
 | 
| 70969 | 334 | |
| 71055 
27a998cdc0f4
back to plain name, to have it accepted my mysql;
 wenzelm parents: 
71054diff
changeset | 335 |     File.write(Path.explode("/etc/mysql/mysql.conf.d/" + phabricator_name(ext = "cnf")),
 | 
| 71051 | 336 | """[mysqld] | 
| 337 | max_allowed_packet = 32M | |
| 338 | innodb_buffer_pool_size = 1600M | |
| 339 | local_infile = 0 | |
| 340 | """) | |
| 341 | ||
| 342 |     Linux.service_restart("mysql")
 | |
| 343 | ||
| 344 | ||
| 71266 
8451c86ffa85
proper mysql user setup: avoid superuser powers in production;
 wenzelm parents: 
71265diff
changeset | 345 | def mysql_conf(R: Regex, which: String): String = | 
| 
8451c86ffa85
proper mysql user setup: avoid superuser powers in production;
 wenzelm parents: 
71265diff
changeset | 346 |     {
 | 
| 
8451c86ffa85
proper mysql user setup: avoid superuser powers in production;
 wenzelm parents: 
71265diff
changeset | 347 |       val conf = Path.explode("/etc/mysql/debian.cnf")
 | 
| 
8451c86ffa85
proper mysql user setup: avoid superuser powers in production;
 wenzelm parents: 
71265diff
changeset | 348 |       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 | 349 | case Some(res) => res | 
| 
8451c86ffa85
proper mysql user setup: avoid superuser powers in production;
 wenzelm parents: 
71265diff
changeset | 350 |         case None => error("Cannot determine " + which + " from " + conf)
 | 
| 
8451c86ffa85
proper mysql user setup: avoid superuser powers in production;
 wenzelm parents: 
71265diff
changeset | 351 | } | 
| 70969 | 352 | } | 
| 353 | ||
| 71266 
8451c86ffa85
proper mysql user setup: avoid superuser powers in production;
 wenzelm parents: 
71265diff
changeset | 354 |     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 | 355 |     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 | 356 | |
| 
8451c86ffa85
proper mysql user setup: avoid superuser powers in production;
 wenzelm parents: 
71265diff
changeset | 357 |     val mysql_name = phabricator_name(name = name).replace("-", "_")
 | 
| 
8451c86ffa85
proper mysql user setup: avoid superuser powers in production;
 wenzelm parents: 
71265diff
changeset | 358 | val mysql_user_string = SQL.string(mysql_name) + "@'localhost'" | 
| 
8451c86ffa85
proper mysql user setup: avoid superuser powers in production;
 wenzelm parents: 
71265diff
changeset | 359 | val mysql_password = Linux.generate_password() | 
| 70969 | 360 | |
| 71266 
8451c86ffa85
proper mysql user setup: avoid superuser powers in production;
 wenzelm parents: 
71265diff
changeset | 361 |     Isabelle_System.bash("mysql --user=" + Bash.string(mysql_root_user) +
 | 
| 
8451c86ffa85
proper mysql user setup: avoid superuser powers in production;
 wenzelm parents: 
71265diff
changeset | 362 | " --password=" + Bash.string(mysql_root_password) + " --execute=" + | 
| 
8451c86ffa85
proper mysql user setup: avoid superuser powers in production;
 wenzelm parents: 
71265diff
changeset | 363 | Bash.string( | 
| 71274 | 364 | """DROP USER IF EXISTS """ + mysql_user_string + "; " + | 
| 365 | """CREATE USER """ + mysql_user_string + | |
| 71266 
8451c86ffa85
proper mysql user setup: avoid superuser powers in production;
 wenzelm parents: 
71265diff
changeset | 366 | """ IDENTIFIED BY """ + SQL.string(mysql_password) + """ PASSWORD EXPIRE NEVER; """ + | 
| 
8451c86ffa85
proper mysql user setup: avoid superuser powers in production;
 wenzelm parents: 
71265diff
changeset | 367 |         """GRANT ALL ON `""" + (mysql_name + "_%").replace("_", "\\_") +
 | 
| 
8451c86ffa85
proper mysql user setup: avoid superuser powers in production;
 wenzelm parents: 
71265diff
changeset | 368 | """`.* TO """ + mysql_user_string + ";")).check | 
| 70969 | 369 | |
| 71266 
8451c86ffa85
proper mysql user setup: avoid superuser powers in production;
 wenzelm parents: 
71265diff
changeset | 370 |     config.execute("config set mysql.user " + Bash.string(mysql_name))
 | 
| 
8451c86ffa85
proper mysql user setup: avoid superuser powers in production;
 wenzelm parents: 
71265diff
changeset | 371 |     config.execute("config set mysql.pass " + Bash.string(mysql_password))
 | 
| 
8451c86ffa85
proper mysql user setup: avoid superuser powers in production;
 wenzelm parents: 
71265diff
changeset | 372 | |
| 
8451c86ffa85
proper mysql user setup: avoid superuser powers in production;
 wenzelm parents: 
71265diff
changeset | 373 |     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 | 374 |     config.execute("config set storage.default-namespace " + Bash.string(mysql_name))
 | 
| 71051 | 375 |     config.execute("config set storage.mysql-engine.max-size 8388608")
 | 
| 376 | ||
| 71102 | 377 |     progress.bash("bin/storage upgrade --force", cwd = config.home.file, echo = true).check
 | 
| 70969 | 378 | |
| 379 | ||
| 71269 | 380 | /* database dump */ | 
| 381 | ||
| 382 | val dump_name = isabelle_phabricator_name(name = "dump") | |
| 71282 | 383 | command_setup(dump_name, body = | 
| 71269 | 384 | """mkdir -p "$ROOT/database" && chown root:root "$ROOT/database" && chmod 700 "$ROOT/database" | 
| 385 | [ -e "$ROOT/database/dump.sql.gz" ] && mv -f "$ROOT/database/dump.sql.gz" "$ROOT/database/dump-old.sql.gz" | |
| 386 | echo "Creating $ROOT/database/dump.sql.gz" | |
| 71270 | 387 | "$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' """) | 
| 71269 | 388 | |
| 389 | ||
| 71283 | 390 | /* Phabricator upgrade */ | 
| 391 | ||
| 392 | command_setup(isabelle_phabricator_name(name = "upgrade"), | |
| 393 | init = | |
| 71285 | 394 | """BRANCH="${1:-stable}"
 | 
| 71283 | 395 | if [ "$BRANCH" != "master" -a "$BRANCH" != "stable" ] | 
| 396 | then | |
| 397 | echo "Bad branch: \"$BRANCH\"" | |
| 398 | exit 1 | |
| 399 | fi | |
| 400 | ||
| 401 | systemctl stop isabelle-phabricator-phd | |
| 402 | systemctl stop apache2 | |
| 403 | """, | |
| 404 | body = | |
| 405 | """echo -e "\nUpgrading phabricator \"$NAME\" root \"$ROOT\" ..." | |
| 406 | for REPO in libphutil arcanist phabricator | |
| 407 | do | |
| 408 | cd "$ROOT/$REPO" | |
| 409 | echo -e "\nUpdating \"$REPO\" ..." | |
| 410 | git checkout "$BRANCH" | |
| 411 | git pull | |
| 412 | done | |
| 413 | echo -e "\nUpgrading storage ..." | |
| 414 | "$ROOT/phabricator/bin/storage" upgrade --force | |
| 415 | """, | |
| 416 | exit = | |
| 417 | """systemctl start apache2 | |
| 418 | systemctl start isabelle-phabricator-phd""") | |
| 419 | ||
| 420 | ||
| 71051 | 421 | /* PHP setup */ | 
| 422 | ||
| 423 | val php_version = | |
| 424 |       Isabelle_System.bash("""php --run 'echo PHP_MAJOR_VERSION . "." . PHP_MINOR_VERSION;'""")
 | |
| 425 | .check.out | |
| 426 | ||
| 427 | val php_conf = | |
| 428 |       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 | 429 |         Path.explode("apache2/conf.d") +
 | 
| 
6bf53035baf0
clarified name prefixes: global config always uses "isabelle-phabricator";
 wenzelm parents: 
71051diff
changeset | 430 | Path.basic(isabelle_phabricator_name(ext = "ini")) | 
| 71051 | 431 | |
| 432 | File.write(php_conf, | |
| 433 | "post_max_size = 32M\n" + | |
| 434 | "opcache.validate_timestamps = 0\n" + | |
| 71129 | 435 | "memory_limit = 512M\n" + | 
| 436 | "max_execution_time = 120\n") | |
| 71051 | 437 | |
| 438 | ||
| 70968 | 439 | /* Apache setup */ | 
| 440 | ||
| 71079 | 441 |     progress.echo("Apache setup ...")
 | 
| 70968 | 442 | |
| 71068 
510b89906d86
discontinued somewhat pointless Isabelle options: setup implicitly assumes Ubuntu 18.04;
 wenzelm parents: 
71066diff
changeset | 443 |     val apache_root = Path.explode("/etc/apache2")
 | 
| 70968 | 444 |     val apache_sites = apache_root + Path.explode("sites-available")
 | 
| 445 | ||
| 446 |     if (!apache_sites.is_dir) error("Bad Apache sites directory " + apache_sites)
 | |
| 447 | ||
| 71058 | 448 | 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 | 449 | val server_url = "http://" + server_name | 
| 
6bf53035baf0
clarified name prefixes: global config always uses "isabelle-phabricator";
 wenzelm parents: 
71051diff
changeset | 450 | |
| 71058 | 451 | File.write(apache_sites + Path.basic(isabelle_phabricator_name(name = name, ext = "conf")), | 
| 70968 | 452 | """<VirtualHost *:80> | 
| 71052 
6bf53035baf0
clarified name prefixes: global config always uses "isabelle-phabricator";
 wenzelm parents: 
71051diff
changeset | 453 | ServerName """ + server_name + """ | 
| 70968 | 454 | ServerAdmin webmaster@localhost | 
| 70969 | 455 | DocumentRoot """ + config.home.implode + """/webroot | 
| 70968 | 456 | |
| 457 |     ErrorLog ${APACHE_LOG_DIR}/error.log
 | |
| 458 | RewriteEngine on | |
| 459 | RewriteRule ^(.*)$ /index.php?__path__=$1 [B,L,QSA] | |
| 460 | </VirtualHost> | |
| 461 | ||
| 462 | # vim: syntax=apache ts=4 sw=4 sts=4 sr noet | |
| 463 | """) | |
| 464 | ||
| 71051 | 465 | Isabelle_System.bash( """ | 
| 70968 | 466 | set -e | 
| 467 | a2enmod rewrite | |
| 71058 | 468 | a2ensite """ + Bash.string(isabelle_phabricator_name(name = name))).check | 
| 71051 | 469 | |
| 71057 | 470 |     config.execute("config set phabricator.base-uri " + Bash.string(server_url))
 | 
| 471 | ||
| 71051 | 472 |     Linux.service_restart("apache2")
 | 
| 70968 | 473 | |
| 71128 
f79006c533b0
clarified errors: PHP daemon can fail under odd circumstances;
 wenzelm parents: 
71127diff
changeset | 474 |     progress.echo("\nWeb configuration via " + server_url)
 | 
| 
f79006c533b0
clarified errors: PHP daemon can fail under odd circumstances;
 wenzelm parents: 
71127diff
changeset | 475 | |
| 71053 | 476 | |
| 477 | /* PHP daemon */ | |
| 478 | ||
| 71128 
f79006c533b0
clarified errors: PHP daemon can fail under odd circumstances;
 wenzelm parents: 
71127diff
changeset | 479 |     progress.echo("\nPHP daemon setup ...")
 | 
| 71053 | 480 | |
| 71273 | 481 |     val phd_log_path = Path.explode("/var/tmp/phd")
 | 
| 482 | Isabelle_System.mkdirs(phd_log_path) | |
| 483 | Isabelle_System.chown( | |
| 484 | "-R " + Bash.string(daemon_user) + ":" + Bash.string(daemon_user), phd_log_path) | |
| 485 |     Isabelle_System.chmod("755", phd_log_path)
 | |
| 486 | ||
| 71053 | 487 |     config.execute("config set phd.user " + Bash.string(daemon_user))
 | 
| 71112 | 488 |     config.execute("config set phd.log-directory /var/tmp/phd/" +
 | 
| 489 | isabelle_phabricator_name(name = name) + "/log") | |
| 71053 | 490 | |
| 71124 
7dbadecdc118
just one isabelle-phabricator-phd service, which manages all processes uniformly (NB: "bin/phd stop" affects all installations);
 wenzelm parents: 
71122diff
changeset | 491 | val phd_name = isabelle_phabricator_name(name = "phd") | 
| 71127 | 492 | Linux.service_shutdown(phd_name) | 
| 71282 | 493 | 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 | 494 |     try {
 | 
| 
f79006c533b0
clarified errors: PHP daemon can fail under odd circumstances;
 wenzelm parents: 
71127diff
changeset | 495 | Linux.service_install(phd_name, | 
| 71053 | 496 | """[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 | 497 | Description=PHP daemon manager for Isabelle/Phabricator | 
| 71053 | 498 | After=syslog.target network.target apache2.service mysql.service | 
| 499 | ||
| 500 | [Service] | |
| 501 | Type=oneshot | |
| 502 | User=""" + daemon_user + """ | |
| 503 | Group=""" + daemon_user + """ | |
| 504 | 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 | 505 | 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 | 506 | ExecStop=""" + phd_command.implode + """ stop | 
| 71053 | 507 | RemainAfterExit=yes | 
| 508 | ||
| 509 | [Install] | |
| 510 | WantedBy=multi-user.target | |
| 511 | """) | |
| 71128 
f79006c533b0
clarified errors: PHP daemon can fail under odd circumstances;
 wenzelm parents: 
71127diff
changeset | 512 | } | 
| 
f79006c533b0
clarified errors: PHP daemon can fail under odd circumstances;
 wenzelm parents: 
71127diff
changeset | 513 |     catch {
 | 
| 
f79006c533b0
clarified errors: PHP daemon can fail under odd circumstances;
 wenzelm parents: 
71127diff
changeset | 514 | case ERROR(msg) => | 
| 
f79006c533b0
clarified errors: PHP daemon can fail under odd circumstances;
 wenzelm parents: 
71127diff
changeset | 515 |         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 | 516 | error(msg) | 
| 
f79006c533b0
clarified errors: PHP daemon can fail under odd circumstances;
 wenzelm parents: 
71127diff
changeset | 517 | } | 
| 70967 | 518 | } | 
| 519 | ||
| 520 | ||
| 521 | /* Isabelle tool wrapper */ | |
| 522 | ||
| 71097 | 523 | val isabelle_tool2 = | 
| 70967 | 524 |     Isabelle_Tool("phabricator_setup", "setup Phabricator server on Ubuntu Linux", args =>
 | 
| 525 |     {
 | |
| 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 | 526 | var mercurial_source = "" | 
| 71047 | 527 | var repo = "" | 
| 528 | var package_update = false | |
| 71078 | 529 | var name = default_name | 
| 70967 | 530 | var root = "" | 
| 531 | ||
| 532 | val getopts = | |
| 533 |         Getopts("""
 | |
| 71078 | 534 | Usage: isabelle phabricator_setup [OPTIONS] | 
| 70967 | 535 | |
| 536 | 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 | 537 | -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 | 538 | """ + standard_mercurial_source + """ | 
| 71068 
510b89906d86
discontinued somewhat pointless Isabelle options: setup implicitly assumes Ubuntu 18.04;
 wenzelm parents: 
71066diff
changeset | 539 |     -R DIR       repository directory (default: """ + default_repo("NAME") + """)
 | 
| 71047 | 540 | -U full update of system packages before installation | 
| 71078 | 541 | -n NAME Phabricator installation name (default: """ + quote(default_name) + """) | 
| 71068 
510b89906d86
discontinued somewhat pointless Isabelle options: setup implicitly assumes Ubuntu 18.04;
 wenzelm parents: 
71066diff
changeset | 542 |     -r DIR       installation root directory (default: """ + default_root("NAME") + """)
 | 
| 70967 | 543 | |
| 71103 | 544 | Install Phabricator as LAMP application (Linux, Apache, MySQL, PHP). | 
| 70967 | 545 | |
| 71078 | 546 | The installation name (default: """ + quote(default_name) + """) is mapped to a regular | 
| 547 | Unix user; this is relevant for public SSH access. | |
| 70967 | 548 | """, | 
| 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 | 549 | "M:" -> (arg => mercurial_source = (if (arg == ":") standard_mercurial_source else arg)), | 
| 70967 | 550 | "R:" -> (arg => repo = arg), | 
| 71047 | 551 | "U" -> (_ => package_update = true), | 
| 71078 | 552 | "n:" -> (arg => name = arg), | 
| 70967 | 553 | "r:" -> (arg => root = arg)) | 
| 554 | ||
| 555 | val more_args = getopts(args) | |
| 71078 | 556 | if (more_args.nonEmpty) getopts.usage() | 
| 70967 | 557 | |
| 558 | val progress = new Console_Progress | |
| 559 | ||
| 71268 | 560 | val release = Linux.Release() | 
| 561 |       if (!release.is_ubuntu_18_04) error("Bad Linux version: Ubuntu 18.04 LTS required")
 | |
| 562 | ||
| 71078 | 563 | phabricator_setup(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 | 564 | package_update = package_update, mercurial_source = mercurial_source, progress = progress) | 
| 70967 | 565 | }) | 
| 566 | ||
| 567 | ||
| 568 | ||
| 71066 | 569 | /** setup mail **/ | 
| 70967 | 570 | |
| 71072 | 571 | val mailers_template: String = | 
| 572 | """[ | |
| 573 |   {
 | |
| 574 | "key": "example.org", | |
| 575 | "type": "smtp", | |
| 576 |     "options": {
 | |
| 577 | "host": "mail.example.org", | |
| 578 | "port": 465, | |
| 579 | "user": "phabricator@example.org", | |
| 580 | "password": "********", | |
| 581 | "protocol": "ssl", | |
| 582 | "message-id": true | |
| 583 | } | |
| 584 | } | |
| 585 | ]""" | |
| 586 | ||
| 71066 | 587 | def phabricator_setup_mail( | 
| 588 | name: String = default_name, | |
| 589 | config_file: Option[Path] = None, | |
| 590 | test_user: String = "", | |
| 591 | progress: Progress = No_Progress) | |
| 70967 | 592 |   {
 | 
| 593 | Linux.check_system_root() | |
| 594 | ||
| 71066 | 595 | val config = get_config(name) | 
| 71073 | 596 | val default_config_file = config.root + default_mailers | 
| 71066 | 597 | |
| 598 | val mail_config = config_file getOrElse default_config_file | |
| 599 | ||
| 600 | def setup_mail | |
| 601 |     {
 | |
| 602 |       progress.echo("Using mail configuration from " + mail_config)
 | |
| 603 |       config.execute("config set cluster.mailers --stdin < " + File.bash_path(mail_config))
 | |
| 604 | ||
| 605 |       if (test_user.nonEmpty) {
 | |
| 606 |         progress.echo("Sending test mail to " + quote(test_user))
 | |
| 607 | progress.bash(cwd = config.home.file, echo = true, | |
| 71102 | 608 | script = """echo "Test from Phabricator ($(date))" | bin/mail send-test --subject "Test" --to """ + | 
| 71066 | 609 | Bash.string(test_user)).check | 
| 610 | } | |
| 611 | } | |
| 612 | ||
| 613 |     if (config_file.isEmpty) {
 | |
| 71070 | 614 |       if (!default_config_file.is_file) {
 | 
| 615 | File.write(default_config_file, mailers_template) | |
| 71114 | 616 |         Isabelle_System.chmod("600", default_config_file)
 | 
| 71070 | 617 | } | 
| 71066 | 618 |       if (File.read(default_config_file) == mailers_template) {
 | 
| 71131 | 619 |         progress.echo("Please invoke the tool again, after providing details in\n  " +
 | 
| 620 | default_config_file.implode + "\n") | |
| 71066 | 621 | } | 
| 622 | else setup_mail | |
| 623 | } | |
| 624 | else setup_mail | |
| 70967 | 625 | } | 
| 626 | ||
| 627 | ||
| 628 | /* Isabelle tool wrapper */ | |
| 629 | ||
| 71097 | 630 | val isabelle_tool3 = | 
| 71066 | 631 |     Isabelle_Tool("phabricator_setup_mail",
 | 
| 71109 
8c1c717a830b
configure SSH hosting via "isabelle phabricator_setup_ssh";
 wenzelm parents: 
71103diff
changeset | 632 | "setup mail for one Phabricator installation", args => | 
| 70967 | 633 |     {
 | 
| 71066 | 634 | var test_user = "" | 
| 635 | var name = default_name | |
| 636 | var config_file: Option[Path] = None | |
| 637 | ||
| 70967 | 638 | val getopts = | 
| 639 |         Getopts("""
 | |
| 71066 | 640 | Usage: isabelle phabricator_setup_mail [OPTIONS] | 
| 641 | ||
| 642 | Options are: | |
| 643 | -T USER send test mail to Phabricator user | |
| 71103 | 644 | -f FILE config file (default: """ + default_mailers + """ within Phabricator root) | 
| 71066 | 645 | -n NAME Phabricator installation name (default: """ + quote(default_name) + """) | 
| 70967 | 646 | |
| 71077 | 647 | Provide mail configuration for existing Phabricator installation. | 
| 71066 | 648 | """, | 
| 649 | "T:" -> (arg => test_user = arg), | |
| 650 | "f:" -> (arg => config_file = Some(Path.explode(arg))), | |
| 651 | "n:" -> (arg => name = arg)) | |
| 70967 | 652 | |
| 653 | val more_args = getopts(args) | |
| 71066 | 654 | if (more_args.nonEmpty) getopts.usage() | 
| 70967 | 655 | |
| 656 | val progress = new Console_Progress | |
| 657 | ||
| 71066 | 658 | phabricator_setup_mail(name = name, config_file = config_file, | 
| 659 | test_user = test_user, progress = progress) | |
| 70967 | 660 | }) | 
| 71109 
8c1c717a830b
configure SSH hosting via "isabelle phabricator_setup_ssh";
 wenzelm parents: 
71103diff
changeset | 661 | |
| 
8c1c717a830b
configure SSH hosting via "isabelle phabricator_setup_ssh";
 wenzelm parents: 
71103diff
changeset | 662 | |
| 
8c1c717a830b
configure SSH hosting via "isabelle phabricator_setup_ssh";
 wenzelm parents: 
71103diff
changeset | 663 | |
| 
8c1c717a830b
configure SSH hosting via "isabelle phabricator_setup_ssh";
 wenzelm parents: 
71103diff
changeset | 664 | /** setup ssh **/ | 
| 
8c1c717a830b
configure SSH hosting via "isabelle phabricator_setup_ssh";
 wenzelm parents: 
71103diff
changeset | 665 | |
| 
8c1c717a830b
configure SSH hosting via "isabelle phabricator_setup_ssh";
 wenzelm parents: 
71103diff
changeset | 666 | /* sshd config */ | 
| 
8c1c717a830b
configure SSH hosting via "isabelle phabricator_setup_ssh";
 wenzelm parents: 
71103diff
changeset | 667 | |
| 
8c1c717a830b
configure SSH hosting via "isabelle phabricator_setup_ssh";
 wenzelm parents: 
71103diff
changeset | 668 | private val Port = """^\s*Port\s+(\d+)\s*$""".r | 
| 
8c1c717a830b
configure SSH hosting via "isabelle phabricator_setup_ssh";
 wenzelm parents: 
71103diff
changeset | 669 | private val No_Port = """^#\s*Port\b.*$""".r | 
| 
8c1c717a830b
configure SSH hosting via "isabelle phabricator_setup_ssh";
 wenzelm parents: 
71103diff
changeset | 670 | private val Any_Port = """^#?\s*Port\b.*$""".r | 
| 
8c1c717a830b
configure SSH hosting via "isabelle phabricator_setup_ssh";
 wenzelm parents: 
71103diff
changeset | 671 | |
| 
8c1c717a830b
configure SSH hosting via "isabelle phabricator_setup_ssh";
 wenzelm parents: 
71103diff
changeset | 672 | def conf_ssh_port(port: Int): String = | 
| 
8c1c717a830b
configure SSH hosting via "isabelle phabricator_setup_ssh";
 wenzelm parents: 
71103diff
changeset | 673 | if (port == 22) "#Port 22" else "Port " + port | 
| 
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 | def read_ssh_port(conf: Path): Int = | 
| 
8c1c717a830b
configure SSH hosting via "isabelle phabricator_setup_ssh";
 wenzelm parents: 
71103diff
changeset | 676 |   {
 | 
| 
8c1c717a830b
configure SSH hosting via "isabelle phabricator_setup_ssh";
 wenzelm parents: 
71103diff
changeset | 677 | val lines = split_lines(File.read(conf)) | 
| 
8c1c717a830b
configure SSH hosting via "isabelle phabricator_setup_ssh";
 wenzelm parents: 
71103diff
changeset | 678 | val ports = | 
| 
8c1c717a830b
configure SSH hosting via "isabelle phabricator_setup_ssh";
 wenzelm parents: 
71103diff
changeset | 679 |       lines.flatMap({
 | 
| 
8c1c717a830b
configure SSH hosting via "isabelle phabricator_setup_ssh";
 wenzelm parents: 
71103diff
changeset | 680 | case Port(Value.Int(p)) => Some(p) | 
| 
8c1c717a830b
configure SSH hosting via "isabelle phabricator_setup_ssh";
 wenzelm parents: 
71103diff
changeset | 681 | case No_Port() => Some(22) | 
| 
8c1c717a830b
configure SSH hosting via "isabelle phabricator_setup_ssh";
 wenzelm parents: 
71103diff
changeset | 682 | case _ => None | 
| 
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 |     ports match {
 | 
| 
8c1c717a830b
configure SSH hosting via "isabelle phabricator_setup_ssh";
 wenzelm parents: 
71103diff
changeset | 685 | case List(port) => port | 
| 
8c1c717a830b
configure SSH hosting via "isabelle phabricator_setup_ssh";
 wenzelm parents: 
71103diff
changeset | 686 |       case Nil => error("Missing Port specification in " + conf)
 | 
| 
8c1c717a830b
configure SSH hosting via "isabelle phabricator_setup_ssh";
 wenzelm parents: 
71103diff
changeset | 687 |       case _ => error("Multiple Port specifications in " + conf)
 | 
| 
8c1c717a830b
configure SSH hosting via "isabelle phabricator_setup_ssh";
 wenzelm parents: 
71103diff
changeset | 688 | } | 
| 
8c1c717a830b
configure SSH hosting via "isabelle phabricator_setup_ssh";
 wenzelm parents: 
71103diff
changeset | 689 | } | 
| 
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 | def write_ssh_port(conf: Path, port: Int): Boolean = | 
| 
8c1c717a830b
configure SSH hosting via "isabelle phabricator_setup_ssh";
 wenzelm parents: 
71103diff
changeset | 692 |   {
 | 
| 
8c1c717a830b
configure SSH hosting via "isabelle phabricator_setup_ssh";
 wenzelm parents: 
71103diff
changeset | 693 | val old_port = read_ssh_port(conf) | 
| 
8c1c717a830b
configure SSH hosting via "isabelle phabricator_setup_ssh";
 wenzelm parents: 
71103diff
changeset | 694 | if (old_port == port) false | 
| 
8c1c717a830b
configure SSH hosting via "isabelle phabricator_setup_ssh";
 wenzelm parents: 
71103diff
changeset | 695 |     else {
 | 
| 
8c1c717a830b
configure SSH hosting via "isabelle phabricator_setup_ssh";
 wenzelm parents: 
71103diff
changeset | 696 | val lines = split_lines(File.read(conf)) | 
| 
8c1c717a830b
configure SSH hosting via "isabelle phabricator_setup_ssh";
 wenzelm parents: 
71103diff
changeset | 697 |       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 | 698 | File.write(conf, cat_lines(lines1)) | 
| 
8c1c717a830b
configure SSH hosting via "isabelle phabricator_setup_ssh";
 wenzelm parents: 
71103diff
changeset | 699 | true | 
| 
8c1c717a830b
configure SSH hosting via "isabelle phabricator_setup_ssh";
 wenzelm parents: 
71103diff
changeset | 700 | } | 
| 
8c1c717a830b
configure SSH hosting via "isabelle phabricator_setup_ssh";
 wenzelm parents: 
71103diff
changeset | 701 | } | 
| 
8c1c717a830b
configure SSH hosting via "isabelle phabricator_setup_ssh";
 wenzelm parents: 
71103diff
changeset | 702 | |
| 
8c1c717a830b
configure SSH hosting via "isabelle phabricator_setup_ssh";
 wenzelm parents: 
71103diff
changeset | 703 | |
| 
8c1c717a830b
configure SSH hosting via "isabelle phabricator_setup_ssh";
 wenzelm parents: 
71103diff
changeset | 704 | /* phabricator_setup_ssh */ | 
| 
8c1c717a830b
configure SSH hosting via "isabelle phabricator_setup_ssh";
 wenzelm parents: 
71103diff
changeset | 705 | |
| 
8c1c717a830b
configure SSH hosting via "isabelle phabricator_setup_ssh";
 wenzelm parents: 
71103diff
changeset | 706 | def phabricator_setup_ssh( | 
| 
8c1c717a830b
configure SSH hosting via "isabelle phabricator_setup_ssh";
 wenzelm parents: 
71103diff
changeset | 707 | server_port: Int = default_server_port, | 
| 
8c1c717a830b
configure SSH hosting via "isabelle phabricator_setup_ssh";
 wenzelm parents: 
71103diff
changeset | 708 | system_port: Int = default_system_port, | 
| 
8c1c717a830b
configure SSH hosting via "isabelle phabricator_setup_ssh";
 wenzelm parents: 
71103diff
changeset | 709 | progress: Progress = No_Progress) | 
| 
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 | Linux.check_system_root() | 
| 
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 | val configs = read_config() | 
| 
8c1c717a830b
configure SSH hosting via "isabelle phabricator_setup_ssh";
 wenzelm parents: 
71103diff
changeset | 714 | |
| 
8c1c717a830b
configure SSH hosting via "isabelle phabricator_setup_ssh";
 wenzelm parents: 
71103diff
changeset | 715 |     if (server_port == system_port) {
 | 
| 
8c1c717a830b
configure SSH hosting via "isabelle phabricator_setup_ssh";
 wenzelm parents: 
71103diff
changeset | 716 |       error("Port for Phabricator sshd coincides with system port: " + system_port)
 | 
| 
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 | |
| 
8c1c717a830b
configure SSH hosting via "isabelle phabricator_setup_ssh";
 wenzelm parents: 
71103diff
changeset | 719 |     val sshd_conf_system = Path.explode("/etc/ssh/sshd_config")
 | 
| 
8c1c717a830b
configure SSH hosting via "isabelle phabricator_setup_ssh";
 wenzelm parents: 
71103diff
changeset | 720 | val sshd_conf_server = sshd_conf_system.ext(isabelle_phabricator_name()) | 
| 
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 | 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 | 723 | Linux.service_shutdown(ssh_name) | 
| 
cd166c3904dd
more robust: system ssh service is required for Phabricator ssh service;
 wenzelm parents: 
71109diff
changeset | 724 | |
| 71109 
8c1c717a830b
configure SSH hosting via "isabelle phabricator_setup_ssh";
 wenzelm parents: 
71103diff
changeset | 725 | val old_system_port = read_ssh_port(sshd_conf_system) | 
| 
8c1c717a830b
configure SSH hosting via "isabelle phabricator_setup_ssh";
 wenzelm parents: 
71103diff
changeset | 726 |     if (old_system_port != system_port) {
 | 
| 
8c1c717a830b
configure SSH hosting via "isabelle phabricator_setup_ssh";
 wenzelm parents: 
71103diff
changeset | 727 |       progress.echo("Reconfigurig system ssh service")
 | 
| 71111 
cd166c3904dd
more robust: system ssh service is required for Phabricator ssh service;
 wenzelm parents: 
71109diff
changeset | 728 |       Linux.service_shutdown("ssh")
 | 
| 71109 
8c1c717a830b
configure SSH hosting via "isabelle phabricator_setup_ssh";
 wenzelm parents: 
71103diff
changeset | 729 | 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 | 730 |       Linux.service_start("ssh")
 | 
| 71109 
8c1c717a830b
configure SSH hosting via "isabelle phabricator_setup_ssh";
 wenzelm parents: 
71103diff
changeset | 731 | } | 
| 
8c1c717a830b
configure SSH hosting via "isabelle phabricator_setup_ssh";
 wenzelm parents: 
71103diff
changeset | 732 | |
| 
8c1c717a830b
configure SSH hosting via "isabelle phabricator_setup_ssh";
 wenzelm parents: 
71103diff
changeset | 733 |     progress.echo("Configuring " + ssh_name + " service")
 | 
| 
8c1c717a830b
configure SSH hosting via "isabelle phabricator_setup_ssh";
 wenzelm parents: 
71103diff
changeset | 734 | |
| 71282 | 735 | val ssh_command = command_setup(ssh_name, body = | 
| 71122 | 736 | """if [ "$1" = "$NAME" ] | 
| 737 | then | |
| 738 | exec "$ROOT/phabricator/bin/ssh-auth" "$@" | |
| 71270 | 739 | fi""", exit = "exit 1") | 
| 71109 
8c1c717a830b
configure SSH hosting via "isabelle phabricator_setup_ssh";
 wenzelm parents: 
71103diff
changeset | 740 | |
| 
8c1c717a830b
configure SSH hosting via "isabelle phabricator_setup_ssh";
 wenzelm parents: 
71103diff
changeset | 741 | File.write(sshd_conf_server, | 
| 
8c1c717a830b
configure SSH hosting via "isabelle phabricator_setup_ssh";
 wenzelm parents: 
71103diff
changeset | 742 | """# OpenBSD Secure Shell server for Isabelle/Phabricator | 
| 
8c1c717a830b
configure SSH hosting via "isabelle phabricator_setup_ssh";
 wenzelm parents: 
71103diff
changeset | 743 | AuthorizedKeysCommand """ + ssh_command.implode + """ | 
| 
8c1c717a830b
configure SSH hosting via "isabelle phabricator_setup_ssh";
 wenzelm parents: 
71103diff
changeset | 744 | AuthorizedKeysCommandUser """ + daemon_user + """ | 
| 
8c1c717a830b
configure SSH hosting via "isabelle phabricator_setup_ssh";
 wenzelm parents: 
71103diff
changeset | 745 | AuthorizedKeysFile none | 
| 
8c1c717a830b
configure SSH hosting via "isabelle phabricator_setup_ssh";
 wenzelm parents: 
71103diff
changeset | 746 | AllowUsers """ + configs.map(_.name).mkString(" ") + """
 | 
| 
8c1c717a830b
configure SSH hosting via "isabelle phabricator_setup_ssh";
 wenzelm parents: 
71103diff
changeset | 747 | Port """ + server_port + """ | 
| 
8c1c717a830b
configure SSH hosting via "isabelle phabricator_setup_ssh";
 wenzelm parents: 
71103diff
changeset | 748 | Protocol 2 | 
| 
8c1c717a830b
configure SSH hosting via "isabelle phabricator_setup_ssh";
 wenzelm parents: 
71103diff
changeset | 749 | PermitRootLogin no | 
| 
8c1c717a830b
configure SSH hosting via "isabelle phabricator_setup_ssh";
 wenzelm parents: 
71103diff
changeset | 750 | AllowAgentForwarding no | 
| 
8c1c717a830b
configure SSH hosting via "isabelle phabricator_setup_ssh";
 wenzelm parents: 
71103diff
changeset | 751 | AllowTcpForwarding no | 
| 
8c1c717a830b
configure SSH hosting via "isabelle phabricator_setup_ssh";
 wenzelm parents: 
71103diff
changeset | 752 | PrintMotd no | 
| 
8c1c717a830b
configure SSH hosting via "isabelle phabricator_setup_ssh";
 wenzelm parents: 
71103diff
changeset | 753 | PrintLastLog no | 
| 
8c1c717a830b
configure SSH hosting via "isabelle phabricator_setup_ssh";
 wenzelm parents: 
71103diff
changeset | 754 | PasswordAuthentication no | 
| 
8c1c717a830b
configure SSH hosting via "isabelle phabricator_setup_ssh";
 wenzelm parents: 
71103diff
changeset | 755 | ChallengeResponseAuthentication no | 
| 
8c1c717a830b
configure SSH hosting via "isabelle phabricator_setup_ssh";
 wenzelm parents: 
71103diff
changeset | 756 | PidFile /var/run/""" + ssh_name + """.pid | 
| 
8c1c717a830b
configure SSH hosting via "isabelle phabricator_setup_ssh";
 wenzelm parents: 
71103diff
changeset | 757 | """) | 
| 
8c1c717a830b
configure SSH hosting via "isabelle phabricator_setup_ssh";
 wenzelm parents: 
71103diff
changeset | 758 | |
| 
8c1c717a830b
configure SSH hosting via "isabelle phabricator_setup_ssh";
 wenzelm parents: 
71103diff
changeset | 759 | Linux.service_install(ssh_name, | 
| 
8c1c717a830b
configure SSH hosting via "isabelle phabricator_setup_ssh";
 wenzelm parents: 
71103diff
changeset | 760 | """[Unit] | 
| 
8c1c717a830b
configure SSH hosting via "isabelle phabricator_setup_ssh";
 wenzelm parents: 
71103diff
changeset | 761 | Description=OpenBSD Secure Shell server for Isabelle/Phabricator | 
| 
8c1c717a830b
configure SSH hosting via "isabelle phabricator_setup_ssh";
 wenzelm parents: 
71103diff
changeset | 762 | After=network.target auditd.service | 
| 
8c1c717a830b
configure SSH hosting via "isabelle phabricator_setup_ssh";
 wenzelm parents: 
71103diff
changeset | 763 | ConditionPathExists=!/etc/ssh/sshd_not_to_be_run | 
| 
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 | [Service] | 
| 
8c1c717a830b
configure SSH hosting via "isabelle phabricator_setup_ssh";
 wenzelm parents: 
71103diff
changeset | 766 | EnvironmentFile=-/etc/default/ssh | 
| 
8c1c717a830b
configure SSH hosting via "isabelle phabricator_setup_ssh";
 wenzelm parents: 
71103diff
changeset | 767 | ExecStartPre=/usr/sbin/sshd -f """ + sshd_conf_server.implode + """ -t | 
| 
8c1c717a830b
configure SSH hosting via "isabelle phabricator_setup_ssh";
 wenzelm parents: 
71103diff
changeset | 768 | 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 | 769 | ExecReload=/usr/sbin/sshd -f """ + sshd_conf_server.implode + """ -t | 
| 
8c1c717a830b
configure SSH hosting via "isabelle phabricator_setup_ssh";
 wenzelm parents: 
71103diff
changeset | 770 | ExecReload=/bin/kill -HUP $MAINPID | 
| 
8c1c717a830b
configure SSH hosting via "isabelle phabricator_setup_ssh";
 wenzelm parents: 
71103diff
changeset | 771 | KillMode=process | 
| 
8c1c717a830b
configure SSH hosting via "isabelle phabricator_setup_ssh";
 wenzelm parents: 
71103diff
changeset | 772 | Restart=on-failure | 
| 
8c1c717a830b
configure SSH hosting via "isabelle phabricator_setup_ssh";
 wenzelm parents: 
71103diff
changeset | 773 | RestartPreventExitStatus=255 | 
| 
8c1c717a830b
configure SSH hosting via "isabelle phabricator_setup_ssh";
 wenzelm parents: 
71103diff
changeset | 774 | Type=notify | 
| 
8c1c717a830b
configure SSH hosting via "isabelle phabricator_setup_ssh";
 wenzelm parents: 
71103diff
changeset | 775 | RuntimeDirectory=sshd-phabricator | 
| 
8c1c717a830b
configure SSH hosting via "isabelle phabricator_setup_ssh";
 wenzelm parents: 
71103diff
changeset | 776 | RuntimeDirectoryMode=0755 | 
| 
8c1c717a830b
configure SSH hosting via "isabelle phabricator_setup_ssh";
 wenzelm parents: 
71103diff
changeset | 777 | |
| 
8c1c717a830b
configure SSH hosting via "isabelle phabricator_setup_ssh";
 wenzelm parents: 
71103diff
changeset | 778 | [Install] | 
| 
8c1c717a830b
configure SSH hosting via "isabelle phabricator_setup_ssh";
 wenzelm parents: 
71103diff
changeset | 779 | WantedBy=multi-user.target | 
| 
8c1c717a830b
configure SSH hosting via "isabelle phabricator_setup_ssh";
 wenzelm parents: 
71103diff
changeset | 780 | Alias=""" + ssh_name + """.service | 
| 
8c1c717a830b
configure SSH hosting via "isabelle phabricator_setup_ssh";
 wenzelm parents: 
71103diff
changeset | 781 | """) | 
| 
8c1c717a830b
configure SSH hosting via "isabelle phabricator_setup_ssh";
 wenzelm parents: 
71103diff
changeset | 782 | |
| 
8c1c717a830b
configure SSH hosting via "isabelle phabricator_setup_ssh";
 wenzelm parents: 
71103diff
changeset | 783 |     for (config <- configs) {
 | 
| 
8c1c717a830b
configure SSH hosting via "isabelle phabricator_setup_ssh";
 wenzelm parents: 
71103diff
changeset | 784 |       progress.echo("phabricator " + quote(config.name) + " port " +  server_port)
 | 
| 
8c1c717a830b
configure SSH hosting via "isabelle phabricator_setup_ssh";
 wenzelm parents: 
71103diff
changeset | 785 |       config.execute("config set diffusion.ssh-port " + Bash.string(server_port.toString))
 | 
| 71292 
8b745b4d71b5
more rebust setup for Subversion -- assuming that diffusion.ssh-port is only set in phabricator/conf/local/local.json, not in the database;
 wenzelm parents: 
71288diff
changeset | 786 |       if (server_port == 22) config.execute("config delete diffusion.ssh-port")
 | 
| 71109 
8c1c717a830b
configure SSH hosting via "isabelle phabricator_setup_ssh";
 wenzelm parents: 
71103diff
changeset | 787 | } | 
| 
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 | |
| 
8c1c717a830b
configure SSH hosting via "isabelle phabricator_setup_ssh";
 wenzelm parents: 
71103diff
changeset | 791 | /* Isabelle tool wrapper */ | 
| 
8c1c717a830b
configure SSH hosting via "isabelle phabricator_setup_ssh";
 wenzelm parents: 
71103diff
changeset | 792 | |
| 
8c1c717a830b
configure SSH hosting via "isabelle phabricator_setup_ssh";
 wenzelm parents: 
71103diff
changeset | 793 | val isabelle_tool4 = | 
| 
8c1c717a830b
configure SSH hosting via "isabelle phabricator_setup_ssh";
 wenzelm parents: 
71103diff
changeset | 794 |     Isabelle_Tool("phabricator_setup_ssh",
 | 
| 
8c1c717a830b
configure SSH hosting via "isabelle phabricator_setup_ssh";
 wenzelm parents: 
71103diff
changeset | 795 | "setup ssh service for all Phabricator installations", args => | 
| 
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 | var server_port = default_server_port | 
| 
8c1c717a830b
configure SSH hosting via "isabelle phabricator_setup_ssh";
 wenzelm parents: 
71103diff
changeset | 798 | var system_port = default_system_port | 
| 
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 getopts = | 
| 
8c1c717a830b
configure SSH hosting via "isabelle phabricator_setup_ssh";
 wenzelm parents: 
71103diff
changeset | 801 |         Getopts("""
 | 
| 
8c1c717a830b
configure SSH hosting via "isabelle phabricator_setup_ssh";
 wenzelm parents: 
71103diff
changeset | 802 | Usage: isabelle phabricator_setup_ssh [OPTIONS] | 
| 
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 | Options are: | 
| 
8c1c717a830b
configure SSH hosting via "isabelle phabricator_setup_ssh";
 wenzelm parents: 
71103diff
changeset | 805 | -p PORT sshd port for Phabricator servers (default: """ + default_server_port + """) | 
| 
8c1c717a830b
configure SSH hosting via "isabelle phabricator_setup_ssh";
 wenzelm parents: 
71103diff
changeset | 806 | -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 | 807 | |
| 
8c1c717a830b
configure SSH hosting via "isabelle phabricator_setup_ssh";
 wenzelm parents: 
71103diff
changeset | 808 | Configure ssh service for all Phabricator installations: a separate sshd | 
| 
8c1c717a830b
configure SSH hosting via "isabelle phabricator_setup_ssh";
 wenzelm parents: 
71103diff
changeset | 809 | 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 | 810 | be distinct. | 
| 
8c1c717a830b
configure SSH hosting via "isabelle phabricator_setup_ssh";
 wenzelm parents: 
71103diff
changeset | 811 | |
| 
8c1c717a830b
configure SSH hosting via "isabelle phabricator_setup_ssh";
 wenzelm parents: 
71103diff
changeset | 812 | A particular Phabricator installation is addressed by using its | 
| 
8c1c717a830b
configure SSH hosting via "isabelle phabricator_setup_ssh";
 wenzelm parents: 
71103diff
changeset | 813 | 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 | 814 | stored ssh keys. | 
| 
8c1c717a830b
configure SSH hosting via "isabelle phabricator_setup_ssh";
 wenzelm parents: 
71103diff
changeset | 815 | """, | 
| 
8c1c717a830b
configure SSH hosting via "isabelle phabricator_setup_ssh";
 wenzelm parents: 
71103diff
changeset | 816 | "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 | 817 | "q:" -> (arg => system_port = Value.Int.parse(arg))) | 
| 71109 
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 | val more_args = getopts(args) | 
| 
8c1c717a830b
configure SSH hosting via "isabelle phabricator_setup_ssh";
 wenzelm parents: 
71103diff
changeset | 820 | if (more_args.nonEmpty) getopts.usage() | 
| 
8c1c717a830b
configure SSH hosting via "isabelle phabricator_setup_ssh";
 wenzelm parents: 
71103diff
changeset | 821 | |
| 
8c1c717a830b
configure SSH hosting via "isabelle phabricator_setup_ssh";
 wenzelm parents: 
71103diff
changeset | 822 | val progress = new Console_Progress | 
| 
8c1c717a830b
configure SSH hosting via "isabelle phabricator_setup_ssh";
 wenzelm parents: 
71103diff
changeset | 823 | |
| 
8c1c717a830b
configure SSH hosting via "isabelle phabricator_setup_ssh";
 wenzelm parents: 
71103diff
changeset | 824 | 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 | 825 | server_port = server_port, system_port = system_port, progress = progress) | 
| 71109 
8c1c717a830b
configure SSH hosting via "isabelle phabricator_setup_ssh";
 wenzelm parents: 
71103diff
changeset | 826 | }) | 
| 70967 | 827 | } |