-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathchiton-install.in
449 lines (402 loc) · 13.7 KB
/
chiton-install.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
#!/bin/bash
########################################################################
#
# This file is part of Chiton.
#
# Chiton is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# Chiton is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with Chiton. If not, see <https://www.gnu.org/licenses/>.
#
# Copyright 2020 Ed Martin <[email protected]>
#
########################################################################
set -e
export PATH="/sbin:/usr/sbin:$PATH"
#database settings
MYSQL_HOST="localhost"
MYSQL_DB=@PKGNAME@
MYSQL_INSTALL_USER="root"
MYSQL_INSTALL_PW=""
MYSQL_USER=@PKGNAME@
MYSQL_USER_HOST="localhost"
BACKEND_USER=@PKGNAME@
#make a decent default PW
MYSQL_USER_PW=
PW_ENCODE=`which mimencode || echo false`
if [ "x$PW_ENCODE" != "xfalse" ]; then
MYSQL_USER_PW=`dd if=/dev/random bs=1 count=15 2>/dev/null | $PW_ENCODE`
else
PW_ENCODE=`which base64 || echo false`
if [ "x$PW_ENCODE" != "xfalse" ]; then
MYSQL_USER_PW=`dd if=/dev/random bs=1 count=15 2>/dev/null | $PW_ENCODE`
else
MYSQL_USER_PW=changeme
fi
fi
HTML_SRC_DIR=@pkgdatadir@/web
STATE_DIR=@localstatedir@/@PKGNAME@
APACHE_CFG_DIR=@apachecfgdir@
CFGDIR=@cfgdir@
SKIP_ALL=N
WEBDIR=/@PKGNAME@
VID_DIR=$STATE_DIR/vids
#Packaging scripts might want to change this
APACHE_USER=apache
QUIET_MODE=N
SKIP_DB=N
PURGE=N
#take a couple of guesses
if ! getent passwd $APACHE_USER > /dev/null ; then
APACHE_USER=httpd
if ! getent passwd $APACHE_USER > /dev/null ; then
APACHE_USER=www-data
fi
fi
#check for an old script, pull the DB info from the old config
if [ -r "$CFGDIR/chiton.cfg" ]; then
MYSQL_USER=$(cat "$CFGDIR/chiton.cfg" | grep db-user | sed -E -e 's!^\s*.*?\s*=\s*(['\''"]?)(.+)(\1)(\s*|\s*#.*)$!\2!' -e 's!\\(["'\''\\])!\1!g')
MYSQL_USER_PW=$(cat "$CFGDIR/chiton.cfg" | grep db-password | sed -E -e 's!^\s*.*?\s*=\s*(['\''"]?)(.+)(\1)(\s*|\s*#.*)$!\2!' -e 's!\\(["'\''\\])!\1!g')
MYSQL_HOST=$(cat "$CFGDIR/chiton.cfg" | grep db-host | sed -E -e 's!^\s*.*?\s*=\s*(['\''"]?)(.+)(\1)(\s*|\s*#.*)$!\2!' -e 's!\\(["'\''\\])!\1!g')
MYSQL_DB=$(cat "$CFGDIR/chiton.cfg" | grep db-database | sed -E -e 's!^\s*.*?\s*=\s*(['\''"]?)(.+)(\1)(\s*|\s*#.*)$!\2!' -e 's!\\(["'\''\\])!\1!g')
VID_DIR_TMP=$(cat "$CFGDIR/chiton.cfg" | grep output-dir | sed -E -e 's!^\s*.*?\s*=\s*(['\''"]?)(.+)(\1)(\s*|\s*#.*)$!\2!' -e 's!\\(["'\''\\])!\1!g')
if [[ -n $VID_DIR_TMP ]]; then
VID_DIR="$VID_DIR_TMP"
fi
fi
# Check all our options
while getopts "hH:d:U:P:u:p:yv:qkG" opt
do
case $opt in
(h) #help
echo "$0 [args]"
echo "$0 will setup chiton"
echo "args:"
echo " -h"
echo -e "\tThis help message"
echo " -H <host>"
echo -e "\tSet the Database Hostname"
echo " -d <database>"
echo -e "\tSet the database to use"
echo " -U <user>"
echo -e "\tSet the database administrator user (root, usually)"
echo " -P <pass>"
echo -e "\tSet the database administrator password"
echo " -p <pass>"
echo -e "\tSet the databse password for chiton"
echo " -y"
echo -e "\tSelect the default answer to everything"
echo " -v <dir>"
echo -e "\tSet the default location for saving videos"
echo " -q"
echo -e "\tQuiet Output"
echo " -k"
echo -e "\tSkip Database and user creation"
echo " -G"
echo -e "\tPruge Database (Delete all configs and recordings)"
exit 0
;;
(H)
MYSQL_HOST="$OPTARG"
;;
(d)
MYSQL_DB="$OPTARG"
;;
(U)
MYSQL_INSTALL_USER="$OPTARG"
;;
(P)
MYSQL_INSTALL_PW="$OPTARG"
;;
(u)
MYSQL_USER="$OPTARG"
;;
(p)
MYSQL_USER_PW="$OPTARG"
;;
(y)
SKIP_ALL="Y"
;;
(v)
VID_DIR="$OPTARG"
;;
(q)
QUIET_MODE="Y"
;;
(k)
SKIP_DB="Y"
;;
(G)
PURGE="Y"
;;
esac
done
function log_msg() {
if [ "$QUIET_MODE" = "N" ]; then
echo "$@"
fi
}
var=""
if [[ "$PURGE" = "Y" ]]; then
log_msg "================================================================================"
log_msg "= WARNING!!! ="
log_msg "= Purging will delete all recordings! ="
log_msg "================================================================================"
if [ "$SKIP_ALL" != "Y" ]; then
log_msg "Do you want to continue? [y/N]: "
read -p '> ' var
if [[ "$var" != "Y" ]] && [[ "$var" != "y" ]]; then
log_msg "You must enter 'Y' to continue"
exit 1
fi
fi
if ! [ -r "$CFGDIR/chiton.cfg" ]; then
log_msg "$CFGDIR/chiton.cfg not found, refusing to continue"
exit 1
fi
else
log_msg "This will guide you through setting up chiton"
log_msg
fi
log_msg "Database Host [$MYSQL_HOST]:"
if [ "$SKIP_ALL" != "Y" ]; then
read -p '> ' var
fi
if [[ -n $var ]]; then
MYSQL_HOST="$var"
fi
#pick our real hostname if not connecting to localhost
if [[ "$MYSQL_HOST" != "localhost" ]]; then
MYSQL_USER_HOST=$(hostname)
fi
log_msg "Database DB [$MYSQL_DB]:"
if [ "$SKIP_ALL" != "Y" ]; then
read -p '> ' var
fi
if [[ -n $var ]]; then
MYSQL_DB="$var"
fi
log_msg "Database Administrator User [$MYSQL_INSTALL_USER]:"
if [ "$SKIP_ALL" != "Y" ]; then
read -p '> ' var
fi
if [[ -n $var ]]; then
MYSQL_INSTALL_USER="$var"
fi
log_msg "Database Administrator Password []:"
if [ "$SKIP_ALL" != "Y" ]; then
read -s -p '> ' var
fi
if [[ -n $var ]]; then
MYSQL_INSTALL_PW="$var"
fi
if [[ -n $MYSQL_INSTALL_PW ]]; then
MYSQL_INSTALL_PW_CLI="-p${MYSQL_INSTALL_PW}"
else
MYSQL_INSTALL_PW_CLI=""
fi
log_msg "Database User [$MYSQL_USER]:"
if [ "$SKIP_ALL" != "Y" ]; then
read -p '> ' var
fi
if [[ -n $var ]]; then
MYSQL_USER="$var"
fi
log_msg "Database User's Password []:"
if [ "$SKIP_ALL" != "Y" ]; then
read -s -p '> ' var
fi
if [[ -n $var ]]; then
MYSQL_USER_PW="$var"
fi
log_msg "Database User's Host [$MYSQL_USER_HOST]:"
if [ "$SKIP_ALL" != "Y" ]; then
read -s -p '> ' var
fi
if [[ -n $var ]]; then
MYSQL_USER_HOST="$var"
fi
log_msg "Apache Configuration Directory [$APACHE_CFG_DIR]:"
if [ "$SKIP_ALL" != "Y" ]; then
read -p '> ' var
fi
if [[ -n $var ]]; then
APACHE_CFG_DIR="$var"
fi
if [[ "$PURGE" = "N" ]]; then
log_msg "URL path on webserver (with leading slash) [$WEBDIR]:"
if [ "$SKIP_ALL" != "Y" ]; then
read -p '> ' var
fi
if [[ -n $var ]]; then
WEBDIR="$var"
fi
fi
log_msg "Location for saving videos [$VID_DIR]:"
if [ "$SKIP_ALL" != "Y" ]; then
read -p '> ' var
fi
if [[ -n $var ]]; then
VID_DIR="$var"
fi
#escape all these variables
MYSQL_HOST_ESC=${MYSQL_HOST//\'/\\\'}
MYSQL_DB_ESC=${MYSQL_DB//\'/\\\'}
MYSQL_USER_ESC=${MYSQL_USER//\'/\\\'}
MYSQL_USER_PW_ESC=${MYSQL_USER_PW//\'/\\\'}
MYSQL_USER_HOST_ESC=${MYSQL_USER_HOST//\'/\\\'}
if [[ "$PURGE" = "Y" ]]; then
if [[ "SKIP_DB" = "Y" ]]; then
log_msg "Skipping DB & DB user delete"
else
log_msg "Should I Delete the Database and User? [y/n]: "
if [ "$SKIP_ALL" != "Y" ]; then
read -p '> ' var
fi
if [[ "$var" == "y" || "$var" == "" || "$SKIP_ALL" == "Y" ]]; then
mysql -u "$MYSQL_INSTALL_USER" -h "$MYSQL_HOST" "$MYSQL_INSTALL_PW_CLI" <<EOF
DROP DATABASE \`${MYSQL_DB_ESC}\`;
DROP USER '${MYSQL_USER_ESC}'@'${MYSQL_USER_HOST_ESC}' ;
FLUSH PRIVILEGES;
EOF
fi
fi
log_msg "Deleting all videos, this may take a while.."
rm -rf "$VID_DIR/"
log_msg "Deleting all configs"
rm -rf @localstatedir@/@PKGNAME@/cache/* || true
rm -rf @localstatedir@/@PKGNAME@/compile/* || true
rm -f $CFGDIR/chiton.cfg{,.orig} $CFGDIR/config.php{,.orig} || true
if [ "x@apacheappendconf@" != "zno" ]; then
#append an include line to @apacheappendconf@
if [ -f "@apacheappendconf@" ] && grep "apache-chiton.conf" "@apacheappendconf@" ; then
sed -i -e '/# Enable Chiton NVR/d' -e '/Include.*apache-chiton.conf/d' @apacheappendconf@
fi
fi
if [ "x@apachesymlinkdir@" != "xno" ]; then
if [ -d @apachesymlinkdir@ ] && [ ! -f @apachesymlinkdir@/apache-chiton.conf ] ; then
rm -f @apachesymlinkdir@/apache-chiton.conf || true
fi
fi
rm -f "$APACHE_CFG_DIR/apache-chiton.conf"
exit 0
fi
if [[ "$SKIP_DB" = "Y" ]]; then
log_msg "Skipping DB & DB User creation"
else
log_msg "Should I create the database? [y/n]: "
if [ "$SKIP_ALL" != "Y" ]; then
read -p '> ' var
fi
if [[ "$var" == "y" || "$var" == "" || "$SKIP_ALL" == "Y" ]]; then
mysql -u "$MYSQL_INSTALL_USER" -h "$MYSQL_HOST" "$MYSQL_INSTALL_PW_CLI" <<EOF
CREATE DATABASE IF NOT EXISTS \`${MYSQL_DB_ESC}\`;
EOF
fi
log_msg "Should I create the database user '$MYSQL_USER'@'${MYSQL_USER_HOST}' and grant all privs on $MYSQL_DB? [y/n]: "
if [ "$SKIP_ALL" != "Y" ]; then
read -p '> ' var
fi
if [[ "$var" == "y" || "$var" == "" || "$SKIP_ALL" == "Y" ]]; then
mysql -u "$MYSQL_INSTALL_USER" -h "$MYSQL_HOST" "$MYSQL_INSTALL_PW_CLI" <<EOF
CREATE USER IF NOT EXISTS '${MYSQL_USER_ESC}'@'${MYSQL_USER_HOST_ESC}' IDENTIFIED BY '${MYSQL_USER_PW_ESC}';
GRANT ALL PRIVILEGES ON \`${MYSQL_DB_ESC}\`.* TO '${MYSQL_USER_ESC}'@'${MYSQL_USER_HOST_ESC}';
FLUSH PRIVILEGES;
EOF
fi
fi
#create the user if they don't already exist
log_msg "Should I create the system user \`$BACKEND_USER\`? [Y/n]: "
if [ "$SKIP_ALL" != "Y" ]; then
read -p '> ' var
fi
if [[ "$var" == "y" || "$SKIP_ALL" == "Y" ]]; then
if getent passwd $BACKEND_USER > /dev/null ; then
log_msg "The user $BACKEND_USER already exists"
else
useradd $BACKEND_USER -d $STATE_DIR -r -U
fi
fi
log_msg "Should I add \`$APACHE_USER\` to the group $BACKEND_USER? [Y/n]: "
if [ "$SKIP_ALL" != "Y" ]; then
read -p '> ' var
fi
if [[ "$var" == "y" || "$SKIP_ALL" == "Y" ]]; then
if getent passwd $APACHE_USER > /dev/null ; then
usermod -a -G $BACKEND_USER $APACHE_USER
else
log_msg "The user $APACHE_USER does not exist"
fi
fi
log_msg "Creating the configs..."
#and for sed...
MYSQL_HOST_SED=${MYSQL_HOST_ESC//,/\\,}
MYSQL_DB_SED=${MYSQL_DB_ESC//,/\\,}
MYSQL_USER_SED=${MYSQL_USER_ESC//,/\\,}
MYSQL_USER_PW_SED=${MYSQL_USER_PW_ESC//,/\\,}
if [ -r $CFGDIR/chiton.cfg ]; then
mv $CFGDIR/chiton.cfg $CFGDIR/chiton.cfg.orig
fi
sed -e "s,[@]DB_HOST[@],'$MYSQL_HOST_SED',g" \
-e "s,[@]DB_USER[@],'$MYSQL_USER_SED',g" \
-e "s,[@]DB_PASS[@],'$MYSQL_USER_PW_SED',g" \
-e "s,[@]DB_DB[@],'$MYSQL_DB_SED',g" \
-e "s,[@]VID_DIR[@],'$VID_DIR',g" \
$CFGDIR/chiton.cfg.template > $CFGDIR/chiton.cfg
if [ -r $CFGDIR/config.php ]; then
mv $CFGDIR/config.php $CFGDIR/config.php.orig
fi
sed -e "s,[@]DB_HOST[@],'$MYSQL_HOST_SED',g" \
-e "s,[@]DB_USER[@],'$MYSQL_USER_SED',g" \
-e "s,[@]DB_PASS[@],'$MYSQL_USER_PW_SED',g" \
-e "s,[@]DB_DB[@],'$MYSQL_DB_SED',g" \
-e "s,[@]SMARTY_COMPILE_DIR[@],$STATE_DIR/compile,g" \
-e "s,[@]SMARTY_CACHE_DIR[@],$STATE_DIR/cache,g" \
-e "s,[@]CFG_FILE[@],'$CFGDIR/chiton.cfg',g" \
$CFGDIR/config.template.php > $CFGDIR/config.php
#secure our passwords
chown root:$BACKEND_USER $CFGDIR/config.php $CFGDIR/chiton.cfg
chmod 640 $CFGDIR/config.php $CFGDIR/chiton.cfg
#make sure all the directories we need also exist
mkdir -p $STATE_DIR/compile
mkdir -p $STATE_DIR/cache
mkdir -p $VID_DIR
chown $BACKEND_USER:$BACKEND_USER -R $STATE_DIR $VID_DIR
chmod 770 $STATE_DIR/compile $STATE_DIR/cache $VID_DIR
#and configure apache
if [ -r $APACHE_CFG_DIR/apache-chiton.conf ]; then
APACHE_NEW_CFG=$APACHE_CFG_DIR/apache-chiton.conf.new
else
APACHE_NEW_CFG=$APACHE_CFG_DIR/apache-chiton.conf
fi
sed -e "s,[@]root_path[@],\"$WEBDIR\",g" \
-e "s,[@]vid_path[@],\"$WEBDIR/vids\",g" \
-e "s,[@]webdir[@],\"@pkgdatadir@/web\",g" \
-e "s,[@]cfgdir[@],\"$CFGDIR\",g" \
-e "s,[@]viddir[@],\"$VID_DIR\",g" \
@pkgdatadir@/apache/apache-chiton.conf > $APACHE_NEW_CFG
if [ "x@apacheappendconf@" != "zno" ]; then
#append an include line to @apacheappendconf@
if [ -f "@apacheappendconf@" ] && ! grep "$APACHE_CFG_DIR/apache-chiton.conf" "@apacheappendconf@" ; then
echo '# Enable Chiton NVR' >> @apacheappendconf@
echo "Include $APACHE_CFG_DIR/apache-chiton.conf" >> @apacheappendconf@
fi
fi
if [ "x@apachesymlinkdir@" != "xno" ]; then
if [ -d @apachesymlinkdir@ ] && [ ! -f @apachesymlinkdir@/apache-chiton.conf ] ; then
ln -s $APACHE_CFG_DIR/apache-chiton.conf @apachesymlinkdir@
fi
fi
log_msg "$APACHE_CFG_DIR/apache-chiton.conf has been created, you may need to put:"
log_msg -e "\n\n\tInclude \"$APACHE_CFG_DIR/apache-chiton.conf\"\n\n"
log_msg "into your httpd.conf if your distro does not automatically include it"
log_msg "You may now start the backend and restart apache, the URL for your server is:"
log_msg "http://localhost$WEBDIR"