forked from ZoneMinder/zmeventnotification
-
Notifications
You must be signed in to change notification settings - Fork 0
/
install.sh
executable file
·407 lines (325 loc) · 13.3 KB
/
install.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
#!/bin/bash
#-----------------------------------------------------
# Install script for the EventServer and the
# machine learning hooks
#
# /install.sh --help
#
# Note that this doesn't install all the event server
# dependencies. You still need to follow the README
#
# It does however try to install all the hook dependencies
#
#-----------------------------------------------------
# --- Change these if you want --
PYTHON=python3
PIP=pip3
TARGET_CONFIG='/etc/zm'
TARGET_DATA='/var/lib/zmeventnotification'
TARGET_BIN_ES='/usr/bin'
TARGET_BIN_HOOK='/var/lib/zmeventnotification/bin'
WGET=$(which wget)
WEB_OWNER_FROM_PS=$(ps xao user,group,comm | grep -E '(httpd|hiawatha|apache|apache2|nginx)' | grep -v whoami | grep -v root | head -n1 | awk '{print $1}')
#WEB_OWNER='www-data' # uncomment this if the above mechanism fails
WEB_GROUP_FROM_PS=$(ps xao user,group,comm | grep -E '(httpd|hiawatha|apache|apache2|nginx)' | grep -v whoami | grep -v root | head -n1 | awk '{print $2}')
#WEB_GROUP='www-data' # uncomment if above line fails
# make this empty if you don't want backups
MAKE_CONFIG_BACKUP='--backup=numbered'
# --- end of change these ---
# set default values
# if we have a value from ps use it, otherwise look in env
WEB_OWNER=${WEB_OWNER_FROM_PS:-$WEB_OWNER}
WEB_GROUP=${WEB_GROUP_FROM_PS:-$WEB_GROUP}
# if we don't have a value from ps or env, use default
WEB_OWNER=${WEB_OWNER:-www-data}
WEB_GROUP=${WEB_GROUP:-www-data}
WGET=${WGET:-/usr/bin/wget}
# utility functions for color coded pretty printing
print_error() {
COLOR="\033[1;31m"
NOCOLOR="\033[0m"
echo -e "${COLOR}ERROR:${NOCOLOR}$1"
}
print_warning() {
COLOR="\033[0;33m"
NOCOLOR="\033[0m"
echo -e "${COLOR}WARNING:${NOCOLOR}$1"
}
print_success() {
COLOR="\033[1;32m"
NOCOLOR="\033[0m"
echo -e "${COLOR}Success:${NOCOLOR}$1"
}
# generic confirm function that returns 0 for yes and 1 for no
confirm() {
display_str=$1
default_ans=$2
if [[ $default_ans == 'y/N' ]]
then
must_match='[yY]'
else
must_match='[nN]'
fi
read -p "${display_str} [${default_ans}]:" ans
[[ $ans == $must_match ]]
}
# Are we running as root? If not, install may fail
check_root() {
if [[ $EUID -ne 0 ]]
then
echo
echo "********************************************************************************"
print_warning "Unless you have changed paths, this script requires to be run as sudo"
echo "********************************************************************************"
echo
[[ ${INTERACTIVE} == 'yes' ]] && read -p "Press any key to continue or Ctrl+C to quit and run again with sudo..."
fi
}
# Some of these may be default values, so give user a change to change
verify_config() {
if [[ ${INTERACTIVE} == 'no' &&
( ${INSTALL_ES} == 'prompt' || ${INSTALL_HOOK} == 'prompt' ||
${INSTALL_HOOK_CONFIG} == 'prompt' || ${INSTALL_ES_CONFIG} == 'prompt' )
]]
then
print_error 'In non-interactive mode, you need to specify flags for all components'
echo
exit
fi
echo
echo ----------- Configured Values ----------------------------
echo "Your webserver user seems to be ${WEB_OWNER}"
echo "Your webserver group seems to be ${WEB_GROUP}"
echo "wget is at ${WGET}"
echo
echo "Install Event Server: ${INSTALL_ES}"
echo "Install Event Server config: ${INSTALL_ES_CONFIG}"
echo "Install Hooks: ${INSTALL_HOOK}"
echo "Install Hooks config: ${INSTALL_HOOK_CONFIG}"
echo
[[ ${INSTALL_ES} != 'no' ]] && echo "The Event Server will be installed to ${TARGET_BIN_ES}"
[[ ${INSTALL_ES_CONFIG} != 'no' ]] && echo "The Event Server config will be installed to ${TARGET_CONFIG}"
[[ ${INSTALL_HOOK} != 'no' ]] && echo "The hook files will be installed to ${TARGET_DATA} sub-folders"
[[ ${INSTALL_HOOK_CONFIG} != 'no' ]] && echo "The hook config files will be installed to ${TARGET_CONFIG}"
echo
[[ ${INTERACTIVE} == 'yes' ]] && read -p "If any of this looks wrong, please hit Ctrl+C and edit the variables in this script..."
}
# move proc for zmeventnotification.pl
install_es() {
echo '***** Installing ES **********'
mkdir -p "${TARGET_DATA}/push" 2>/dev/null
install -m 755 -o "${WEB_OWNER}" -g "${WEB_GROUP}" zmeventnotification.pl "${TARGET_BIN_ES}" &&
print_success "Completed, but you will still have to install ES dependencies as per https://github.com/pliablepixels/zmeventnotification/blob/master/README.md#install-dependencies" || print_error "failed"
#echo "Done, but you will still have to manually install all ES dependencies as per https://github.com/pliablepixels/zmeventnotification#how-do-i-install-it"
}
# install proc for ML hooks
install_hook() {
echo '*** Installing Hooks ***'
mkdir -p "${TARGET_DATA}/bin" 2>/dev/null
rm -fr "${TARGET_DATA}/bin/*" 2>/dev/null
mkdir -p "${TARGET_DATA}/images" 2>/dev/null
mkdir -p "${TARGET_DATA}/mlapi" 2>/dev/null
mkdir -p "${TARGET_DATA}/known_faces" 2>/dev/null
mkdir -p "${TARGET_DATA}/unknown_faces" 2>/dev/null
mkdir -p "${TARGET_DATA}/models/yolov3" 2>/dev/null
mkdir -p "${TARGET_DATA}/models/tinyyolo" 2>/dev/null
mkdir -p "${TARGET_DATA}/misc" 2>/dev/null
echo "everything that does not fit anywhere else :-)" > "${TARGET_DATA}/misc/README.txt" 2>/dev/null
# If you don't already have data files, get them
# First YOLOV3
echo 'Checking for YoloV3 data files....'
targets=('yolov3.cfg' 'yolov3_classes.txt' 'yolov3.weights')
sources=('https://raw.githubusercontent.com/pjreddie/darknet/master/cfg/yolov3.cfg'
'https://raw.githubusercontent.com/pjreddie/darknet/master/data/coco.names'
'https://pjreddie.com/media/files/yolov3.weights')
for ((i=0;i<${#targets[@]};++i))
do
if [ ! -f "${TARGET_DATA}/models/yolov3/${targets[i]}" ]
then
${WGET} "${sources[i]}" -O"${TARGET_DATA}/models/yolov3/${targets[i]}"
else
echo "${targets[i]} exists, no need to download"
fi
done
# Next up, TinyYOLO
echo
echo 'Checking for TinyYOLO data files...'
targets=('yolov3-tiny.cfg' 'yolov3-tiny.txt' 'yolov3-tiny.weights')
sources=('https://raw.githubusercontent.com/pjreddie/darknet/master/cfg/yolov3-tiny.cfg'
'https://raw.githubusercontent.com/pjreddie/darknet/master/data/coco.names'
'https://pjreddie.com/media/files/yolov3-tiny.weights')
for ((i=0;i<${#targets[@]};++i))
do
if [ ! -f "${TARGET_DATA}/models/tinyyolo/${targets[i]}" ]
then
${WGET} "${sources[i]}" -O"${TARGET_DATA}/models/tinyyolo/${targets[i]}"
else
echo "${targets[i]} exists, no need to download"
fi
done
# Now install the ML hooks
echo "*** Installing push api plugins ***"
install -m 755 -o "${WEB_OWNER}" pushapi_plugins/pushapi_pushover.py "${TARGET_BIN_HOOK}"
echo "*** Installing detection scripts ***"
install -m 755 -o "${WEB_OWNER}" hook/zm_event_start.sh "${TARGET_BIN_HOOK}"
install -m 755 -o "${WEB_OWNER}" hook/zm_event_end.sh "${TARGET_BIN_HOOK}"
install -m 755 -o "${WEB_OWNER}" hook/zm_detect.py "${TARGET_BIN_HOOK}"
install -m 755 -o "${WEB_OWNER}" hook/zm_train_faces.py "${TARGET_BIN_HOOK}"
#python setup.py install && print_success "Done" || print_error "python setup failed"
echo "Removing old version of zmes-hooks, if any"
${PY_SUDO} ${PIP} uninstall -y zmes-hooks >/dev/null 2>&1
${PY_SUDO} ${PIP} install hook/ && print_opencv_message || print_error "python hooks setup failed"
}
# move ES config files
install_es_config() {
echo 'Replacing ES config file'
install ${MAKE_CONFIG_BACKUP} -o "${WEB_OWNER}" -g "${WEB_GROUP}" -m 644 zmeventnotification.ini "${TARGET_CONFIG}" &&
print_success "config copied" || print_error "could not copy config"
if [ ! -f "${TARGET_CONFIG}/secrets.ini" ]; then
install ${MAKE_CONFIG_BACKUP} -o "${WEB_OWNER}" -g "${WEB_GROUP}" -m 644 secrets.ini "${TARGET_CONFIG}" &&
print_success "secrets copied" || print_error "could not copy secrets"
fi
echo "====> Remember to fill in the right values in the config files, or your system won't work! <============="
echo
}
# move Hook config files
install_hook_config() {
echo 'Replacing Hook config file'
install ${MAKE_CONFIG_BACKUP} -o "${WEB_OWNER}" -g "${WEB_GROUP}" -m 644 hook/objectconfig.ini "${TARGET_CONFIG}" &&
print_success "config copied" || print_error "could not copy config"
echo "====> Remember to fill in the right values in the config files, or your system won't work! <============="
echo "====> If you changed $TARGET_CONFIG remember to fix ${TARGET_BIN_HOOK}/zm_event_start.sh! <========"
echo
}
print_opencv_message() {
print_success "Done"
cat << EOF
|-------------------------- NOTE -------------------------------------|
Hooks are installed, but please make sure you have the right version
of OpenCV installed. I recommend removing any pip packages you may
have installed of opencv* and compiling OpenCV 4.2.x from source.
See https://zmeventnotification.readthedocs.io/en/latest/guides/hooks.html#opencv-install
|----------------------------------------------------------------------|
EOF
}
# wuh
display_help() {
cat << EOF
$0 [-h|--help] [--install_es|--no_install_es] [--install_hook|--no_install_hook] [--install_config|--no_install_config] [--nosudo]
When used without any parameters executes in interactive mode
-h: This help
--install-es: installs Event Server without prompting
--no-install-es: skips Event Server install without prompting
--install-hook: installs hooks without prompting
--no-install-hook: skips hooks install without prompting
--install-config: installs/overwrites config files without prompting
--no-install-config: skips config install without prompting
--no-interactive: run automatically, but you need to specify flags for all components
--no-pysudo: If specified will install python packages
without sudo (some users don't install packages globally)
EOF
}
# parses arguments and does a bit of conflict sanitization
check_args() {
# credit: https://stackoverflow.com/a/14203146/1361529
INSTALL_ES='prompt'
INSTALL_HOOK='prompt'
INSTALL_ES_CONFIG='prompt'
INSTALL_HOOK_CONFIG='prompt'
INTERACTIVE='yes'
PY_SUDO='sudo -H'
for key in "${cmd_args[@]}"
do
case $key in
-h|--help)
display_help && exit
shift
;;
--no-pysudo)
PY_SUDO=''
shift
;;
--no-interactive)
INTERACTIVE='no'
shift
;;
--install-es)
INSTALL_ES='yes'
shift
;;
--no-install-es)
INSTALL_ES='no'
shift
;;
--install-hook)
INSTALL_HOOK='yes'
shift
;;
--no-install-hook)
INSTALL_HOOK='no'
shift
;;
--install-config)
INSTALL_HOOK_CONFIG='yes'
INSTALL_ES_CONFIG='yes'
shift
;;
--no-install-config)
INSTALL_ES_CONFIG='no'
INSTALL_HOOK_CONFIG='no'
shift
;;
*) # unknown option
shift
;;
esac
done
# if ES won't be installed, doesn't make sense to copy ES config. Umm actually...
[[ ${INSTALL_ES} == 'no' ]] && INSTALL_ES_CONFIG='no'
# If we are prompting for ES, lets also prompt for config and not auto
[[ ${INSTALL_ES} == 'prompt' && ${INSTALL_ES_CONFIG} == 'yes' ]] && INSTALL_ES_CONFIG='prompt'
# same logic as above
[[ ${INSTALL_HOOK} == 'no' ]] && INSTALL_HOOK_CONFIG='no'
[[ ${INSTALL_HOOK} == 'prompt' && ${INSTALL_HOOK_CONFIG} == 'yes' ]] && INSTALL_HOOK_CONFIG='prompt'
}
###################################################
# script main
###################################################
cmd_args=("$@") # because we need a function to access them
check_args
check_root
verify_config
echo
echo
[[ ${INSTALL_ES} == 'yes' ]] && install_es
[[ ${INSTALL_ES} == 'no' ]] && echo 'Skipping Event Server install'
if [[ ${INSTALL_ES} == 'prompt' ]]
then
confirm 'Install Event Server' 'y/N' && install_es || echo 'Skipping Event Server install'
fi
echo
echo
[[ ${INSTALL_ES_CONFIG} == 'yes' ]] && install_es_config
[[ ${INSTALL_ES_CONFIG} == 'no' ]] && echo 'Skipping Event Server config install'
if [[ ${INSTALL_ES_CONFIG} == 'prompt' ]]
then
confirm 'Install Event Server Config' 'y/N' && install_es_config || echo 'Skipping Event Server config install'
fi
echo
echo
[[ ${INSTALL_HOOK} == 'yes' ]] && install_hook
[[ ${INSTALL_HOOK} == 'no' ]] && echo 'Skipping Hook'
if [[ ${INSTALL_HOOK} == 'prompt' ]]
then
confirm 'Install Hook' 'y/N' && install_hook || echo 'Skipping Hook install'
fi
echo
echo
[[ ${INSTALL_HOOK_CONFIG} == 'yes' ]] && install_hook_config
[[ ${INSTALL_HOOK_CONFIG} == 'no' ]] && echo 'Skipping Hook config install'
if [[ ${INSTALL_HOOK_CONFIG} == 'prompt' ]]
then
confirm 'Install Hook Config' 'y/N' && install_hook_config || echo 'Skipping Hook config install'
fi
# Make sure webserver can access them
chown -R ${WEB_OWNER}:${WEB_GROUP} "${TARGET_DATA}"