diff --git a/.devcontainer/devcontainer.json b/.devcontainer/devcontainer.json index 259bd9c7c..81ddfc91f 100644 --- a/.devcontainer/devcontainer.json +++ b/.devcontainer/devcontainer.json @@ -1,16 +1,50 @@ { - "name": "Frigate Dev", + "name": "Frigate Devcontainer", "dockerComposeFile": "../docker-compose.yml", - "service": "dev", - "workspaceFolder": "/lab/frigate", + "service": "devcontainer", + "workspaceFolder": "/workspace/frigate", + "initializeCommand": ".devcontainer/initialize.sh", + "postCreateCommand": ".devcontainer/post_create.sh", + "overrideCommand": false, + "remoteUser": "vscode", + "features": { + "ghcr.io/devcontainers/features/common-utils:1": {} + }, + "forwardPorts": [5000, 5001, 5173, 1935, 8554, 8555], + "portsAttributes": { + "5000": { + "label": "NGINX", + "onAutoForward": "silent" + }, + "5001": { + "label": "Frigate API", + "onAutoForward": "silent" + }, + "5173": { + "label": "Vite Server", + "onAutoForward": "silent" + }, + "1935": { + "label": "RTMP", + "onAutoForward": "silent" + }, + "8554": { + "label": "gortc RTSP", + "onAutoForward": "silent" + }, + "8555": { + "label": "go2rtc WebRTC", + "onAutoForward": "silent" + } + }, "extensions": [ + "ms-python.vscode-pylance", "ms-python.python", "visualstudioexptteam.vscodeintellicode", "mhutchie.git-graph", "ms-azuretools.vscode-docker", "streetsidesoftware.code-spell-checker", "esbenp.prettier-vscode", - "ms-python.vscode-pylance", "dbaeumer.vscode-eslint", "mikestead.dotenv", "csstools.postcss", @@ -18,6 +52,7 @@ "bradlc.vscode-tailwindcss" ], "settings": { + "remote.autoForwardPorts": false, "python.linting.pylintEnabled": true, "python.linting.enabled": true, "python.formatting.provider": "black", diff --git a/.devcontainer/initialize.sh b/.devcontainer/initialize.sh new file mode 100755 index 000000000..2300df187 --- /dev/null +++ b/.devcontainer/initialize.sh @@ -0,0 +1,13 @@ +#!/bin/bash + +set -euo pipefail + +# These folders needs to be created and owned by the host user +mkdir -p debug web/dist + +if [[ -f "config/config.yml" ]]; then + echo "config/config.yml already exists, skipping initialization" >&2 +else + echo "initializing config/config.yml" >&2 + cp -fv config/config.yml.example config/config.yml +fi diff --git a/.devcontainer/post_create.sh b/.devcontainer/post_create.sh new file mode 100755 index 000000000..9dd1249c6 --- /dev/null +++ b/.devcontainer/post_create.sh @@ -0,0 +1,17 @@ +#!/bin/bash + +set -euxo pipefail + +# Frigate normal container runs as root, so it have permission to create +# the folders. But the devcontainer runs as the host user, so we need to +# create the folders and give the host user permission to write to them. +sudo mkdir -p /media/frigate +sudo chown -R "$(id -u):$(id -g)" /media/frigate + +make version + +cd web + +npm install + +npm run build diff --git a/.dockerignore b/.dockerignore index 9ce459f25..b22b1b536 100644 --- a/.dockerignore +++ b/.dockerignore @@ -9,4 +9,8 @@ core *.mp4 *.jpg *.db -*.ts \ No newline at end of file +*.ts + +web/dist/ +web/node_modules/ +web/.npm diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index eb9735b2c..7818a52a6 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -26,7 +26,7 @@ jobs: registry: ghcr.io username: ${{ github.actor }} password: ${{ secrets.GITHUB_TOKEN }} - - name: Build web - run: make build_web - name: Build image - run: make push \ No newline at end of file + run: make push + env: + IMAGE_REPO: ghcr.io/${{ github.actor }}/frigate diff --git a/.github/workflows/pull_request.yml b/.github/workflows/pull_request.yml index 27a3fd477..ae36e12f6 100644 --- a/.github/workflows/pull_request.yml +++ b/.github/workflows/pull_request.yml @@ -6,6 +6,30 @@ env: DEFAULT_PYTHON: 3.9 jobs: + build_devcontainer: + runs-on: ubuntu-latest + name: Build Devcontainer + # The Dockerfile contains features that requires buildkit, and since the + # devcontainer cli uses docker-compose to build the image, the only way to + # ensure docker-compose uses buildkit is to explicitly enable it. + env: + DOCKER_BUILDKIT: "1" + steps: + - uses: actions/checkout@v3 + - uses: actions/setup-node@master + with: + node-version: 16.x + - name: Install devcontainer cli + run: npm install --global @devcontainers/cli + - name: Build devcontainer + run: devcontainer build --workspace-folder . + # It would be nice to also test the following commands, but for some + # reason they don't work even though in VS Code devcontainer works. + # - name: Start devcontainer + # run: devcontainer up --workspace-folder . + # - name: Run devcontainer scripts + # run: devcontainer run-user-commands --workspace-folder . + web_lint: name: Web - Lint runs-on: ubuntu-latest @@ -36,7 +60,7 @@ jobs: python_checks: runs-on: ubuntu-latest - name: Python checks + name: Python Checks steps: - name: Check out the repository uses: actions/checkout@v3 @@ -70,8 +94,6 @@ jobs: uses: docker/setup-qemu-action@v2 - name: Set up Docker Buildx uses: docker/setup-buildx-action@v2 - - name: Create Version Module - run: make version - name: Build run: make - name: Run mypy diff --git a/docker/Dockerfile b/Dockerfile similarity index 82% rename from docker/Dockerfile rename to Dockerfile index d765dd029..c9844d36d 100644 --- a/docker/Dockerfile +++ b/Dockerfile @@ -1,8 +1,8 @@ # syntax=docker/dockerfile:1.2 -FROM blakeblackshear/frigate-nginx:1.0.2 as nginx +FROM blakeblackshear/frigate-nginx:1.0.2 AS nginx -FROM debian:11 as wheels +FROM debian:11 AS wheels ARG TARGETARCH ENV DEBIAN_FRONTEND=noninteractive @@ -44,8 +44,8 @@ RUN pip3 install -r requirements.txt COPY requirements-wheels.txt /requirements-wheels.txt RUN pip3 wheel --wheel-dir=/wheels -r requirements-wheels.txt -# Frigate Container -FROM debian:11-slim +# Frigate deps (ffmpeg, python, nginx, go2rtc, s6-overlay, etc) +FROM debian:11-slim AS deps ARG TARGETARCH # https://askubuntu.com/questions/972516/debian-frontend-environment-variable @@ -65,7 +65,7 @@ RUN --mount=type=bind,from=wheels,source=/wheels,target=/wheels \ gnupg \ wget \ procps \ - unzip tzdata libxml2 xz-utils \ + unzip locales tzdata libxml2 xz-utils \ python3-pip \ # add raspberry pi repo && apt-key adv --keyserver keyserver.ubuntu.com --recv-keys 9165938D90FDDD2E \ @@ -94,10 +94,12 @@ RUN --mount=type=bind,from=wheels,source=/wheels,target=/wheels \ fi \ # arch specific packages && if [ "${TARGETARCH}" = "amd64" ]; then \ - echo 'deb http://deb.debian.org/debian testing main non-free' >> /etc/apt/sources.list.d/deb.list \ + # Use debian testing repo only for hwaccel packages + echo 'deb http://deb.debian.org/debian testing main non-free' > /etc/apt/sources.list.d/debian-testing.list \ && apt-get -qq update \ && apt-get -qq install --no-install-recommends --no-install-suggests -y \ - mesa-va-drivers libva-drm2 intel-media-va-driver-non-free i965-va-driver libmfx1; \ + mesa-va-drivers libva-drm2 intel-media-va-driver-non-free i965-va-driver libmfx1 \ + && rm -f /etc/apt/sources.list.d/debian-testing.list; \ fi \ && if [ "${TARGETARCH}" = "arm64" ]; then \ apt-get -qq install --no-install-recommends --no-install-suggests -y \ @@ -133,12 +135,6 @@ COPY labelmap.txt /labelmap.txt RUN wget -q https://github.com/google-coral/test_data/raw/release-frogfish/ssdlite_mobiledet_coco_qat_postprocess_edgetpu.tflite -O /edgetpu_model.tflite RUN wget -q https://github.com/google-coral/test_data/raw/release-frogfish/ssdlite_mobiledet_coco_qat_postprocess.tflite -O /cpu_model.tflite -WORKDIR /opt/frigate/ -ADD frigate frigate/ -ADD migrations migrations/ - -COPY web/dist web/ - COPY docker/rootfs/ / # s6-overlay @@ -156,4 +152,51 @@ EXPOSE 8555 ENTRYPOINT ["/init"] +# Frigate deps with Node.js and NPM +FROM deps AS deps-node + +# Install Node 16 +RUN wget -qO- https://deb.nodesource.com/setup_16.x | bash - \ + && apt-get install -y nodejs \ + && npm install -g npm@9 + +# Devcontainer +FROM deps-node AS devcontainer + +WORKDIR /workspace/frigate + +RUN apt-get update \ + && apt-get install make -y \ + && rm -rf /var/lib/apt/lists/* + +RUN --mount=type=bind,source=./requirements-dev.txt,target=/workspace/frigate/requirements-dev.txt \ + pip3 install -r requirements-dev.txt + +CMD ["sleep", "infinity"] + + +# Frigate web build +FROM deps-node AS web-build + +WORKDIR /work +COPY web/package.json web/package-lock.json ./ +RUN npm install + +COPY web/ ./ +RUN npm run build + +# Frigate web dist files +FROM scratch AS web-dist + +COPY --from=web-build /work/dist/ / + + +# Frigate final container +FROM deps + +WORKDIR /opt/frigate/ +COPY frigate frigate/ +COPY migrations migrations/ +COPY --from=web-dist / web/ + CMD ["python3", "-u", "-m", "frigate"] diff --git a/Makefile b/Makefile index 33e605144..269647714 100644 --- a/Makefile +++ b/Makefile @@ -2,35 +2,33 @@ default_target: local COMMIT_HASH := $(shell git log -1 --pretty=format:"%h"|tail -1) VERSION = 0.12.0 +IMAGE_OWNER ?= ghcr.io/blakeblackshear/frigate CURRENT_UID := $(shell id -u) CURRENT_GID := $(shell id -g) version: - echo "VERSION=\"$(VERSION)-$(COMMIT_HASH)\"" > frigate/version.py + echo 'VERSION = "$(VERSION)-$(COMMIT_HASH)"' > frigate/version.py + +local: version + docker buildx build --tag frigate:latest --load . build_web: - docker run -e npm_config_cache=/web/.npm --volume ${PWD}/web:/web -w /web --group-add $(CURRENT_GID) -u $(CURRENT_UID):$(CURRENT_GID) node:16 /bin/bash -c "npm install && npm run build" - -nginx_frigate: - docker buildx build --push --platform linux/arm/v7,linux/arm64/v8,linux/amd64 --tag blakeblackshear/frigate-nginx:1.0.2 --file docker/Dockerfile.nginx . - -local: - DOCKER_BUILDKIT=1 docker build -t frigate -f docker/Dockerfile . + docker buildx build --target web-dist --output web/dist . amd64: - docker buildx build --platform linux/amd64 --tag blakeblackshear/frigate:$(VERSION)-$(COMMIT_HASH) --file docker/Dockerfile . + docker buildx build --platform linux/amd64 --tag $(IMAGE_REPO):$(VERSION)-$(COMMIT_HASH) . arm64: - docker buildx build --platform linux/arm64 --tag blakeblackshear/frigate:$(VERSION)-$(COMMIT_HASH) --file docker/Dockerfile . + docker buildx build --platform linux/arm64 --tag $(IMAGE_REPO):$(VERSION)-$(COMMIT_HASH) . armv7: - docker buildx build --platform linux/arm/v7 --tag blakeblackshear/frigate:$(VERSION)-$(COMMIT_HASH) --file docker/Dockerfile . + docker buildx build --platform linux/arm/v7 --tag $(IMAGE_REPO):$(VERSION)-$(COMMIT_HASH) . build: version amd64 arm64 armv7 - docker buildx build --platform linux/arm/v7,linux/arm64/v8,linux/amd64 --tag blakeblackshear/frigate:$(VERSION)-$(COMMIT_HASH) --file docker/Dockerfile . + docker buildx build --platform linux/arm/v7,linux/arm64/v8,linux/amd64 --tag $(IMAGE_REPO):$(VERSION)-$(COMMIT_HASH) . push: build - docker buildx build --push --platform linux/arm/v7,linux/arm64/v8,linux/amd64 --tag ghcr.io/blakeblackshear/frigate:${GITHUB_REF_NAME}-$(COMMIT_HASH) --file docker/Dockerfile . + docker buildx build --push --platform linux/arm/v7,linux/arm64/v8,linux/amd64 --tag $(IMAGE_REPO):${GITHUB_REF_NAME}-$(COMMIT_HASH) . run_tests: frigate docker run --rm --entrypoint=python3 frigate:latest -u -m unittest diff --git a/config/config.yml.example b/config/config.yml.example new file mode 100644 index 000000000..87deab160 --- /dev/null +++ b/config/config.yml.example @@ -0,0 +1,16 @@ +mqtt: + host: mqtt + +cameras: + test: + ffmpeg: + inputs: + - path: /media/frigate/car-stopping.mp4 + input_args: -re -stream_loop -1 -fflags +genpts + roles: + - detect + - rtmp + detect: + height: 1080 + width: 1920 + fps: 5 diff --git a/docker-compose.yml b/docker-compose.yml index 1dac6d49a..3ed08493c 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -1,8 +1,7 @@ version: "3" services: - dev: - container_name: frigate-dev - user: vscode + devcontainer: + container_name: frigate-devcontainer # add groups from host for render, plugdev, video group_add: - "109" # render @@ -12,25 +11,17 @@ services: shm_size: "256mb" build: context: . - dockerfile: docker/Dockerfile.dev + target: devcontainer devices: - /dev/bus/usb:/dev/bus/usb - - /dev/dri:/dev/dri # for intel hwaccel, needs to be updated for your hardware + # - /dev/dri:/dev/dri # for intel hwaccel, needs to be updated for your hardware volumes: + - .:/workspace/frigate:cached + - ./web/dist:/opt/frigate/web:cached - /etc/localtime:/etc/localtime:ro - - .:/lab/frigate:cached - ./config/config.yml:/config/config.yml:ro - ./debug:/media/frigate - /dev/bus/usb:/dev/bus/usb - ports: - - "1935:1935" - - "3000:3000" - - "5000:5000" - - "5001:5001" - - "8080:8080" - - "8554:8554" - entrypoint: ["sudo", "/init"] - command: /bin/sh -c "while sleep 1000; do :; done" mqtt: container_name: mqtt image: eclipse-mosquitto:1.6 diff --git a/docker/Dockerfile.dev b/docker/Dockerfile.dev deleted file mode 100644 index 7f47c3768..000000000 --- a/docker/Dockerfile.dev +++ /dev/null @@ -1,27 +0,0 @@ -FROM frigate:latest - -ARG USERNAME=vscode -ARG USER_UID=1000 -ARG USER_GID=$USER_UID - -# Create the user -RUN groupadd --gid $USER_GID $USERNAME \ - && useradd --uid $USER_UID --gid $USER_GID -m $USERNAME -s /bin/bash \ - # - # [Optional] Add sudo support. Omit if you don't need to install software after connecting. - && apt-get update \ - && apt-get install -y sudo \ - && echo $USERNAME ALL=\(root\) NOPASSWD:ALL > /etc/sudoers.d/$USERNAME \ - && chmod 0440 /etc/sudoers.d/$USERNAME - -RUN apt-get update \ - && apt-get install -y git curl vim htop - -COPY requirements-dev.txt /opt/frigate/requirements-dev.txt -RUN pip3 install -r requirements-dev.txt - -# Install Node 16 -RUN curl -sL https://deb.nodesource.com/setup_16.x | bash - \ - && apt-get install -y nodejs - -RUN npm install -g npm@latest