diff --git a/.github/workflows/container_workflows.yaml b/.github/workflows/container_workflows.yaml index 142af70..25ec23b 100644 --- a/.github/workflows/container_workflows.yaml +++ b/.github/workflows/container_workflows.yaml @@ -1,9 +1,13 @@ # This is a workflow to build images name: Build Docker Images -on: [push] +on: + push: + branches: + - release jobs: + #TODO: redo the tests with the new arrangement build: name: Build Docker Image runs-on: ubuntu-latest diff --git a/README.md b/README.md index c9726d1..0ee97f6 100644 --- a/README.md +++ b/README.md @@ -87,6 +87,6 @@ We welcome pull requests! ### Organization of source code -- `app` is the frontend TypeScript / React application -- `api` is the backend Python / FastApi application -- `deploy` has code for deploying the system +- `hubfrontend` is a Typescript / React app that serves as the frontend for the system. +- `backendconfigserver` is an HTTP server which serves the configuration for the detectors. +- `deploy` has code for deploying the system in various environments diff --git a/.dockerignore b/backendconfigserver/.dockerignore similarity index 100% rename from .dockerignore rename to backendconfigserver/.dockerignore diff --git a/backend.Dockerfile b/backendconfigserver/Dockerfile similarity index 74% rename from backend.Dockerfile rename to backendconfigserver/Dockerfile index 54313e5..0200abe 100644 --- a/backend.Dockerfile +++ b/backendconfigserver/Dockerfile @@ -1,11 +1,16 @@ -FROM python:3.9-slim-bookworm +FROM python:3.11-slim-bookworm + +RUN apt-get update && \ + apt-get install -y \ + sudo && \ + apt-get clean WORKDIR /app COPY ./requirements.txt /app/requirements.txt RUN pip3 install --no-cache-dir --upgrade -r /app/requirements.txt -RUN pip3 install opencv-python-headless==4.8.0.74 +RUN pip3 install opencv-python-headless>=4.8 RUN pip install --index-url=https://www.piwheels.org/simple --no-cache-dir -r /app/requirements.txt RUN pip install --no-deps groundlight diff --git a/backend-armv7.Dockerfile b/backendconfigserver/Dockerfile-armv7 similarity index 78% rename from backend-armv7.Dockerfile rename to backendconfigserver/Dockerfile-armv7 index 9d57009..184aca2 100644 --- a/backend-armv7.Dockerfile +++ b/backendconfigserver/Dockerfile-armv7 @@ -1,4 +1,9 @@ -FROM python:3.9-slim-buster +FROM python:3.11-slim-buster + +RUN apt-get update && \ + apt-get install -y \ + sudo && \ + apt-get clean WORKDIR /app diff --git a/api/gl_process.py b/backendconfigserver/api/gl_process.py similarity index 100% rename from api/gl_process.py rename to backendconfigserver/api/gl_process.py diff --git a/api/index.py b/backendconfigserver/api/index.py similarity index 100% rename from api/index.py rename to backendconfigserver/api/index.py diff --git a/api/logging_config.yaml b/backendconfigserver/api/logging_config.yaml similarity index 100% rename from api/logging_config.yaml rename to backendconfigserver/api/logging_config.yaml diff --git a/api/notifications.py b/backendconfigserver/api/notifications.py similarity index 100% rename from api/notifications.py rename to backendconfigserver/api/notifications.py diff --git a/backendconfigserver/api/rtsp_scan.sh b/backendconfigserver/api/rtsp_scan.sh new file mode 100755 index 0000000..f1e085d --- /dev/null +++ b/backendconfigserver/api/rtsp_scan.sh @@ -0,0 +1,93 @@ +#!/bin/bash + +# check if the -v flag is present +if [[ $1 == "-v" ]]; then + VERBOSE=1 +else + VERBOSE=0 +fi + +# Function to install necessary dependencies +install_dependencies() { + echo "Installing dependencies..." + sudo apt update + sudo apt install -y nmap net-tools iproute2 +} + +# Function to find subnets +find_subnets() { + # This function finds the subnets associated with your physical network interfaces + # It excludes loopback, virtual, and docker interfaces + + # List all network interfaces except loopback and docker/virtual interfaces + local interfaces=$(ip -br link | awk '$1 !~ /^lo|docker|veth|br-|virbr/ {print $1}') + + # Loop through each interface to find its subnet + for interface in $interfaces; do + # Get the IP address and subnet mask for each interface + ip -4 addr show $interface | grep -oP '(?<=inet\s)\d+(\.\d+){3}/\d+' | while read -r subnet; do + # check if the subnet is bigger than /24 first + # first + echo "$subnet" + done + done +} + + +# Function to scan a given subnet +scan_subnet() { + local subnet=$1 + + # parse the subnet string, which looks like "192.168.1.7/16" into base and mask + local base=$(echo "$subnet" | cut -d '/' -f 1) + local mask=$(echo "$subnet" | cut -d '/' -f 2) + + # If the mask is smaller than /24, we'll want to use a faster scan + # Otherwise, we'll use a slower but more reliable scan + if [[ $mask -lt 24 ]]; then + if [[ $mask -lt 22 ]]; then + # Limit size of scan + mask=22 + if [[ $VERBOSE -eq 1 ]]; then + echo "Limiting subnet $subnet to /$mask so scan doesn't take forever." + echo "For a full scan, run 'nmap -T4 -p 554 --open -oG - $subnet'" + fi + fi + # T5 is "insane" and super fast, but can be unreliable. (e.g. 5s for /22) + # Limiting to T4 since that finds things reliably for me. + SPEED=-T4 + else + # It's 24 or less, so use a fast but reliable scan + SPEED=-T4 + fi + # re-assamble the subnet string + subnet="$base/$mask" + if [[ $VERBOSE -eq 1 ]]; then + echo "Scanning subnet $subnet at speed $SPEED" + fi + + # T5 is "insane" and super fast, but perhaps unreliable. (e.g. 5s for /22) + # T4 is safer, but much slower. (e.g. 26s for /22) + SPEED=-T5 + + # Run Nmap with Grepable output, then parse to list hosts with open port 554 + nmap $SPEED -p 554 --open -oG - $subnet | grep '/open/' | awk '{ print $2 }' | while read -r host; do + echo "rtsp://admin:admin@$host:554" + done +} + + +# Main execution +main() { + # check if `ip` and `nmap` are available + if ! command -v ip &> /dev/null || ! command -v nmap &> /dev/null; then + echo "Missing dependencies. Attempting to install them" + install_dependencies + fi + find_subnets | while read -r subnet; do + scan_subnet "$subnet" + done +} + +main + diff --git a/pi_requirements.txt b/backendconfigserver/pi_requirements.txt similarity index 100% rename from pi_requirements.txt rename to backendconfigserver/pi_requirements.txt diff --git a/requirements.txt b/backendconfigserver/requirements.txt similarity index 76% rename from requirements.txt rename to backendconfigserver/requirements.txt index f13411c..44eff91 100644 --- a/requirements.txt +++ b/backendconfigserver/requirements.txt @@ -1,7 +1,7 @@ fastapi==0.95.2 uvicorn[standard] groundlight -opencv-python==4.8.0.74 +opencv-python>=4.8 framegrab pypylon slack_sdk diff --git a/deploy/docker-compose.yml b/deploy/docker-compose.yml deleted file mode 100644 index 4321534..0000000 --- a/deploy/docker-compose.yml +++ /dev/null @@ -1,19 +0,0 @@ -services: - frontend: - image: docker.io/groundlight/monitoring-notification-server-frontend:latest - ports: - - "3000:3000" - depends_on: - - backend - backend: - image: docker.io/groundlight/monitoring-notification-server-backend:latest - ports: - - "8000:8000" - devices: - - /dev/video0:/dev/video0 - - /dev/video1:/dev/video1 - - /dev/video2:/dev/video2 - - /dev/video3:/dev/video3 - privileged: true - volumes: - - /dev/bus/usb:/dev/bus/usb diff --git a/docker-compose.yml b/docker-compose.yml new file mode 100644 index 0000000..61c73b7 --- /dev/null +++ b/docker-compose.yml @@ -0,0 +1,21 @@ +version: "2" +services: + hubfrontend: + build: ./hubfrontend + ports: + - "80:3000" + depends_on: + - backendconfigserver + backendconfigserver: + build: ./backendconfigserver + ports: + - "8000:8000" + devices: + - /dev/video0:/dev/video0 + - /dev/video1:/dev/video1 + - /dev/video2:/dev/video2 + - /dev/video3:/dev/video3 + privileged: true + # Seems balena doesn't like volume mounting. + #volumes: + #- /dev/bus/usb:/dev/bus/usb diff --git a/frontend.Dockerfile b/hubfrontend/Dockerfile similarity index 82% rename from frontend.Dockerfile rename to hubfrontend/Dockerfile index 31ed593..b9380cf 100644 --- a/frontend.Dockerfile +++ b/hubfrontend/Dockerfile @@ -8,7 +8,7 @@ FROM base AS deps WORKDIR /app # Install dependencies based on the preferred package manager -COPY --link package.json yarn.lock* package-lock.json* pnpm-lock.yaml* ./ +COPY package.json yarn.lock* package-lock.json* pnpm-lock.yaml* ./ RUN \ if [ -f yarn.lock ]; then yarn --frozen-lockfile; \ elif [ -f package-lock.json ]; then npm ci; \ @@ -20,8 +20,8 @@ RUN \ # Rebuild the source code only when needed FROM base AS builder WORKDIR /app -COPY --from=deps --link /app/node_modules ./node_modules -COPY --link . . +COPY --from=deps /app/node_modules ./node_modules +COPY . . # Next.js collects completely anonymous telemetry data about general usage. # Learn more here: https://nextjs.org/telemetry @@ -45,12 +45,12 @@ RUN \ addgroup --system --gid 1001 nodejs; \ adduser --system --uid 1001 nextjs -COPY --from=builder --link /app/public ./public +COPY --from=builder /app/public ./public # Automatically leverage output traces to reduce image size # https://nextjs.org/docs/advanced-features/output-file-tracing -COPY --from=builder --link --chown=1001:1001 /app/.next/standalone ./ -COPY --from=builder --link --chown=1001:1001 /app/.next/static ./.next/static +COPY --from=builder --chown=1001:1001 /app/.next/standalone ./ +COPY --from=builder --chown=1001:1001 /app/.next/static ./.next/static USER nextjs diff --git a/frontend-armv7.Dockerfile b/hubfrontend/Dockerfile-arm7 similarity index 100% rename from frontend-armv7.Dockerfile rename to hubfrontend/Dockerfile-arm7 diff --git a/app/detectors/page.tsx b/hubfrontend/app/detectors/page.tsx similarity index 100% rename from app/detectors/page.tsx rename to hubfrontend/app/detectors/page.tsx diff --git a/app/favicon.ico b/hubfrontend/app/favicon.ico similarity index 100% rename from app/favicon.ico rename to hubfrontend/app/favicon.ico diff --git a/app/globals.css b/hubfrontend/app/globals.css similarity index 100% rename from app/globals.css rename to hubfrontend/app/globals.css diff --git a/app/layout.tsx b/hubfrontend/app/layout.tsx similarity index 100% rename from app/layout.tsx rename to hubfrontend/app/layout.tsx diff --git a/app/page.tsx b/hubfrontend/app/page.tsx similarity index 100% rename from app/page.tsx rename to hubfrontend/app/page.tsx diff --git a/app/settings/page.tsx b/hubfrontend/app/settings/page.tsx similarity index 100% rename from app/settings/page.tsx rename to hubfrontend/app/settings/page.tsx diff --git a/app/sources/page.tsx b/hubfrontend/app/sources/page.tsx similarity index 100% rename from app/sources/page.tsx rename to hubfrontend/app/sources/page.tsx diff --git a/components/Dropdown.tsx b/hubfrontend/components/Dropdown.tsx similarity index 100% rename from components/Dropdown.tsx rename to hubfrontend/components/Dropdown.tsx diff --git a/components/EditDetectorOverlay.tsx b/hubfrontend/components/EditDetectorOverlay.tsx similarity index 100% rename from components/EditDetectorOverlay.tsx rename to hubfrontend/components/EditDetectorOverlay.tsx diff --git a/components/EditNotificationsOverlay.tsx b/hubfrontend/components/EditNotificationsOverlay.tsx similarity index 100% rename from components/EditNotificationsOverlay.tsx rename to hubfrontend/components/EditNotificationsOverlay.tsx diff --git a/components/Nav.tsx b/hubfrontend/components/Nav.tsx similarity index 100% rename from components/Nav.tsx rename to hubfrontend/components/Nav.tsx diff --git a/components/NewCameraOverlay.tsx b/hubfrontend/components/NewCameraOverlay.tsx similarity index 100% rename from components/NewCameraOverlay.tsx rename to hubfrontend/components/NewCameraOverlay.tsx diff --git a/components/PushStacklightConfigButton.tsx b/hubfrontend/components/PushStacklightConfigButton.tsx similarity index 100% rename from components/PushStacklightConfigButton.tsx rename to hubfrontend/components/PushStacklightConfigButton.tsx diff --git a/components/Spinner.tsx b/hubfrontend/components/Spinner.tsx similarity index 100% rename from components/Spinner.tsx rename to hubfrontend/components/Spinner.tsx diff --git a/images/Groundlight-Detector-Dashboard.png b/hubfrontend/images/Groundlight-Detector-Dashboard.png similarity index 100% rename from images/Groundlight-Detector-Dashboard.png rename to hubfrontend/images/Groundlight-Detector-Dashboard.png diff --git a/images/Groundlight-Docker-Frontpage.png b/hubfrontend/images/Groundlight-Docker-Frontpage.png similarity index 100% rename from images/Groundlight-Docker-Frontpage.png rename to hubfrontend/images/Groundlight-Docker-Frontpage.png diff --git a/middleware.ts b/hubfrontend/middleware.ts similarity index 100% rename from middleware.ts rename to hubfrontend/middleware.ts diff --git a/next.config.js b/hubfrontend/next.config.js similarity index 100% rename from next.config.js rename to hubfrontend/next.config.js diff --git a/package-lock.json b/hubfrontend/package-lock.json similarity index 100% rename from package-lock.json rename to hubfrontend/package-lock.json diff --git a/package.json b/hubfrontend/package.json similarity index 100% rename from package.json rename to hubfrontend/package.json diff --git a/postcss.config.js b/hubfrontend/postcss.config.js similarity index 100% rename from postcss.config.js rename to hubfrontend/postcss.config.js diff --git a/public/favicon.ico b/hubfrontend/public/favicon.ico similarity index 100% rename from public/favicon.ico rename to hubfrontend/public/favicon.ico diff --git a/tailwind.config.js b/hubfrontend/tailwind.config.js similarity index 100% rename from tailwind.config.js rename to hubfrontend/tailwind.config.js diff --git a/tsconfig.json b/hubfrontend/tsconfig.json similarity index 100% rename from tsconfig.json rename to hubfrontend/tsconfig.json diff --git a/utils/types.ts b/hubfrontend/utils/types.ts similarity index 100% rename from utils/types.ts rename to hubfrontend/utils/types.ts diff --git a/utils/useAvailableDetectors.ts b/hubfrontend/utils/useAvailableDetectors.ts similarity index 100% rename from utils/useAvailableDetectors.ts rename to hubfrontend/utils/useAvailableDetectors.ts diff --git a/utils/useDetectors.ts b/hubfrontend/utils/useDetectors.ts similarity index 100% rename from utils/useDetectors.ts rename to hubfrontend/utils/useDetectors.ts diff --git a/utils/useEscape.tsx b/hubfrontend/utils/useEscape.tsx similarity index 100% rename from utils/useEscape.tsx rename to hubfrontend/utils/useEscape.tsx diff --git a/utils/useImageSources.ts b/hubfrontend/utils/useImageSources.ts similarity index 100% rename from utils/useImageSources.ts rename to hubfrontend/utils/useImageSources.ts diff --git a/utils/useKeypress.tsx b/hubfrontend/utils/useKeypress.tsx similarity index 100% rename from utils/useKeypress.tsx rename to hubfrontend/utils/useKeypress.tsx