-
Notifications
You must be signed in to change notification settings - Fork 1
/
unifi-video
executable file
·367 lines (314 loc) · 9.01 KB
/
unifi-video
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
#!/bin/sh
# Ubiquiti unifi-video service script -*- shell-script -*-
# Copyright (c) 2013-2015 Ubiquiti Networks, Inc. http://www.ubnt.com
# vim: ft=sh
set -e
NAME=unifi-video
PKGUSER=unifi-video
BASEDIR="/usr/lib/${NAME}"
DATADIR="${BASEDIR}/data"
PIDFILE="/var/run/${NAME}/${NAME}.pid"
TMPFS_DIR="/var/cache/${NAME}"
MAINCLASS="com.ubnt.airvision.Main"
MAINJAR="${BASEDIR}/lib/airvision.jar"
# ENABLE_TMPFS=yes
ENABLE_TMPFS=no
TMPFS_SIZE=15%
UFV_VERBOSE=
UFV_DEBUG=
UFV_DAEMONIZE=true
NVR_APPLIANCE=false
# Default java heap to be 1408M if this is an NVR appliance
if grep "ubnt" /proc/version >/dev/null 2>&1; then
JVM_MAX_HEAP_SZ="1408M"
NVR_APPLIANCE=true
else
JVM_MAX_HEAP_SZ=`free -m | awk 'NR==2{printf "%dM\n", $2*0.26 }'`
fi
log_error() {
printf >2& "(unifi-video) ERROR: $@\n"
}
log_verbose() {
[ -z "${UFV_VERBOSE}" ] || printf "(unifi-video) $@\n"
}
log_debug() {
[ -z "${UFV_DEBUG}" ] || printf "(unifi-video) $@\n"
}
log() {
printf "(unifi-video) $@\n"
}
is_java7_compat() {
local J V
J=$1
V=$($J -version 2>&1 |awk -F\" '{print $2}')
case "$V" in
1.7*|1.8*) return 0
;;
esac
return 1
}
java_autodetect() {
JAVA=$(readlink -e $(which java))
JAVA_HOME=
for f in $1 ${JAVA} /usr/lib/jvm/*/bin/java; do
if is_java7_compat $f; then
JAVA_HOME=$(dirname $(dirname $f))
JAVA=$f
break
fi
done
}
update_limits() {
grep -q -F '* hard nofile 65534' /etc/security/limits.conf ||
sed -ie '/End of file/ i\* hard nofile 65534' /etc/security/limits.conf
grep -q -F '* soft nofile 65534' /etc/security/limits.conf ||
sed -ie '/End of file/ i\* soft nofile 65534' /etc/security/limits.conf
sed -i '/pam_limits.so/c\session required pam_limits.so' /etc/pam.d/su
}
prepare_tmpfs() {
local DIR SIZE PKGUSERID MNT_OPTIONS
DIR=$1
SIZE=$2
PKGUSERID=$(id -u ${PKGUSER})
MNT_OPTIONS="noatime,nodiratime,noexec,size=${SIZE},mode=0700"
[ -z "${PKGUSERID}" ] || MNT_OPTIONS="${MNT_OPTIONS},uid=${PKGUSERID}"
mkdir -p ${DIR} || true
chmod -R 0700 ${DIR}
if mountpoint -q ${DIR}; then
mount -o remount,${MNT_OPTIONS} ${DIR}
else
mount -t tmpfs -o ${MNT_OPTIONS} tmpfs ${DIR}
fi
}
require_root() {
[ -z "${EUID}" ] && EUID=$(id -u)
if [ "x${EUID}" != "x0" ]; then
log_error "This program requires administrative privileges."
exit 1
fi
}
pidfile_info() {
local pidfile pid
pidfile=$1
[ -r "${pidfile}" ] || return 4
read pid < "${pidfile}"
if $(kill -0 "${pid}" 2>/dev/null); then
printf "${pid}"
return 0
elif $(ps "${pid}" >/dev/null 2>&1); then
printf "${pid}"
return 0
else
return 1
fi
return 4
}
is_service_pid() {
local pid
pid=$1
if grep "${MAINCLASS}" /proc/${pid}/cmdline >/dev/null 2>&1; then
return 0
else
return 1
fi
}
is_service_running() {
local pidfile pid rc PIDOF pids
pidfile=$1
rc=0
pid=$(pidfile_info "${pidfile}") || rc=$?
if [ "$rc" = "0" ]; then
if is_service_pid "${pid}"; then
echo "${pid}"
return 0
else
rm -f "${pidfile}" || true
return 1
fi
else
# pidfile is either missing or invalid
rm -f "${pidfile}" || true
# try pidof if it exists
PIDOF=$(command -v pidof 2>/dev/null)
[ -z "${PIDOF}" ] && return 1
pids=$(${PIDOF} ${JSVC})
for pid in ${pids}; do
if is_service_pid "${pid}"; then
echo "${pid}"
return 0
fi
done
return 1
fi
}
usage() {
printf \
"unifi-video service utility, (c) 2013-2015 Ubiquiti Networks, Inc.\n\
Usage: $(basename $0) [options] <start|stop>\n\
\t-h,--help \tprint this help and quit\n\
\t-D,--nodetach\tdon't detach from parent process\n\
\t-v,--version \tprint version and quit\n\n\
The following environment variables can be used to tune service parameters:\n\
\tJAVA_HOME\tpreferred Java environment\n\
\tJVM_MX\tmaximum heap size for JVM (see java -Xmx option for valid values)\n\
\t
"
}
# rudimentary option parsing
ACTION=help
for arg in $@; do
case "${arg}" in
start|stop|status)
ACTION=${arg}
;;
-h|--help)
ACTION=help
;;
-g|--verbose)
UFV_VERBOSE=true
;;
-D|--nodetach)
UFV_DAEMONIZE=false
;;
--debug)
UFV_DEBUG=true
;;
-v|-V|--version)
ACTION=version
;;
esac
done
if [ "x${ACTION}" = "xhelp" ]; then
usage
exit 1
fi
[ -n "${JAVA_HOME}" ] && ENV_JAVA="${JAVA_HOME}"/bin/java
java_autodetect ${ENV_JAVA}
if [ -z "$JAVA_HOME" ]; then
log_error "no suitable Java 7 (or later) environment found!"
exit 1
fi
log_debug "Java Runtime: ${JAVA_HOME}"
JSVC=$(command -v jsvc)
if [ $? -ne 0 ]; then
log_error "jsvc is missing!"
exit 1
fi
log_debug "JSVC: ${JSVC}"
[ -z "${JVM_MX}" ] && JVM_MX=${JVM_MAX_HEAP_SZ}
[ -z "${JVM_JMXREMOTE_PORT}" ] && JVM_JMXREMOTE_PORT=7654
JSVC_EXTRA_OPTS=
[ -f /etc/default/${NAME} ] && . /etc/default/${NAME}
if [ -n "${AV_DATADIR}" ]; then
DATADIR="${AV_DATADIR}"
JSVC_EXTRA_OPTS="${JSVC_EXTRA_OPTS} -Dav.datadir=${AV_DATADIR}"
fi
if [ "x${ENABLE_TMPFS}" = "xyes" ]; then
JSVC_EXTRA_OPTS="${JSVC_EXTRA_OPTS} -Dav.tempdir=${TMPFS_DIR}"
fi
[ -e /dev/urandom ] && \
JVM_EXTRA_OPTS="-Djava.security.egd=file:/dev/./urandom ${JVM_EXTRA_OPTS}"
JVM_OPTS="${JVM_EXTRA_OPTS} \
-Xmx${JVM_MX} \
-Djava.library.path=${BASEDIR}/lib \
-Djava.awt.headless=true \
-Djavax.net.ssl.trustStore=${DATADIR}/ufv-truststore \
-Dfile.encoding=UTF-8"
# check whether jsvc requires -cwd option
if ${JSVC} -java-home ${JAVA_HOME} -cwd / -help >/dev/null 2>&1; then
JSVC_OPTS="${JSVC_OPTS} -cwd ${BASEDIR}"
fi
if [ -n "${UFV_DEBUG}" ]; then
JSVC_OPTS="${JSVC_OPTS} -debug"
JVM_OPTS="${JVM_OPTS} \
-Dcom.sun.management.jmxremote \
-Dcom.sun.management.jmxremote.ssl=false \
-Dcom.sun.management.jmxremote.authenticate=false \
-Dcom.sun.management.jmxremote.port=${JVM_JMXREMOTE_PORT}"
[ -z "${JVM_JMXREMOTE_HOST}" ] && \
JVM_JMXREMOTE_HOST=$(hostname -I | cut -d' ' -f1)
[ -z "${JVM_JMXREMOTE_HOST}" ] || \
JVM_OPTS="${JVM_OPTS} -Djava.rmi.server.hostname=${JVM_JMXREMOTE_HOST}"
fi
[ "x${UFV_DAEMONIZE}" != "xtrue" ] && JSVC_OPTS="${JSVC_OPTS} -nodetach"
JSVC_OPTS="${JSVC_OPTS} \
-user ${PKGUSER} \
-home ${JAVA_HOME} \
-cp /usr/share/java/commons-daemon.jar:${MAINJAR} \
-pidfile ${PIDFILE} \
-procname ${NAME} \
${JSVC_EXTRA_OPTS} \
${JVM_OPTS}"
log_debug "\nJVM options: ${JSVC_EXTRA_OPTS} ${JVM_OPTS}"
log_debug "\nJSVC options: ${JSVC_OPTS}"
waitfor_srv_mountpoint() {
if [ "x${NVR_APPLIANCE}" = "xtrue" ]; then
# wait 60s for the /srv partition to be mounted
# update system.properties if it is missing
# if not mounted, exit the script with an error
UFV_MOUNT_POINT="/srv"
TIMEOUT=600
log "waiting for $UFV_MOUNT_POINT to be mounted..."
while ! mountpoint -q $UFV_MOUNT_POINT; do
sleep 0.1
TIMEOUT=$(( $TIMEOUT - 1 ))
if [ $TIMEOUT -le 0 ]; then
log "ERROR: Failed to start unifi-video. $UFV_MOUNT_POINT is not mounted"
exit 10
fi
done
fi
log "checking for system.properties and truststore files..."
if [ ! -f "${DATADIR}/system.properties" ]; then
log "WARNING!!!! system.properties cannot be found..restoring from : ${BASEDIR}/etc/system.propetties"
cp -f "${BASEDIR}/etc/system.properties" "${DATADIR}/system.properties"
fi
[ ! -f "${DATADIR}/truststore" ] && rm -f "${DATADIR}/truststore"
[ -f "${DATADIR}/ufv-truststore" ] || cp -f "${BASEDIR}/etc/ufv-truststore" "${DATADIR}/ufv-truststore"
chown -h ${PKGUSER}:${PKGUSER} "${DATADIR}" "${DATADIR}/system.properties" "${DATADIR}/ufv-truststore" "/var/run/${NAME}"
}
case $ACTION in
start)
require_root
update_limits
if is_service_running "${PIDFILE}" >/dev/null; then
log_verbose "${NAME} is already running..."
else
[ -d /var/run/${NAME} ] || mkdir -p /var/run/${NAME}
[ "x${ENABLE_TMPFS}" = "xyes" ] && prepare_tmpfs ${TMPFS_DIR} ${TMPFS_SIZE}
[ -d "${BASEDIR}/work/Catalina" ] && rm -rf "${BASEDIR}/work/Catalina"
waitfor_srv_mountpoint
log_verbose "Starting ${NAME}..."
cd "${BASEDIR}" && exec ${JSVC} ${JSVC_OPTS} ${MAINCLASS} start
fi
;;
stop)
require_root
log_verbose "Backing up ${DATADIR}/system.properties in ${BASEDIR}/etc/system.properties"
cp -f "${DATADIR}/system.properties" "${BASEDIR}/etc/system.properties"
rc=0
pid=$(is_service_running ${PIDFILE}) || rc=$?
if [ "0" = "${rc}" ]; then
# jsvc won't even try to do anything if pidfile is missing..
[ -e "${PIDFILE}" ] || echo "${pid}" > ${PIDFILE}
log_verbose "Stopping ${NAME}..."
cd "${BASEDIR}" && exec ${JSVC} ${JSVC_OPTS} -stop ${MAINCLASS} stop
else
log_verbose "${NAME} is not running"
fi
;;
status)
log_verbose "Checking status of ${NAME}..."
rc=0
pid=$(is_service_running ${PIDFILE}) || rc=$?
if [ "0" = "${rc}" ]; then
log_verbose "${NAME} is running, PID: ${pid}"
else
log_verbose "${NAME} is NOT running"
fi
exit ${rc}
;;
version)
cd ${BASEDIR} && ${JAVA} -jar ${MAINJAR} --version
;;
esac