| author | wenzelm | 
| Thu, 14 Nov 2019 11:36:14 +0100 | |
| changeset 71115 | 3199c08e6413 | 
| parent 71114 | 6cfec8029831 | 
| child 71116 | aa1338a778c1 | 
| 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] = | |
| 24 | Build_Docker.packages ::: | |
| 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 | |
| 30 | "php-zip", "python-pygments", "ssh") | |
| 31 | ||
| 32 | ||
| 33 | /* global system resources */ | |
| 34 | ||
| 71068 
510b89906d86
discontinued somewhat pointless Isabelle options: setup implicitly assumes Ubuntu 18.04;
 wenzelm parents: 
71066diff
changeset | 35 | val www_user = "www-data" | 
| 
510b89906d86
discontinued somewhat pointless Isabelle options: setup implicitly assumes Ubuntu 18.04;
 wenzelm parents: 
71066diff
changeset | 36 | |
| 71049 | 37 | val daemon_user = "phabricator" | 
| 38 | ||
| 71109 
8c1c717a830b
configure SSH hosting via "isabelle phabricator_setup_ssh";
 wenzelm parents: 
71103diff
changeset | 39 |   val sshd_config = Path.explode("/etc/ssh/sshd_config")
 | 
| 71049 | 40 | |
| 41 | ||
| 42 | /* installation parameters */ | |
| 43 | ||
| 70967 | 44 | val default_name = "vcs" | 
| 45 | ||
| 71052 
6bf53035baf0
clarified name prefixes: global config always uses "isabelle-phabricator";
 wenzelm parents: 
71051diff
changeset | 46 | def phabricator_name(name: String = "", ext: String = ""): String = | 
| 
6bf53035baf0
clarified name prefixes: global config always uses "isabelle-phabricator";
 wenzelm parents: 
71051diff
changeset | 47 | "phabricator" + (if (name.isEmpty) "" else "-" + name) + (if (ext.isEmpty) "" else "." + ext) | 
| 
6bf53035baf0
clarified name prefixes: global config always uses "isabelle-phabricator";
 wenzelm parents: 
71051diff
changeset | 48 | |
| 
6bf53035baf0
clarified name prefixes: global config always uses "isabelle-phabricator";
 wenzelm parents: 
71051diff
changeset | 49 | def isabelle_phabricator_name(name: String = "", ext: String = ""): String = | 
| 
6bf53035baf0
clarified name prefixes: global config always uses "isabelle-phabricator";
 wenzelm parents: 
71051diff
changeset | 50 | "isabelle-" + phabricator_name(name = name, ext = ext) | 
| 70967 | 51 | |
| 71068 
510b89906d86
discontinued somewhat pointless Isabelle options: setup implicitly assumes Ubuntu 18.04;
 wenzelm parents: 
71066diff
changeset | 52 | def default_root(name: String): Path = | 
| 
510b89906d86
discontinued somewhat pointless Isabelle options: setup implicitly assumes Ubuntu 18.04;
 wenzelm parents: 
71066diff
changeset | 53 |     Path.explode("/var/www") + Path.basic(phabricator_name(name = name))
 | 
| 70967 | 54 | |
| 71068 
510b89906d86
discontinued somewhat pointless Isabelle options: setup implicitly assumes Ubuntu 18.04;
 wenzelm parents: 
71066diff
changeset | 55 |   def default_repo(name: String): Path = default_root(name) + Path.basic("repo")
 | 
| 70967 | 56 | |
| 71072 | 57 |   val default_mailers: Path = Path.explode("mailers.json")
 | 
| 71066 | 58 | |
| 71109 
8c1c717a830b
configure SSH hosting via "isabelle phabricator_setup_ssh";
 wenzelm parents: 
71103diff
changeset | 59 | val default_system_port = 22 | 
| 
8c1c717a830b
configure SSH hosting via "isabelle phabricator_setup_ssh";
 wenzelm parents: 
71103diff
changeset | 60 | val alternative_system_port = 222 | 
| 
8c1c717a830b
configure SSH hosting via "isabelle phabricator_setup_ssh";
 wenzelm parents: 
71103diff
changeset | 61 | val default_server_port = 2222 | 
| 
8c1c717a830b
configure SSH hosting via "isabelle phabricator_setup_ssh";
 wenzelm parents: 
71103diff
changeset | 62 | |
| 70967 | 63 | |
| 64 | ||
| 65 | /** global configuration **/ | |
| 66 | ||
| 71052 
6bf53035baf0
clarified name prefixes: global config always uses "isabelle-phabricator";
 wenzelm parents: 
71051diff
changeset | 67 |   val global_config = Path.explode("/etc/" + isabelle_phabricator_name(ext = "conf"))
 | 
| 70967 | 68 | |
| 69 | sealed case class Config(name: String, root: Path) | |
| 70968 | 70 |   {
 | 
| 71052 
6bf53035baf0
clarified name prefixes: global config always uses "isabelle-phabricator";
 wenzelm parents: 
71051diff
changeset | 71 | def home: Path = root + Path.explode(phabricator_name()) | 
| 70969 | 72 | |
| 73 | def execute(command: String): Process_Result = | |
| 71102 | 74 |       Isabelle_System.bash("bin/" + command, cwd = home.file, redirect = true).check
 | 
| 70968 | 75 | } | 
| 70967 | 76 | |
| 77 | def read_config(): List[Config] = | |
| 78 |   {
 | |
| 79 |     if (global_config.is_file) {
 | |
| 80 | for (entry <- Library.trim_split_lines(File.read(global_config)) if entry.nonEmpty) | |
| 81 |       yield {
 | |
| 82 |         space_explode(':', entry) match {
 | |
| 83 | case List(name, root) => Config(name, Path.explode(root)) | |
| 84 |           case _ => error("Malformed config file " + global_config + "\nentry " + quote(entry))
 | |
| 85 | } | |
| 86 | } | |
| 87 | } | |
| 88 | else Nil | |
| 89 | } | |
| 90 | ||
| 91 | def write_config(configs: List[Config]) | |
| 92 |   {
 | |
| 93 | File.write(global_config, | |
| 94 |       configs.map(config => config.name + ":" + config.root.implode).mkString("", "\n", "\n"))
 | |
| 95 | } | |
| 96 | ||
| 97 | def get_config(name: String): Config = | |
| 98 | read_config().find(config => config.name == name) getOrElse | |
| 99 |       error("Bad Isabelle/Phabricator installation " + quote(name))
 | |
