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