diff --git a/.github/workflows/qnap.yml b/.github/workflows/qnap.yml new file mode 100644 index 000000000..ece9e9826 --- /dev/null +++ b/.github/workflows/qnap.yml @@ -0,0 +1,67 @@ +name: QNAP packages + +# Builds the Lens QPKGs (packaging/qnap) for Intel/AMD and ARM NAS and attaches them to the release. +# Runs when a release is published (release.yml makes the draft; publishing it starts this), or by hand for a tag. +on: + release: + types: [published] + workflow_dispatch: + inputs: + tag: + description: "Release tag to build and attach the packages to" + required: true + type: string + +permissions: + contents: write + +concurrency: + group: qnap-${{ github.event.release.tag_name || inputs.tag }} + cancel-in-progress: false + +jobs: + qpkg: + name: QPKG ${{ matrix.arch }} + strategy: + fail-fast: false + matrix: + include: + - arch: x86_64 + runner: ubuntu-latest + # Built under QEMU unless the repository variable QNAP_ARM_RUNNER names an ARM runner (ubuntu-24.04-arm) + - arch: arm_64 + runner: ${{ vars.QNAP_ARM_RUNNER || 'ubuntu-latest' }} + runs-on: ${{ matrix.runner }} + timeout-minutes: 300 + env: + TAG: ${{ github.event.release.tag_name || inputs.tag }} + GH_TOKEN: ${{ github.token }} + steps: + - uses: actions/checkout@v4 + with: + ref: ${{ env.TAG }} + + - name: Make room for the images + run: | + sudo rm -rf /usr/share/dotnet /usr/local/lib/android /opt/ghc /opt/hostedtoolcache/CodeQL + docker system prune -af >/dev/null + df -h / + + - uses: docker/setup-qemu-action@v3 + - uses: docker/setup-buildx-action@v3 + with: + driver: docker # images go straight into the local Docker, where build.sh saves them from + + - name: Build the package + run: | + version="${TAG#v}" + packaging/qnap/build.sh --arch "${{ matrix.arch }}" --version "$version" + + - name: Attach to the release + run: | + ls -lh packaging/qnap/build/ + # GitHub release assets are limited to 2 GiB each + for f in packaging/qnap/build/*.qpkg; do + [ "$(stat -c %s "$f")" -lt 2147483648 ] || { echo "::error::$f is over 2 GiB, too big for a release asset"; exit 1; } + done + gh release upload "$TAG" packaging/qnap/build/*.qpkg packaging/qnap/build/*.qpkg.md5 --clobber diff --git a/CHANGELOG.md b/CHANGELOG.md index 162f4e7f1..8fc475e06 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -113,6 +113,15 @@ The backend (`fastapi_backend`) and the frontend (`nextjs-frontend`) are version "table 'seq' does not exist" (SurrealDB 3). The counters table is defined with the rest of the schema now, so nothing reads it before it exists. +- **Lens for QNAP NAS.** `packaging/qnap/build.sh` builds a self-contained QPKG (with + [QDK](https://github.com/qnap-dev/QDK)) for Intel/AMD or ARM models that installs from the App Center's Install + Manually: it carries the Lens images and SurrealDB, loads them into Container Station on its first start, and runs + the stack there, so there are no containers to set up by hand. The first start writes the settings with fresh + secrets to a data folder (`/share/Container/lens`) that outlasts removing the app, and puts the first-admin setup + code in the QTS system log; Lens sees only `Multimedia/Lens` on the NAS, a folder the setup steps can watch. + Upgrades install over the old version and remove its images. Publishing a GitHub release builds both packages and + attaches them to it (`.github/workflows/qnap.yml`). Steps in packaging/qnap/README.md. + - **Fix: Chat answers no longer break off with "The answer stopped before it finished".** With some model servers an answer ended mid-stream with nothing saved, so the question sat unanswered in the conversation. Now each one ends in an answer, or an error that's shown and saved with the conversation. diff --git a/packaging/qnap/.gitignore b/packaging/qnap/.gitignore new file mode 100644 index 000000000..0a5f58e88 --- /dev/null +++ b/packaging/qnap/.gitignore @@ -0,0 +1,2 @@ +.work/ +build/ diff --git a/packaging/qnap/README.md b/packaging/qnap/README.md new file mode 100644 index 000000000..8aed48a8a --- /dev/null +++ b/packaging/qnap/README.md @@ -0,0 +1,121 @@ +# Lens for QNAP NAS + +A self-contained QPKG, built with [QDK](https://github.com/qnap-dev/QDK), that installs Lens from the App Center like +any other app. Lens runs in **Container Station**, but you don't set up any containers: the package carries the +Lens images (API and worker, web app) and SurrealDB, loads them on its first start, and runs the stack. Nothing is +built or downloaded on the NAS. + +- **NAS:** QTS 5 or QuTS hero 5 with Container Station 3, on an Intel/AMD (`x86_64`) or ARM 64-bit (`arm_64`) model. + 4 GB of RAM or more; transcription is heavy. +- **Space:** a few GB on the volume: the package's image files while they're loaded on the first start (they're + deleted afterwards), the images in Container Station, and whatever your archive holds. + +## Build the package + +On any machine with Docker and buildx (Linux, macOS, Windows with WSL), from the repository root: + +```bash +packaging/qnap/build.sh # Intel/AMD NAS: packaging/qnap/build/Lens_0.3.0_x86_64.qpkg +packaging/qnap/build.sh --arch arm_64 # ARM NAS: packaging/qnap/build/Lens_0.3.0_arm_64.qpkg +``` + +- The version comes from `fastapi_backend/pyproject.toml` (`--version` overrides it, 10 characters at most). +- The backend image is the `lean` target by default: it reads audio, video, PDFs and images. `--target full` adds + LibreOffice and Chromium, to read Office files, text, web pages and emails, and object detection, for a much bigger + package. `--extras "msg"` adds Outlook `.msg` emails (see docs/deployment.md). +- Building for the other architecture than your machine's uses QEMU. Docker Desktop has it; on Linux, once: + `docker run --privileged --rm tonistiigi/binfmt --install arm64` (or `amd64`). It's slow; building on a machine of + the NAS's architecture is quicker. +- The first run builds a small `lens-qdk` image with QDK in it, so QDK needn't be installed. +- The package's images are tagged `lens-backend:qnap-` and `lens-frontend:qnap-`, so they never + clash with Lens images built some other way. `--prebuilt` packages images with those tags already in Docker + instead of building them (from CI, say). + +Releases get the packages automatically: publishing a GitHub release runs `.github/workflows/qnap.yml`, which builds +both and attaches `Lens__x86_64.qpkg` and `Lens__arm_64.qpkg` to it (run it by hand from the Actions tab, +with a tag, to rebuild them). The ARM package is built under QEMU, which is slow; set the repository variable +`QNAP_ARM_RUNNER` to an ARM runner (`ubuntu-24.04-arm`) if the repository has one. + +## Install on the NAS + +1. Install **Container Station** from the App Center and open it once, so it finishes its setup. +2. App Center > the gear icon (top right) > **Install Manually** > choose the `.qpkg` for your NAS > **Install**. + QTS warns that the app isn't digitally signed; accept to continue. (Or, over SSH: + `sh Lens_0.3.0_x86_64.qpkg`.) +3. The first start loads the images into Container Station, which takes a few minutes. When Lens shows as running, + click **Open** in the App Center (or browse to `http://:3000`). +4. Create the first admin account with the **setup code**. It's in the QTS system log (Control Panel > System > + System Logs, an entry from Lens saying "create the first admin account with setup code …"), in `lens.env` in the + data folder (`LENS_SETUP_CODE`), and over SSH: `/share/CACHEDEV1_DATA/.qpkg/Lens/lens.sh setup-code` (the + path depends on the volume you installed to: `getcfg Lens Install_Path -f /etc/config/qpkg.conf`). +5. Follow the setup steps: the first namespace, the model provider, and storage, where you can watch `/audio` (the + NAS's `Multimedia/Lens` folder). Each can be skipped, or answered ahead in `lens.env` (see Settings). + +The containers show up in Container Station as the application `lens` (surrealdb, backend, worker, frontend), where +you can see their logs and resource use. + +## Where things are + +The **data folder** is `/share/Container/lens` (the `Container` shared folder Container Station creates), or +`Lens-data` next to the app's folder if that share doesn't exist. It holds: + +| Path | What | +|---|---| +| `lens.env` | Settings and secrets, written on the first start. Keep it with the rest: losing `ARCHIVE_SECRET_KEY` makes stored credentials and model API keys unreadable. | +| `surrealdb/` | The database. | +| `archive/` | Uploads, media and derived files. | + +Back up the whole folder (Hybrid Backup Sync works, as it's a shared folder) with Lens stopped. Removing the app keeps +the data folder and drops the images; reinstalling picks up where it was. Delete the folder yourself to remove +everything. + +Your **recordings and documents**: Lens sees one folder on the NAS, `Lens` in the `Multimedia` share +(`/share/Multimedia/Lens`, created on the first start), as `/audio`, read-only. The setup steps after the first admin +account (namespace, model provider, storage) can watch it, or a folder in it, so what you put there is picked up. Set +`AUDIO_DIR` in `lens.env` to use another folder. Settings changed in the web app are kept in the database. + +## Settings + +Edit `lens.env` in the data folder, then stop and start Lens in the App Center (or `lens.sh restart` over SSH). + +- **Address and port.** `FRONTEND_URL` is the NAS's LAN address at install time, `http://:3000`. Change it, and + add the host name to `ARCHIVE_ALLOWED_HOSTS`, if you reach Lens another way: a fixed host name, a reverse proxy with + HTTPS (QTS's own reverse proxy in Control Panel > Network Access, or myQNAPcloud), or if the NAS's address changes. + For another port set `LENS_PORT` and the port in `FRONTEND_URL` alike. Sharing and IIIF need HTTPS. +- **Mail** for password resets: the `MAIL_*` settings. +- **Unattended setup.** The `LENS_ADMIN_*`, `LENS_NAMESPACE`, `LENS_LLM_*` and `LENS_SETUP_WIZARD` settings answer the + first-run setup instead of the web app (see docs/configuration.md); empty ones are left to the web app. +- **Models.** Lens talks to OpenAI-compatible servers; set them up in the web app. A model server on the NAS itself + (Ollama in Container Station, say) is reachable at the NAS's LAN address, not `localhost`. +- To move the data folder: stop Lens, move the folder, then + `setcfg Lens Data_Path /share/ -f /etc/config/qpkg.conf` and start Lens. + +## Over SSH + +`lens.sh` in the app's folder (`getcfg Lens Install_Path -f /etc/config/qpkg.conf`): + +```bash +lens.sh status # the containers +lens.sh logs [backend] # follow the logs (all, or one service) +lens.sh setup-code # the first-admin setup code +lens.sh restart +``` + +## Upgrading + +Build the new version's package and install it over the old one, as in step 2. The new images are loaded on the +next start and the old version's are removed; the data folder carries over. + +## How the package is made + +`build.sh` builds the images for the NAS's platform, saves them into `/images/` next to the `qpkg/` sources +(with a `manifest` naming each one), and runs QDK's `qbuild` on the lot. In the package: + +- `qpkg.cfg`: requires Container Station 3, opens the web app on port 3000, gives the first start 15 minutes. +- `lens.sh`: the service script. On start it waits for Container Station's Docker, writes `lens.env` with fresh + secrets on the first run, loads any image that isn't in Docker yet, and runs `docker compose up` on + `docker-compose.yml` as the project `lens`. On removal it stops the stack and removes its own images (tagged `qnap-`). +- `docker-compose.yml`: the same stack as `docker-compose.prod.yml`, with the bundled images, the data folder as bind + mounts and the web app on the NAS's port 3000. +- `archive.yaml`: the processing configuration: `/audio` as the folder the web app may watch, and no namespaces, so + the first-run setup asks for one. diff --git a/packaging/qnap/build.sh b/packaging/qnap/build.sh new file mode 100755 index 000000000..b0dd1522d --- /dev/null +++ b/packaging/qnap/build.sh @@ -0,0 +1,103 @@ +#!/usr/bin/env bash +# Builds the Lens QPKG for QNAP NAS: one self-contained .qpkg per architecture, with the Lens images (API and worker, +# web app) and SurrealDB inside, so the NAS needs nothing but Container Station. +# +# packaging/qnap/build.sh # x86_64 (Intel and AMD NAS), the lean image +# packaging/qnap/build.sh --arch arm_64 # ARM NAS (on an x86 machine this needs QEMU, see README.md) +# packaging/qnap/build.sh --arch x86_64 --arch arm_64 --target full --extras "msg" +# +# Needs only Docker with buildx (Linux, macOS, or Windows with WSL); QDK runs in a container built the first time. +# Output: packaging/qnap/build/Lens__.qpkg +set -euo pipefail + +HERE=$(cd "$(dirname "$0")" && pwd) +REPO=$(cd "$HERE/../.." && pwd) +QDK_REF=v2.5.3 +WORK="$HERE/.work" +OUT="$HERE/build" +SURREAL_IMAGE=surrealdb/surrealdb:v3.2.4 + +ARCHES=() +TARGET=lean # full: adds LibreOffice and Chromium, to read Office files, text, web pages and emails +EXTRAS="" +PREBUILT="" # --prebuilt: package the lens-backend:qnap- and lens-frontend:qnap- images already in Docker +VERSION=$(sed -n 's/^version = "\(.*\)"/\1/p' "$REPO/fastapi_backend/pyproject.toml" | head -n 1) + +while [ $# -gt 0 ]; do + case "$1" in + --arch) ARCHES+=("$2"); shift 2 ;; + --target) TARGET="$2"; shift 2 ;; + --extras) EXTRAS="$2"; shift 2 ;; + --version) VERSION="$2"; shift 2 ;; + --prebuilt) PREBUILT=yes; shift ;; + -h|--help) sed -n '2,10p' "$0"; exit 0 ;; + *) echo "unknown option: $1" >&2; exit 2 ;; + esac +done +[ ${#ARCHES[@]} -gt 0 ] || ARCHES=(x86_64) +[ ${#VERSION} -le 10 ] || { echo "QPKG versions are at most 10 characters: $VERSION" >&2; exit 2; } + +platform_of() { + case "$1" in + x86_64) echo linux/amd64 ;; + arm_64) echo linux/arm64 ;; + *) echo "unsupported architecture: $1 (x86_64 or arm_64)" >&2; exit 2 ;; + esac +} +for a in "${ARCHES[@]}"; do platform_of "$a" >/dev/null; done + +# --- QDK, in a small Linux image of its own (qbuild expects GNU tools under /bin) -------------------------------- +docker image inspect "lens-qdk:${QDK_REF#v}" >/dev/null 2>&1 || +docker build -q -t "lens-qdk:${QDK_REF#v}" --build-arg QDK_REF="$QDK_REF" - >/dev/null <<'DOCKERFILE' +FROM debian:bookworm-slim +RUN apt-get update && apt-get install -y --no-install-recommends ca-certificates git gcc libc6-dev rsync bsdextrautils \ + && rm -rf /var/lib/apt/lists/* +ARG QDK_REF +RUN git clone -q --depth 1 --branch "$QDK_REF" https://github.com/qnap-dev/QDK.git /tmp/QDK \ + && cp -R /tmp/QDK/shared /usr/share/QDK && mkdir -p /etc/config \ + && printf 'QDK_VERSION=%s\nQDK_PATH=/usr/share/QDK\n' "${QDK_REF#v}" > /etc/config/qdk.conf \ + && gcc -o /usr/local/bin/qpkg_encrypt /tmp/QDK/src/qpkg_encrypt.c && rm -rf /tmp/QDK +ENV PATH="/usr/share/QDK/bin:$PATH" +DOCKERFILE + +qbuild() { + docker run --rm -u "$(id -u):$(id -g)" -v "$HERE:/qnap" -w /qnap/.work "lens-qdk:${QDK_REF#v}" qbuild "$@" +} + +save_args=() +mkdir -p "$OUT" +for arch in "${ARCHES[@]}"; do + platform=$(platform_of "$arch") + echo "==> Lens $VERSION for $arch ($platform), backend target $TARGET" + + rm -rf "$WORK" + mkdir -p "$WORK" + cp -R "$HERE/qpkg/." "$WORK/" + images="$WORK/$arch/images" + mkdir -p "$images" + + if [ -z "$PREBUILT" ]; then + docker buildx build --platform "$platform" --target "$TARGET" --build-arg EXTRAS="$EXTRAS" \ + --load -t "lens-backend:qnap-$VERSION" "$REPO/fastapi_backend" + docker buildx build --platform "$platform" -f "$REPO/nextjs-frontend/Dockerfile.prod" \ + --load -t "lens-frontend:qnap-$VERSION" "$REPO/nextjs-frontend" + fi + [ "$(docker image inspect -f '{{.Os}}/{{.Architecture}}' "$SURREAL_IMAGE" 2>/dev/null)" = "$platform" ] || + docker pull -q --platform "$platform" "$SURREAL_IMAGE" + + # With Docker's containerd image store, save only the platform being packaged + docker save --help 2>/dev/null | grep -q -- '--platform' && save_args=(--platform "$platform") + : > "$images/manifest" + for ref in "lens-backend:qnap-$VERSION" "lens-frontend:qnap-$VERSION" "$SURREAL_IMAGE"; do + got=$(docker image inspect -f '{{.Os}}/{{.Architecture}}' "$ref") + [ "$got" = "$platform" ] || { echo "$ref is $got, expected $platform" >&2; exit 1; } + file=$(echo "$ref" | tr '/:' '__').tar # left uncompressed: qbuild compresses the whole package + echo " saving $ref" + docker save ${save_args[@]+"${save_args[@]}"} -o "$images/$file" "$ref" + echo "$ref $file" >> "$images/manifest" + done + + qbuild --build-arch "$arch" --build-version "$VERSION" --build-dir /qnap/build +done +rm -rf "$WORK" +ls -lh "$OUT"/*.qpkg diff --git a/packaging/qnap/qpkg/icons/Lens.gif b/packaging/qnap/qpkg/icons/Lens.gif new file mode 100644 index 000000000..319bc77ac Binary files /dev/null and b/packaging/qnap/qpkg/icons/Lens.gif differ diff --git a/packaging/qnap/qpkg/icons/Lens_80.gif b/packaging/qnap/qpkg/icons/Lens_80.gif new file mode 100644 index 000000000..2bce0b170 Binary files /dev/null and b/packaging/qnap/qpkg/icons/Lens_80.gif differ diff --git a/packaging/qnap/qpkg/icons/Lens_gray.gif b/packaging/qnap/qpkg/icons/Lens_gray.gif new file mode 100644 index 000000000..14f1da9cd Binary files /dev/null and b/packaging/qnap/qpkg/icons/Lens_gray.gif differ diff --git a/packaging/qnap/qpkg/package_routines b/packaging/qnap/qpkg/package_routines new file mode 100644 index 000000000..cc1eb4f1b --- /dev/null +++ b/packaging/qnap/qpkg/package_routines @@ -0,0 +1,9 @@ +###################################################################### +# Lens install and removal hooks, sourced by QDK's qinstall.sh (see QDK's package_routines template for what's +# available). The containers, the image loading and the data folder are lens.sh's job, on the first start; removal +# runs "lens.sh stop" and "lens.sh remove" itself, which keep the data folder. +###################################################################### + +pkg_post_install(){ + $CMD_CHMOD 755 "$SYS_QPKG_DIR/lens.sh" +} diff --git a/packaging/qnap/qpkg/qpkg.cfg b/packaging/qnap/qpkg/qpkg.cfg new file mode 100644 index 000000000..dcbd51f1c --- /dev/null +++ b/packaging/qnap/qpkg/qpkg.cfg @@ -0,0 +1,27 @@ +# +# Lens for QNAP (QTS 5 / QuTS hero), built with QDK: https://github.com/qnap-dev/QDK +# packaging/qnap/build.sh fills in QPKG_VER and the per-architecture image folders (x86_64/, arm_64/). +# +QPKG_NAME="Lens" +QPKG_DISPLAY_NAME="Lens" +QPKG_VER="0.3.0" +QPKG_AUTHOR="Adeel Ahmad" +QPKG_LICENSE="MIT" +QPKG_SUMMARY="Searchable archive of your recordings and documents: transcripts, entities, chat." +QPKG_RC_NUM="190" +QPKG_SERVICE_PROGRAM="lens.sh" + +# The containers run in Container Station's Docker engine, which also stores their images. +QPKG_REQUIRE="container-station >= 3.0" +QPKG_REQUIRE_MSG="Lens runs in Container Station. Install Container Station 3 from the App Center, then install Lens again." +QTS_MINI_VERSION="5.0.0" + +# The web app. lens.sh rewrites Web_Port from LENS_PORT in the data folder's lens.env. +QPKG_WEB_PORT="3000" +QPKG_WEBUI="/" +QPKG_DESKTOP_APP="0" + +# Loading the bundled images on the first start takes a few minutes. +QPKG_TIMEOUT="900,180" + +QPKG_VOLUME_SELECT="1" diff --git a/packaging/qnap/qpkg/shared/archive.yaml b/packaging/qnap/qpkg/shared/archive.yaml new file mode 100644 index 000000000..bafe9f2ab --- /dev/null +++ b/packaging/qnap/qpkg/shared/archive.yaml @@ -0,0 +1,12 @@ +# Processing configuration for Lens on a QNAP NAS (lens.sh mounts it read-only; settings changed in the app are kept in +# the database). The folder AUDIO_DIR in lens.env (/share/Multimedia/Lens by default) appears inside Lens as /audio. +# No namespaces here: the first-run setup in the web app asks for one (or LENS_NAMESPACE in lens.env names it), and +# can watch a folder under /audio into it. +data_dir: /data +server: + # "backend" is the Host the web app uses to reach the API inside the compose network + allowed_hosts: [127.0.0.1, localhost, backend] +workers: + inline: 0 # the worker service does the work +sources: + local_roots: [/audio] # folders the web app may watch diff --git a/packaging/qnap/qpkg/shared/docker-compose.yml b/packaging/qnap/qpkg/shared/docker-compose.yml new file mode 100644 index 000000000..2eab62b62 --- /dev/null +++ b/packaging/qnap/qpkg/shared/docker-compose.yml @@ -0,0 +1,85 @@ +# The Lens stack on a QNAP NAS, run by lens.sh in Container Station's Docker engine (project "lens"). +# The images come inside the QPKG (lens-backend and lens-frontend tagged qnap-, and SurrealDB) and +# are loaded on the first start, so nothing is pulled or built on the NAS. +# Settings and secrets: lens.env in the data folder (LENS_DATA), written on the first start. Everything Lens keeps +# lives under LENS_DATA, which outlasts removing the app. + +x-backend: &backend + image: lens-backend:qnap-${LENS_VERSION:?} + environment: + ARCHIVE_CONFIG: /qnap/archive.yaml + ACCESS_SECRET_KEY: ${ACCESS_SECRET_KEY:?set ACCESS_SECRET_KEY in lens.env} + ARCHIVE_SECRET_KEY: ${ARCHIVE_SECRET_KEY:?set ARCHIVE_SECRET_KEY in lens.env} + LENS_SETUP_CODE: ${LENS_SETUP_CODE:-} + SURREAL_URL: ws://surrealdb:8000 + SURREAL_USER: root + SURREAL_PASS: ${SURREAL_PASS:?set SURREAL_PASS in lens.env} + RUN_BACKGROUND: "false" + FRONTEND_URL: ${FRONTEND_URL:?set FRONTEND_URL in lens.env} + CORS_ORIGINS: '["${FRONTEND_URL}"]' + ARCHIVE_ALLOWED_HOSTS: ${ARCHIVE_ALLOWED_HOSTS:-backend,localhost,127.0.0.1} + MAIL_SERVER: ${MAIL_SERVER:-} + MAIL_PORT: ${MAIL_PORT:-587} + MAIL_USERNAME: ${MAIL_USERNAME:-} + MAIL_PASSWORD: ${MAIL_PASSWORD:-} + MAIL_FROM: ${MAIL_FROM:-} + # Optional answers to the first-run setup (empty means the web app asks) + LENS_ADMIN_EMAIL: ${LENS_ADMIN_EMAIL:-} + LENS_ADMIN_PASSWORD: ${LENS_ADMIN_PASSWORD:-} + LENS_ADMIN_NAME: ${LENS_ADMIN_NAME:-} + LENS_NAMESPACE: ${LENS_NAMESPACE:-} + LENS_LLM_BASE_URL: ${LENS_LLM_BASE_URL:-} + LENS_LLM_MODEL: ${LENS_LLM_MODEL:-} + LENS_LLM_API_KEY: ${LENS_LLM_API_KEY:-} + LENS_LLM_VISION_MODEL: ${LENS_LLM_VISION_MODEL:-} + LENS_SETUP_WIZARD: ${LENS_SETUP_WIZARD:-} + volumes: + - ./archive.yaml:/qnap/archive.yaml:ro + - ${LENS_DATA:?}/archive:/data + - ${AUDIO_DIR:?set AUDIO_DIR in lens.env}:/audio:ro + depends_on: + surrealdb: + condition: service_healthy + restart: unless-stopped + networks: [lens] + +services: + surrealdb: + image: surrealdb/surrealdb:v3.2.4 + command: start --log info --user root --pass ${SURREAL_PASS:?} surrealkv:/data/lens.db + user: root + volumes: + - ${LENS_DATA:?}/surrealdb:/data + healthcheck: + test: ["CMD", "/surreal", "is-ready", "--endpoint", "http://localhost:8000"] + interval: 5s + timeout: 3s + retries: 60 + restart: unless-stopped + networks: [lens] + + backend: + <<: *backend + + worker: + <<: *backend + command: lens worker + healthcheck: + disable: true + + frontend: + image: lens-frontend:qnap-${LENS_VERSION:?} + environment: + API_BASE_URL: http://backend:8000 + AUTH_SECRET: ${AUTH_SECRET:?set AUTH_SECRET in lens.env} + AUTH_TRUST_HOST: "true" + AUTH_URL: ${FRONTEND_URL:?} + ports: + - "${LENS_PORT:-3000}:3000" + depends_on: [backend] + restart: unless-stopped + networks: [lens] + +networks: + lens: + driver: bridge diff --git a/packaging/qnap/qpkg/shared/lens.sh b/packaging/qnap/qpkg/shared/lens.sh new file mode 100755 index 000000000..f45d3134e --- /dev/null +++ b/packaging/qnap/qpkg/shared/lens.sh @@ -0,0 +1,221 @@ +#!/bin/sh +# Lens on a QNAP NAS: starts and stops the Lens containers in Container Station's Docker engine. +# lens.sh start | stop | restart | status | logs [service] | setup-code +# App Center's start/stop buttons and the NAS's boot and shutdown call start and stop. +# +# Everything Lens keeps is in the data folder (Data_Path in /etc/config/qpkg.conf; /share/Container/lens when the +# Container share exists): lens.env (settings and secrets, written on the first start), surrealdb/ (the database) and +# archive/ (uploads, media, derived files). Removing the app leaves it in place, so reinstalling picks up where it was. + +CONF=/etc/config/qpkg.conf +QPKG_NAME=Lens +QPKG_ROOT=$(/sbin/getcfg $QPKG_NAME Install_Path -f $CONF) +LENS_VERSION=$(/sbin/getcfg $QPKG_NAME Version -f $CONF) +COMPOSE_FILE="$QPKG_ROOT/docker-compose.yml" +IMAGES_DIR="$QPKG_ROOT/images" +export QNAP_QPKG=$QPKG_NAME + +say() { echo "$QPKG_NAME: $*"; } +syslog() { # the QTS system log (Control Panel > System Logs), where the first-admin setup code is easiest to find + say "$1" + /sbin/write_log "[$QPKG_NAME] $1" "${2:-4}" 2>/dev/null || /sbin/log_tool -t "${3:-0}" -a "[$QPKG_NAME] $1" 2>/dev/null || true +} +fail() { syslog "$1" 1 2; exit 1; } + +# --- Container Station ------------------------------------------------------------------------------------------ +CS_ROOT=$(/sbin/getcfg container-station Install_Path -f $CONF) +export PATH="$CS_ROOT/bin:$CS_ROOT/usr/bin:$PATH" +DOCKER=$(command -v docker || echo "$CS_ROOT/bin/docker") + +compose() { + if "$DOCKER" compose version >/dev/null 2>&1; then + "$DOCKER" compose -p lens -f "$COMPOSE_FILE" --env-file "$LENS_DATA/lens.env" "$@" + elif command -v docker-compose >/dev/null 2>&1; then + docker-compose -p lens -f "$COMPOSE_FILE" --env-file "$LENS_DATA/lens.env" "$@" + else + fail "Docker Compose was not found in Container Station ($CS_ROOT). Update Container Station to version 3." + fi +} + +wait_for_docker() { # at boot, Container Station may still be starting + i=0 + until "$DOCKER" info >/dev/null 2>&1; do + i=$((i + 1)) + [ $i -gt 90 ] && fail "Container Station's Docker engine is not running. Start Container Station, then Lens." + sleep 2 + done +} + +# --- The data folder and lens.env ------------------------------------------------------------------------------- +data_path() { + p=$(/sbin/getcfg $QPKG_NAME Data_Path -f $CONF) + if [ -z "$p" ]; then + if [ -d /share/Container ]; then p=/share/Container/lens; else p="$(dirname "$QPKG_ROOT")/Lens-data"; fi + /sbin/setcfg $QPKG_NAME Data_Path "$p" -f $CONF + fi + echo "$p" +} +LENS_DATA=$(data_path) + +random_hex() { openssl rand -hex "$1" 2>/dev/null || head -c "$1" /dev/urandom | od -An -tx1 | tr -d ' \n'; } + +lan_address() { + a=$(ip -4 route get 1.1.1.1 2>/dev/null | sed -n 's/.* src \([0-9.]*\).*/\1/p' | head -n 1) + [ -n "$a" ] || a=$(ifconfig 2>/dev/null | sed -n 's/.*inet addr:\([0-9.]*\).*/\1/p' | grep -v '^127\.' | head -n 1) + [ -n "$a" ] || a=$(hostname) + echo "$a" +} + +write_env() { + host=$(lan_address) + hosts=backend + for h in localhost 127.0.0.1 "$host" "$(hostname)"; do + case ",$hosts," in *",$h,"*) ;; *) hosts="$hosts,$h" ;; esac + done + if [ -d /share/Multimedia ]; then audio=/share/Multimedia/Lens; else audio="$LENS_DATA/media"; fi + umask 077 + cat > "$LENS_DATA/lens.env" < " per line. Each image is loaded once; its file is then deleted to give +# the space back (an upgrade brings new ones). This package's images are tagged qnap-; earlier versions' are +# removed after an upgrade, and other Lens images in Docker (built by hand, say) are left alone. +load_images() { + [ -f "$IMAGES_DIR/manifest" ] || return 0 + while read -r ref file; do + [ -n "$ref" ] || continue + if ! "$DOCKER" image inspect "$ref" >/dev/null 2>&1; then + [ -f "$IMAGES_DIR/$file" ] || fail "The image $ref is missing and its file is gone. Reinstall Lens from the .qpkg file." + say "loading $ref (first start after install or upgrade; this takes a few minutes)" + "$DOCKER" load -i "$IMAGES_DIR/$file" >/dev/null || fail "Could not load $ref into Container Station (is the volume full?)." + fi + rm -f "$IMAGES_DIR/$file" + done < "$IMAGES_DIR/manifest" + for repo in lens-backend lens-frontend; do + "$DOCKER" image ls --format '{{.Repository}}:{{.Tag}}' "$repo" 2>/dev/null | grep ":qnap-" | grep -v ":qnap-$LENS_VERSION\$" | while read -r old; do + "$DOCKER" image rm "$old" >/dev/null 2>&1 || true + done + done +} + +# --- Commands --------------------------------------------------------------------------------------------------- +export LENS_VERSION LENS_DATA +FIRST_START="" + +case "$1" in + start) + ENABLED=$(/sbin/getcfg $QPKG_NAME Enable -u -d FALSE -f $CONF) + if [ "$ENABLED" != "TRUE" ]; then + say "disabled in the App Center." + exit 1 + fi + wait_for_docker + prepare_data + load_images + compose up -d --remove-orphans || fail "The containers did not start. Details: $QPKG_ROOT/lens.sh logs" + if [ -n "$FIRST_START" ]; then + syslog "Open $(env_value FRONTEND_URL) and create the first admin account with setup code $(env_value LENS_SETUP_CODE). Settings: $LENS_DATA/lens.env" + else + say "running at $(env_value FRONTEND_URL)" + fi + ;; + + stop) + "$DOCKER" info >/dev/null 2>&1 || exit 0 # Container Station already stopped (shutdown): its containers are too + [ -f "$LENS_DATA/lens.env" ] && compose down --remove-orphans + ;; + + restart) + $0 stop + $0 start + ;; + + status) + compose ps + ;; + + logs) + shift + compose logs --tail 200 -f "$@" + ;; + + setup-code) + echo "Open $(env_value FRONTEND_URL) and create the first admin with setup code: $(env_value LENS_SETUP_CODE)" + echo "(It works only until the first account exists.)" + ;; + + remove) + # Called by the uninstaller: stop the stack and drop its images. The data folder stays. + if "$DOCKER" info >/dev/null 2>&1; then + [ -f "$LENS_DATA/lens.env" ] && compose down --remove-orphans + for repo in lens-backend lens-frontend; do + "$DOCKER" image ls --format '{{.Repository}}:{{.Tag}}' "$repo" 2>/dev/null | grep ":qnap-" | while read -r img; do + "$DOCKER" image rm "$img" >/dev/null 2>&1 || true + done + done + fi + syslog "Removed. The data folder $LENS_DATA was kept; delete it to remove the database and archive too." + ;; + + *) + echo "Usage: $0 {start|stop|restart|status|logs [service]|setup-code}" + exit 1 + ;; +esac + +exit 0