NextNVR/setup.sh

89 lines
2.4 KiB
Bash
Executable file

#!/bin/bash
# NextNVR v0.1.0 — Deployment Script
# Clones the repository and deploys to /opt/nextnvr
set -e
GIT_URL="https://git.lohmar.co.uk/cclohmar/NextNVR.git"
DEPLOY_DIR="/opt/nextnvr"
SERVICE_NAME="nextnvr"
GO_BIN="/home/master/.local/go/bin/go"
FFMPEG_BIN="ffmpeg"
echo "=== NextNVR Deployment ==="
echo "Target: ${DEPLOY_DIR}"
echo "Git: ${GIT_URL}"
# 1. Install system dependencies if missing.
if ! command -v ffmpeg &>/dev/null; then
echo "Installing ffmpeg..."
sudo apt-get update -qq
sudo apt-get install -y -qq ffmpeg
fi
# 2. Clone or pull repository.
if [ -d "${DEPLOY_DIR}" ]; then
echo "Repository exists, pulling latest..."
cd "${DEPLOY_DIR}"
git pull origin main
else
echo "Cloning repository..."
sudo mkdir -p "${DEPLOY_DIR}"
sudo chown master:master "${DEPLOY_DIR}"
git clone "${GIT_URL}" "${DEPLOY_DIR}"
cd "${DEPLOY_DIR}"
fi
# 3. Build the Go binary with embedded static files.
echo "Building NextNVR binary..."
export PATH=$HOME/.local/go/bin:$PATH
export GOPATH=$HOME/go
go mod tidy
CGO_ENABLED=0 go build -ldflags="-s -w -X main.Version=$(cat VERSION) -X main.BuildTime=$(date -u +%Y-%m-%dT%H:%M:%SZ)" -o nextnvr .
# 4. Copy config if not present.
if [ ! -f "${DEPLOY_DIR}/config.yaml" ]; then
cp config.yaml "${DEPLOY_DIR}/config.yaml"
echo "Default config.yaml created."
fi
# 5. Set permissions.
sudo chown -R master:master "${DEPLOY_DIR}"
chmod +x "${DEPLOY_DIR}/nextnvr"
# 6. Install systemd service.
SERVICE_FILE="/etc/systemd/system/${SERVICE_NAME}.service"
if [ ! -f "${SERVICE_FILE}" ]; then
echo "Creating systemd service..."
sudo tee "${SERVICE_FILE}" > /dev/null <<EOF
[Unit]
Description=NextNVR — IP Camera Recorder
After=network.target
[Service]
Type=simple
User=master
WorkingDirectory=${DEPLOY_DIR}
ExecStart=${DEPLOY_DIR}/nextnvr --config ${DEPLOY_DIR}/config.yaml
Restart=always
RestartSec=10
LimitNOFILE=65536
[Install]
WantedBy=multi-user.target
EOF
sudo systemctl daemon-reload
sudo systemctl enable "${SERVICE_NAME}"
fi
# 7. Restart service.
sudo systemctl restart "${SERVICE_NAME}"
sleep 2
sudo systemctl status "${SERVICE_NAME}" --no-pager
echo ""
echo "=== Deployment Complete ==="
echo "NextNVR running on http://127.0.0.1:8080"
echo "Service: sudo systemctl [start|stop|restart|status] ${SERVICE_NAME}"
echo "Logs: sudo journalctl -u ${SERVICE_NAME} -f"
echo "Config: ${DEPLOY_DIR}/config.yaml"