| 100 | ||
| 101 | ||
| 102 | ||
| 71097 | 103 | /** command-line tools **/ | 
| 104 | ||
| 105 | /* Isabelle tool wrapper */ | |
| 106 | ||
| 107 | val isabelle_tool1 = | |
| 108 |     Isabelle_Tool("phabricator", "invoke command-line tool within Phabricator home directory", args =>
 | |
| 109 |     {
 | |
| 71101 | 110 | var list = false | 
| 71097 | 111 | var name = default_name | 
| 112 | ||
| 113 | val getopts = | |
| 114 |         Getopts("""
 | |
| 115 | Usage: isabelle phabricator [OPTIONS] COMMAND [ARGS...] | |
| 116 | ||
| 117 | Options are: | |
| 71101 | 118 | -l list available Phabricator installations | 
| 71097 | 119 | -n NAME Phabricator installation name (default: """ + quote(default_name) + """) | 
| 120 | ||
| 71103 | 121 | Invoke a command-line tool within the home directory of the named | 
| 122 | Phabricator installation. | |
| 71097 | 123 | """, | 
| 71101 | 124 | "l" -> (_ => list = true), | 
| 71097 | 125 | "n:" -> (arg => name = arg)) | 
| 126 | ||
| 127 | val more_args = getopts(args) | |
| 71101 | 128 | if (more_args.isEmpty && !list) getopts.usage() | 
| 71097 | 129 | |
| 130 | val progress = new Console_Progress | |
| 131 | ||
| 71101 | 132 |       if (list) {
 | 
| 133 |         for (config <- read_config()) {
 | |
| 71103 | 134 |           progress.echo("phabricator " + quote(config.name) + " root " + config.root)
 | 
| 71101 | 135 | } | 
| 136 | } | |
| 137 | ||
| 71097 | 138 | val config = get_config(name) | 
| 139 | ||
| 71098 | 140 | val result = progress.bash(Bash.strings(more_args), cwd = config.home.file, echo = true) | 
| 141 |       if (!result.ok) error("Return code: " + result.rc.toString)
 | |
| 71097 | 142 | }) | 
| 143 | ||
| 144 | ||
| 145 | ||
| 70967 | 146 | /** setup **/ | 
| 147 | ||
| 71049 | 148 | def user_setup(name: String, description: String, ssh_setup: Boolean = false) | 
| 149 |   {
 | |
| 150 |     if (!Linux.user_exists(name)) {
 | |
| 71054 
b64fc38327ae
prefer system user setup, e.g. avoid occurrence on login screen;
 wenzelm parents: 
71053diff
changeset | 151 | Linux.user_add(name, description = description, system = true, ssh_setup = ssh_setup) | 
| 71049 | 152 | } | 
| 153 |     else if (Linux.user_description(name) != description) {
 | |
| 154 |       error("User " + quote(name) + " already exists --" +
 | |
| 155 | " for Phabricator it should have the description:\n " + quote(description)) | |
| 156 | } | |
| 157 | } | |
| 158 | ||
| 70967 | 159 | def phabricator_setup( | 
| 160 | name: String = default_name, | |
| 161 | root: String = "", | |
| 162 | repo: String = "", | |
| 71047 | 163 | package_update: Boolean = false, | 
| 70967 | 164 | progress: Progress = No_Progress) | 
| 165 |   {
 | |
| 166 | /* system environment */ | |
| 167 | ||
| 168 | Linux.check_system_root() | |
| 169 | ||
| 71079 | 170 |     progress.echo("System packages ...")
 | 
| 171 | ||
| 71047 | 172 |     if (package_update) {
 | 
| 173 | Linux.package_update(progress = progress) | |
| 174 | Linux.check_reboot_required() | |
| 175 | } | |
| 70967 | 176 | |
| 177 | Linux.package_install(packages, progress = progress) | |
| 178 | Linux.check_reboot_required() | |
| 179 | ||
| 180 | ||
| 71049 | 181 | /* users */ | 
| 182 | ||
| 183 |     if (name == daemon_user) {
 | |
| 184 |       error("Clash of installation name with daemon user " + quote(daemon_user))
 | |
| 185 | } | |
| 186 | ||
| 187 | user_setup(daemon_user, "Phabricator Daemon User", ssh_setup = true) | |
| 188 | user_setup(name, "Phabricator SSH User") | |
| 189 | ||
| 190 | ||
| 70967 | 191 | /* basic installation */ | 
| 192 | ||
| 71079 | 193 |     progress.echo("\nPhabricator installation ...")
 | 
| 71076 | 194 | |
| 71068 
510b89906d86
discontinued somewhat pointless Isabelle options: setup implicitly assumes Ubuntu 18.04;
 wenzelm parents: 
71066diff
changeset | 195 | 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 | 196 | val repo_path = if (repo.nonEmpty) Path.explode(repo) else default_repo(name) | 
| 70967 | 197 | |
| 198 | val configs = read_config() | |
| 199 | ||
| 200 |     for (config <- configs if config.name == name) {
 | |
| 201 |       error("Duplicate Phabricator installation " + quote(name) + " in " + config.root)
 | |
| 202 | } | |
| 203 | ||
| 204 |     if (!Isabelle_System.bash("mkdir -p " + File.bash_path(root_path)).ok) {
 | |
| 205 |       error("Failed to create root directory " + root_path)
 | |
| 206 | } | |
| 207 | ||
| 208 | progress.bash(cwd = root_path.file, echo = true, | |
| 209 | script = """ | |
| 210 | set -e | |
| 71050 | 211 | chown """ + Bash.string(www_user) + ":" + Bash.string(www_user) + """ . | 
| 70967 | 212 | chmod 755 . | 
| 213 | ||
| 214 | git clone https://github.com/phacility/libphutil.git | |
| 215 | git clone https://github.com/phacility/arcanist.git | |
| 216 | git clone https://github.com/phacility/phabricator.git | |
| 217 | """).check | |
| 218 | ||
| 219 | val config = Config(name, root_path) | |
| 220 | write_config(configs ::: List(config)) | |
| 70968 | 221 | |
| 71051 | 222 |     config.execute("config set pygments.enabled true")
 | 
| 223 | ||
| 70968 | 224 | |
| 71050 | 225 | /* local repository directory */ | 
| 226 | ||
| 71109 
8c1c717a830b
configure SSH hosting via "isabelle phabricator_setup_ssh";
 wenzelm parents: 
71103diff
changeset | 227 |     progress.echo("\nRepository hosting setup ...")
 | 
| 
8c1c717a830b
configure SSH hosting via "isabelle phabricator_setup_ssh";
 wenzelm parents: 
71103diff
changeset | 228 | |
| 71050 | 229 |     if (!Isabelle_System.bash("mkdir -p " + File.bash_path(repo_path)).ok) {
 | 
| 230 |       error("Failed to create local repository directory " + repo_path)
 | |
| 231 | } | |
| 232 | ||
| 71114 | 233 | Isabelle_System.chown( | 
| 234 | "-R " + Bash.string(daemon_user) + ":" + Bash.string(daemon_user), repo_path) | |
| 235 |     Isabelle_System.chmod("755", repo_path)
 | |
| 71050 | 236 | |
| 237 |     config.execute("config set repository.default-local-path " + File.bash_path(repo_path))
 | |
| 238 | ||
| 239 | ||
| 71109 
8c1c717a830b
configure SSH hosting via "isabelle phabricator_setup_ssh";
 wenzelm parents: 
71103diff
changeset | 240 |     val sudoers_file = Path.explode("/etc/sudoers.d") + Path.basic(isabelle_phabricator_name())
 | 
| 
8c1c717a830b
configure SSH hosting via "isabelle phabricator_setup_ssh";
 wenzelm parents: 
71103diff
changeset | 241 | File.write(sudoers_file, | 
| 
8c1c717a830b
configure SSH hosting via "isabelle phabricator_setup_ssh";
 wenzelm parents: 
71103diff
changeset | 242 |       www_user + " ALL=(" + daemon_user + ") SETENV: NOPASSWD: /usr/bin/git, /usr/bin/hg, /usr/bin/ssh, /usr/bin/id\n" +
 | 
| 
8c1c717a830b
configure SSH hosting via "isabelle phabricator_setup_ssh";
 wenzelm parents: 
71103diff
changeset | 243 |       name + " ALL=(" + daemon_user + ") SETENV: NOPASSWD: /usr/bin/git, /usr/bin/git-upload-pack, /usr/bin/git-receive-pack, /usr/bin/hg, /usr/bin/svnserve, /usr/bin/ssh, /usr/bin/id\n")
 | 
| 
8c1c717a830b
configure SSH hosting via "isabelle phabricator_setup_ssh";
 wenzelm parents: 
71103diff
changeset | 244 | |
| 71115 | 245 |     Isabelle_System.chmod("440", sudoers_file)
 | 
| 71109 
8c1c717a830b
configure SSH hosting via "isabelle phabricator_setup_ssh";
 wenzelm parents: 
71103diff
changeset | 246 | |
| 
8c1c717a830b
configure SSH hosting via "isabelle phabricator_setup_ssh";
 wenzelm parents: 
71103diff
changeset | 247 |     config.execute("config set diffusion.ssh-user " + Bash.string(config.name))
 | 
| 
8c1c717a830b
configure SSH hosting via "isabelle phabricator_setup_ssh";
 wenzelm parents: 
71103diff
changeset | 248 | |
| 
8c1c717a830b
configure SSH hosting via "isabelle phabricator_setup_ssh";
 wenzelm parents: 
71103diff
changeset | 249 | |
| 70969 | 250 | /* MySQL setup */ | 
| 251 | ||
| 71079 | 252 |     progress.echo("\nMySQL setup ...")
 | 
| 70969 | 253 | |
| 71055 
27a998cdc0f4
back to plain name, to have it accepted my mysql;
 wenzelm parents: 
71054diff
changeset | 254 |     File.write(Path.explode("/etc/mysql/mysql.conf.d/" + phabricator_name(ext = "cnf")),
 | 
| 71051 | 255 | """[mysqld] | 
| 256 | max_allowed_packet = 32M | |
| 257 | innodb_buffer_pool_size = 1600M | |
| 258 | local_infile = 0 | |
| 259 | """) | |
| 260 | ||
| 261 |     Linux.service_restart("mysql")
 | |
| 262 | ||
| 263 | ||
| 70969 | 264 | def mysql_conf(R: Regex): Option[String] = | 
| 71068 
510b89906d86
discontinued somewhat pointless Isabelle options: setup implicitly assumes Ubuntu 18.04;
 wenzelm parents: 
71066diff
changeset | 265 |       split_lines(File.read(Path.explode("/etc/mysql/debian.cnf"))).collectFirst({ case R(a) => a })
 | 
| 70969 | 266 | |
| 267 |     for (user <- mysql_conf("""^user\s*=\s*(\S*)\s*$""".r)) {
 | |
| 268 |       config.execute("config set mysql.user " + Bash.string(user))
 | |
| 269 | } | |
| 270 | ||
| 271 |     for (pass <- mysql_conf("""^password\s*=\s*(\S*)\s*$""".r)) {
 | |
| 272 |       config.execute("config set mysql.pass " + Bash.string(pass))
 | |
| 273 | } | |
| 274 | ||
| 275 |     config.execute("config set storage.default-namespace " +
 | |
| 71052 
6bf53035baf0
clarified name prefixes: global config always uses "isabelle-phabricator";
 wenzelm parents: 
71051diff
changeset | 276 |       Bash.string(phabricator_name(name = name).replace("-", "_")))
 | 
| 70969 | 277 | |
| 71051 | 278 |     config.execute("config set storage.mysql-engine.max-size 8388608")
 | 
| 279 | ||
| 71102 | 280 |     progress.bash("bin/storage upgrade --force", cwd = config.home.file, echo = true).check
 | 
| 70969 | 281 | |
| 282 | ||
| 71051 | 283 | /* PHP setup */ | 
| 284 | ||
| 285 | val php_version = | |
| 286 |       Isabelle_System.bash("""php --run 'echo PHP_MAJOR_VERSION . "." . PHP_MINOR_VERSION;'""")
 | |
| 287 | .check.out | |
| 288 | ||
| 289 | val php_conf = | |
| 290 |       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 | 291 |         Path.explode("apache2/conf.d") +
 | 
| 
6bf53035baf0
clarified name prefixes: global config always uses "isabelle-phabricator";
 wenzelm parents: 
71051diff
changeset | 292 | Path.basic(isabelle_phabricator_name(ext = "ini")) | 
| 71051 | 293 | |
| 294 | File.write(php_conf, | |
| 295 | "post_max_size = 32M\n" + | |
| 296 | "opcache.validate_timestamps = 0\n" + | |
| 297 | "memory_limit = 512M\n") | |
| 298 | ||
| 299 | ||
| 70968 | 300 | /* Apache setup */ | 
| 301 | ||
| 71079 | 302 |     progress.echo("Apache setup ...")
 | 
| 70968 | 303 | |
| 71068 
510b89906d86
discontinued somewhat pointless Isabelle options: setup implicitly assumes Ubuntu 18.04;
 wenzelm parents: 
71066diff
changeset | 304 |     val apache_root = Path.explode("/etc/apache2")
 | 
| 70968 | 305 |     val apache_sites = apache_root + Path.explode("sites-available")
 | 
| 306 | ||
| 307 |     if (!apache_sites.is_dir) error("Bad Apache sites directory " + apache_sites)
 | |
| 308 | ||
| 71058 | 309 | 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 | 310 | val server_url = "http://" + server_name | 
| 
6bf53035baf0
clarified name prefixes: global config always uses "isabelle-phabricator";
 wenzelm parents: 
71051diff
changeset | 311 | |
| 71058 | 312 | File.write(apache_sites + Path.basic(isabelle_phabricator_name(name = name, ext = "conf")), | 
| 70968 | 313 | """<VirtualHost *:80> | 
| 71052 
6bf53035baf0
clarified name prefixes: global config always uses "isabelle-phabricator";
 wenzelm parents: 
71051diff
changeset | 314 | ServerName """ + server_name + """ | 
| 70968 | 315 | ServerAdmin webmaster@localhost | 
| 70969 | 316 | DocumentRoot """ + config.home.implode + """/webroot | 
| 70968 | 317 | |
| 318 |     ErrorLog ${APACHE_LOG_DIR}/error.log
 | |
| 319 | RewriteEngine on | |
| 320 | RewriteRule ^(.*)$ /index.php?__path__=$1 [B,L,QSA] | |
| 321 | </VirtualHost> | |
| 322 | ||
| 323 | # vim: syntax=apache ts=4 sw=4 sts=4 sr noet | |
| 324 | """) | |
| 325 | ||
| 71051 | 326 | Isabelle_System.bash( """ | 
| 70968 | 327 | set -e | 
| 328 | a2enmod rewrite | |
| 71058 | 329 | a2ensite """ + Bash.string(isabelle_phabricator_name(name = name))).check | 
| 71051 | 330 | |
| 71057 | 331 |     config.execute("config set phabricator.base-uri " + Bash.string(server_url))
 | 
| 332 | ||
| 71051 | 333 |     Linux.service_restart("apache2")
 | 
| 70968 | 334 | |
| 71053 | 335 | |
| 336 | /* PHP daemon */ | |
| 337 | ||
| 71079 | 338 |     progress.echo("PHP daemon setup ...")
 | 
| 71053 | 339 | |
| 340 |     config.execute("config set phd.user " + Bash.string(daemon_user))
 | |
| 71112 | 341 |     config.execute("config set phd.log-directory /var/tmp/phd/" +
 | 
| 342 | isabelle_phabricator_name(name = name) + "/log") | |
| 71053 | 343 | |
| 71056 
ee3c43eb79ae
proper service name (again): it is specific to each installation;
 wenzelm parents: 
71055diff
changeset | 344 | Linux.service_install(isabelle_phabricator_name(name = name), | 
| 71053 | 345 | """[Unit] | 
| 346 | Description=PHP daemon for Isabelle/Phabricator """ + quote(name) + """ | |
| 347 | After=syslog.target network.target apache2.service mysql.service | |
| 348 | ||
| 349 | [Service] | |
| 350 | Type=oneshot | |
| 351 | User=""" + daemon_user + """ | |
| 352 | Group=""" + daemon_user + """ | |
| 353 | Environment=PATH=/sbin:/usr/sbin:/usr/local/sbin:/usr/local/bin:/usr/bin:/bin | |
| 71112 | 354 | ExecStart=""" + config.home.implode + """/bin/phd start --force | 
| 71053 | 355 | ExecStop=""" + config.home.implode + """/bin/phd stop | 
| 356 | RemainAfterExit=yes | |
| 357 | ||
| 358 | [Install] | |
| 359 | WantedBy=multi-user.target | |
| 360 | """) | |
| 361 | ||
| 362 | ||
| 71052 
6bf53035baf0
clarified name prefixes: global config always uses "isabelle-phabricator";
 wenzelm parents: 
71051diff
changeset | 363 |     progress.echo("\nDONE\nWeb configuration via " + server_url)
 | 
| 70967 | 364 | } | 
| 365 | ||
| 366 | ||
| 367 | /* Isabelle tool wrapper */ | |
| 368 | ||
| 71097 | 369 | val isabelle_tool2 = | 
| 70967 | 370 |     Isabelle_Tool("phabricator_setup", "setup Phabricator server on Ubuntu Linux", args =>
 | 
| 371 |     {
 | |
| 71047 | 372 | var repo = "" | 
| 373 | var package_update = false | |
| 71078 | 374 | var name = default_name | 
| 70967 | 375 | var root = "" | 
| 376 | ||
| 377 | val getopts = | |
| 378 |         Getopts("""
 | |
| 71078 | 379 | Usage: isabelle phabricator_setup [OPTIONS] | 
| 70967 | 380 | |
| 381 | Options are: | |
| 71068 
510b89906d86
discontinued somewhat pointless Isabelle options: setup implicitly assumes Ubuntu 18.04;
 wenzelm parents: 
71066diff
changeset | 382 |     -R DIR       repository directory (default: """ + default_repo("NAME") + """)
 | 
| 71047 | 383 | -U full update of system packages before installation | 
| 71078 | 384 | -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 | 385 |     -r DIR       installation root directory (default: """ + default_root("NAME") + """)
 | 
| 70967 | 386 | |
| 71103 | 387 | Install Phabricator as LAMP application (Linux, Apache, MySQL, PHP). | 
| 70967 | 388 | |
| 71078 | 389 | The installation name (default: """ + quote(default_name) + """) is mapped to a regular | 
| 390 | Unix user; this is relevant for public SSH access. | |
| 70967 | 391 | """, | 
| 392 | "R:" -> (arg => repo = arg), | |
| 71047 | 393 | "U" -> (_ => package_update = true), | 
| 71078 | 394 | "n:" -> (arg => name = arg), | 
| 70967 | 395 | "r:" -> (arg => root = arg)) | 
| 396 | ||
| 397 | val more_args = getopts(args) | |
| 71078 | 398 | if (more_args.nonEmpty) getopts.usage() | 
| 70967 | 399 | |
| 400 | val progress = new Console_Progress | |
| 401 | ||
| 71078 | 402 | phabricator_setup(name = name, root = root, repo = repo, | 
| 71047 | 403 | package_update = package_update, progress = progress) | 
| 70967 | 404 | }) | 
| 405 | ||
| 406 | ||
| 407 | ||
| 71066 | 408 | /** setup mail **/ | 
| 70967 | 409 | |
| 71072 | 410 | val mailers_template: String = | 
| 411 | """[ | |
| 412 |   {
 | |
| 413 | "key": "example.org", | |
| 414 | "type": "smtp", | |
| 415 |     "options": {
 | |
| 416 | "host": "mail.example.org", | |
| 417 | "port": 465, | |
| 418 | "user": "phabricator@example.org", | |
| 419 | "password": "********", | |
| 420 | "protocol": "ssl", | |
| 421 | "message-id": true | |
| 422 | } | |
| 423 | } | |
| 424 | ]""" | |
| 425 | ||
| 71066 | 426 | def phabricator_setup_mail( | 
| 427 | name: String = default_name, | |
| 428 | config_file: Option[Path] = None, | |
| 429 | test_user: String = "", | |
| 430 | progress: Progress = No_Progress) | |
| 70967 | 431 |   {
 | 
| 432 | Linux.check_system_root() | |
| 433 | ||
| 71066 | 434 | val config = get_config(name) | 
| 71073 | 435 | val default_config_file = config.root + default_mailers | 
| 71066 | 436 | |
| 437 | val mail_config = config_file getOrElse default_config_file | |
| 438 | ||
| 439 | def setup_mail | |
| 440 |     {
 | |
| 441 |       progress.echo("Using mail configuration from " + mail_config)
 | |
| 442 |       config.execute("config set cluster.mailers --stdin < " + File.bash_path(mail_config))
 | |
| 443 | ||
| 444 |       if (test_user.nonEmpty) {
 | |
| 445 |         progress.echo("Sending test mail to " + quote(test_user))
 | |
| 446 | progress.bash(cwd = config.home.file, echo = true, | |
| 71102 | 447 | script = """echo "Test from Phabricator ($(date))" | bin/mail send-test --subject "Test" --to """ + | 
| 71066 | 448 | Bash.string(test_user)).check | 
| 449 | } | |
| 450 | } | |
| 451 | ||
| 452 |     if (config_file.isEmpty) {
 | |
| 71070 | 453 |       if (!default_config_file.is_file) {
 | 
| 454 | File.write(default_config_file, mailers_template) | |
| 71114 | 455 |         Isabelle_System.chmod("600", default_config_file)
 | 
| 71070 | 456 | } | 
| 71066 | 457 |       if (File.read(default_config_file) == mailers_template) {
 | 
| 458 | progress.echo( | |
| 71077 | 459 | """ | 
| 460 | Please invoke the tool again, after providing details in | |
| 461 | """ + default_config_file.implode + """ | |
| 462 | ||
| 463 | See also section "Mailer: SMTP" in | |
| 464 | https://secure.phabricator.com/book/phabricator/article/configuring_outbound_email | |
| 465 | """) | |
| 71066 | 466 | } | 
| 467 | else setup_mail | |
| 468 | } | |
| 469 | else setup_mail | |
| 70967 | 470 | } | 
| 471 | ||
| 472 | ||
| 473 | /* Isabelle tool wrapper */ | |
| 474 | ||
| 71097 | 475 | val isabelle_tool3 = | 
| 71066 | 476 |     Isabelle_Tool("phabricator_setup_mail",
 | 
| 71109 
8c1c717a830b
configure SSH hosting via "isabelle phabricator_setup_ssh";
 wenzelm parents: 
71103diff
changeset | 477 | "setup mail for one Phabricator installation", args => | 
| 70967 | 478 |     {
 | 
| 71066 | 479 | var test_user = "" | 
| 480 | var name = default_name | |
| 481 | var config_file: Option[Path] = None | |
| 482 | ||
| 70967 | 483 | val getopts = | 
| 484 |         Getopts("""
 | |
| 71066 | 485 | Usage: isabelle phabricator_setup_mail [OPTIONS] | 
| 486 | ||
| 487 | Options are: | |
| 488 | -T USER send test mail to Phabricator user | |
| 71103 | 489 | -f FILE config file (default: """ + default_mailers + """ within Phabricator root) | 
| 71066 | 490 | -n NAME Phabricator installation name (default: """ + quote(default_name) + """) | 
| 70967 | 491 | |
| 71077 | 492 | Provide mail configuration for existing Phabricator installation. | 
| 71066 | 493 | """, | 
| 494 | "T:" -> (arg => test_user = arg), | |
| 495 | "f:" -> (arg => config_file = Some(Path.explode(arg))), | |
| 496 | "n:" -> (arg => name = arg)) | |
| 70967 | 497 | |
| 498 | val more_args = getopts(args) | |
| 71066 | 499 | if (more_args.nonEmpty) getopts.usage() | 
| 70967 | 500 | |
| 501 | val progress = new Console_Progress | |
| 502 | ||
| 71066 | 503 | phabricator_setup_mail(name = name, config_file = config_file, | 
| 504 | test_user = test_user, progress = progress) | |
| 70967 | 505 | }) | 
| 71109 
8c1c717a830b
configure SSH hosting via "isabelle phabricator_setup_ssh";
 wenzelm parents: 
