forked from jsuto/piler
-
Notifications
You must be signed in to change notification settings - Fork 0
/
postinstall.sh.in
executable file
·469 lines (342 loc) · 11.4 KB
/
postinstall.sh.in
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
#!/bin/bash
set -o nounset
set -o errexit
set -o pipefail
SCRIPT_PATH="$(readlink -f "$0")"
SCRIPT_DIR="${SCRIPT_PATH%/*}"
INDEXER="$(command -v indexer)"
SEARCHD="$(command -v searchd)"
CRON_ORIG="/tmp/crontab.piler.orig"
CRON_TMP="/tmp/crontab.piler"
PILERCONF_TMP="/tmp/config.piler.88"
SOCKET_HELPER_SCRIPT="aaa.pl"
load_default_values() {
PILERUSER="piler"
SYSCONFDIR=__SYSCONFDIR
LOCALSTATEDIR=__LOCALSTATEDIR
LIBEXECDIR=__LIBEXECDIR
DATAROOTDIR=__DATAROOTDIR
PILER_CONFIG_DIR="${SYSCONFDIR}/piler"
CONFIG_SITE_PHP="${PILER_CONFIG_DIR}/config-site.php"
KEYTMPFILE="piler.key"
KEYFILE="${PILER_CONFIG_DIR}/piler.key"
HOSTNAME="$(hostname --fqdn)"
MYSQL_HOSTNAME="localhost"
MYSQL_DATABASE="piler"
MYSQL_USERNAME="piler"
MYSQL_PASSWORD=""
MYSQL_ROOT_PASSWORD=""
MYSQL_SOCKET=""
RT=1
SEARCHCFG="${PILER_CONFIG_DIR}/manticore.conf"
WWWGROUP="www-data"
DOCROOT="/var/piler/www"
SMARTHOST=""
SMARTHOST_PORT=25
SSL_CERT_DATA="/C=US/ST=Denial/L=Springfield/O=Dis/CN=archive.example.com"
}
make_certificate() {
local piler_pem="${PILER_CONFIG_DIR}/piler.pem"
if [[ ! -f "$piler_pem" ]]; then
echo -n "Making an ssl certificate ... "
openssl req -new -newkey rsa:4096 -days 3650 -nodes -x509 -subj "$SSL_CERT_DATA" -keyout "$piler_pem" -out 1.cert -sha1
cat 1.cert >> "$piler_pem"
chmod 640 "$piler_pem"
chgrp "$PILERUSER" "$piler_pem"
rm -f 1.cert
fi
}
display_install_intro() {
echo ""
echo ""
echo "This is the postinstall utility for piler"
echo "It should be run only at the first install. DO NOT run on an existing piler installation!"
echo ""
askYN "Continue? [Y/N]" "N"
if [[ "$response" != "yes" ]]; then
echo "Aborted."
exit 1
fi
echo ""
}
check_user() {
user="$1"
if [[ $(whoami) != "$user" ]]; then echo "ERROR: postinstaller must be run as ${user} user"; exit 1; fi
}
isFQDN() {
# we need min. 2 dots
if [[ "$1" == "xdogfood" ]]; then
echo 1
return
fi
if [[ "$1" == "x" ]]; then
echo 0
return
fi
NF="$(echo "$1" | awk -F. '{print NF}')"
if [[ $NF -ge 2 ]]; then
echo 1
else
echo 0
fi
}
ask() {
PROMPT=$1
DEFAULT=$2
echo ""
echo -n "$PROMPT [$DEFAULT] "
read -r response
if [[ -z "$response" ]]; then
response=$DEFAULT
fi
}
askNoEcho() {
PROMPT=$1
DEFAULT=$2
stty -echo
ask "$PROMPT" "$DEFAULT"
stty echo
echo ""
}
askNonBlankNoEcho() {
PROMPT=$1
DEFAULT=$2
while true; do
stty -echo
ask "$PROMPT" "$DEFAULT"
stty echo
echo ""
if [[ -n "$response" ]]; then
break
fi
echo "A non-blank answer is required"
done
}
askNonBlank() {
PROMPT="$1"
DEFAULT="$2"
while true; do
ask "$PROMPT" "$DEFAULT"
if [[ -n "$response" ]]; then
break
fi
echo "A non-blank answer is required"
done
}
askYN() {
PROMPT=$1
DEFAULT=$2
if [[ "$DEFAULT" == "yes" || "$DEFAULT" == "Yes" || "$DEFAULT" == "xy" || "$DEFAULT" == "xY" ]]; then
DEFAULT="Y"
else
DEFAULT="N"
fi
while true; do
ask "$PROMPT" "$DEFAULT"
response=$(perl -e "print lc(\"$response\");")
if [[ -z "$response" ]]; then
:
else
if [[ "$response" == "yes" || "$response" == "y" ]]; then
response="yes"
break
else
if [[ "$response" == "no" || "$response" == "n" ]]; then
response="no"
break
fi
fi
fi
echo "A Yes/No answer is required"
done
}
preinstall_check() {
check_user root
if [[ "$INDEXER" == "" ]]; then "ERROR: cannot find indexer binary"; echo ""; exit ; fi
if [[ "$SEARCHD" == "" ]]; then "ERROR: cannot find searchd binary"; echo ""; exit 0; fi
if [[ -f "$KEYFILE" ]]; then echo "ERROR: found existing keyfile (${KEYFILE}), aborting install"; echo ""; exit 0; fi
}
gather_webserver_data() {
askNonBlank "Please enter the webserver groupname" "$WWWGROUP"
WWWGROUP="$response"
}
gather_mysql_account() {
if [[ -e /var/lib/mysql/mysql.sock ]]; then MYSQL_SOCKET="/var/lib/mysql/mysql.sock"; fi
if [[ -e /var/run/mysqld/mysqld.sock ]]; then MYSQL_SOCKET="/var/run/mysqld/mysqld.sock"; fi
askNonBlank "Please enter mysql hostname" "$MYSQL_HOSTNAME"
MYSQL_HOSTNAME="$response"
if [[ $MYSQL_HOSTNAME == "localhost" ]]; then
askNonBlank "Please enter mysql socket path" "$MYSQL_SOCKET"
MYSQL_SOCKET="$response"
else
MYSQL_SOCKET=""
fi
askNonBlank "Please enter mysql database" "${MYSQL_DATABASE}"
MYSQL_DATABASE="$response"
askNonBlank "Please enter mysql user name" "${MYSQL_USERNAME}"
MYSQL_USERNAME="$response"
askNoEcho "Please enter mysql password for ${MYSQL_USERNAME}" ""
MYSQL_PASSWORD="$response"
askNonBlankNoEcho "Please enter mysql root password. If its a recent version of mysql and uses socket authentication, then any string would do here" ""
MYSQL_ROOT_PASSWORD="$response"
askYN "Real time index for manticore? [Y/N]" "Y"
if [[ "$response" != "yes" ]]; then
RT=0
fi
s=$(echo "use information_schema; select TABLE_NAME from TABLES where TABLE_SCHEMA='${MYSQL_DATABASE}'" | mysql -h "$MYSQL_HOSTNAME" -u root --password="$MYSQL_ROOT_PASSWORD")
# shellcheck disable=SC2181
if [[ $? -eq 0 ]]; then
echo "mysql connection successful"; echo;
if [[ $(echo "$s" | grep -c metadata) -eq 1 ]]; then echo "ERROR: Detected metadata table in ${MYSQL_DATABASE}. Aborting"; exit 0; fi
else
echo "ERROR: failed to connect to mysql";
gather_mysql_account
fi
}
gather_smtp_relay_data() {
ask "Please enter smtp relay" "$SMARTHOST"
SMARTHOST="$response"
ask "Please enter smtp relay port" "$SMARTHOST_PORT"
SMARTHOST_PORT="$response"
}
make_cron_entries() {
crontab -u "$PILERUSER" -l || true > "$CRON_ORIG"
if grep PILERSTART "$CRON_ORIG" > /dev/null 2>&1 != 0; then
cat /dev/null > "$CRON_ORIG"
fi
if grep PILEREND "$CRON_ORIG" > /dev/null 2>&1 != 0; then
cat /dev/null > "$CRON_ORIG"
fi
rm -f "$CRON_TMP"
{
echo "";
echo "### PILERSTART";
[[ $RT -eq 1 ]] || echo "5,35 * * * * ${LIBEXECDIR}/piler/indexer.delta.sh";
[[ $RT -eq 1 ]] || echo "30 2 * * * ${LIBEXECDIR}/piler/indexer.main.sh";
echo "40 3 * * * ${LIBEXECDIR}/piler/purge.sh";
[[ $RT -eq 1 ]] || echo "*/15 * * * * ${INDEXER} --quiet tag1 --rotate --config ${PILER_CONFIG_DIR}/manticore.conf";
[[ $RT -eq 1 ]] || echo "*/15 * * * * ${INDEXER} --quiet note1 --rotate --config ${PILER_CONFIG_DIR}/manticore.conf";
echo "30 6 * * * /usr/bin/php ${LIBEXECDIR}/piler/generate_stats.php --webui ${DOCROOT} >/dev/null";
echo "*/5 * * * * /usr/bin/find ${LOCALSTATEDIR}/piler/error -type f|wc -l > ${LOCALSTATEDIR}/piler/stat/error";
echo "*/5 * * * * /usr/bin/find ${DOCROOT}/tmp -type f -name i.\* -exec rm -f {} \;";
echo "*/10 * * * * ${LIBEXECDIR}/piler/import.sh";
echo "### PILEREND";
} >> "$CRON_TMP"
}
make_new_key() {
dd if=/dev/urandom bs=56 count=1 of="$KEYTMPFILE" 2>/dev/null
if [ "$(stat -c '%s' "$KEYTMPFILE")" -ne 56 ]; then echo "could not read 56 bytes from /dev/urandom to ${KEYTMPFILE}"; exit 1; fi
}
show_summary() {
echo
echo
echo "INSTALLATION SUMMARY:"
echo
echo "piler user: ${PILERUSER}"
echo "keyfile: ${KEYFILE}"
echo
echo "mysql host: ${MYSQL_HOSTNAME}"
echo "mysql socket: ${MYSQL_SOCKET}"
echo "mysql database: ${MYSQL_DATABASE}"
echo "mysql username: ${MYSQL_USERNAME}"
echo "mysql password: *******"
echo
echo "indexer: ${INDEXER}"
echo "config file: ${SEARCHCFG}"
echo "real time index: ${RT}"
echo
echo "vhost docroot: ${DOCROOT}"
echo "www group: ${WWWGROUP}"
echo
echo "smtp relay host: ${SMARTHOST}"
echo "smtp relay port: ${SMARTHOST_PORT}"
echo
echo "piler crontab:"
cat "$CRON_TMP"
echo; echo;
export HOSTNAME SITE_NAME DOCROOT SMTP_DOMAIN MYSQL_HOSTNAME MYSQL_USERNAME MYSQL_PASSWORD MYSQL_DATABASE SMARTHOST SMARTHOST_PORT RT
askYN "Correct? [Y/N]" "N"
if [[ $response != "yes" ]]; then
echo "Aborted."
exit
fi
}
execute_post_install_tasks() {
askYN "Continue and modify system? [Y/N]" "N"
if [[ "$response" != "yes" ]]; then
echo "Aborted."
exit
fi
echo;
echo -n "Creating mysql database... ";
sed -e "s%MYSQL_HOSTNAME%${MYSQL_HOSTNAME}%g" -e "s%MYSQL_DATABASE%${MYSQL_DATABASE}%g" -e "s%MYSQL_USERNAME%${MYSQL_USERNAME}%g" -e "s%MYSQL_PASSWORD%${MYSQL_PASSWORD}%g" "${DATAROOTDIR}/piler/db-mysql-root.sql.in" | mysql -h "$MYSQL_HOSTNAME" -u root --password="$MYSQL_ROOT_PASSWORD"
mysql -h "$MYSQL_HOSTNAME" -u "$MYSQL_USERNAME" --password="$MYSQL_PASSWORD" "$MYSQL_DATABASE" < "${DATAROOTDIR}/piler/db-mysql.sql"
echo "Done."
echo -n "Writing manticore configuration... ";
sed -e "s%MYSQL_HOSTNAME%${MYSQL_HOSTNAME}%" -e "s%MYSQL_DATABASE%${MYSQL_DATABASE}%" -e "s%MYSQL_USERNAME%${MYSQL_USERNAME}%" -e "s%MYSQL_PASSWORD%${MYSQL_PASSWORD}%" "${SYSCONFDIR}/piler/manticore.conf.dist" > "$SEARCHCFG"
chmod +x "$SEARCHCFG"
echo "Done."
if [[ $RT -eq 0 ]]; then
echo -n "Initializing manticore index data... "
sed -i "s/define('RT', 1)/define('RT', 0)/" "$SEARCHCFG"
su "$PILERUSER" -c "indexer --all --config ${SEARCHCFG}"
echo "Done."
fi
echo -n "installing cron entries for ${PILERUSER}... "
crontab -u "$PILERUSER" "$CRON_TMP"
echo "Done."
echo -n "installing keyfile (${KEYTMPFILE}) to ${KEYFILE}... "
cp "$KEYTMPFILE" "$KEYFILE"
chgrp "$PILERUSER" "$KEYFILE"
chmod 640 "$KEYFILE"
rm -f "$KEYTMPFILE"
echo "Done."
echo "Fix piler.conf path in pilerpurge.py"
sed -i "s%\/etc\/piler\/piler\.conf%${SYSCONFDIR}\/piler\/piler\.conf%" "${LIBEXECDIR}/piler/pilerpurge.py"
envsubst < "${SCRIPT_DIR}/config-site.php.in" | sed s%config%\$config%g > "$CONFIG_SITE_PHP"
make_certificate
cat <<SOCKHELPER > "$SOCKET_HELPER_SCRIPT"
\$a=\$ARGV[0];
\$a=~s/\//\\\\\//g;
print \$a;
SOCKHELPER
MYSQL_SOCKET=$(perl "$SOCKET_HELPER_SCRIPT" "$MYSQL_SOCKET")
sed -e "s/mysqlpwd=verystrongpassword/mysqlpwd=${MYSQL_PASSWORD}/" -e "s/tls_enable=0/tls_enable=1/" -e "s/mysqlsocket=\/var\/run\/mysqld\/mysqld.sock/mysqlsocket=${MYSQL_SOCKET}/" "${SYSCONFDIR}/piler/piler.conf.dist" > "$PILERCONF_TMP"
if [[ $RT -eq 0 ]]; then sed -e "s/rtindex=.*/rtindex=0/" "$PILERCONF_TMP"; fi
if ! grep queuedir "$PILERCONF_TMP"; then echo queuedir=/var/piler/store >> "$PILERCONF_TMP"; fi
cat "$PILERCONF_TMP" > "${SYSCONFDIR}/piler/piler.conf"
rm -f "$PILERCONF_TMP"
# Manticore is the preferred search engine
touch "${SYSCONFDIR}/piler/MANTICORE"
chmod 755 "${LOCALSTATEDIR}/piler/stat"
if [[ -d webui ]]; then
echo -n "Copying www files to ${DOCROOT}... "
mkdir -p "$DOCROOT" || exit 1
cp -R webui/* "$DOCROOT"
cp webui/.htaccess "$DOCROOT"
fi
if [[ -d /var/www/piler.example.com ]]; then
mv /var/www/piler.example.com "$DOCROOT"
fi
if [[ -d "$DOCROOT" ]]; then webui_install; fi
}
webui_install() {
chmod 770 "${DOCROOT}/tmp" "${DOCROOT}/images"
chown "${PILERUSER}:${WWWGROUP}" "${DOCROOT}/tmp"
echo "Done."
}
clean_up_temp_stuff() {
rm -f "$CRON_TMP"
echo; echo "Done post installation tasks."; echo
}
load_default_values
preinstall_check
display_install_intro
gather_webserver_data
gather_mysql_account
gather_smtp_relay_data
make_cron_entries
make_new_key
show_summary
execute_post_install_tasks
clean_up_temp_stuff