2016-12-01 04:33:49 -05:00
|
|
|
#!/bin/bash -x
|
2016-02-08 01:59:29 -05:00
|
|
|
#
|
|
|
|
# This file is part of Moonfire NVR, a security camera network video recorder.
|
|
|
|
# Copyright (C) 2016 Scott Lamb <slamb@slamb.org>
|
|
|
|
#
|
|
|
|
# This program 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.
|
|
|
|
#
|
|
|
|
# In addition, as a special exception, the copyright holders give
|
|
|
|
# permission to link the code of portions of this program with the
|
|
|
|
# OpenSSL library under certain conditions as described in each
|
|
|
|
# individual source file, and distribute linked combinations including
|
|
|
|
# the two.
|
|
|
|
#
|
|
|
|
# You must obey the GNU General Public License in all respects for all
|
|
|
|
# of the code used other than OpenSSL. If you modify file(s) with this
|
|
|
|
# exception, you may extend this exception to your version of the
|
|
|
|
# file(s), but you are not obligated to do so. If you do not wish to do
|
|
|
|
# so, delete this exception statement from your version. If you delete
|
|
|
|
# this exception statement from all source files in the program, then
|
|
|
|
# also delete it here.
|
|
|
|
#
|
|
|
|
# This program 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 this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
#
|
|
|
|
#
|
|
|
|
# Script to prepare for moonfire-nvr operations
|
|
|
|
#
|
|
|
|
# Command line options:
|
2016-11-25 17:34:00 -05:00
|
|
|
# -D: Skip database initialization
|
2016-02-08 01:59:29 -05:00
|
|
|
# -S: Skip apt-get update and install
|
|
|
|
#
|
|
|
|
|
|
|
|
# Configuration variables. Should only need minimal, or zero, changes.
|
2016-12-01 01:05:52 -05:00
|
|
|
# Empty values will use defaults. For convenience, we attempt to read
|
|
|
|
# customizations from prep.config first
|
2016-02-08 01:59:29 -05:00
|
|
|
#
|
2016-12-01 01:05:52 -05:00
|
|
|
if [ -r prep.config ]; then
|
|
|
|
. prep.config
|
|
|
|
fi
|
2016-02-08 01:59:29 -05:00
|
|
|
|
|
|
|
# User and group
|
|
|
|
# Default: or whatever is in $NVR_USER (default "moonfire-nvr")
|
|
|
|
#
|
2016-04-29 19:20:42 -04:00
|
|
|
#NVR_USER=
|
|
|
|
#NVR_GROUP=
|
2016-02-08 01:59:29 -05:00
|
|
|
|
|
|
|
# Port for web server
|
|
|
|
# Default: 8080
|
|
|
|
#
|
2016-04-29 19:20:42 -04:00
|
|
|
#NVR_PORT=
|
2016-02-08 01:59:29 -05:00
|
|
|
|
|
|
|
# This should, ideally, be a location on flash storage under which the
|
|
|
|
# moonfire user's home directory will be created.
|
|
|
|
# Default: "/var/lib"
|
|
|
|
#
|
2016-04-29 19:20:42 -04:00
|
|
|
#NVR_HOME_BASE=
|
2016-02-08 01:59:29 -05:00
|
|
|
|
|
|
|
# Set to mountpoint of media directory, empty to stay in home directory
|
|
|
|
# Default: empty
|
2016-12-01 01:05:52 -05:00
|
|
|
#SAMPLES_DIR=/media/nvr/samples
|
2016-02-08 01:59:29 -05:00
|
|
|
|
|
|
|
# Set to path for media directory relative to mountpoint
|
|
|
|
# Default: "samples"
|
|
|
|
#
|
2016-04-29 19:20:42 -04:00
|
|
|
#SAMPLES_DIR_NAME=
|
2016-02-08 01:59:29 -05:00
|
|
|
|
|
|
|
# Binary location
|
|
|
|
# Default: "/usr/local/bin/moonfire-nvr"
|
|
|
|
#
|
2016-12-01 01:10:34 -05:00
|
|
|
#SERVICE_BIN=
|
2016-02-08 01:59:29 -05:00
|
|
|
|
|
|
|
# Service name
|
|
|
|
# Default: "moonfire-nvr"
|
|
|
|
#
|
2016-04-29 19:20:42 -04:00
|
|
|
#SERVICE_NAME=
|
2016-02-08 01:59:29 -05:00
|
|
|
|
|
|
|
# Service Description
|
|
|
|
# Default: "Moonfire NVR"
|
|
|
|
#
|
2016-04-29 19:20:42 -04:00
|
|
|
#SERVICE_DESC=
|
2016-02-08 01:59:29 -05:00
|
|
|
|
|
|
|
# --------------------------------------------------------------------
|
|
|
|
# Derived variables: Do not modify!
|
|
|
|
#
|
|
|
|
# Determine directory path of this script
|
|
|
|
#
|
|
|
|
SOURCE="${BASH_SOURCE[0]}"
|
|
|
|
while [ -h "$SOURCE" ]; do # resolve $SOURCE until file no longer a symlink
|
|
|
|
DIR="$( cd -P "$( dirname "$SOURCE" )" && pwd )"
|
|
|
|
SOURCE="$(readlink "$SOURCE")"
|
|
|
|
# if $SOURCE was relative symlink, resolve relative to path of symlink
|
|
|
|
[[ "$SOURCE" != /* ]] && SOURCE="$DIR/$SOURCE"
|
|
|
|
done
|
|
|
|
|
|
|
|
SRC_DIR="$( cd -P "$( dirname "$SOURCE" )" && pwd )"/src
|
|
|
|
NVR_USER="${NVR_USER:-moonfire-nvr}"
|
|
|
|
NVR_GROUP="${NVR_GROUP:-$NVR_USER}"
|
|
|
|
NVR_PORT="${NVR_PORT:-8080}"
|
|
|
|
NVR_HOME_BASE="${NVR_HOME_BASE:-/var/lib}"
|
|
|
|
NVR_HOME="${NVR_HOME_BASE}/${NVR_USER}"
|
|
|
|
DB_NAME="${DB_NAME:-db}"
|
|
|
|
DB_DIR="${DB_DIR:-$NVR_HOME/db}"
|
|
|
|
SAMPLES_DIR_NAME="${SAMPLES_DIR_NAME:-samples}"
|
|
|
|
SAMPLES_DIR="${SAMPLES_DIR:-$NVR_HOME/$SAMPLES_DIR_NAME}"
|
|
|
|
SERVICE_NAME="${SERVICE_NAME:-moonfire-nvr}"
|
|
|
|
SERVICE_DESC="${SERVICE_DESC:-Moonfire NVR}"
|
|
|
|
SERVICE_BIN="${SERVICE_BIN:-/usr/local/bin/moonfire-nvr}"
|
|
|
|
|
|
|
|
# Process command line options
|
|
|
|
#
|
2016-11-25 17:34:00 -05:00
|
|
|
while getopts ":DS" opt; do
|
2016-02-08 01:59:29 -05:00
|
|
|
case $opt in
|
|
|
|
D) SKIP_DB=1
|
|
|
|
;;
|
|
|
|
S) SKIP_APT=1
|
|
|
|
;;
|
|
|
|
:)
|
|
|
|
echo "Option -$OPTARG requires an argument." >&2
|
|
|
|
exit 1
|
|
|
|
;;
|
|
|
|
\?)
|
|
|
|
echo "Invalid option: -$OPTARG" >&2
|
|
|
|
exit 1
|
|
|
|
;;
|
|
|
|
esac
|
|
|
|
done
|
|
|
|
|
2016-11-25 17:34:00 -05:00
|
|
|
# Setup all apt packages we need
|
2016-02-08 01:59:29 -05:00
|
|
|
#
|
|
|
|
echo 'Preparing and downloading packages we need...'; echo
|
|
|
|
if [ "${SKIP_APT:-0}" != 1 ]; then
|
|
|
|
sudo apt-get update
|
|
|
|
sudo apt-get install \
|
|
|
|
build-essential \
|
|
|
|
libavcodec-dev \
|
|
|
|
libavformat-dev \
|
|
|
|
libavutil-dev \
|
|
|
|
sqlite3 \
|
|
|
|
libsqlite3-dev \
|
2016-11-25 17:34:00 -05:00
|
|
|
uuid-runtime
|
2016-02-08 01:59:29 -05:00
|
|
|
fi
|
|
|
|
|
|
|
|
# Check if binary is installed. Setup for build if it is not
|
|
|
|
#
|
|
|
|
if [ ! -x "${SERVICE_BIN}" ]; then
|
|
|
|
echo "Binary not installed, building..."; echo
|
2016-11-25 17:34:00 -05:00
|
|
|
if ! cargo --version; then
|
|
|
|
echo "cargo not installed/working."
|
|
|
|
echo "Install a nightly Rust (see http://rustup.us) first."
|
|
|
|
echo
|
|
|
|
exit 1
|
|
|
|
fi
|
2016-12-20 21:29:45 -05:00
|
|
|
if ! cargo test; then
|
2016-12-01 01:05:52 -05:00
|
|
|
echo "test failed. Try to run the following manually for more info"
|
|
|
|
echo "RUST_TEST_THREADS=1 cargo test --verbose"
|
|
|
|
echo
|
|
|
|
exit 1
|
|
|
|
fi
|
2016-12-01 04:33:49 -05:00
|
|
|
if ! cargo build --release; then
|
|
|
|
echo "Build failed."
|
|
|
|
echo "RUST_TEST_THREADS=1 cargo build --release --verbose"
|
|
|
|
echo
|
|
|
|
exit 1
|
|
|
|
fi
|
2016-11-25 17:34:00 -05:00
|
|
|
sudo install -m 755 target/release/moonfire-nvr ${SERVICE_BIN}
|
2016-02-08 01:59:29 -05:00
|
|
|
if [ -x "${SERVICE_BIN}" ]; then
|
|
|
|
echo "Binary installed..."; echo
|
|
|
|
else
|
|
|
|
echo "Build failed to install..."; echo
|
|
|
|
exit 1
|
|
|
|
fi
|
|
|
|
fi
|
|
|
|
|
|
|
|
# Create user and groups
|
|
|
|
#
|
|
|
|
echo 'Create user/group and directories we need...'; echo
|
|
|
|
sudo addgroup --quiet --system ${NVR_GROUP}
|
2016-11-25 06:58:50 -05:00
|
|
|
sudo adduser --quiet --system ${NVR_USER} --ingroup "${NVR_GROUP}" --home "${NVR_HOME}"
|
2016-02-08 01:59:29 -05:00
|
|
|
if [ ! -d "${NVR_HOME}" ]; then
|
|
|
|
sudo mkdir "${NVR_HOME}"
|
|
|
|
fi
|
|
|
|
sudo chown ${NVR_USER}:${NVR_GROUP} "${NVR_HOME}"
|
|
|
|
|
|
|
|
# Prepare samples directory
|
|
|
|
#
|
|
|
|
if [ -z "${SAMPLES_DIR}" ]; then
|
|
|
|
SAMPLES_PATH="${NVR_HOME}/${SAMPLES_DIR_NAME}"
|
|
|
|
if [ ! -d "${SAMPLES_PATH}" ]; then
|
|
|
|
sudo -u ${NVR_USER} -H mkdir "${SAMPLES_PATH}"
|
2016-04-29 19:20:42 -04:00
|
|
|
else
|
|
|
|
chown -R ${NVR_USER}.${NVR_USER} "${SAMPLES_PATH}"
|
2016-02-08 01:59:29 -05:00
|
|
|
fi
|
|
|
|
else
|
|
|
|
SAMPLES_PATH="${SAMPLES_DIR}"
|
|
|
|
if [ ! -d "${SAMPLES_PATH}" ]; then
|
|
|
|
sudo mkdir "${SAMPLES_PATH}"
|
|
|
|
echo ">>> Do not forget to edit /etc/fstab to mount samples media"; echo
|
|
|
|
fi
|
2016-04-29 19:20:42 -04:00
|
|
|
chown -R ${NVR_USER}.${NVR_USER} "${SAMPLES_PATH}"
|
2016-02-08 01:59:29 -05:00
|
|
|
fi
|
|
|
|
|
|
|
|
|
|
|
|
# Prepare for sqlite directory and set schema into db
|
|
|
|
#
|
|
|
|
echo 'Create database...'; echo
|
|
|
|
if [ ! -d "${DB_DIR}" ]; then
|
|
|
|
sudo -u ${NVR_USER} -H mkdir "${DB_DIR}"
|
|
|
|
fi
|
|
|
|
DB_PATH="${DB_DIR}/${DB_NAME}"
|
|
|
|
CAMERAS_PATH="${SRC_DIR}/../cameras.sql"
|
|
|
|
[ "${SKIP_DB:-0}" == 0 ] && sudo -u ${NVR_USER} -H sqlite3 "${DB_PATH}" < "${SRC_DIR}/schema.sql"
|
|
|
|
if [ -r "${CAMERAS_PATH}" ]; then
|
|
|
|
echo 'Add cameras...'; echo
|
|
|
|
sudo -u ${NVR_USER} -H sqlite3 "${DB_PATH}" < "${CAMERAS_PATH}"
|
|
|
|
fi
|
|
|
|
|
|
|
|
# Prepare service files
|
|
|
|
#
|
|
|
|
SERVICE_PATH="/etc/systemd/system/${SERVICE_NAME}.service"
|
|
|
|
sudo sh -c "cat > ${SERVICE_PATH}" <<NVR_EOF
|
|
|
|
[Unit]
|
|
|
|
Description=${SERVICE_DESC}
|
|
|
|
After=network-online.target
|
|
|
|
|
|
|
|
[Service]
|
|
|
|
ExecStart=${SERVICE_BIN} \\
|
2016-11-25 17:34:00 -05:00
|
|
|
--sample-file-dir=${SAMPLES_PATH} \\
|
|
|
|
--db-dir=${DB_DIR} \\
|
|
|
|
--http-addr=0.0.0.0:${NVR_PORT}
|
|
|
|
Environment=RUST_LOG=info
|
2016-02-08 01:59:29 -05:00
|
|
|
Type=simple
|
|
|
|
User=${NVR_USER}
|
|
|
|
Nice=-20
|
|
|
|
Restart=on-abnormal
|
|
|
|
CPUAccounting=true
|
|
|
|
MemoryAccounting=true
|
|
|
|
BlockIOAccounting=true
|
|
|
|
|
|
|
|
[Install]
|
|
|
|
WantedBy=multi-user.target
|
|
|
|
NVR_EOF
|
|
|
|
|
|
|
|
# Configure and start service
|
|
|
|
#
|
|
|
|
if [ -f "${SERVICE_PATH}" ]; then
|
|
|
|
echo 'Configuring system daemon...'; echo
|
|
|
|
sudo systemctl daemon-reload
|
2016-04-29 19:20:42 -04:00
|
|
|
sudo systemctl enable ${SERVICE_NAME}
|
2016-02-08 01:59:29 -05:00
|
|
|
sudo systemctl restart ${SERVICE_NAME}
|
|
|
|
echo 'Getting system daemon status...'; echo
|
|
|
|
sudo systemctl status ${SERVICE_NAME}
|
|
|
|
fi
|