71103diff
changeset | 506 | |
| 
8c1c717a830b
configure SSH hosting via "isabelle phabricator_setup_ssh";
 wenzelm parents: 
71103diff
changeset | 507 | |
| 
8c1c717a830b
configure SSH hosting via "isabelle phabricator_setup_ssh";
 wenzelm parents: 
71103diff
changeset | 508 | |
| 
8c1c717a830b
configure SSH hosting via "isabelle phabricator_setup_ssh";
 wenzelm parents: 
71103diff
changeset | 509 | /** setup ssh **/ | 
| 
8c1c717a830b
configure SSH hosting via "isabelle phabricator_setup_ssh";
 wenzelm parents: 
71103diff
changeset | 510 | |
| 
8c1c717a830b
configure SSH hosting via "isabelle phabricator_setup_ssh";
 wenzelm parents: 
71103diff
changeset | 511 | /* sshd config */ | 
| 
8c1c717a830b
configure SSH hosting via "isabelle phabricator_setup_ssh";
 wenzelm parents: 
71103diff
changeset | 512 | |
| 
8c1c717a830b
configure SSH hosting via "isabelle phabricator_setup_ssh";
 wenzelm parents: 
71103diff
changeset | 513 | private val Port = """^\s*Port\s+(\d+)\s*$""".r | 
| 
8c1c717a830b
configure SSH hosting via "isabelle phabricator_setup_ssh";
 wenzelm parents: 
