author | wenzelm |
Wed, 11 Dec 2019 22:09:34 +0100 | |
changeset 71274 | 5212ca49598a |
parent 71273 | 6b8cbdc9713b |
child 71276 | b4401dfd6544 |
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 |
||
70969 | 14 |
import scala.util.matching.Regex |
15 |
||
16 |
||
70967 | 17 |
object Phabricator |
18 |
{ |
|
19 |
/** defaults **/ |
|
20 |
||
71049 | 21 |
/* required packages */ |
22 |
||
23 |
val packages: List[String] = |
|
71265 | 24 |
Build_Docker.packages ::: Linux.packages ::: |
71049 | 25 |
List( |
26 |
// https://secure.phabricator.com/source/phabricator/browse/master/scripts/install/install_ubuntu.sh 15e6e2adea61 |
|
27 |
"git", "mysql-server", "apache2", "libapache2-mod-php", "php", "php-mysql", |
|
28 |
"php-gd", "php-curl", "php-apcu", "php-cli", "php-json", "php-mbstring", |
|
29 |
// more packages |
|
71135 | 30 |
"php-zip", "python-pygments", "ssh", "subversion", "mercurial") |
71049 | 31 |
|
32 |
||
33 |
/* global system resources */ |
|
34 |
||
71068
510b89906d86
discontinued somewhat pointless Isabelle options: setup implicitly assumes Ubuntu 18.04;
wenzelm
parents:
71066
diff
changeset
|
35 |
val www_user = "www-data" |
510b89906d86
discontinued somewhat pointless Isabelle options: setup implicitly assumes Ubuntu 18.04;
wenzelm
parents:
71066
diff
changeset
|
36 |
|
71049 | 37 |
val daemon_user = "phabricator" |
38 |
||
71109
8c1c717a830b
configure SSH hosting via "isabelle phabricator_setup_ssh";
wenzelm
parents:
71103
diff
changeset
|
39 |
val sshd_config = Path.explode("/etc/ssh/sshd_config") |
71049 | 40 |
|
41 |
||
42 |
/* installation parameters */ |
|
43 |
||
70967 | 44 |
val default_name = "vcs" |
45 |
||
71052
6bf53035baf0
clarified name prefixes: global config always uses "isabelle-phabricator";
wenzelm
parents:
71051
diff
changeset
|
46 |
def phabricator_name(name: String = "", ext: String = ""): String = |
6bf53035baf0
clarified name prefixes: global config always uses "isabelle-phabricator";
wenzelm
parents:
71051
diff
changeset
|
47 |
"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
|
48 |
|
6bf53035baf0
clarified name prefixes: global config always uses "isabelle-phabricator";
wenzelm
parents:
71051
diff
changeset
|
49 |
def isabelle_phabricator_name(name: String = "", ext: String = ""): String = |
6bf53035baf0
clarified name prefixes: global config always uses "isabelle-phabricator";
wenzelm
parents:
71051
diff
changeset
|
50 |
"isabelle-" + phabricator_name(name = name, ext = ext) |
70967 | 51 |
|
71068
510b89906d86
discontinued somewhat pointless Isabelle options: setup implicitly assumes Ubuntu 18.04;
wenzelm
parents:
71066
diff
changeset
|
52 |
def default_root(name: String): Path = |
510b89906d86
discontinued somewhat pointless Isabelle options: setup implicitly assumes Ubuntu 18.04;
wenzelm
parents:
71066
diff
changeset
|
53 |
Path.explode("/var/www") + Path.basic(phabricator_name(name = name)) |
70967 | 54 |
|
71068
510b89906d86
discontinued somewhat pointless Isabelle options: setup implicitly assumes Ubuntu 18.04;
wenzelm
parents:
71066
diff
changeset
|
55 |
def default_repo(name: String): Path = default_root(name) + Path.basic("repo") |
70967 | 56 |
|
71072 | 57 |
val default_mailers: Path = Path.explode("mailers.json") |
71066 | 58 |
|
71109
8c1c717a830b
configure SSH hosting via "isabelle phabricator_setup_ssh";
wenzelm
parents:
71103
diff
changeset
|
59 |
val default_system_port = 22 |
8c1c717a830b
configure SSH hosting via "isabelle phabricator_setup_ssh";
wenzelm
parents:
71103
diff
changeset
|
60 |
val alternative_system_port = 222 |
8c1c717a830b
configure SSH hosting via "isabelle phabricator_setup_ssh";
wenzelm
parents:
71103
diff
changeset
|
61 |
val default_server_port = 2222 |
8c1c717a830b
configure SSH hosting via "isabelle phabricator_setup_ssh";
wenzelm
parents:
71103
diff
changeset
|
62 |
|
70967 | 63 |
|
64 |
||
65 |
/** global configuration **/ |
|
66 |
||
71052
6bf53035baf0
clarified name prefixes: global config always uses "isabelle-phabricator";
wenzelm
parents:
71051
diff
changeset
|
67 |
val global_config = Path.explode("/etc/" + isabelle_phabricator_name(ext = "conf")) |
70967 | 68 |
|
71122 | 69 |
def global_config_script( |
70 |
header: Boolean = false, |
|
71 |
init: String = "", |
|
72 |
body: String = "", |
|
73 |
exit: String = ""): String = |
|
74 |
{ |
|
75 |
(if (header) "#!/bin/bash\n" else "") + |
|
76 |
""" |
|
77 |
{""" + (if (init.nonEmpty) "\n" + Library.prefix_lines(" ", init) else "") + """ |
|
78 |
while { unset REPLY; read -r; test "$?" = 0 -o -n "$REPLY"; } |
|
79 |
do |
|
80 |
NAME="$(echo "$REPLY" | cut -d: -f1)" |
|
81 |
ROOT="$(echo "$REPLY" | cut -d: -f2)" |
|
82 |
""" + Library.prefix_lines(" ", body) + """ |
|
83 |
done""" + |
|
84 |
(if (exit.nonEmpty) "\n" + Library.prefix_lines(" ", exit) else "") + """ |
|
85 |
} < """ + File.bash_path(global_config) + """ |
|
86 |
""" |
|
87 |
} |
|
88 |
||
70967 | 89 |
sealed case class Config(name: String, root: Path) |
70968 | 90 |
{ |
71052
6bf53035baf0
clarified name prefixes: global config always uses "isabelle-phabricator";
wenzelm
parents:
71051
diff
changeset
|
91 |
def home: Path = root + Path.explode(phabricator_name()) |
70969 | 92 |
|
93 |
def execute(command: String): Process_Result = |
|
71102 | 94 |
Isabelle_System.bash("bin/" + command, cwd = home.file, redirect = true).check |
70968 | 95 |
} |
70967 | 96 |
|
97 |
def read_config(): List[Config] = |
|
98 |
{ |
|
99 |
if (global_config.is_file) { |
|
100 |
for (entry <- Library.trim_split_lines(File.read(global_config)) if entry.nonEmpty) |
|
101 |
yield { |
|
102 |
space_explode(':', entry) match { |
|
103 |
case List(name, root) => Config(name, Path.explode(root)) |
|
104 |
case _ => error("Malformed config file " + global_config + "\nentry " + quote(entry)) |
|
105 |
} |
|
106 |
} |
|
107 |
} |
|
108 |
else Nil |
|
109 |
} |
|
110 |
||
111 |
def write_config(configs: List[Config]) |
|
112 |
{ |
|
113 |
File.write(global_config, |
|
114 |
configs.map(config => config.name + ":" + config.root.implode).mkString("", "\n", "\n")) |
|
115 |
} |
|
116 |
||
117 |
def get_config(name: String): Config = |
|
118 |
read_config().find(config => config.name == name) getOrElse |
|
119 |
error("Bad Isabelle/Phabricator installation " + quote(name)) |
|
120 |
||
121 |
||
122 |
||
71097 | 123 |
/** command-line tools **/ |
124 |
||
125 |
/* Isabelle tool wrapper */ |
|
126 |
||
127 |
val isabelle_tool1 = |
|
128 |
Isabelle_Tool("phabricator", "invoke command-line tool within Phabricator home directory", args => |
|
129 |
{ |
|
71101 | 130 |
var list = false |
71097 | 131 |
var name = default_name |
132 |
||
133 |
val getopts = |
|
134 |
Getopts(""" |
|
135 |
Usage: isabelle phabricator [OPTIONS] COMMAND [ARGS...] |
|
136 |
||
137 |
Options are: |
|
71101 | 138 |
-l list available Phabricator installations |
71097 | 139 |
-n NAME Phabricator installation name (default: """ + quote(default_name) + """) |
140 |
||
71103 | 141 |
Invoke a command-line tool within the home directory of the named |
142 |
Phabricator installation. |
|
71097 | 143 |
""", |
71101 | 144 |
"l" -> (_ => list = true), |
71097 | 145 |
"n:" -> (arg => name = arg)) |
146 |
||
147 |
val more_args = getopts(args) |
|
71101 | 148 |
if (more_args.isEmpty && !list) getopts.usage() |
71097 | 149 |
|
150 |
val progress = new Console_Progress |
|
151 |
||
71101 | 152 |
if (list) { |
153 |
for (config <- read_config()) { |
|
71103 | 154 |
progress.echo("phabricator " + quote(config.name) + " root " + config.root) |
71101 | 155 |
} |
156 |
} |
|
157 |
||
71097 | 158 |
val config = get_config(name) |
159 |
||
71098 | 160 |
val result = progress.bash(Bash.strings(more_args), cwd = config.home.file, echo = true) |
161 |
if (!result.ok) error("Return code: " + result.rc.toString) |
|
71097 | 162 |
}) |
163 |
||
164 |
||
165 |
||
70967 | 166 |
/** setup **/ |
167 |
||
71049 | 168 |
def user_setup(name: String, description: String, ssh_setup: Boolean = false) |
169 |
{ |
|
170 |
if (!Linux.user_exists(name)) { |
|
71054
b64fc38327ae
prefer system user setup, e.g. avoid occurrence on login screen;
wenzelm
parents:
71053
diff
changeset
|
171 |
Linux.user_add(name, description = description, system = true, ssh_setup = ssh_setup) |
71049 | 172 |
} |
173 |
else if (Linux.user_description(name) != description) { |
|
174 |
error("User " + quote(name) + " already exists --" + |
|
175 |
" for Phabricator it should have the description:\n " + quote(description)) |
|
176 |
} |
|
177 |
} |
|
178 |
||
71270 | 179 |
def command_setup(name: String, body: String, exit: String = ""): Path = |
180 |
{ |
|
181 |
val command = Path.explode("/usr/local/bin") + Path.basic(name) |
|
182 |
File.write(command, global_config_script(header = true, body = body, exit = exit)) |
|
183 |
Isabelle_System.chmod("755", command) |
|
184 |
Isabelle_System.chown("root:root", command) |
|
185 |
command |
|
186 |
} |
|
187 |
||
70967 | 188 |
def phabricator_setup( |
189 |
name: String = default_name, |
|
190 |
root: String = "", |
|
191 |
repo: String = "", |
|
71047 | 192 |
package_update: Boolean = false, |
70967 | 193 |
progress: Progress = No_Progress) |
194 |
{ |
|
195 |
/* system environment */ |
|
196 |
||
197 |
Linux.check_system_root() |
|
198 |
||
71079 | 199 |
progress.echo("System packages ...") |
200 |
||
71047 | 201 |
if (package_update) { |
202 |
Linux.package_update(progress = progress) |
|
203 |
Linux.check_reboot_required() |
|
204 |
} |
|
70967 | 205 |
|
206 |
Linux.package_install(packages, progress = progress) |
|
207 |
Linux.check_reboot_required() |
|
208 |
||
209 |
||
71049 | 210 |
/* users */ |
211 |
||
71125 | 212 |
if (name.contains((c: Char) => !(Symbol.is_ascii_letter(c) || Symbol.is_ascii_digit(c))) || |
71269 | 213 |
Set("", "ssh", "phd", "dump", daemon_user).contains(name)) { |
71125 | 214 |
error("Bad installation name: " + quote(name)) |
71049 | 215 |
} |
216 |
||
217 |
user_setup(daemon_user, "Phabricator Daemon User", ssh_setup = true) |
|
218 |
user_setup(name, "Phabricator SSH User") |
|
219 |
||
220 |
||
70967 | 221 |
/* basic installation */ |
222 |
||
71079 | 223 |
progress.echo("\nPhabricator installation ...") |
71076 | 224 |
|
71068
510b89906d86
discontinued somewhat pointless Isabelle options: setup implicitly assumes Ubuntu 18.04;
wenzelm
parents:
71066
diff
changeset
|
225 |
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
|
226 |
val repo_path = if (repo.nonEmpty) Path.explode(repo) else default_repo(name) |
70967 | 227 |
|
228 |
val configs = read_config() |
|
229 |
||
230 |
for (config <- configs if config.name == name) { |
|
231 |
error("Duplicate Phabricator installation " + quote(name) + " in " + config.root) |
|
232 |
} |
|
233 |
||
234 |
if (!Isabelle_System.bash("mkdir -p " + File.bash_path(root_path)).ok) { |
|
235 |
error("Failed to create root directory " + root_path) |
|
236 |
} |
|
237 |
||
71116 | 238 |
Isabelle_System.chown(Bash.string(www_user) + ":" + Bash.string(www_user), root_path) |
239 |
Isabelle_System.chmod("755", root_path) |
|
240 |
||
70967 | 241 |
progress.bash(cwd = root_path.file, echo = true, |
242 |
script = """ |
|
243 |
set -e |
|
71126 | 244 |
echo "Cloning distribution repositories:" |
70967 | 245 |
git clone https://github.com/phacility/libphutil.git |
246 |
git clone https://github.com/phacility/arcanist.git |
|
247 |
git clone https://github.com/phacility/phabricator.git |
|
248 |
""").check |
|
249 |
||
250 |
val config = Config(name, root_path) |
|
251 |
write_config(configs ::: List(config)) |
|
70968 | 252 |
|
71051 | 253 |
config.execute("config set pygments.enabled true") |
254 |
||
70968 | 255 |
|
71050 | 256 |
/* local repository directory */ |
257 |
||
71109
8c1c717a830b
configure SSH hosting via "isabelle phabricator_setup_ssh";
wenzelm
parents:
71103
diff
changeset
|
258 |
progress.echo("\nRepository hosting setup ...") |
8c1c717a830b
configure SSH hosting via "isabelle phabricator_setup_ssh";
wenzelm
parents:
71103
diff
changeset
|
259 |
|
71050 | 260 |
if (!Isabelle_System.bash("mkdir -p " + File.bash_path(repo_path)).ok) { |
261 |
error("Failed to create local repository directory " + repo_path) |
|
262 |
} |
|
263 |
||
71114 | 264 |
Isabelle_System.chown( |
265 |
"-R " + Bash.string(daemon_user) + ":" + Bash.string(daemon_user), repo_path) |
|
266 |
Isabelle_System.chmod("755", repo_path) |
|
71050 | 267 |
|
268 |
config.execute("config set repository.default-local-path " + File.bash_path(repo_path)) |
|
269 |
||
270 |
||
71109
8c1c717a830b
configure SSH hosting via "isabelle phabricator_setup_ssh";
wenzelm
parents:
71103
diff
changeset
|
271 |
val sudoers_file = Path.explode("/etc/sudoers.d") + Path.basic(isabelle_phabricator_name()) |
8c1c717a830b
configure SSH hosting via "isabelle phabricator_setup_ssh";
wenzelm
parents:
71103
diff
changeset
|
272 |
File.write(sudoers_file, |
8c1c717a830b
configure SSH hosting via "isabelle phabricator_setup_ssh";
wenzelm
parents:
71103
diff
changeset
|
273 |
www_user + " ALL=(" + daemon_user + ") SETENV: NOPASSWD: /usr/bin/git, /usr/bin/hg, /usr/bin/ssh, /usr/bin/id\n" + |
8c1c717a830b
configure SSH hosting via "isabelle phabricator_setup_ssh";
wenzelm
parents:
71103
diff
changeset
|
274 |
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") |
8c1c717a830b
configure SSH hosting via "isabelle phabricator_setup_ssh";
wenzelm
parents:
71103
diff
changeset
|
275 |
|
71115 | 276 |
Isabelle_System.chmod("440", sudoers_file) |
71109
8c1c717a830b
configure SSH hosting via "isabelle phabricator_setup_ssh";
wenzelm
parents:
71103
diff
changeset
|
277 |
|
8c1c717a830b
configure SSH hosting via "isabelle phabricator_setup_ssh";
wenzelm
parents:
71103
diff
changeset
|
278 |
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
|
279 |
|
8c1c717a830b
configure SSH hosting via "isabelle phabricator_setup_ssh";
wenzelm
parents:
71103
diff
changeset
|
280 |
|
70969 | 281 |
/* MySQL setup */ |
282 |
||
71079 | 283 |
progress.echo("\nMySQL setup ...") |
70969 | 284 |
|
71055
27a998cdc0f4
back to plain name, to have it accepted my mysql;
wenzelm
parents:
71054
diff
changeset
|
285 |
File.write(Path.explode("/etc/mysql/mysql.conf.d/" + phabricator_name(ext = "cnf")), |
71051 | 286 |
"""[mysqld] |
287 |
max_allowed_packet = 32M |
|
288 |
innodb_buffer_pool_size = 1600M |
|
289 |
local_infile = 0 |
|
290 |
""") |
|
291 |
||
292 |
Linux.service_restart("mysql") |
|
293 |
||
294 |
||
71266
8451c86ffa85
proper mysql user setup: avoid superuser powers in production;
wenzelm
parents:
71265
diff
changeset
|
295 |
def mysql_conf(R: Regex, which: String): String = |
8451c86ffa85
proper mysql user setup: avoid superuser powers in production;
wenzelm
parents:
71265
diff
changeset
|
296 |
{ |
8451c86ffa85
proper mysql user setup: avoid superuser powers in production;
wenzelm
parents:
71265
diff
changeset
|
297 |
val conf = Path.explode("/etc/mysql/debian.cnf") |
8451c86ffa85
proper mysql user setup: avoid superuser powers in production;
wenzelm
parents:
71265
diff
changeset
|
298 |
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
|
299 |
case Some(res) => res |
8451c86ffa85
proper mysql user setup: avoid superuser powers in production;
wenzelm
parents:
71265
diff
changeset
|
300 |
case None => error("Cannot determine " + which + " from " + conf) |
8451c86ffa85
proper mysql user setup: avoid superuser powers in production;
wenzelm
parents:
71265
diff
changeset
|
301 |
} |
70969 | 302 |
} |
303 |
||
71266
8451c86ffa85
proper mysql user setup: avoid superuser powers in production;
wenzelm
parents:
71265
diff
changeset
|
304 |
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
|
305 |
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
|
306 |
|
8451c86ffa85
proper mysql user setup: avoid superuser powers in production;
wenzelm
parents:
71265
diff
changeset
|
307 |
val mysql_name = phabricator_name(name = name).replace("-", "_") |
8451c86ffa85
proper mysql user setup: avoid superuser powers in production;
wenzelm
parents:
71265
diff
changeset
|
308 |
val mysql_user_string = SQL.string(mysql_name) + "@'localhost'" |
8451c86ffa85
proper mysql user setup: avoid superuser powers in production;
wenzelm
parents:
71265
diff
changeset
|
309 |
val mysql_password = Linux.generate_password() |
70969 | 310 |
|
71266
8451c86ffa85
proper mysql user setup: avoid superuser powers in production;
wenzelm
parents:
71265
diff
changeset
|
311 |
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
|
312 |
" --password=" + Bash.string(mysql_root_password) + " --execute=" + |
8451c86ffa85
proper mysql user setup: avoid superuser powers in production;
wenzelm
parents:
71265
diff
changeset
|
313 |
Bash.string( |
71274 | 314 |
"""DROP USER IF EXISTS """ + mysql_user_string + "; " + |
315 |
"""CREATE USER """ + mysql_user_string + |
|
71266
8451c86ffa85
proper mysql user setup: avoid superuser powers in production;
wenzelm
parents:
71265
diff
changeset
|
316 |
""" IDENTIFIED BY """ + SQL.string(mysql_password) + """ PASSWORD EXPIRE NEVER; """ + |
8451c86ffa85
proper mysql user setup: avoid superuser powers in production;
wenzelm
parents:
71265
diff
changeset
|
317 |
"""GRANT ALL ON `""" + (mysql_name + "_%").replace("_", "\\_") + |
8451c86ffa85
proper mysql user setup: avoid superuser powers in production;
wenzelm
parents:
71265
diff
changeset
|
318 |
"""`.* TO """ + mysql_user_string + ";")).check |
70969 | 319 |
|
71266
8451c86ffa85
proper mysql user setup: avoid superuser powers in production;
wenzelm
parents:
71265
diff
changeset
|
320 |
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
|
321 |
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
|
322 |
|
8451c86ffa85
proper mysql user setup: avoid superuser powers in production;
wenzelm
parents:
71265
diff
changeset
|
323 |
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
|
324 |
config.execute("config set storage.default-namespace " + Bash.string(mysql_name)) |
71051 | 325 |
config.execute("config set storage.mysql-engine.max-size 8388608") |
326 |
||
71102 | 327 |
progress.bash("bin/storage upgrade --force", cwd = config.home.file, echo = true).check |
70969 | 328 |
|
329 |
||
71269 | 330 |
/* database dump */ |
331 |
||
332 |
val dump_name = isabelle_phabricator_name(name = "dump") |
|
71270 | 333 |
command_setup(dump_name, |
71269 | 334 |
"""mkdir -p "$ROOT/database" && chown root:root "$ROOT/database" && chmod 700 "$ROOT/database" |
335 |
[ -e "$ROOT/database/dump.sql.gz" ] && mv -f "$ROOT/database/dump.sql.gz" "$ROOT/database/dump-old.sql.gz" |
|
336 |
echo "Creating $ROOT/database/dump.sql.gz" |
|
71270 | 337 |
"$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' """) |
71269 | 338 |
|
339 |
||
71051 | 340 |
/* PHP setup */ |
341 |
||
342 |
val php_version = |
|
343 |
Isabelle_System.bash("""php --run 'echo PHP_MAJOR_VERSION . "." . PHP_MINOR_VERSION;'""") |
|
344 |
.check.out |
|
345 |
||
346 |
val php_conf = |
|
347 |
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
|
348 |
Path.explode("apache2/conf.d") + |
6bf53035baf0
clarified name prefixes: global config always uses "isabelle-phabricator";
wenzelm
parents:
71051
diff
changeset
|
349 |
Path.basic(isabelle_phabricator_name(ext = "ini")) |
71051 | 350 |
|
351 |
File.write(php_conf, |
|
352 |
"post_max_size = 32M\n" + |
|
353 |
"opcache.validate_timestamps = 0\n" + |
|
71129 | 354 |
"memory_limit = 512M\n" + |
355 |
"max_execution_time = 120\n") |
|
71051 | 356 |
|
357 |
||
70968 | 358 |
/* Apache setup */ |
359 |
||
71079 | 360 |
progress.echo("Apache setup ...") |
70968 | 361 |
|
71068
510b89906d86
discontinued somewhat pointless Isabelle options: setup implicitly assumes Ubuntu 18.04;
wenzelm
parents:
71066
diff
changeset
|
362 |
val apache_root = Path.explode("/etc/apache2") |
70968 | 363 |
val apache_sites = apache_root + Path.explode("sites-available") |
364 |
||
365 |
if (!apache_sites.is_dir) error("Bad Apache sites directory " + apache_sites) |
|
366 |
||
71058 | 367 |
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
|
368 |
val server_url = "http://" + server_name |
6bf53035baf0
clarified name prefixes: global config always uses "isabelle-phabricator";
wenzelm
parents:
71051
diff
changeset
|
369 |
|
71058 | 370 |
File.write(apache_sites + Path.basic(isabelle_phabricator_name(name = name, ext = "conf")), |
70968 | 371 |
"""<VirtualHost *:80> |
71052
6bf53035baf0
clarified name prefixes: global config always uses "isabelle-phabricator";
wenzelm
parents:
71051
diff
changeset
|
372 |
ServerName """ + server_name + """ |
70968 | 373 |
ServerAdmin webmaster@localhost |
70969 | 374 |
DocumentRoot """ + config.home.implode + """/webroot |
70968 | 375 |
|
376 |
ErrorLog ${APACHE_LOG_DIR}/error.log |
|
377 |
RewriteEngine on |
|
378 |
RewriteRule ^(.*)$ /index.php?__path__=$1 [B,L,QSA] |
|
379 |
</VirtualHost> |
|
380 |
||
381 |
# vim: syntax=apache ts=4 sw=4 sts=4 sr noet |
|
382 |
""") |
|
383 |
||
71051 | 384 |
Isabelle_System.bash( """ |
70968 | 385 |
set -e |
386 |
a2enmod rewrite |
|
71058 | 387 |
a2ensite """ + Bash.string(isabelle_phabricator_name(name = name))).check |
71051 | 388 |
|
71057 | 389 |
config.execute("config set phabricator.base-uri " + Bash.string(server_url)) |
390 |
||
71051 | 391 |
Linux.service_restart("apache2") |
70968 | 392 |
|
71128
f79006c533b0
clarified errors: PHP daemon can fail under odd circumstances;
wenzelm
parents:
71127
diff
changeset
|
393 |
progress.echo("\nWeb configuration via " + server_url) |
f79006c533b0
clarified errors: PHP daemon can fail under odd circumstances;
wenzelm
parents:
71127
diff
changeset
|
394 |
|
71053 | 395 |
|
396 |
/* PHP daemon */ |
|
397 |
||
71128
f79006c533b0
clarified errors: PHP daemon can fail under odd circumstances;
wenzelm
parents:
71127
diff
changeset
|
398 |
progress.echo("\nPHP daemon setup ...") |
71053 | 399 |
|
71273 | 400 |
val phd_log_path = Path.explode("/var/tmp/phd") |
401 |
Isabelle_System.mkdirs(phd_log_path) |
|
402 |
Isabelle_System.chown( |
|
403 |
"-R " + Bash.string(daemon_user) + ":" + Bash.string(daemon_user), phd_log_path) |
|
404 |
Isabelle_System.chmod("755", phd_log_path) |
|
405 |
||
71053 | 406 |
config.execute("config set phd.user " + Bash.string(daemon_user)) |
71112 | 407 |
config.execute("config set phd.log-directory /var/tmp/phd/" + |
408 |
isabelle_phabricator_name(name = name) + "/log") |
|
71053 | 409 |
|
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
|
410 |
val phd_name = isabelle_phabricator_name(name = "phd") |
71127 | 411 |
Linux.service_shutdown(phd_name) |
71270 | 412 |
val phd_command = command_setup(phd_name, """"$ROOT/phabricator/bin/phd" "$@" """) |
71128
f79006c533b0
clarified errors: PHP daemon can fail under odd circumstances;
wenzelm
parents:
71127
diff
changeset
|
413 |
try { |
f79006c533b0
clarified errors: PHP daemon can fail under odd circumstances;
wenzelm
parents:
71127
diff
changeset
|
414 |
Linux.service_install(phd_name, |
71053 | 415 |
"""[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
|
416 |
Description=PHP daemon manager for Isabelle/Phabricator |
71053 | 417 |
After=syslog.target network.target apache2.service mysql.service |
418 |
||
419 |
[Service] |
|
420 |
Type=oneshot |
|
421 |
User=""" + daemon_user + """ |
|
422 |
Group=""" + daemon_user + """ |
|
423 |
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
|
424 |
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
|
425 |
ExecStop=""" + phd_command.implode + """ stop |
71053 | 426 |
RemainAfterExit=yes |
427 |
||
428 |
[Install] |
|
429 |
WantedBy=multi-user.target |
|
430 |
""") |
|
71128
f79006c533b0
clarified errors: PHP daemon can fail under odd circumstances;
wenzelm
parents:
71127
diff
changeset
|
431 |
} |
f79006c533b0
clarified errors: PHP daemon can fail under odd circumstances;
wenzelm
parents:
71127
diff
changeset
|
432 |
catch { |
f79006c533b0
clarified errors: PHP daemon can fail under odd circumstances;
wenzelm
parents:
71127
diff
changeset
|
433 |
case ERROR(msg) => |
f79006c533b0
clarified errors: PHP daemon can fail under odd circumstances;
wenzelm
parents:
71127
diff
changeset
|
434 |
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
|
435 |
error(msg) |
f79006c533b0
clarified errors: PHP daemon can fail under odd circumstances;
wenzelm
parents:
71127
diff
changeset
|
436 |
} |
70967 | 437 |
} |
438 |
||
439 |
||
440 |
/* Isabelle tool wrapper */ |
|
441 |
||
71097 | 442 |
val isabelle_tool2 = |
70967 | 443 |
Isabelle_Tool("phabricator_setup", "setup Phabricator server on Ubuntu Linux", args => |
444 |
{ |
|
71047 | 445 |
var repo = "" |
446 |
var package_update = false |
|
71078 | 447 |
var name = default_name |
70967 | 448 |
var root = "" |
449 |
||
450 |
val getopts = |
|
451 |
Getopts(""" |
|
71078 | 452 |
Usage: isabelle phabricator_setup [OPTIONS] |
70967 | 453 |
|
454 |
Options are: |
|
71068
510b89906d86
discontinued somewhat pointless Isabelle options: setup implicitly assumes Ubuntu 18.04;
wenzelm
parents:
71066
diff
changeset
|
455 |
-R DIR repository directory (default: """ + default_repo("NAME") + """) |
71047 | 456 |
-U full update of system packages before installation |
71078 | 457 |
-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
|
458 |
-r DIR installation root directory (default: """ + default_root("NAME") + """) |
70967 | 459 |
|
71103 | 460 |
Install Phabricator as LAMP application (Linux, Apache, MySQL, PHP). |
70967 | 461 |
|
71078 | 462 |
The installation name (default: """ + quote(default_name) + """) is mapped to a regular |
463 |
Unix user; this is relevant for public SSH access. |
|
70967 | 464 |
""", |
465 |
"R:" -> (arg => repo = arg), |
|
71047 | 466 |
"U" -> (_ => package_update = true), |
71078 | 467 |
"n:" -> (arg => name = arg), |
70967 | 468 |
"r:" -> (arg => root = arg)) |
469 |
||
470 |
val more_args = getopts(args) |
|
71078 | 471 |
if (more_args.nonEmpty) getopts.usage() |
70967 | 472 |
|
473 |
val progress = new Console_Progress |
|
474 |
||
71268 | 475 |
val release = Linux.Release() |
476 |
if (!release.is_ubuntu_18_04) error("Bad Linux version: Ubuntu 18.04 LTS required") |
|
477 |
||
71078 | 478 |
phabricator_setup(name = name, root = root, repo = repo, |
71047 | 479 |
package_update = package_update, progress = progress) |
70967 | 480 |
}) |
481 |
||
482 |
||
483 |
||
71066 | 484 |
/** setup mail **/ |
70967 | 485 |
|
71072 | 486 |
val mailers_template: String = |
487 |
"""[ |
|
488 |
{ |
|
489 |
"key": "example.org", |
|
490 |
"type": "smtp", |
|
491 |
"options": { |
|
492 |
"host": "mail.example.org", |
|
493 |
"port": 465, |
|
494 |
"user": "phabricator@example.org", |
|
495 |
"password": "********", |
|
496 |
"protocol": "ssl", |
|
497 |
"message-id": true |
|
498 |
} |
|
499 |
} |
|
500 |
]""" |
|
501 |
||
71066 | 502 |
def phabricator_setup_mail( |
503 |
name: String = default_name, |
|
504 |
config_file: Option[Path] = None, |
|
505 |
test_user: String = "", |
|
506 |
progress: Progress = No_Progress) |
|
70967 | 507 |
{ |
508 |
Linux.check_system_root() |
|
509 |
||
71066 | 510 |
val config = get_config(name) |
71073 | 511 |
val default_config_file = config.root + default_mailers |
71066 | 512 |
|
513 |
val mail_config = config_file getOrElse default_config_file |
|
514 |
||
515 |
def setup_mail |
|
516 |
{ |
|
517 |
progress.echo("Using mail configuration from " + mail_config) |
|
518 |
config.execute("config set cluster.mailers --stdin < " + File.bash_path(mail_config)) |
|
519 |
||
520 |
if (test_user.nonEmpty) { |
|
521 |
progress.echo("Sending test mail to " + quote(test_user)) |
|
522 |
progress.bash(cwd = config.home.file, echo = true, |
|
71102 | 523 |
script = """echo "Test from Phabricator ($(date))" | bin/mail send-test --subject "Test" --to """ + |
71066 | 524 |
Bash.string(test_user)).check |
525 |
} |
|
526 |
} |
|
527 |
||
528 |
if (config_file.isEmpty) { |
|
71070 | 529 |
if (!default_config_file.is_file) { |
530 |
File.write(default_config_file, mailers_template) |
|
71114 | 531 |
Isabelle_System.chmod("600", default_config_file) |
71070 | 532 |
} |
71066 | 533 |
if (File.read(default_config_file) == mailers_template) { |
71131 | 534 |
progress.echo("Please invoke the tool again, after providing details in\n " + |
535 |
default_config_file.implode + "\n") |
|
71066 | 536 |
} |
537 |
else setup_mail |
|
538 |
} |
|
539 |
else setup_mail |
|
70967 | 540 |
} |
541 |
||
542 |
||
543 |
/* Isabelle tool wrapper */ |
|
544 |
||
71097 | 545 |
val isabelle_tool3 = |
71066 | 546 |
Isabelle_Tool("phabricator_setup_mail", |
71109
8c1c717a830b
configure SSH hosting via "isabelle phabricator_setup_ssh";
wenzelm
parents:
71103
diff
changeset
|
547 |
"setup mail for one Phabricator installation", args => |
70967 | 548 |
{ |
71066 | 549 |
var test_user = "" |
550 |
var name = default_name |
|
551 |
var config_file: Option[Path] = None |
|
552 |
||
70967 | 553 |
val getopts = |
554 |
Getopts(""" |
|
71066 | 555 |
Usage: isabelle phabricator_setup_mail [OPTIONS] |
556 |
||
557 |
Options are: |
|
558 |
-T USER send test mail to Phabricator user |
|
71103 | 559 |
-f FILE config file (default: """ + default_mailers + """ within Phabricator root) |
71066 | 560 |
-n NAME Phabricator installation name (default: """ + quote(default_name) + """) |
70967 | 561 |
|
71077 | 562 |
Provide mail configuration for existing Phabricator installation. |
71066 | 563 |
""", |
564 |
"T:" -> (arg => test_user = arg), |
|
565 |
"f:" -> (arg => config_file = Some(Path.explode(arg))), |
|
566 |
"n:" -> (arg => name = arg)) |
|
70967 | 567 |
|
568 |
val more_args = getopts(args) |
|
71066 | 569 |
if (more_args.nonEmpty) getopts.usage() |
70967 | 570 |
|
571 |
val progress = new Console_Progress |
|
572 |
||
71066 | 573 |
phabricator_setup_mail(name = name, config_file = config_file, |
574 |
test_user = test_user, progress = progress) |
|
70967 | 575 |
}) |
71109
8c1c717a830b
configure SSH hosting via "isabelle phabricator_setup_ssh";
wenzelm
parents:
71103
diff
changeset
|
576 |
|
8c1c717a830b
configure SSH hosting via "isabelle phabricator_setup_ssh";
wenzelm
parents:
71103
diff
changeset
|
577 |
|
8c1c717a830b
configure SSH hosting via "isabelle phabricator_setup_ssh";
wenzelm
parents:
71103
diff
changeset
|
578 |
|
8c1c717a830b
configure SSH hosting via "isabelle phabricator_setup_ssh";
wenzelm
parents:
71103
diff
changeset
|
579 |
/** setup ssh **/ |
8c1c717a830b
configure SSH hosting via "isabelle phabricator_setup_ssh";
wenzelm
parents:
71103
diff
changeset
|
580 |
|
8c1c717a830b
configure SSH hosting via "isabelle phabricator_setup_ssh";
wenzelm
parents:
71103
diff
changeset
|
581 |
/* sshd config */ |
8c1c717a830b
configure SSH hosting via "isabelle phabricator_setup_ssh";
wenzelm
parents:
71103
diff
changeset
|
582 |
|
8c1c717a830b
configure SSH hosting via "isabelle phabricator_setup_ssh";
wenzelm
parents:
71103
diff
changeset
|
583 |
private val Port = """^\s*Port\s+(\d+)\s*$""".r |
8c1c717a830b
configure SSH hosting via "isabelle phabricator_setup_ssh";
wenzelm
parents:
71103
diff
changeset
|
584 |
private val No_Port = """^#\s*Port\b.*$""".r |
8c1c717a830b
configure SSH hosting via "isabelle phabricator_setup_ssh";
wenzelm
parents:
71103
diff
changeset
|
585 |
private val Any_Port = """^#?\s*Port\b.*$""".r |
8c1c717a830b
configure SSH hosting via "isabelle phabricator_setup_ssh";
wenzelm
parents:
71103
diff
changeset
|
586 |
|
8c1c717a830b
configure SSH hosting via "isabelle phabricator_setup_ssh";
wenzelm
parents:
71103
diff
changeset
|
587 |
def conf_ssh_port(port: Int): String = |
8c1c717a830b
configure SSH hosting via "isabelle phabricator_setup_ssh";
wenzelm
parents:
71103
diff
changeset
|
588 |
if (port == 22) "#Port 22" else "Port " + port |
8c1c717a830b
configure SSH hosting via "isabelle phabricator_setup_ssh";
wenzelm
parents:
71103
diff
changeset
|
589 |
|
8c1c717a830b
configure SSH hosting via "isabelle phabricator_setup_ssh";
wenzelm
parents:
71103
diff
changeset
|
590 |
def read_ssh_port(conf: Path): Int = |
8c1c717a830b
configure SSH hosting via "isabelle phabricator_setup_ssh";
wenzelm
parents:
71103
diff
changeset
|
591 |
{ |
8c1c717a830b
configure SSH hosting via "isabelle phabricator_setup_ssh";
wenzelm
parents:
71103
diff
changeset
|
592 |
val lines = split_lines(File.read(conf)) |
8c1c717a830b
configure SSH hosting via "isabelle phabricator_setup_ssh";
wenzelm
parents:
71103
diff
changeset
|
593 |
val ports = |
8c1c717a830b
configure SSH hosting via "isabelle phabricator_setup_ssh";
wenzelm
parents:
71103
diff
changeset
|
594 |
lines.flatMap({ |
8c1c717a830b
configure SSH hosting via "isabelle phabricator_setup_ssh";
wenzelm
parents:
71103
diff
changeset
|
595 |
case Port(Value.Int(p)) => Some(p) |
8c1c717a830b
configure SSH hosting via "isabelle phabricator_setup_ssh";
wenzelm
parents:
71103
diff
changeset
|
596 |
case No_Port() => Some(22) |
8c1c717a830b
configure SSH hosting via "isabelle phabricator_setup_ssh";
wenzelm
parents:
71103
diff
changeset
|
597 |
case _ => None |
8c1c717a830b
configure SSH hosting via "isabelle phabricator_setup_ssh";
wenzelm
parents:
71103
diff
changeset
|
598 |
}) |
8c1c717a830b
configure SSH hosting via "isabelle phabricator_setup_ssh";
wenzelm
parents:
71103
diff
changeset
|
599 |
ports match { |
8c1c717a830b
configure SSH hosting via "isabelle phabricator_setup_ssh";
wenzelm
parents:
71103
diff
changeset
|
600 |
case List(port) => port |
8c1c717a830b
configure SSH hosting via "isabelle phabricator_setup_ssh";
wenzelm
parents:
71103
diff
changeset
|
601 |
case Nil => error("Missing Port specification in " + conf) |
8c1c717a830b
configure SSH hosting via "isabelle phabricator_setup_ssh";
wenzelm
parents:
71103
diff
changeset
|
602 |
case _ => error("Multiple Port specifications in " + conf) |
8c1c717a830b
configure SSH hosting via "isabelle phabricator_setup_ssh";
wenzelm
parents:
71103
diff
changeset
|
603 |
} |
8c1c717a830b
configure SSH hosting via "isabelle phabricator_setup_ssh";
wenzelm
parents:
71103
diff
changeset
|
604 |
} |
8c1c717a830b
configure SSH hosting via "isabelle phabricator_setup_ssh";
wenzelm
parents:
71103
diff
changeset
|
605 |
|
8c1c717a830b
configure SSH hosting via "isabelle phabricator_setup_ssh";
wenzelm
parents:
71103
diff
changeset
|
606 |
def write_ssh_port(conf: Path, port: Int): Boolean = |
8c1c717a830b
configure SSH hosting via "isabelle phabricator_setup_ssh";
wenzelm
parents:
71103
diff
changeset
|
607 |
{ |
8c1c717a830b
configure SSH hosting via "isabelle phabricator_setup_ssh";
wenzelm
parents:
71103
diff
changeset
|
608 |
val old_port = read_ssh_port(conf) |
8c1c717a830b
configure SSH hosting via "isabelle phabricator_setup_ssh";
wenzelm
parents:
71103
diff
changeset
|
609 |
if (old_port == port) false |
8c1c717a830b
configure SSH hosting via "isabelle phabricator_setup_ssh";
wenzelm
parents:
71103
diff
changeset
|
610 |
else { |
8c1c717a830b
configure SSH hosting via "isabelle phabricator_setup_ssh";
wenzelm
parents:
71103
diff
changeset
|
611 |
val lines = split_lines(File.read(conf)) |
8c1c717a830b
configure SSH hosting via "isabelle phabricator_setup_ssh";
wenzelm
parents:
71103
diff
changeset
|
612 |
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
|
613 |
File.write(conf, cat_lines(lines1)) |
8c1c717a830b
configure SSH hosting via "isabelle phabricator_setup_ssh";
wenzelm
parents:
71103
diff
changeset
|
614 |
true |
8c1c717a830b
configure SSH hosting via "isabelle phabricator_setup_ssh";
wenzelm
parents:
71103
diff
changeset
|
615 |
} |
8c1c717a830b
configure SSH hosting via "isabelle phabricator_setup_ssh";
wenzelm
parents:
71103
diff
changeset
|
616 |
} |
8c1c717a830b
configure SSH hosting via "isabelle phabricator_setup_ssh";
wenzelm
parents:
71103
diff
changeset
|
617 |
|
8c1c717a830b
configure SSH hosting via "isabelle phabricator_setup_ssh";
wenzelm
parents:
71103
diff
changeset
|
618 |
|
8c1c717a830b
configure SSH hosting via "isabelle phabricator_setup_ssh";
wenzelm
parents:
71103
diff
changeset
|
619 |
/* phabricator_setup_ssh */ |
8c1c717a830b
configure SSH hosting via "isabelle phabricator_setup_ssh";
wenzelm
parents:
71103
diff
changeset
|
620 |
|
8c1c717a830b
configure SSH hosting via "isabelle phabricator_setup_ssh";
wenzelm
parents:
71103
diff
changeset
|
621 |
def phabricator_setup_ssh( |
8c1c717a830b
configure SSH hosting via "isabelle phabricator_setup_ssh";
wenzelm
parents:
71103
diff
changeset
|
622 |
server_port: Int = default_server_port, |
8c1c717a830b
configure SSH hosting via "isabelle phabricator_setup_ssh";
wenzelm
parents:
71103
diff
changeset
|
623 |
system_port: Int = default_system_port, |
8c1c717a830b
configure SSH hosting via "isabelle phabricator_setup_ssh";
wenzelm
parents:
71103
diff
changeset
|
624 |
test_server: Boolean = false, |
8c1c717a830b
configure SSH hosting via "isabelle phabricator_setup_ssh";
wenzelm
parents:
71103
diff
changeset
|
625 |
progress: Progress = No_Progress) |
8c1c717a830b
configure SSH hosting via "isabelle phabricator_setup_ssh";
wenzelm
parents:
71103
diff
changeset
|
626 |
{ |
8c1c717a830b
configure SSH hosting via "isabelle phabricator_setup_ssh";
wenzelm
parents:
71103
diff
changeset
|
627 |
Linux.check_system_root() |
8c1c717a830b
configure SSH hosting via "isabelle phabricator_setup_ssh";
wenzelm
parents:
71103
diff
changeset
|
628 |
|
8c1c717a830b
configure SSH hosting via "isabelle phabricator_setup_ssh";
wenzelm
parents:
71103
diff
changeset
|
629 |
val configs = read_config() |
8c1c717a830b
configure SSH hosting via "isabelle phabricator_setup_ssh";
wenzelm
parents:
71103
diff
changeset
|
630 |
|
8c1c717a830b
configure SSH hosting via "isabelle phabricator_setup_ssh";
wenzelm
parents:
71103
diff
changeset
|
631 |
if (server_port == system_port) { |
8c1c717a830b
configure SSH hosting via "isabelle phabricator_setup_ssh";
wenzelm
parents:
71103
diff
changeset
|
632 |
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
|
633 |
} |
8c1c717a830b
configure SSH hosting via "isabelle phabricator_setup_ssh";
wenzelm
parents:
71103
diff
changeset
|
634 |
|
8c1c717a830b
configure SSH hosting via "isabelle phabricator_setup_ssh";
wenzelm
parents:
71103
diff
changeset
|
635 |
val sshd_conf_system = Path.explode("/etc/ssh/sshd_config") |
8c1c717a830b
configure SSH hosting via "isabelle phabricator_setup_ssh";
wenzelm
parents:
71103
diff
changeset
|
636 |
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
|
637 |
|
8c1c717a830b
configure SSH hosting via "isabelle phabricator_setup_ssh";
wenzelm
parents:
71103
diff
changeset
|
638 |
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
|
639 |
Linux.service_shutdown(ssh_name) |
cd166c3904dd
more robust: system ssh service is required for Phabricator ssh service;
wenzelm
parents:
71109
diff
changeset
|
640 |
|
71109
8c1c717a830b
configure SSH hosting via "isabelle phabricator_setup_ssh";
wenzelm
parents:
71103
diff
changeset
|
641 |
val old_system_port = read_ssh_port(sshd_conf_system) |
8c1c717a830b
configure SSH hosting via "isabelle phabricator_setup_ssh";
wenzelm
parents:
71103
diff
changeset
|
642 |
if (old_system_port != system_port) { |
8c1c717a830b
configure SSH hosting via "isabelle phabricator_setup_ssh";
wenzelm
parents:
71103
diff
changeset
|
643 |
progress.echo("Reconfigurig system ssh service") |
71111
cd166c3904dd
more robust: system ssh service is required for Phabricator ssh service;
wenzelm
parents:
71109
diff
changeset
|
644 |
Linux.service_shutdown("ssh") |
71109
8c1c717a830b
configure SSH hosting via "isabelle phabricator_setup_ssh";
wenzelm
parents:
71103
diff
changeset
|
645 |
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
|
646 |
Linux.service_start("ssh") |
71109
8c1c717a830b
configure SSH hosting via "isabelle phabricator_setup_ssh";
wenzelm
parents:
71103
diff
changeset
|
647 |
} |
8c1c717a830b
configure SSH hosting via "isabelle phabricator_setup_ssh";
wenzelm
parents:
71103
diff
changeset
|
648 |
|
8c1c717a830b
configure SSH hosting via "isabelle phabricator_setup_ssh";
wenzelm
parents:
71103
diff
changeset
|
649 |
progress.echo("Configuring " + ssh_name + " service") |
8c1c717a830b
configure SSH hosting via "isabelle phabricator_setup_ssh";
wenzelm
parents:
71103
diff
changeset
|
650 |
|
71270 | 651 |
val ssh_command = command_setup(ssh_name, |
71122 | 652 |
"""if [ "$1" = "$NAME" ] |
653 |
then |
|
654 |
exec "$ROOT/phabricator/bin/ssh-auth" "$@" |
|
71270 | 655 |
fi""", exit = "exit 1") |
71109
8c1c717a830b
configure SSH hosting via "isabelle phabricator_setup_ssh";
wenzelm
parents:
71103
diff
changeset
|
656 |
|
8c1c717a830b
configure SSH hosting via "isabelle phabricator_setup_ssh";
wenzelm
parents:
71103
diff
changeset
|
657 |
File.write(sshd_conf_server, |
8c1c717a830b
configure SSH hosting via "isabelle phabricator_setup_ssh";
wenzelm
parents:
71103
diff
changeset
|
658 |
"""# OpenBSD Secure Shell server for Isabelle/Phabricator |
8c1c717a830b
configure SSH hosting via "isabelle phabricator_setup_ssh";
wenzelm
parents:
71103
diff
changeset
|
659 |
AuthorizedKeysCommand """ + ssh_command.implode + """ |
8c1c717a830b
configure SSH hosting via "isabelle phabricator_setup_ssh";
wenzelm
parents:
71103
diff
changeset
|
660 |
AuthorizedKeysCommandUser """ + daemon_user + """ |
8c1c717a830b
configure SSH hosting via "isabelle phabricator_setup_ssh";
wenzelm
parents:
71103
diff
changeset
|
661 |
AuthorizedKeysFile none |
8c1c717a830b
configure SSH hosting via "isabelle phabricator_setup_ssh";
wenzelm
parents:
71103
diff
changeset
|
662 |
AllowUsers """ + configs.map(_.name).mkString(" ") + """ |
8c1c717a830b
configure SSH hosting via "isabelle phabricator_setup_ssh";
wenzelm
parents:
71103
diff
changeset
|
663 |
Port """ + server_port + """ |
8c1c717a830b
configure SSH hosting via "isabelle phabricator_setup_ssh";
wenzelm
parents:
71103
diff
changeset
|
664 |
Protocol 2 |
8c1c717a830b
configure SSH hosting via "isabelle phabricator_setup_ssh";
wenzelm
parents:
71103
diff
changeset
|
665 |
PermitRootLogin no |
8c1c717a830b
configure SSH hosting via "isabelle phabricator_setup_ssh";
wenzelm
parents:
71103
diff
changeset
|
666 |
AllowAgentForwarding no |
8c1c717a830b
configure SSH hosting via "isabelle phabricator_setup_ssh";
wenzelm
parents:
71103
diff
changeset
|
667 |
AllowTcpForwarding no |
8c1c717a830b
configure SSH hosting via "isabelle phabricator_setup_ssh";
wenzelm
parents:
71103
diff
changeset
|
668 |
PrintMotd no |
8c1c717a830b
configure SSH hosting via "isabelle phabricator_setup_ssh";
wenzelm
parents:
71103
diff
changeset
|
669 |
PrintLastLog no |
8c1c717a830b
configure SSH hosting via "isabelle phabricator_setup_ssh";
wenzelm
parents:
71103
diff
changeset
|
670 |
PasswordAuthentication no |
8c1c717a830b
configure SSH hosting via "isabelle phabricator_setup_ssh";
wenzelm
parents:
71103
diff
changeset
|
671 |
ChallengeResponseAuthentication no |
8c1c717a830b
configure SSH hosting via "isabelle phabricator_setup_ssh";
wenzelm
parents:
71103
diff
changeset
|
672 |
PidFile /var/run/""" + ssh_name + """.pid |
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 |
|
8c1c717a830b
configure SSH hosting via "isabelle phabricator_setup_ssh";
wenzelm
parents:
71103
diff
changeset
|
675 |
Linux.service_install(ssh_name, |
8c1c717a830b
configure SSH hosting via "isabelle phabricator_setup_ssh";
wenzelm
parents:
71103
diff
changeset
|
676 |
"""[Unit] |
8c1c717a830b
configure SSH hosting via "isabelle phabricator_setup_ssh";
wenzelm
parents:
71103
diff
changeset
|
677 |
Description=OpenBSD Secure Shell server for Isabelle/Phabricator |
8c1c717a830b
configure SSH hosting via "isabelle phabricator_setup_ssh";
wenzelm
parents:
71103
diff
changeset
|
678 |
After=network.target auditd.service |
8c1c717a830b
configure SSH hosting via "isabelle phabricator_setup_ssh";
wenzelm
parents:
71103
diff
changeset
|
679 |
ConditionPathExists=!/etc/ssh/sshd_not_to_be_run |
8c1c717a830b
configure SSH hosting via "isabelle phabricator_setup_ssh";
wenzelm
parents:
71103
diff
changeset
|
680 |
|
8c1c717a830b
configure SSH hosting via "isabelle phabricator_setup_ssh";
wenzelm
parents:
71103
diff
changeset
|
681 |
[Service] |
8c1c717a830b
configure SSH hosting via "isabelle phabricator_setup_ssh";
wenzelm
parents:
71103
diff
changeset
|
682 |
EnvironmentFile=-/etc/default/ssh |
8c1c717a830b
configure SSH hosting via "isabelle phabricator_setup_ssh";
wenzelm
parents:
71103
diff
changeset
|
683 |
ExecStartPre=/usr/sbin/sshd -f """ + sshd_conf_server.implode + """ -t |
8c1c717a830b
configure SSH hosting via "isabelle phabricator_setup_ssh";
wenzelm
parents:
71103
diff
changeset
|
684 |
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
|
685 |
ExecReload=/usr/sbin/sshd -f """ + sshd_conf_server.implode + """ -t |
8c1c717a830b
configure SSH hosting via "isabelle phabricator_setup_ssh";
wenzelm
parents:
71103
diff
changeset
|
686 |
ExecReload=/bin/kill -HUP $MAINPID |
8c1c717a830b
configure SSH hosting via "isabelle phabricator_setup_ssh";
wenzelm
parents:
71103
diff
changeset
|
687 |
KillMode=process |
8c1c717a830b
configure SSH hosting via "isabelle phabricator_setup_ssh";
wenzelm
parents:
71103
diff
changeset
|
688 |
Restart=on-failure |
8c1c717a830b
configure SSH hosting via "isabelle phabricator_setup_ssh";
wenzelm
parents:
71103
diff
changeset
|
689 |
RestartPreventExitStatus=255 |
8c1c717a830b
configure SSH hosting via "isabelle phabricator_setup_ssh";
wenzelm
parents:
71103
diff
changeset
|
690 |
Type=notify |
8c1c717a830b
configure SSH hosting via "isabelle phabricator_setup_ssh";
wenzelm
parents:
71103
diff
changeset
|
691 |
RuntimeDirectory=sshd-phabricator |
8c1c717a830b
configure SSH hosting via "isabelle phabricator_setup_ssh";
wenzelm
parents:
71103
diff
changeset
|
692 |
RuntimeDirectoryMode=0755 |
8c1c717a830b
configure SSH hosting via "isabelle phabricator_setup_ssh";
wenzelm
parents:
71103
diff
changeset
|
693 |
|
8c1c717a830b
configure SSH hosting via "isabelle phabricator_setup_ssh";
wenzelm
parents:
71103
diff
changeset
|
694 |
[Install] |
8c1c717a830b
configure SSH hosting via "isabelle phabricator_setup_ssh";
wenzelm
parents:
71103
diff
changeset
|
695 |
WantedBy=multi-user.target |
8c1c717a830b
configure SSH hosting via "isabelle phabricator_setup_ssh";
wenzelm
parents:
71103
diff
changeset
|
696 |
Alias=""" + ssh_name + """.service |
8c1c717a830b
configure SSH hosting via "isabelle phabricator_setup_ssh";
wenzelm
parents:
71103
diff
changeset
|
697 |
""") |
8c1c717a830b
configure SSH hosting via "isabelle phabricator_setup_ssh";
wenzelm
parents:
71103
diff
changeset
|
698 |
|
8c1c717a830b
configure SSH hosting via "isabelle phabricator_setup_ssh";
wenzelm
parents:
71103
diff
changeset
|
699 |
for (config <- configs) { |
8c1c717a830b
configure SSH hosting via "isabelle phabricator_setup_ssh";
wenzelm
parents:
71103
diff
changeset
|
700 |
progress.echo("phabricator " + quote(config.name) + " port " + server_port) |
8c1c717a830b
configure SSH hosting via "isabelle phabricator_setup_ssh";
wenzelm
parents:
71103
diff
changeset
|
701 |
config.execute("config set diffusion.ssh-port " + Bash.string(server_port.toString)) |
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 |
if (test_server) { |
8c1c717a830b
configure SSH hosting via "isabelle phabricator_setup_ssh";
wenzelm
parents:
71103
diff
changeset
|
704 |
progress.bash( |
8c1c717a830b
configure SSH hosting via "isabelle phabricator_setup_ssh";
wenzelm
parents:
71103
diff
changeset
|
705 |
"""unset DISPLAY |
8c1c717a830b
configure SSH hosting via "isabelle phabricator_setup_ssh";
wenzelm
parents:
71103
diff
changeset
|
706 |
echo "{}" | ssh -p """ + Bash.string(server_port.toString) + |
8c1c717a830b
configure SSH hosting via "isabelle phabricator_setup_ssh";
wenzelm
parents:
71103
diff
changeset
|
707 |
" -o StrictHostKeyChecking=false " + |
8c1c717a830b
configure SSH hosting via "isabelle phabricator_setup_ssh";
wenzelm
parents:
71103
diff
changeset
|
708 |
Bash.string(config.name) + """@localhost conduit conduit.ping""").print |
8c1c717a830b
configure SSH hosting via "isabelle phabricator_setup_ssh";
wenzelm
parents:
71103
diff
changeset
|
709 |
} |
8c1c717a830b
configure SSH hosting via "isabelle phabricator_setup_ssh";
wenzelm
parents:
71103
diff
changeset
|
710 |
} |
8c1c717a830b
configure SSH hosting via "isabelle phabricator_setup_ssh";
wenzelm
parents:
71103
diff
changeset
|
711 |
} |
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 |
|
8c1c717a830b
configure SSH hosting via "isabelle phabricator_setup_ssh";
wenzelm
parents:
71103
diff
changeset
|
714 |
/* Isabelle tool wrapper */ |
8c1c717a830b
configure SSH hosting via "isabelle phabricator_setup_ssh";
wenzelm
parents:
71103
diff
changeset
|
715 |
|
8c1c717a830b
configure SSH hosting via "isabelle phabricator_setup_ssh";
wenzelm
parents:
71103
diff
changeset
|
716 |
val isabelle_tool4 = |
8c1c717a830b
configure SSH hosting via "isabelle phabricator_setup_ssh";
wenzelm
parents:
71103
diff
changeset
|
717 |
Isabelle_Tool("phabricator_setup_ssh", |
8c1c717a830b
configure SSH hosting via "isabelle phabricator_setup_ssh";
wenzelm
parents:
71103
diff
changeset
|
718 |
"setup ssh service for all Phabricator installations", args => |
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 |
var server_port = default_server_port |
8c1c717a830b
configure SSH hosting via "isabelle phabricator_setup_ssh";
wenzelm
parents:
71103
diff
changeset
|
721 |
var system_port = default_system_port |
8c1c717a830b
configure SSH hosting via "isabelle phabricator_setup_ssh";
wenzelm
parents:
71103
diff
changeset
|
722 |
var test_server = false |
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 getopts = |
8c1c717a830b
configure SSH hosting via "isabelle phabricator_setup_ssh";
wenzelm
parents:
71103
diff
changeset
|
725 |
Getopts(""" |
8c1c717a830b
configure SSH hosting via "isabelle phabricator_setup_ssh";
wenzelm
parents:
71103
diff
changeset
|
726 |
Usage: isabelle phabricator_setup_ssh [OPTIONS] |
8c1c717a830b
configure SSH hosting via "isabelle phabricator_setup_ssh";
wenzelm
parents:
71103
diff
changeset
|
727 |
|
8c1c717a830b
configure SSH hosting via "isabelle phabricator_setup_ssh";
wenzelm
parents:
71103
diff
changeset
|
728 |
Options are: |
8c1c717a830b
configure SSH hosting via "isabelle phabricator_setup_ssh";
wenzelm
parents:
71103
diff
changeset
|
729 |
-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
|
730 |
-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
|
731 |
-T test the ssh service for each Phabricator installation |
8c1c717a830b
configure SSH hosting via "isabelle phabricator_setup_ssh";
wenzelm
parents:
71103
diff
changeset
|
732 |
|
8c1c717a830b
configure SSH hosting via "isabelle phabricator_setup_ssh";
wenzelm
parents:
71103
diff
changeset
|
733 |
Configure ssh service for all Phabricator installations: a separate sshd |
8c1c717a830b
configure SSH hosting via "isabelle phabricator_setup_ssh";
wenzelm
parents:
71103
diff
changeset
|
734 |
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
|
735 |
be distinct. |
8c1c717a830b
configure SSH hosting via "isabelle phabricator_setup_ssh";
wenzelm
parents:
71103
diff
changeset
|
736 |
|
8c1c717a830b
configure SSH hosting via "isabelle phabricator_setup_ssh";
wenzelm
parents:
71103
diff
changeset
|
737 |
A particular Phabricator installation is addressed by using its |
8c1c717a830b
configure SSH hosting via "isabelle phabricator_setup_ssh";
wenzelm
parents:
71103
diff
changeset
|
738 |
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
|
739 |
stored ssh keys. |
8c1c717a830b
configure SSH hosting via "isabelle phabricator_setup_ssh";
wenzelm
parents:
71103
diff
changeset
|
740 |
""", |
8c1c717a830b
configure SSH hosting via "isabelle phabricator_setup_ssh";
wenzelm
parents:
71103
diff
changeset
|
741 |
"p:" -> (arg => server_port = Value.Int.parse(arg)), |
8c1c717a830b
configure SSH hosting via "isabelle phabricator_setup_ssh";
wenzelm
parents:
71103
diff
changeset
|
742 |
"q:" -> (arg => system_port = Value.Int.parse(arg)), |
8c1c717a830b
configure SSH hosting via "isabelle phabricator_setup_ssh";
wenzelm
parents:
71103
diff
changeset
|
743 |
"T" -> (_ => test_server = true)) |
8c1c717a830b
configure SSH hosting via "isabelle phabricator_setup_ssh";
wenzelm
parents:
71103
diff
changeset
|
744 |
|
8c1c717a830b
configure SSH hosting via "isabelle phabricator_setup_ssh";
wenzelm
parents:
71103
diff
changeset
|
745 |
val more_args = getopts(args) |
8c1c717a830b
configure SSH hosting via "isabelle phabricator_setup_ssh";
wenzelm
parents:
71103
diff
changeset
|
746 |
if (more_args.nonEmpty) getopts.usage() |
8c1c717a830b
configure SSH hosting via "isabelle phabricator_setup_ssh";
wenzelm
parents:
71103
diff
changeset
|
747 |
|
8c1c717a830b
configure SSH hosting via "isabelle phabricator_setup_ssh";
wenzelm
parents:
71103
diff
changeset
|
748 |
val progress = new Console_Progress |
8c1c717a830b
configure SSH hosting via "isabelle phabricator_setup_ssh";
wenzelm
parents:
71103
diff
changeset
|
749 |
|
8c1c717a830b
configure SSH hosting via "isabelle phabricator_setup_ssh";
wenzelm
parents:
71103
diff
changeset
|
750 |
phabricator_setup_ssh( |
8c1c717a830b
configure SSH hosting via "isabelle phabricator_setup_ssh";
wenzelm
parents:
71103
diff
changeset
|
751 |
server_port = server_port, system_port = system_port, test_server = test_server, |
8c1c717a830b
configure SSH hosting via "isabelle phabricator_setup_ssh";
wenzelm
parents:
71103
diff
changeset
|
752 |
progress = progress) |
8c1c717a830b
configure SSH hosting via "isabelle phabricator_setup_ssh";
wenzelm
parents:
71103
diff
changeset
|
753 |
}) |
70967 | 754 |
} |