author | wenzelm |
Tue, 05 Nov 2019 22:00:29 +0100 | |
changeset 71054 | b64fc38327ae |
parent 71053 | ba14aa0b5a5d |
child 71055 | 27a998cdc0f4 |
permissions | -rw-r--r-- |
70967 | 1 |
/* Title: Pure/Tools/phabricator.scala |
2 |
Author: Makarius |
|
3 |
||
4 |
Support for Phabricator server. See also: |
|
5 |
- https://www.phacility.com/phabricator |
|
6 |
- https://secure.phabricator.com/book/phabricator |
|
7 |
*/ |
|
8 |
||
9 |
package isabelle |
|
10 |
||
11 |
||
70969 | 12 |
import scala.util.matching.Regex |
13 |
||
14 |
||
70967 | 15 |
object Phabricator |
16 |
{ |
|
17 |
/** defaults **/ |
|
18 |
||
71049 | 19 |
/* required packages */ |
20 |
||
21 |
val packages: List[String] = |
|
22 |
Build_Docker.packages ::: |
|
23 |
List( |
|
24 |
// https://secure.phabricator.com/source/phabricator/browse/master/scripts/install/install_ubuntu.sh 15e6e2adea61 |
|
25 |
"git", "mysql-server", "apache2", "libapache2-mod-php", "php", "php-mysql", |
|
26 |
"php-gd", "php-curl", "php-apcu", "php-cli", "php-json", "php-mbstring", |
|
27 |
// more packages |
|
28 |
"php-zip", "python-pygments", "ssh") |
|
29 |
||
30 |
||
31 |
/* global system resources */ |
|
32 |
||
33 |
val daemon_user = "phabricator" |
|
34 |
||
35 |
val ssh_standard = 22 |
|
36 |
val ssh_alternative1 = 222 |
|
37 |
val ssh_alternative2 = 2222 |
|
38 |
||
39 |
||
40 |
/* installation parameters */ |
|
41 |
||
70967 | 42 |
val default_name = "vcs" |
43 |
||
71052
6bf53035baf0
clarified name prefixes: global config always uses "isabelle-phabricator";
wenzelm
parents:
71051
diff
changeset
|
44 |
def phabricator_name(name: String = "", ext: String = ""): String = |
6bf53035baf0
clarified name prefixes: global config always uses "isabelle-phabricator";
wenzelm
parents:
71051
diff
changeset
|
45 |
"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
|
46 |
|
6bf53035baf0
clarified name prefixes: global config always uses "isabelle-phabricator";
wenzelm
parents:
71051
diff
changeset
|
47 |
def isabelle_phabricator_name(name: String = "", ext: String = ""): String = |
6bf53035baf0
clarified name prefixes: global config always uses "isabelle-phabricator";
wenzelm
parents:
71051
diff
changeset
|
48 |
"isabelle-" + phabricator_name(name = name, ext = ext) |
70967 | 49 |
|
50 |
def default_root(options: Options, name: String): Path = |
|
71052
6bf53035baf0
clarified name prefixes: global config always uses "isabelle-phabricator";
wenzelm
parents:
71051
diff
changeset
|
51 |
Path.explode(options.string("phabricator_www_root")) + |
6bf53035baf0
clarified name prefixes: global config always uses "isabelle-phabricator";
wenzelm
parents:
71051
diff
changeset
|
52 |
Path.basic(phabricator_name(name = name)) |
70967 | 53 |
|
54 |
def default_repo(options: Options, name: String): Path = |
|
55 |
default_root(options, name) + Path.basic("repo") |
|
56 |
||
57 |
||
58 |
||
59 |
/** global configuration **/ |
|
60 |
||
71052
6bf53035baf0
clarified name prefixes: global config always uses "isabelle-phabricator";
wenzelm
parents:
71051
diff
changeset
|
61 |
val global_config = Path.explode("/etc/" + isabelle_phabricator_name(ext = "conf")) |
70967 | 62 |
|
63 |
sealed case class Config(name: String, root: Path) |
|
70968 | 64 |
{ |
71052
6bf53035baf0
clarified name prefixes: global config always uses "isabelle-phabricator";
wenzelm
parents:
71051
diff
changeset
|
65 |
def home: Path = root + Path.explode(phabricator_name()) |
70969 | 66 |
|
67 |
def execute(command: String): Process_Result = |
|
68 |
Isabelle_System.bash("./bin/" + command, cwd = home.file).check |
|
70968 | 69 |
} |
70967 | 70 |
|
71 |
def read_config(): List[Config] = |
|
72 |
{ |
|
73 |
if (global_config.is_file) { |
|
74 |
for (entry <- Library.trim_split_lines(File.read(global_config)) if entry.nonEmpty) |
|
75 |
yield { |
|
76 |
space_explode(':', entry) match { |
|
77 |
case List(name, root) => Config(name, Path.explode(root)) |
|
78 |
case _ => error("Malformed config file " + global_config + "\nentry " + quote(entry)) |
|
79 |
} |
|
80 |
} |
|
81 |
} |
|
82 |
else Nil |
|
83 |
} |
|
84 |
||
85 |
def write_config(configs: List[Config]) |
|
86 |
{ |
|
87 |
File.write(global_config, |
|
88 |
configs.map(config => config.name + ":" + config.root.implode).mkString("", "\n", "\n")) |
|
89 |
} |
|
90 |
||
91 |
def get_config(name: String): Config = |
|
92 |
read_config().find(config => config.name == name) getOrElse |
|
93 |
error("Bad Isabelle/Phabricator installation " + quote(name)) |
|
94 |
||
95 |
||
96 |
||
97 |
/** setup **/ |
|
98 |
||
71049 | 99 |
def user_setup(name: String, description: String, ssh_setup: Boolean = false) |
100 |
{ |
|
101 |
if (!Linux.user_exists(name)) { |
|
71054
b64fc38327ae
prefer system user setup, e.g. avoid occurrence on login screen;
wenzelm
parents:
71053
diff
changeset
|
102 |
Linux.user_add(name, description = description, system = true, ssh_setup = ssh_setup) |
71049 | 103 |
} |
104 |
else if (Linux.user_description(name) != description) { |
|
105 |
error("User " + quote(name) + " already exists --" + |
|
106 |
" for Phabricator it should have the description:\n " + quote(description)) |
|
107 |
} |
|
108 |
} |
|
109 |
||
70967 | 110 |
def phabricator_setup( |
111 |
options: Options, |
|
112 |
name: String = default_name, |
|
113 |
root: String = "", |
|
114 |
repo: String = "", |
|
71047 | 115 |
package_update: Boolean = false, |
70967 | 116 |
progress: Progress = No_Progress) |
117 |
{ |
|
118 |
/* system environment */ |
|
119 |
||
120 |
Linux.check_system_root() |
|
121 |
||
71047 | 122 |
if (package_update) { |
123 |
Linux.package_update(progress = progress) |
|
124 |
Linux.check_reboot_required() |
|
125 |
} |
|
70967 | 126 |
|
127 |
Linux.package_install(packages, progress = progress) |
|
128 |
Linux.check_reboot_required() |
|
129 |
||
130 |
||
71049 | 131 |
/* users */ |
132 |
||
133 |
if (name == daemon_user) { |
|
134 |
error("Clash of installation name with daemon user " + quote(daemon_user)) |
|
135 |
} |
|
136 |
||
137 |
user_setup(daemon_user, "Phabricator Daemon User", ssh_setup = true) |
|
138 |
user_setup(name, "Phabricator SSH User") |
|
139 |
||
140 |
val www_user = options.string("phabricator_www_user") |
|
141 |
||
142 |
||
70967 | 143 |
/* basic installation */ |
144 |
||
145 |
val root_path = if (root.nonEmpty) Path.explode(root) else default_root(options, name) |
|
146 |
val repo_path = if (repo.nonEmpty) Path.explode(repo) else default_repo(options, name) |
|
147 |
||
148 |
val configs = read_config() |
|
149 |
||
150 |
for (config <- configs if config.name == name) { |
|
151 |
error("Duplicate Phabricator installation " + quote(name) + " in " + config.root) |
|
152 |
} |
|
153 |
||
154 |
if (!Isabelle_System.bash("mkdir -p " + File.bash_path(root_path)).ok) { |
|
155 |
error("Failed to create root directory " + root_path) |
|
156 |
} |
|
157 |
||
158 |
progress.bash(cwd = root_path.file, echo = true, |
|
159 |
script = """ |
|
160 |
set -e |
|
71050 | 161 |
chown """ + Bash.string(www_user) + ":" + Bash.string(www_user) + """ . |
70967 | 162 |
chmod 755 . |
163 |
||
164 |
git clone https://github.com/phacility/libphutil.git |
|
165 |
git clone https://github.com/phacility/arcanist.git |
|
166 |
git clone https://github.com/phacility/phabricator.git |
|
167 |
""").check |
|
168 |
||
169 |
val config = Config(name, root_path) |
|
170 |
write_config(configs ::: List(config)) |
|
70968 | 171 |
|
71051 | 172 |
config.execute("config set pygments.enabled true") |
173 |
||
70968 | 174 |
|
71050 | 175 |
/* local repository directory */ |
176 |
||
177 |
if (!Isabelle_System.bash("mkdir -p " + File.bash_path(repo_path)).ok) { |
|
178 |
error("Failed to create local repository directory " + repo_path) |
|
179 |
} |
|
180 |
||
181 |
Isabelle_System.bash(cwd = repo_path.file, |
|
182 |
script = """ |
|
183 |
set -e |
|
184 |
chown -R """ + Bash.string(daemon_user) + ":" + Bash.string(daemon_user) + """ . |
|
185 |
chmod 755 . |
|
186 |
""").check |
|
187 |
||
188 |
config.execute("config set repository.default-local-path " + File.bash_path(repo_path)) |
|
189 |
||
190 |
||
70969 | 191 |
/* MySQL setup */ |
192 |
||
193 |
progress.echo("MySQL setup...") |
|
194 |
||
71052
6bf53035baf0
clarified name prefixes: global config always uses "isabelle-phabricator";
wenzelm
parents:
71051
diff
changeset
|
195 |
File.write(Path.explode("/etc/mysql/mysql.conf.d/" + isabelle_phabricator_name(ext = "cnf")), |
71051 | 196 |
"""[mysqld] |
197 |
max_allowed_packet = 32M |
|
198 |
innodb_buffer_pool_size = 1600M |
|
199 |
local_infile = 0 |
|
200 |
""") |
|
201 |
||
202 |
Linux.service_restart("mysql") |
|
203 |
||
204 |
||
70969 | 205 |
def mysql_conf(R: Regex): Option[String] = |
206 |
split_lines(File.read(Path.explode(options.string("phabricator_mysql_config")))). |
|
207 |
collectFirst({ case R(a) => a }) |
|
208 |
||
209 |
for (user <- mysql_conf("""^user\s*=\s*(\S*)\s*$""".r)) { |
|
210 |
config.execute("config set mysql.user " + Bash.string(user)) |
|
211 |
} |
|
212 |
||
213 |
for (pass <- mysql_conf("""^password\s*=\s*(\S*)\s*$""".r)) { |
|
214 |
config.execute("config set mysql.pass " + Bash.string(pass)) |
|
215 |
} |
|
216 |
||
217 |
config.execute("config set storage.default-namespace " + |
|
71052
6bf53035baf0
clarified name prefixes: global config always uses "isabelle-phabricator";
wenzelm
parents:
71051
diff
changeset
|
218 |
Bash.string(phabricator_name(name = name).replace("-", "_"))) |
70969 | 219 |
|
71051 | 220 |
config.execute("config set storage.mysql-engine.max-size 8388608") |
221 |
||
70969 | 222 |
config.execute("storage upgrade --force") |
223 |
||
224 |
||
71049 | 225 |
/* SSH hosting */ |
226 |
||
227 |
progress.echo("SSH hosting setup...") |
|
228 |
||
229 |
val ssh_port = ssh_alternative2 |
|
230 |
||
231 |
config.execute("config set diffusion.ssh-user " + Bash.string(name)) |
|
232 |
config.execute("config set diffusion.ssh-port " + ssh_port) |
|
233 |
||
71052
6bf53035baf0
clarified name prefixes: global config always uses "isabelle-phabricator";
wenzelm
parents:
71051
diff
changeset
|
234 |
val sudoers_file = Path.explode("/etc/sudoers.d") + Path.basic(isabelle_phabricator_name()) |
71049 | 235 |
File.write(sudoers_file, |
236 |
www_user + " ALL=(" + daemon_user + ") SETENV: NOPASSWD: /usr/bin/git, /usr/bin/hg, /usr/bin/ssh, /usr/bin/id\n" + |
|
237 |
name + " ALL=(" + daemon_user + ") SETENV: NOPASSWD: /usr/bin/git, /usr/bin/git-upload-pack, /usr/bin/git-receive-pack, /usr/bin/hg, /usr/bin/svnserve, /usr/bin/ssh, /usr/bin/id\n") |
|
238 |
||
239 |
Isabelle_System.bash("chmod 0440 " + File.bash_path(sudoers_file)).check |
|
240 |
||
241 |
||
71051 | 242 |
/* PHP setup */ |
243 |
||
244 |
val php_version = |
|
245 |
Isabelle_System.bash("""php --run 'echo PHP_MAJOR_VERSION . "." . PHP_MINOR_VERSION;'""") |
|
246 |
.check.out |
|
247 |
||
248 |
val php_conf = |
|
249 |
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
|
250 |
Path.explode("apache2/conf.d") + |
6bf53035baf0
clarified name prefixes: global config always uses "isabelle-phabricator";
wenzelm
parents:
71051
diff
changeset
|
251 |
Path.basic(isabelle_phabricator_name(ext = "ini")) |
71051 | 252 |
|
253 |
File.write(php_conf, |
|
254 |
"post_max_size = 32M\n" + |
|
255 |
"opcache.validate_timestamps = 0\n" + |
|
256 |
"memory_limit = 512M\n") |
|
257 |
||
258 |
||
70968 | 259 |
/* Apache setup */ |
260 |
||
261 |
progress.echo("Apache setup...") |
|
262 |
||
263 |
val apache_root = Path.explode(options.string("phabricator_apache_root")) |
|
264 |
val apache_sites = apache_root + Path.explode("sites-available") |
|
265 |
||
266 |
if (!apache_sites.is_dir) error("Bad Apache sites directory " + apache_sites) |
|
267 |
||
71052
6bf53035baf0
clarified name prefixes: global config always uses "isabelle-phabricator";
wenzelm
parents:
71051
diff
changeset
|
268 |
val server_name = phabricator_name(ext = "lvh.me") // alias for "localhost" for testing |
6bf53035baf0
clarified name prefixes: global config always uses "isabelle-phabricator";
wenzelm
parents:
71051
diff
changeset
|
269 |
val server_url = "http://" + server_name |
6bf53035baf0
clarified name prefixes: global config always uses "isabelle-phabricator";
wenzelm
parents:
71051
diff
changeset
|
270 |
|
6bf53035baf0
clarified name prefixes: global config always uses "isabelle-phabricator";
wenzelm
parents:
71051
diff
changeset
|
271 |
File.write(apache_sites + Path.basic(isabelle_phabricator_name(ext = "conf")), |
70968 | 272 |
"""<VirtualHost *:80> |
71052
6bf53035baf0
clarified name prefixes: global config always uses "isabelle-phabricator";
wenzelm
parents:
71051
diff
changeset
|
273 |
ServerName """ + server_name + """ |
70968 | 274 |
ServerAdmin webmaster@localhost |
70969 | 275 |
DocumentRoot """ + config.home.implode + """/webroot |
70968 | 276 |
|
277 |
ErrorLog ${APACHE_LOG_DIR}/error.log |
|
278 |
RewriteEngine on |
|
279 |
RewriteRule ^(.*)$ /index.php?__path__=$1 [B,L,QSA] |
|
280 |
</VirtualHost> |
|
281 |
||
282 |
# vim: syntax=apache ts=4 sw=4 sts=4 sr noet |
|
283 |
""") |
|
284 |
||
71051 | 285 |
Isabelle_System.bash( """ |
70968 | 286 |
set -e |
287 |
a2enmod rewrite |
|
71052
6bf53035baf0
clarified name prefixes: global config always uses "isabelle-phabricator";
wenzelm
parents:
71051
diff
changeset
|
288 |
a2ensite """ + Bash.string(isabelle_phabricator_name())).check |
71051 | 289 |
|
290 |
Linux.service_restart("apache2") |
|
70968 | 291 |
|
71053 | 292 |
|
293 |
/* PHP daemon */ |
|
294 |
||
295 |
progress.echo("PHP daemon setup...") |
|
296 |
||
297 |
config.execute("config set phd.user " + Bash.string(daemon_user)) |
|
298 |
||
299 |
Linux.service_install(isabelle_phabricator_name(), |
|
300 |
"""[Unit] |
|
301 |
Description=PHP daemon for Isabelle/Phabricator """ + quote(name) + """ |
|
302 |
After=syslog.target network.target apache2.service mysql.service |
|
303 |
||
304 |
[Service] |
|
305 |
Type=oneshot |
|
306 |
User=""" + daemon_user + """ |
|
307 |
Group=""" + daemon_user + """ |
|
308 |
Environment=PATH=/sbin:/usr/sbin:/usr/local/sbin:/usr/local/bin:/usr/bin:/bin |
|
309 |
ExecStart=""" + config.home.implode + """/bin/phd start |
|
310 |
ExecStop=""" + config.home.implode + """/bin/phd stop |
|
311 |
RemainAfterExit=yes |
|
312 |
||
313 |
[Install] |
|
314 |
WantedBy=multi-user.target |
|
315 |
""") |
|
316 |
||
317 |
||
71052
6bf53035baf0
clarified name prefixes: global config always uses "isabelle-phabricator";
wenzelm
parents:
71051
diff
changeset
|
318 |
progress.echo("\nDONE\nWeb configuration via " + server_url) |
70967 | 319 |
} |
320 |
||
321 |
||
322 |
/* Isabelle tool wrapper */ |
|
323 |
||
324 |
val isabelle_tool1 = |
|
325 |
Isabelle_Tool("phabricator_setup", "setup Phabricator server on Ubuntu Linux", args => |
|
326 |
{ |
|
71047 | 327 |
var repo = "" |
328 |
var package_update = false |
|
70967 | 329 |
var options = Options.init() |
330 |
var root = "" |
|
331 |
||
332 |
val getopts = |
|
333 |
Getopts(""" |
|
334 |
Usage: isabelle phabricator_setup [OPTIONS] [NAME] |
|
335 |
||
336 |
Options are: |
|
337 |
-R DIR repository directory (default: """ + default_repo(options, "NAME") + """) |
|
71047 | 338 |
-U full update of system packages before installation |
70967 | 339 |
-o OPTION override Isabelle system OPTION (via NAME=VAL or NAME) |
340 |
-r DIR installation root directory (default: """ + default_root(options, "NAME") + """) |
|
341 |
||
342 |
Install Phabricator as Ubuntu LAMP application (Linux, Apache, MySQL, PHP). |
|
343 |
||
344 |
Slogan: "Discuss. Plan. Code. Review. Test. |
|
345 |
Every application your project needs, all in one tool." |
|
346 |
||
347 |
The installation NAME (default: """ + quote(default_name) + """) is mapped to |
|
348 |
a regular Unix user and used for public SSH access. |
|
349 |
""", |
|
350 |
"R:" -> (arg => repo = arg), |
|
71047 | 351 |
"U" -> (_ => package_update = true), |
70967 | 352 |
"o:" -> (arg => options = options + arg), |
353 |
"r:" -> (arg => root = arg)) |
|
354 |
||
355 |
val more_args = getopts(args) |
|
356 |
||
357 |
val name = |
|
358 |
more_args match { |
|
359 |
case Nil => default_name |
|
360 |
case List(name) => name |
|
361 |
case _ => getopts.usage() |
|
362 |
} |
|
363 |
||
364 |
val progress = new Console_Progress |
|
365 |
||
71052
6bf53035baf0
clarified name prefixes: global config always uses "isabelle-phabricator";
wenzelm
parents:
71051
diff
changeset
|
366 |
phabricator_setup(options, name, root = root, repo = repo, |
71047 | 367 |
package_update = package_update, progress = progress) |
70967 | 368 |
}) |
369 |
||
370 |
||
371 |
||
372 |
/** update **/ |
|
373 |
||
374 |
def phabricator_update(name: String, progress: Progress = No_Progress) |
|
375 |
{ |
|
376 |
Linux.check_system_root() |
|
377 |
||
378 |
??? |
|
379 |
} |
|
380 |
||
381 |
||
382 |
/* Isabelle tool wrapper */ |
|
383 |
||
384 |
val isabelle_tool2 = |
|
385 |
Isabelle_Tool("phabricator_update", "update Phabricator server installation", args => |
|
386 |
{ |
|
387 |
val getopts = |
|
388 |
Getopts(""" |
|
389 |
Usage: isabelle phabricator_update [NAME] |
|
390 |
||
391 |
Update Phabricator installation, with lookup of NAME (default + """ + quote(default_name) + """) |
|
392 |
in """ + global_config + "\n") |
|
393 |
||
394 |
val more_args = getopts(args) |
|
395 |
val name = |
|
396 |
more_args match { |
|
397 |
case Nil => default_name |
|
398 |
case List(name) => name |
|
399 |
case _ => getopts.usage() |
|
400 |
} |
|
401 |
||
402 |
val progress = new Console_Progress |
|
403 |
||
404 |
phabricator_update(name, progress = progress) |
|
405 |
}) |
|
406 |
} |