71103diff
changeset | 514 | private val No_Port = """^#\s*Port\b.*$""".r | 
| 
8c1c717a830b
configure SSH hosting via "isabelle phabricator_setup_ssh";
 wenzelm parents: 
71103diff
changeset | 515 | private val Any_Port = """^#?\s*Port\b.*$""".r | 
| 
8c1c717a830b
configure SSH hosting via "isabelle phabricator_setup_ssh";
 wenzelm parents: 
71103diff
changeset | 516 | |
| 
8c1c717a830b
configure SSH hosting via "isabelle phabricator_setup_ssh";
 wenzelm parents: 
71103diff
changeset | 517 | def conf_ssh_port(port: Int): String = | 
| 
8c1c717a830b
configure SSH hosting via "isabelle phabricator_setup_ssh";
 wenzelm parents: 
71103diff
changeset | 518 | if (port == 22) "#Port 22" else "Port " + port | 
| 
8c1c717a830b
configure SSH hosting via "isabelle phabricator_setup_ssh";
 wenzelm parents: 
71103diff
changeset | 519 | |
| 
8c1c717a830b
configure SSH hosting via "isabelle phabricator_setup_ssh";
 wenzelm parents: 
71103diff
changeset | 520 | def read_ssh_port(conf: Path): Int = | 
| 
8c1c717a830b
configure SSH hosting via "isabelle phabricator_setup_ssh";
 wenzelm parents: 
