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