You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

202 lines
8.2 KiB
Bash

#!/bin/sh
APP_NAME="frigate"
ACTION="${1}"
shift 1
get_image() {
do_install_detail
}
do_install_detail() {
local config port IMAGE_NAME LAN_IP
local usbcoral hwaccel storage mqtt host coral type device
echo "Fetching port from configuration..."
port=$(uci get frigate.docker.port 2>/dev/null)
[ -z "$port" ] && { echo "Error: Port not found in configuration."; exit 1; }
echo "Fetching image name from configuration..."
IMAGE_NAME=$(uci get frigate.docker.image 2>/dev/null)
[ -z "$IMAGE_NAME" ] && { echo "Error: Image name not found in configuration."; exit 1; }
echo "Fetching USB Coral path from configuration..."
usbcoral=$(uci get frigate.docker.usbcoral 2>/dev/null)
[ -z "$usbcoral" ] && { echo "Error: USB Coral path not found in configuration."; exit 1; }
echo "Fetching hardware acceleration path from configuration..."
hwaccel=$(uci get frigate.docker.hwaccel 2>/dev/null)
[ -z "$hwaccel" ] && { echo "Error: Hardware acceleration path not found in configuration."; exit 1; }
echo "Fetching storage path from configuration..."
storage=$(uci get frigate.docker.storage 2>/dev/null)
[ -z "$storage" ] && { echo "Error: Storage path not found in configuration."; exit 1; }
echo "Fetching MQTT status from configuration..."
mqtt=$(uci get frigate.mqtt.enabled 2>/dev/null)
[ -z "$mqtt" ] && { echo "Error: MQTT status not found in configuration."; exit 1; }
echo "Fetching MQTT host from configuration..."
host=$(uci get frigate.mqtt.host 2>/dev/null)
[ -z "$host" ] && { echo "Error: MQTT host not found in configuration."; exit 1; }
echo "Fetching Coral status from configuration..."
coral=$(uci get frigate.tpu.enabled 2>/dev/null)
[ -z "$coral" ] && { echo "Error: Coral status not found in configuration."; exit 1; }
echo "Fetching Coral type from configuration..."
type=$(uci get frigate.tpu.type 2>/dev/null)
[ -z "$type" ] && { echo "Error: Coral type not found in configuration."; exit 1; }
echo "Fetching Coral device from configuration..."
device=$(uci get frigate.tpu.device 2>/dev/null)
[ -z "$device" ] && { echo "Error: Coral device not found in configuration."; exit 1; }
LAN_IP=$(uci get network.lan.ipaddr)
LAN_IP="${LAN_IP%/*}"
[ -z "$port" ] && port=1880
[ -z "$IMAGE_NAME" ] && IMAGE_NAME="ghcr.io/blakeblackshear/frigate:stable"
rm -r /opt/docker2/compose/frigate 2>/dev/null
mkdir -p /opt/docker2/compose/frigate
touch /opt/docker2/compose/frigate/config.yml
touch /opt/docker2/compose/frigate/docker-compose.yml
cat > /opt/docker2/compose/frigate/docker-compose.yml <<EOF
version: "3.9"
services:
frigate:
container_name: $APP_NAME
privileged: true # this may not be necessary for all setups
restart: unless-stopped
image: $IMAGE_NAME
shm_size: "64mb" # update for your cameras based on calculation above
devices:
- $usbcoral:/dev/bus/usb # passes the USB Coral, needs to be modified for other versions
#- /dev/apex_0:/dev/apex_0 # passes a PCIe Coral, follow driver instructions here https://coral.ai/docs/m2/get-started/#2a-on-linux
- $hwaccel # for intel hwaccel, needs to be updated for your hardware
volumes:
#- ./frigate/etc/localtime:/etc/localtime:ro
- ./config.yml:/config/config.yml
- $storage:/media/frigate
#- type: tmpfs # Optional: 1GB of memory, reduces SSD/SD Card wear
# target: /tmp/cache
# tmpfs:
# size: 1000000000
ports:
- "$port:5000"
- "8554:8554" # RTSP feeds
- "8555:8555/tcp" # WebRTC over tcp
- "8555:8555/udp" # WebRTC over udp
environment:
FRIGATE_RTSP_PASSWORD: "password"
EOF
# Initialize the base structure
echo "{}" > /opt/docker2/compose/frigate/config.yml
# Set global configurations
yq eval ".usbcoral = \"$usbcoral\"" -i /opt/docker2/compose/frigate/config.yml
yq eval ".hwaccel = \"$hwaccel\"" -i /opt/docker2/compose/frigate/config.yml
yq eval ".storage = \"$storage\"" -i /opt/docker2/compose/frigate/config.yml
yq eval ".mqtt.enabled = $mqtt" -i /opt/docker2/compose/frigate/config.yml
yq eval ".mqtt.host = \"$host\"" -i /opt/docker2/compose/frigate/config.yml
yq eval ".detectors.coral.type = \"$type\"" -i /opt/docker2/compose/frigate/config.yml
yq eval ".detectors.coral.device = \"$coral\"" -i /opt/docker2/compose/frigate/config.yml
# Initialize cameras structure
yq eval ".cameras = {}" -i /opt/docker2/compose/frigate/config.yml
# Write each camera's configuration to config.yml
local camera_index=0
while : ; do
# Try to fetch camera configuration
local name=$(uci get frigate.@camera_config[$camera_index].name 2>/dev/null)
# Exit loop if no more cameras are found
[[ -z "$name" ]] && break
# Retrieve the rest of the camera settings here...
local path=$(uci get frigate.@camera_config[$camera_index].path)
local roles=$(uci get frigate.@camera_config[$camera_index].roles)
local record=$(uci get frigate.@camera_config[$camera_index].record)
local snapshots=$(uci get frigate.@camera_config[$camera_index].snapshots)
local mask=$(uci get frigate.@camera_config[$camera_index].mask)
# Initialize camera structure
yq eval ".cameras.$name = {}" -i /opt/docker2/compose/frigate/config.yml
# Write to config.yml using yq
yq eval ".cameras.$name.ffmpeg = { inputs: [{ path: \"$path\", roles: [\"detect\"] }] }" -i /opt/docker2/compose/frigate/config.yml
yq eval ".cameras.$name.detect = { width: 1280, height: 720 }" -i /opt/docker2/compose/frigate/config.yml
yq eval ".cameras.$name.record = { enabled: $record }" -i /opt/docker2/compose/frigate/config.yml
yq eval ".cameras.$name.snapshots = { enabled: $snapshots }" -i /opt/docker2/compose/frigate/config.yml
yq eval ".cameras.$name.motion = { mask: [\"$mask\"] }" -i /opt/docker2/compose/frigate/config.yml
camera_index=$((camera_index+1))
done
docker-compose -f /opt/docker2/compose/frigate/docker-compose.yml up -d
uci add shortcutmenu lists
uci set shortcutmenu.@lists[-1].webname="$APP_NAME"
uci set shortcutmenu.@lists[-1].weburl="$LAN_IP:$port"
uci set shortcutmenu.@lists[-1].webpath="/"
uci commit shortcutmenu
}
usage() {
echo "usage: $0 sub-command"
echo "where sub-command is one of:"
echo " install Install frigate"
echo " upgrade Upgrade frigate"
echo " rm/start/stop/restart Remove/Start/Stop/Restart frigate"
echo " status frigate status"
echo " port frigate port"
}
case "${ACTION}" in
"install" | "upgrade")
do_install_detail
;;
"rm")
IMAGE_NAME=$(uci get frigate.@frigate[0].image_name 2>/dev/null)
[ -z "$IMAGE_NAME" ] && IMAGE_NAME="ghcr.io/blakeblackshear/frigate:stable"
CONTAINER_IDS=$(docker ps -a --filter "ancestor=${IMAGE_NAME}" --format '{{.ID}}')
echo "Stopping and removing containers..."
for ID in $CONTAINER_IDS; do
docker stop "$ID"
docker rm "$ID"
done
docker rmi -f "$IMAGE_NAME"
rm -r /opt/docker2/compose/frigate 2>/dev/null
;;
"start" | "stop" | "restart")
APP_NAME="frigate"
CONTAINER_IDS=$(docker ps -a --filter "name=${APP_NAME}" --format '{{.ID}}')
for ID in $CONTAINER_IDS; do
docker "${ACTION}" "${ID}"
done
;;
"status")
APP_NAME="frigate"
CONTAINER_NAME=$(docker ps -a --filter "name=${APP_NAME}" --format '{{.Names}}')
CONTAINER_STATUS=$(docker ps --all --filter "name=${CONTAINER_NAME}" --format '{{.Status}}' | awk '/^Up/ { print "up " substr($0, 4) } !/^Up/ && /.+/ { print "down" }')
if [ -z "$CONTAINER_NAME" ]; then
echo "${APP_NAME} is not installed"
else
echo "${CONTAINER_STATUS}"
fi
;;
"port")
APP_NAME="frigate"
CONTAINER_NAME=$(docker ps -a --filter "name=${APP_NAME}" --format '{{.Names}}')
# Fetch the port from UCI configuration
uci_port=$(uci get frigate.docker.port 2>/dev/null)
[ -z "$uci_port" ] && exit 1 # Exit silently if UCI port is not found
# Use the UCI port to filter the docker ps output and return only the external port
docker ps --all -f "name=${CONTAINER_NAME}" --format '{{.Ports}}' | grep -o "0.0.0.0:$uci_port->[0-9]*/tcp" | sed "s/0.0.0.0://;s/->[0-9]*\/tcp//"
;;
esac