71103diff
changeset | 521 |   {
 | 
| 
8c1c717a830b
configure SSH hosting via "isabelle phabricator_setup_ssh";
 wenzelm parents: 
71103diff
changeset | 522 | val lines = split_lines(File.read(conf)) | 
| 
8c1c717a830b
configure SSH hosting via "isabelle phabricator_setup_ssh";
 wenzelm parents: 
71103diff
changeset | 523 | val ports = | 
| 
8c1c717a830b
configure SSH hosting via "isabelle phabricator_setup_ssh";
 wenzelm parents: 
71103diff
changeset | 524 |       lines.flatMap({
 | 
| 
8c1c717a830b
configure SSH hosting via "isabelle phabricator_setup_ssh";
 wenzelm parents: 
71103diff
changeset | 525 | case Port(Value.Int(p)) => Some(p) | 
| 
8c1c717a830b
configure SSH hosting via "isabelle phabricator_setup_ssh";
 wenzelm parents: 
71103diff
changeset | 526 | case No_Port() => Some(22) | 
| 
8c1c717a830b
configure SSH hosting via "isabelle phabricator_setup_ssh";
 wenzelm parents: 
71103diff
changeset | 527 | case _ => None | 
| 
8c1c717a830b
configure SSH hosting via "isabelle phabricator_setup_ssh";
 wenzelm parents: 
71103diff
changeset | 528 | }) | 
| 
8c1c717a830b
configure SSH hosting via "isabelle phabricator_setup_ssh";
 wenzelm parents: 
71103diff
changeset | 529 |     ports match {
 | 
| 
8c1c717a830b
configure SSH hosting via "isabelle phabricator_setup_ssh";
 wenzelm parents: 
71103diff
changeset | 530 | case List(port) => port | 
| 
8c1c717a830b
configure SSH hosting via "isabelle phabricator_setup_ssh";
 wenzelm parents: 
71103diff
changeset | 531 |       case Nil => error("Missing Port specification in " + conf)
 | 
| 
8c1c717a830b
configure SSH hosting via "isabelle phabricator_setup_ssh";
 wenzelm parents: 
71103diff
changeset | 532 |       case _ => error("Multiple Port specifications in " + conf)
 | 
| 
8c1c717a830b
configure SSH hosting via "isabelle phabricator_setup_ssh";
 wenzelm parents: 
71103diff
changeset | 533 | } | 
| 
8c1c717a830b
configure SSH hosting via "isabelle phabricator_setup_ssh";
 wenzelm parents: 
71103diff
changeset | 534 | } | 
| 
8c1c717a830b
configure SSH hosting via "isabelle phabricator_setup_ssh";
 wenzelm parents: 
71103diff
changeset | 535 | |
| 
8c1c717a830b
configure SSH hosting via "isabelle phabricator_setup_ssh";
 wenzelm parents: 
71103diff
changeset | 536 | def write_ssh_port(conf: Path, port: Int): Boolean = | 
| 
8c1c717a830b
configure SSH hosting via "isabelle phabricator_setup_ssh";
 wenzelm parents: 
71103diff
changeset | 537 |   {
 | 
| 
8c1c717a830b
configure SSH hosting via "isabelle phabricator_setup_ssh";
 wenzelm parents: 
71103diff
changeset | 538 | val old_port = read_ssh_port(conf) | 
| 
8c1c717a830b
configure SSH hosting via "isabelle phabricator_setup_ssh";
 wenzelm parents: 
71103diff
changeset | 539 | if (old_port == port) false | 
| 
8c1c717a830b
configure SSH hosting via "isabelle phabricator_setup_ssh";
 wenzelm parents: 
71103diff
changeset | 540 |     else {
 | 
| 
8c1c717a830b
configure SSH hosting via "isabelle phabricator_setup_ssh";
 wenzelm parents: 
71103diff
changeset | 541 | val lines = split_lines(File.read(conf)) | 
| 
8c1c717a830b
configure SSH hosting via "isabelle phabricator_setup_ssh";
 wenzelm parents: 
71103diff
changeset | 542 |       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 | 543 | File.write(conf, cat_lines(lines1)) | 
| 
8c1c717a830b
configure SSH hosting via "isabelle phabricator_setup_ssh";
 wenzelm parents: 
71103diff
changeset | 544 | true | 
| 
8c1c717a830b
configure SSH hosting via "isabelle phabricator_setup_ssh";
 wenzelm parents: 
71103diff
changeset | 545 | } | 
| 
8c1c717a830b
configure SSH hosting via "isabelle phabricator_setup_ssh";
 wenzelm parents: 
71103diff
changeset | 546 | } | 
| 
8c1c717a830b
configure SSH hosting via "isabelle phabricator_setup_ssh";
 wenzelm parents: 
71103diff
changeset | 547 | |
| 
8c1c717a830b
configure SSH hosting via "isabelle phabricator_setup_ssh";
 wenzelm parents: 
71103diff
changeset | 548 | |
| 
8c1c717a830b
configure SSH hosting via "isabelle phabricator_setup_ssh";
 wenzelm parents: 
71103diff
changeset | 549 | /* phabricator_setup_ssh */ | 
| 
8c1c717a830b
configure SSH hosting via "isabelle phabricator_setup_ssh";
 wenzelm parents: 
71103diff
changeset | 550 | |
| 
8c1c717a830b
configure SSH hosting via "isabelle phabricator_setup_ssh";
 wenzelm parents: 
71103diff
changeset | 551 | def phabricator_setup_ssh( | 
| 
8c1c717a830b
configure SSH hosting via "isabelle phabricator_setup_ssh";
 wenzelm parents: 
71103diff
changeset | 552 | server_port: Int = default_server_port, | 
| 
8c1c717a830b
configure SSH hosting via "isabelle phabricator_setup_ssh";
 wenzelm parents: 
71103diff
changeset | 553 | system_port: Int = default_system_port, | 
| 
8c1c717a830b
configure SSH hosting via "isabelle phabricator_setup_ssh";
 wenzelm parents: 
71103diff
changeset | 554 | test_server: Boolean = false, | 
| 
8c1c717a830b
configure SSH hosting via "isabelle phabricator_setup_ssh";
 wenzelm parents: 
71103diff
changeset | 555 | progress: Progress = No_Progress) | 
| 
8c1c717a830b
configure SSH hosting via "isabelle phabricator_setup_ssh";
 wenzelm parents: 
