-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathmementoembed-install-script.sh
executable file
·434 lines (338 loc) · 10.6 KB
/
mementoembed-install-script.sh
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
#!/bin/bash
set -e
function test_command(){
command=$1
printf "checking for command: $command [ ]"
set +e
which $command > /dev/null
status=$?
if [ $status -ne 0 ]; then
printf "\b\b\b\b\b"
printf "FAIL]\n"
echo "the command '$command' is required to install MementoEmbed; it was not detected in your PATH"
exit 2
fi
printf "\b\b\b\b\b"
printf " OK ]\n"
set -e
}
function test_access() {
directory=$1
printf "verifying that user `whoami` has write access to $directory [ ]"
set +e
touch ${directory}/mementoembed-install-testfile.deleteme > /dev/null 2>&1
status=$?
printf "\b\b\b\b\b"
if [ $status -ne 0 ]; then
printf "FAIL]\n"
echo "this installer needs to be able to write to '${directory}', which `whoami` cannot write to, please run it as a user with these permissions"
exit 22
fi
rm ${directory}/mementoembed-install-testfile.deleteme
printf " OK ]\n"
set -e
}
function run_command() {
text_to_print=$1
command_to_run=$2
newline=$3
if [ -z $newline ]; then
newline="yes"
fi
printf "${text_to_print} [ ]"
command_output_file=`mktemp`
set +e
eval "$command_to_run" > $command_output_file 2>&1
status=$?
printf "\b\b\b\b\b\b"
if [ $status -ne 0 ]; then
printf "[FAIL]\n"
echo
cat "$command_output_file"
echo
echo "${text_to_print} FAILED"
exit 2
fi
if [ $newline == "nonewline" ]; then
printf "[ OK ]"
else
printf "[ OK ]\n"
fi
set -e
}
function verify_absolute_path() {
directory_to_check=$1
printf "checking if $directory_to_check is an absolute path [ ]"
printf "\b\b\b\b\b\b"
if [ "${directory_to_check:0:1}" = "/" ]; then
printf "[ OK ]\n"
absolute_path_check=0
else
printf "[FAIL]\n"
absolute_path_check=1
fi
}
function check_for_systemctl() {
printf "checking for systemctl -- does this server run systemd? [ ]"
set +e
which systemctl
status=$?
SYSTEMD_SERVER=$status
printf "\b\b\b\b\b\b"
if [ $status -eq 0 ]; then
printf "[ OK ]\n"
systemctl_check=0
else
printf "[ NO ]\n"
systemctl_check=1
fi
set -e
}
function check_for_systemd() {
printf "checking for systemd [ ]"
printf "\b\b\b\b\b\b"
# not all systemd servers have this directory
# checkdir="/run/systemd/system"
# Ubuntu 21.04 and Centos 8 have this directory
checkdir="/etc/systemd/system"
if [[ -e ${checkdir} ]]; then
printf "[ OK ]\n"
systemd_check=0
else
printf "[ NO ]\n"
systemd_check=1
fi
}
function check_for_checkconfig() {
printf "checking for checkconfig -- does this server use initd runlevels instead of systemd? [ ]"
set +e
which systemctl > /dev/null
status=$?
SYSTEMD_SERVER=$status
printf "\b\b\b\b\b\b"
if [ $status -eq 0 ]; then
printf "[ OK ]\n"
checkconfig_check=0
else
printf "[ NO ]\n"
checkconfig_check=1
fi
set -e
}
function create_generic_startup_scripts() {
printf "creating startup wrapper [ ]"
set +e
cat <<EOF > ${INSTALL_DIRECTORY}/start-mementoembed.sh
#!/bin/bash
set -e
printf "starting MementoEmbed [ ]"
${INSTALL_DIRECTORY}/mementoembed-virtualenv/bin/waitress-serve --host=${FLASK_IP} --port=${FLASK_PORT} --call mementoembed:create_app &
status=\$?
pid=\$!
if [ \$status -eq 0 ]; then
echo \$pid > ${INSTALL_DIRECTORY}/var/run/mementoembed.pid
printf "[ OK ]\n"
else
printf "[FAIL]\n"
fi
EOF
status=$?
printf "\b\b\b\b\b\b"
if [ $status -eq 0 ]; then
printf "[ OK ]\n"
else
printf "[FAIL]\n"
echo "FAILED: could not create MementoEmbed startup wrapper"
exit 2
fi
set -e
printf "creating shutdown wrapper [ ]"
set +e
cat <<EOF > ${INSTALL_DIRECTORY}/stop-mementoembed.sh
#!/bin/bash
set -e
printf "stopping MementoEmbed "
pid=\`cat ${INSTALL_DIRECTORY}/var/run/mementoembed.pid\`
kill \$pid
ps -ef | grep \$pid | grep -v \$pid
status=$?
if [ $status -eq 0 ]; then
printf "[ OK ]\n"
rm ${INSTALL_DIRECTORY}/var/run/mementoembed.pid
else
printf "[FAIL]\n"
echo "could not stop process \$pid"
fi
EOF
status=$?
printf "\b\b\b\b\b\b"
if [ $status -eq 0 ]; then
printf "[ OK ]\n"
else
printf "[FAIL]\n"
echo "FAILED: could not create MementoEmbed startup wrapper"
exit 2
fi
set -e
run_command "setting permissions on startup wrapper" "chmod 0755 ${INSTALL_DIRECTORY}/start-mementoembed.sh"
run_command "setting permissions on shutdown wrapper" "chmod 0755 ${INSTALL_DIRECTORY}/stop-mementoembed.sh"
}
function create_systemd_startup() {
printf "creating systemd MementoEmbed service file [ ]"
set +e
cat <<EOF > /etc/systemd/system/mementoembed.service
[Unit]
Description=The MementoEmbed service
After=syslog.target network.target remote-fs.target nss-lookup.target redis
Requires=redis
[Service]
ExecStart=${INSTALL_DIRECTORY}/mementoembed-virtualenv/bin/waitress-serve --host=${FLASK_IP} --port=${FLASK_PORT} --call mementoembed:create_app
User=${MEMENTOEMBED_USER}
[Install]
WantedBy=multi-user.target
EOF
status=$?
set -e
printf "\b\b\b\b\b\b"
if [ $status -eq 0 ]; then
printf "[ OK ]\n"
else
printf "[FAIL]\n"
exit 2
fi
}
function update_configuration_for_environment_and_install() {
printf "setting configuration based on install environment: [ ]"
sed "s?{{ INSTALL_DIRECTORY }}?${INSTALL_DIRECTORY}?g" ${INSTALL_DIRECTORY}/template_appconfig.cfg > /tmp/me.cfg1
sed "s?{{ LOG_DIRECTORY }}?${INSTALL_DIRECTORY}/var/log?g" /tmp/me.cfg1 > /tmp/me.cfg2
sed "s?{{ WORKING_DIRECTORY }}?${INSTALL_DIRECTORY}/var/run?g" /tmp/me.cfg2 > /tmp/me.cfg3
set +e
grep "{{ [A-Z_]* }}" /tmp/m3.cfg > /dev/null 2>&1
status=$?
set -e
if [ $status -eq 0 ]; then
printf "\b\b\b\b\b"
printf "FAIL]\n"
echo "something went wrong updating the configuration with the appropriate values"
else
printf "\b\b\b\b\b"
printf " OK ]\n"
fi
printf "copying configuration file to /etc/mementoembed.cfg [ ]"
cp /tmp/me.cfg3 /etc/mementoembed.cfg
status=$?
if [ $status -ne 0 ]; then
printf "\b\b\b\b\b"
printf "FAIL]\n"
echo "could not copy the updated configuration to the /etc/ directory"
else
printf "\b\b\b\b\b"
printf " OK ]\n"
fi
}
function perform_install() {
test_command "ls"
test_command "mkdir"
test_command "touch"
test_command "whoami"
test_command "tar"
test_command "dirname"
test_command "python"
test_command "virtualenv"
test_command "node"
test_command "npm"
test_access `dirname ${INSTALL_DIRECTORY}`
test_access "/etc"
run_command "setting install directory to $INSTALL_DIRECTORY" ""
verify_absolute_path "$INSTALL_DIRECTORY"
if [ $absolute_path_check -ne 0 ]; then
echo "please specify an absolute path for the installation directory"
exit 22
fi
run_command "setting umask to 0022 for duration of install" "umask 0022"
run_command "creating $INSTALL_DIRECTORY" "mkdir -p $INSTALL_DIRECTORY"
run_command "creating ${INSTALL_DIRECTORY}/var/run" "mkdir -p $INSTALL_DIRECTORY/var/run"
run_command "creating ${INSTALL_DIRECTORY}/var/log" "mkdir -p $INSTALL_DIRECTORY/var/log"
run_command "removing existing virtualenv, if present" "rm -rf $INSTALL_DIRECTORY/mementoembed-virtualenv"
run_command "discovering MementoEmbed archive" "ls mementoembed-*.tar.gz"
MEMENTOEMBED_TARBALL=`cat ${command_output_file}`
run_command "creating virtualenv for MementoEmbed" "virtualenv $INSTALL_DIRECTORY/mementoembed-virtualenv --python ${PYTHON_EXE}"
run_command "installing MementoEmbed and dependencies" "${INSTALL_DIRECTORY}/mementoembed-virtualenv/bin/pip install --no-cache-dir ${MEMENTOEMBED_TARBALL}"
run_command "installing waitress" "${INSTALL_DIRECTORY}/mementoembed-virtualenv/bin/pip install waitress"
run_command "copying package-lock.json" "cp package-lock.json ${INSTALL_DIRECTORY}"
run_command "installing NodeJS dependencies" "(cd ${INSTALL_DIRECTORY}; npm install --save)"
# run_command "establishing package.json" "(cd ${INSTALL_DIRECTORY}; npm init)"
run_command "removing package-lock.json" "rm ${INSTALL_DIRECTORY}/package-lock.json"
run_command "installing Puppeteer" "(cd ${INSTALL_DIRECTORY}; npm install puppeteer --save --unsafe-perm)"
run_command "copying template configuration file into ${INSTALL_DIRECTORY}" "cp template_appconfig.cfg ${INSTALL_DIRECTORY}"
update_configuration_for_environment_and_install
if [ $FORCE_SYSTEMD -eq 0 ]; then
create_systemd_startup
fi
check_for_systemd
if [ $systemd_check -ne 0 ]; then
check_for_checkconfig
if [ $checkconfig_check -ne 0 ]; then
create_generic_startup_scripts
echo
echo "Notes:"
echo "* to start MementoEmbed, run ${INSTALL_DIRECTORY}/start-mementoembed.sh"
echo "* to stop MementoEmbed, run ${INSTALL_DIRECTORY}/stop-mementoembed.sh"
# TODO: check for macOS and create an icon or something in /Applications for the user to start it there
else
create_initd_startup
fi
else
create_systemd_startup
fi
}
echo
echo "Welcome to MementoEmbed - beginning Unix/Linux install"
echo
INSTALL_DIRECTORY="/opt/mementoembed"
FLASK_PORT=5550
FLASK_IP="0.0.0.0"
MEMENTOEMBED_USER="root"
FORCE_SYSTEMD=1
PYTHON_EXE="/usr/bin/python"
while test $# -gt 0; do
case "$1" in
--install-directory)
shift
INSTALL_DIRECTORY=$1
;;
--service-port)
shift
FLASK_PORT=$1
;;
--cli-wrapper-path)
shift
WRAPPER_SCRIPT_PATH=$1
;;
--mementoembed-user)
shift
MEMENTOEMBED_USER=$1
;;
--python-exe)
shift
PYTHON_EXE=$1
;;
--force-systemd)
shift
FORCE_SYSTEMD=0
;;
--force-initd)
shift
FORCE_INITD=0
;;
--django_IP)
shift
FLASK_IP=$1
;;
esac
shift
done
echo "installing to directory ${INSTALL_DIRECTORY}"
perform_install $@
echo
echo "Done with Unix/Linux install. Please read the documentation for details on more setup options and how to use MementoEmbed."