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