71103diff
changeset | 556 |   {
 | 
| 
8c1c717a830b
configure SSH hosting via "isabelle phabricator_setup_ssh";
 wenzelm parents: 
71103diff
changeset | 557 | Linux.check_system_root() | 
| 
8c1c717a830b
configure SSH hosting via "isabelle phabricator_setup_ssh";
 wenzelm parents: 
71103diff
changeset | 558 | |
| 
8c1c717a830b
configure SSH hosting via "isabelle phabricator_setup_ssh";
 wenzelm parents: 
71103diff
changeset | 559 | val configs = read_config() | 
| 
8c1c717a830b
configure SSH hosting via "isabelle phabricator_setup_ssh";
 wenzelm parents: 
71103diff
changeset | 560 | |
| 
8c1c717a830b
configure SSH hosting via "isabelle phabricator_setup_ssh";
 wenzelm parents: 
71103diff
changeset | 561 |     if (server_port == system_port) {
 | 
| 
8c1c717a830b
configure SSH hosting via "isabelle phabricator_setup_ssh";
 wenzelm parents: 
71103diff
changeset | 562 |       error("Port for Phabricator sshd coincides with system port: " + system_port)
 | 
| 
8c1c717a830b
configure SSH hosting via "isabelle phabricator_setup_ssh";
 wenzelm parents: 
71103diff
changeset | 563 | } | 
| 
8c1c717a830b
configure SSH hosting via "isabelle phabricator_setup_ssh";
 wenzelm parents: 
71103diff
changeset | 564 | |
| 
8c1c717a830b
configure SSH hosting via "isabelle phabricator_setup_ssh";
 wenzelm parents: 
71103diff
changeset | 565 |     val sshd_conf_system = Path.explode("/etc/ssh/sshd_config")
 | 
| 
8c1c717a830b
configure SSH hosting via "isabelle phabricator_setup_ssh";
 wenzelm parents: 
71103diff
changeset | 566 | val sshd_conf_server = sshd_conf_system.ext(isabelle_phabricator_name()) | 
| 
8c1c717a830b
configure SSH hosting via "isabelle phabricator_setup_ssh";
 wenzelm parents: 
71103diff
changeset | 567 | |
| 
8c1c717a830b
configure SSH hosting via "isabelle phabricator_setup_ssh";
 wenzelm parents: 
71103diff
changeset | 568 | val ssh_name = isabelle_phabricator_name(name = "ssh") | 
| 
8c1c717a830b
configure SSH hosting via "isabelle phabricator_setup_ssh";
 wenzelm parents: 
71103diff
changeset | 569 |     val ssh_command = Path.explode("/usr/local/bin") + Path.basic(ssh_name)
 | 
| 
8c1c717a830b
configure SSH hosting via "isabelle phabricator_setup_ssh";
 wenzelm parents: 
71103diff
changeset | 570 | |
| 71111 
cd166c3904dd
more robust: system ssh service is required for Phabricator ssh service;
 wenzelm parents: 
71109diff
changeset | 571 | Linux.service_shutdown(ssh_name) | 
| 
cd166c3904dd
more robust: system ssh service is required for Phabricator ssh service;
 wenzelm parents: 
71109diff
changeset | 572 | |
| 71109 
8c1c717a830b
configure SSH hosting via "isabelle phabricator_setup_ssh";
 wenzelm parents: 
71103diff
changeset | 573 | val old_system_port = read_ssh_port(sshd_conf_system) | 
| 
8c1c717a830b
configure SSH hosting via "isabelle phabricator_setup_ssh";
 wenzelm parents: 
71103diff
changeset | 574 |     if (old_system_port != system_port) {
 | 
| 
8c1c717a830b
configure SSH hosting via "isabelle phabricator_setup_ssh";
 wenzelm parents: 
71103diff
changeset | 575 |       progress.echo("Reconfigurig system ssh service")
 | 
| 71111 
cd166c3904dd
more robust: system ssh service is required for Phabricator ssh service;
 wenzelm parents: 
71109diff
changeset | 576 |       Linux.service_shutdown("ssh")
 | 
| 71109 
8c1c717a830b
configure SSH hosting via "isabelle phabricator_setup_ssh";
 wenzelm parents: 
71103diff
changeset | 577 | 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 | 578 |       Linux.service_start("ssh")
 | 
| 71109 
8c1c717a830b
configure SSH hosting via "isabelle phabricator_setup_ssh";
 wenzelm parents: 
71103diff
changeset | 579 | } | 
| 
8c1c717a830b
configure SSH hosting via "isabelle phabricator_setup_ssh";
 wenzelm parents: 
71103diff
changeset | 580 | |
| 
8c1c717a830b
configure SSH hosting via "isabelle phabricator_setup_ssh";
 wenzelm parents: 
71103diff
changeset | 581 |     progress.echo("Configuring " + ssh_name + " service")
 | 
| 
8c1c717a830b
configure SSH hosting via "isabelle phabricator_setup_ssh";
 wenzelm parents: 
71103diff
changeset | 582 | |
| 
8c1c717a830b
configure SSH hosting via "isabelle phabricator_setup_ssh";
 wenzelm parents: 
71103diff
changeset | 583 | File.write(ssh_command, | 
| 
8c1c717a830b
configure SSH hosting via "isabelle phabricator_setup_ssh";
 wenzelm parents: 
71103diff
changeset | 584 | """#!/bin/bash | 
| 
8c1c717a830b
configure SSH hosting via "isabelle phabricator_setup_ssh";
 wenzelm parents: 
71103diff
changeset | 585 | {
 | 
| 
8c1c717a830b
configure SSH hosting via "isabelle phabricator_setup_ssh";
 wenzelm parents: 
71103diff
changeset | 586 |   while { unset REPLY; read -r; test "$?" = 0 -o -n "$REPLY"; }
 | 
| 
8c1c717a830b
configure SSH hosting via "isabelle phabricator_setup_ssh";
 wenzelm parents: 
71103diff
changeset | 587 | do | 
| 
8c1c717a830b
configure SSH hosting via "isabelle phabricator_setup_ssh";
 wenzelm parents: 
71103diff
changeset | 588 | NAME="$(echo "$REPLY" | cut -d: -f1)" | 
| 
8c1c717a830b
configure SSH hosting via "isabelle phabricator_setup_ssh";
 wenzelm parents: 
71103diff
changeset | 589 | ROOT="$(echo "$REPLY" | cut -d: -f2)" | 
| 
8c1c717a830b
configure SSH hosting via "isabelle phabricator_setup_ssh";
 wenzelm parents: 
71103diff
changeset | 590 | if [ "$1" = "$NAME" ] | 
| 
8c1c717a830b
configure SSH hosting via "isabelle phabricator_setup_ssh";
 wenzelm parents: 
71103diff
changeset | 591 | then | 
| 
8c1c717a830b
configure SSH hosting via "isabelle phabricator_setup_ssh";
 wenzelm parents: 
71103diff
changeset | 592 | exec "$ROOT/phabricator/bin/ssh-auth" "$@" | 
| 
8c1c717a830b
configure SSH hosting via "isabelle phabricator_setup_ssh";
 wenzelm parents: 
71103diff
changeset | 593 | fi | 
| 
8c1c717a830b
configure SSH hosting via "isabelle phabricator_setup_ssh";
 wenzelm parents: 
71103diff
changeset | 594 | done | 
| 
8c1c717a830b
configure SSH hosting via "isabelle phabricator_setup_ssh";
 wenzelm parents: 
71103diff
changeset | 595 | exit 1 | 
| 
8c1c717a830b
configure SSH hosting via "isabelle phabricator_setup_ssh";
 wenzelm parents: 
71103diff
changeset | 596 | } < /etc/isabelle-phabricator.conf | 
| 
8c1c717a830b
configure SSH hosting via "isabelle phabricator_setup_ssh";
 wenzelm parents: 
71103diff
changeset | 597 | """) | 
| 71114 | 598 |     Isabelle_System.chmod("755", ssh_command)
 | 
| 599 |     Isabelle_System.chown("root:root", ssh_command)
 | |
| 71109 
8c1c717a830b
configure SSH hosting via "isabelle phabricator_setup_ssh";
 wenzelm parents: 
71103diff
changeset | 600 | |
| 
8c1c717a830b
configure SSH hosting via "isabelle phabricator_setup_ssh";
 wenzelm parents: 
71103diff
changeset | 601 | File.write(sshd_conf_server, | 
| 
8c1c717a830b
configure SSH hosting via "isabelle phabricator_setup_ssh";
 wenzelm parents: 
71103diff
changeset | 602 | """# OpenBSD Secure Shell server for Isabelle/Phabricator | 
| 
8c1c717a830b
configure SSH hosting via "isabelle phabricator_setup_ssh";
 wenzelm parents: 
71103diff
changeset | 603 | AuthorizedKeysCommand """ + ssh_command.implode + """ | 
| 
8c1c717a830b
configure SSH hosting via "isabelle phabricator_setup_ssh";
 wenzelm parents: 
71103diff
changeset | 604 | AuthorizedKeysCommandUser """ + daemon_user + """ | 
| 
8c1c717a830b
configure SSH hosting via "isabelle phabricator_setup_ssh";
 wenzelm parents: 
71103diff
changeset | 605 | AuthorizedKeysFile none | 
| 
8c1c717a830b
configure SSH hosting via "isabelle phabricator_setup_ssh";
 wenzelm parents: 
71103diff
changeset | 606 | AllowUsers """ + configs.map(_.name).mkString(" ") + """
 | 
| 
8c1c717a830b
configure SSH hosting via "isabelle phabricator_setup_ssh";
 wenzelm parents: 
71103diff
changeset | 607 | Port """ + server_port + """ | 
| 
8c1c717a830b
configure SSH hosting via "isabelle phabricator_setup_ssh";
 wenzelm parents: 
71103diff
changeset | 608 | Protocol 2 | 
| 
8c1c717a830b
configure SSH hosting via "isabelle phabricator_setup_ssh";
 wenzelm parents: 
71103diff
changeset | 609 | PermitRootLogin no | 
| 
8c1c717a830b
configure SSH hosting via "isabelle phabricator_setup_ssh";
 wenzelm parents: 
71103diff
changeset | 610 | AllowAgentForwarding no | 
| 
8c1c717a830b
configure SSH hosting via "isabelle phabricator_setup_ssh";
 wenzelm parents: 
71103diff
changeset | 611 | AllowTcpForwarding no | 
| 
8c1c717a830b
configure SSH hosting via "isabelle phabricator_setup_ssh";
 wenzelm parents: 
71103diff
changeset | 612 | PrintMotd no | 
| 
8c1c717a830b
configure SSH hosting via "isabelle phabricator_setup_ssh";
 wenzelm parents: 
71103diff
changeset | 613 | PrintLastLog no | 
| 
8c1c717a830b
configure SSH hosting via "isabelle phabricator_setup_ssh";
 wenzelm parents: 
71103diff
changeset | 614 | PasswordAuthentication no | 
| 
8c1c717a830b
configure SSH hosting via "isabelle phabricator_setup_ssh";
 wenzelm parents: 
71103diff
changeset | 615 | ChallengeResponseAuthentication no | 
| 
8c1c717a830b
configure SSH hosting via "isabelle phabricator_setup_ssh";
 wenzelm parents: 
71103diff
changeset | 616 | PidFile /var/run/""" + ssh_name + """.pid | 
| 
8c1c717a830b
configure SSH hosting via "isabelle phabricator_setup_ssh";
 wenzelm parents: 
71103diff
changeset | 617 | """) | 
| 
8c1c717a830b
configure SSH hosting via "isabelle phabricator_setup_ssh";
 wenzelm parents: 
71103diff
changeset | 618 | |
| 
8c1c717a830b
configure SSH hosting via "isabelle phabricator_setup_ssh";
 wenzelm parents: 
71103diff
changeset | 619 | Linux.service_install(ssh_name, | 
| 
8c1c717a830b
configure SSH hosting via "isabelle phabricator_setup_ssh";
 wenzelm parents: 
71103diff
changeset | 620 | """[Unit] | 
| 
8c1c717a830b
configure SSH hosting via "isabelle phabricator_setup_ssh";
 wenzelm parents: 
71103diff
changeset | 621 | Description=OpenBSD Secure Shell server for Isabelle/Phabricator | 
| 
8c1c717a830b
configure SSH hosting via "isabelle phabricator_setup_ssh";
 wenzelm parents: 
71103diff
changeset | 622 | After=network.target auditd.service | 
| 
8c1c717a830b
configure SSH hosting via "isabelle phabricator_setup_ssh";
 wenzelm parents: 
71103diff
changeset | 623 | ConditionPathExists=!/etc/ssh/sshd_not_to_be_run | 
| 
8c1c717a830b
configure SSH hosting via "isabelle phabricator_setup_ssh";
 wenzelm parents: 
71103diff
changeset | 624 | |
| 
8c1c717a830b
configure SSH hosting via "isabelle phabricator_setup_ssh";
 wenzelm parents: 
71103diff
changeset | 625 | [Service] | 
| 
8c1c717a830b
configure SSH hosting via "isabelle phabricator_setup_ssh";
 wenzelm parents: 
71103diff
changeset | 626 | EnvironmentFile=-/etc/default/ssh | 
| 
8c1c717a830b
configure SSH hosting via "isabelle phabricator_setup_ssh";
 wenzelm parents: 
71103diff
changeset | 627 | ExecStartPre=/usr/sbin/sshd -f """ + sshd_conf_server.implode + """ -t | 
| 
8c1c717a830b
configure SSH hosting via "isabelle phabricator_setup_ssh";
 wenzelm parents: 
71103diff
changeset | 628 | 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 | 629 | ExecReload=/usr/sbin/sshd -f """ + sshd_conf_server.implode + """ -t | 
| 
8c1c717a830b
configure SSH hosting via "isabelle phabricator_setup_ssh";
 wenzelm parents: 
71103diff
changeset | 630 | ExecReload=/bin/kill -HUP $MAINPID | 
| 
8c1c717a830b
configure SSH hosting via "isabelle phabricator_setup_ssh";
 wenzelm parents: 
71103diff
changeset | 631 | KillMode=process | 
| 
8c1c717a830b
configure SSH hosting via "isabelle phabricator_setup_ssh";
 wenzelm parents: 
71103diff
changeset | 632 | Restart=on-failure | 
| 
8c1c717a830b
configure SSH hosting via "isabelle phabricator_setup_ssh";
 wenzelm parents: 
71103diff
changeset | 633 | RestartPreventExitStatus=255 | 
| 
8c1c717a830b
configure SSH hosting via "isabelle phabricator_setup_ssh";
 wenzelm parents: 
71103diff
changeset | 634 | Type=notify | 
| 
8c1c717a830b
configure SSH hosting via "isabelle phabricator_setup_ssh";
 wenzelm parents: 
71103diff
changeset | 635 | RuntimeDirectory=sshd-phabricator | 
| 
8c1c717a830b
configure SSH hosting via "isabelle phabricator_setup_ssh";
 wenzelm parents: 
71103diff
changeset | 636 | RuntimeDirectoryMode=0755 | 
| 
8c1c717a830b
configure SSH hosting via "isabelle phabricator_setup_ssh";
 wenzelm parents: 
71103diff
changeset | 637 | |
| 
8c1c717a830b
configure SSH hosting via "isabelle phabricator_setup_ssh";
 wenzelm parents: 
71103diff
changeset | 638 | [Install] | 
| 
8c1c717a830b
configure SSH hosting via "isabelle phabricator_setup_ssh";
 wenzelm parents: 
71103diff
changeset | 639 | WantedBy=multi-user.target | 
| 
8c1c717a830b
configure SSH hosting via "isabelle phabricator_setup_ssh";
 wenzelm parents: 
71103diff
changeset | 640 | Alias=""" + ssh_name + """.service | 
| 
8c1c717a830b
configure SSH hosting via "isabelle phabricator_setup_ssh";
 wenzelm parents: 
71103diff
changeset | 641 | """) | 
| 
8c1c717a830b
configure SSH hosting via "isabelle phabricator_setup_ssh";
 wenzelm parents: 
71103diff
changeset | 642 | |
| 
8c1c717a830b
configure SSH hosting via "isabelle phabricator_setup_ssh";
 wenzelm parents: 
71103diff
changeset | 643 |     for (config <- configs) {
 | 
| 
8c1c717a830b
configure SSH hosting via "isabelle phabricator_setup_ssh";
 wenzelm parents: 
71103diff
changeset | 644 |       progress.echo("phabricator " + quote(config.name) + " port " +  server_port)
 | 
| 
8c1c717a830b
configure SSH hosting via "isabelle phabricator_setup_ssh";
 wenzelm parents: 
71103diff
changeset | 645 |       config.execute("config set diffusion.ssh-port " + Bash.string(server_port.toString))
 | 
| 
8c1c717a830b
configure SSH hosting via "isabelle phabricator_setup_ssh";
 wenzelm parents: 
71103diff
changeset | 646 | |
| 
8c1c717a830b
configure SSH hosting via "isabelle phabricator_setup_ssh";
 wenzelm parents: 
71103diff
changeset | 647 |       if (test_server) {
 | 
| 
8c1c717a830b
configure SSH hosting via "isabelle phabricator_setup_ssh";
 wenzelm parents: 
71103diff
changeset | 648 | progress.bash( | 
| 
8c1c717a830b
configure SSH hosting via "isabelle phabricator_setup_ssh";
 wenzelm parents: 
71103diff
changeset | 649 | """unset DISPLAY | 
| 
8c1c717a830b
configure SSH hosting via "isabelle phabricator_setup_ssh";
 wenzelm parents: 
71103diff
changeset | 650 |           echo "{}" | ssh -p """ + Bash.string(server_port.toString) +
 | 
| 
8c1c717a830b
configure SSH hosting via "isabelle phabricator_setup_ssh";
 wenzelm parents: 
71103diff
changeset | 651 | " -o StrictHostKeyChecking=false " + | 
| 
8c1c717a830b
configure SSH hosting via "isabelle phabricator_setup_ssh";
 wenzelm parents: 
71103diff
changeset | 652 | Bash.string(config.name) + """@localhost conduit conduit.ping""").print | 
| 
8c1c717a830b
configure SSH hosting via "isabelle phabricator_setup_ssh";
 wenzelm parents: 
71103diff
changeset | 653 | } | 
| 
8c1c717a830b
configure SSH hosting via "isabelle phabricator_setup_ssh";
 wenzelm parents: 
71103diff
changeset | 654 | } | 
| 
8c1c717a830b
configure SSH hosting via "isabelle phabricator_setup_ssh";
 wenzelm parents: 
71103diff
changeset | 655 | } | 
| 
8c1c717a830b
configure SSH hosting via "isabelle phabricator_setup_ssh";
 wenzelm parents: 
