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