71103diff
changeset | 656 | |
| 
8c1c717a830b
configure SSH hosting via "isabelle phabricator_setup_ssh";
 wenzelm parents: 
71103diff
changeset | 657 | |
| 
8c1c717a830b
configure SSH hosting via "isabelle phabricator_setup_ssh";
 wenzelm parents: 
71103diff
changeset | 658 | /* Isabelle tool wrapper */ | 
| 
8c1c717a830b
configure SSH hosting via "isabelle phabricator_setup_ssh";
 wenzelm parents: 
71103diff
changeset | 659 | |
| 
8c1c717a830b
configure SSH hosting via "isabelle phabricator_setup_ssh";
 wenzelm parents: 
71103diff
changeset | 660 | val isabelle_tool4 = | 
| 
8c1c717a830b
configure SSH hosting via "isabelle phabricator_setup_ssh";
 wenzelm parents: 
71103diff
changeset | 661 |     Isabelle_Tool("phabricator_setup_ssh",
 | 
| 
8c1c717a830b
configure SSH hosting via "isabelle phabricator_setup_ssh";
 wenzelm parents: 
71103diff
changeset | 662 | "setup ssh service for all Phabricator installations", args => | 
| 
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 | var server_port = default_server_port | 
| 
8c1c717a830b
configure SSH hosting via "isabelle phabricator_setup_ssh";
 wenzelm parents: 
71103diff
changeset | 665 | var system_port = default_system_port | 
| 
8c1c717a830b
configure SSH hosting via "isabelle phabricator_setup_ssh";
 wenzelm parents: 
71103diff
changeset | 666 | var test_server = false | 
| 
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 | val getopts = | 
| 
8c1c717a830b
configure SSH hosting via "isabelle phabricator_setup_ssh";
 wenzelm parents: 
71103diff
changeset | 669 |         Getopts("""
 | 
| 
8c1c717a830b
configure SSH hosting via "isabelle phabricator_setup_ssh";
 wenzelm parents: 
71103diff
changeset | 670 | Usage: isabelle phabricator_setup_ssh [OPTIONS] | 
| 
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 | Options are: | 
| 
8c1c717a830b
configure SSH hosting via "isabelle phabricator_setup_ssh";
 wenzelm parents: 
71103diff
changeset | 673 | -p PORT sshd port for Phabricator servers (default: """ + default_server_port + """) | 
| 
8c1c717a830b
configure SSH hosting via "isabelle phabricator_setup_ssh";
 wenzelm parents: 
71103diff
changeset | 674 | -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 | 675 | -T test the ssh service for each Phabricator installation | 
| 
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 | Configure ssh service for all Phabricator installations: a separate sshd | 
| 
8c1c717a830b
configure SSH hosting via "isabelle phabricator_setup_ssh";
 wenzelm parents: 
71103diff
changeset | 678 | 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 | 679 | be distinct. | 
| 
8c1c717a830b
configure SSH hosting via "isabelle phabricator_setup_ssh";
 wenzelm parents: 
71103diff
changeset | 680 | |
| 
8c1c717a830b
configure SSH hosting via "isabelle phabricator_setup_ssh";
 wenzelm parents: 
71103diff
changeset | 681 | A particular Phabricator installation is addressed by using its | 
| 
8c1c717a830b
configure SSH hosting via "isabelle phabricator_setup_ssh";
 wenzelm parents: 
71103diff
changeset | 682 | 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 | 683 | stored ssh keys. | 
| 
8c1c717a830b
configure SSH hosting via "isabelle phabricator_setup_ssh";
 wenzelm parents: 
71103diff
changeset | 684 | """, | 
| 
8c1c717a830b
configure SSH hosting via "isabelle phabricator_setup_ssh";
 wenzelm parents: 
71103diff
changeset | 685 | "p:" -> (arg => server_port = Value.Int.parse(arg)), | 
| 
8c1c717a830b
configure SSH hosting via "isabelle phabricator_setup_ssh";
 wenzelm parents: 
71103diff
changeset | 686 | "q:" -> (arg => system_port = Value.Int.parse(arg)), | 
| 
8c1c717a830b
configure SSH hosting via "isabelle phabricator_setup_ssh";
 wenzelm parents: 
71103diff
changeset | 687 | "T" -> (_ => test_server = true)) | 
| 
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 | val more_args = getopts(args) | 
| 
8c1c717a830b
configure SSH hosting via "isabelle phabricator_setup_ssh";
 wenzelm parents: 
71103diff
changeset | 690 | if (more_args.nonEmpty) getopts.usage() | 
| 
8c1c717a830b
configure SSH hosting via "isabelle phabricator_setup_ssh";
 wenzelm parents: 
71103diff
changeset | 691 | |
| 
8c1c717a830b
configure SSH hosting via "isabelle phabricator_setup_ssh";
 wenzelm parents: 
71103diff
changeset | 692 | val progress = new Console_Progress | 
| 
8c1c717a830b
configure SSH hosting via "isabelle phabricator_setup_ssh";
 wenzelm parents: 
71103diff
changeset | 693 | |
| 
8c1c717a830b
configure SSH hosting via "isabelle phabricator_setup_ssh";
 wenzelm parents: 
71103diff
changeset | 694 | phabricator_setup_ssh( | 
| 
8c1c717a830b
configure SSH hosting via "isabelle phabricator_setup_ssh";
 wenzelm parents: 
71103diff
changeset | 695 | server_port = server_port, system_port = system_port, test_server = test_server, | 
| 
8c1c717a830b
configure SSH hosting via "isabelle phabricator_setup_ssh";
 wenzelm parents: 
71103diff
changeset | 696 | progress = progress) | 
| 
8c1c717a830b
configure SSH hosting via "isabelle phabricator_setup_ssh";
 wenzelm parents: 
71103diff
changeset | 697 | }) | 
| 70967 | 698 | } |