Files
00c89c1b48 feat(printer): badge print queue, Pi worker, captive-portal setup, admin printing view
- migration 20261018170000: pgmq badge_print/badge_print_dlq, print_stations,
  token-gated printer RPCs (claim/complete/fail/heartbeat), admin RPCs,
  welcome_checkin enqueue; badge_print_tests.sql (101 assertions)
- apps/printer: Python worker (Playwright render -> brother_ql via pyusb),
  comitup captive-portal (Wi-Fi + HTTPS endpoint + anon key + token),
  arm64 image build + idempotent Pi setup/install scripts, 77 pytest
- apps/admin: Printing view, station management/DLQ, global printer status
  pill, Print button on Registrations, standalone print renderer entry
  (print.html -> dist-print) with bundled fonts
- docs: printing.md + architecture/schema/rpcs/tests/admin-app updates,
  apps/printer/AGENT.md operating manual
- e2e: printing.spec.ts

Co-authored-by: Stephan Charles <[email protected]>
Co-authored-by: Amelia Hernandez <[email protected]>
Co-authored-by: Markus Franz <[email protected]>
Co-authored-by: Sabrina Hota <[email protected]>
Co-authored-by: Nilüfer Doğan <[email protected]>
2026-10-18 22:25:38 +02:00

299 lines
12 KiB
Bash
Executable File

#!/usr/bin/env bash
# Convivo badge printer — Raspberry Pi 4 provisioning (Raspberry Pi OS / Debian trixie, arm64).
# Usage (on the Pi): sudo bash setup.sh
# Idempotent: safe to re-run for updates. Fill a copy as setup.local.sh (git-ignored); never commit the token.
set -euo pipefail
# ---- Configuration -----------------------------------------------------------
# Empty CONVIVO_URL / CONVIVO_ANON_KEY / PRINTER_STATION_TOKEN keep the values already
# in $INSTALL_DIR/.env (e.g. configured through the portal), so re-runs never reset them.
: "${CONVIVO_URL:=}"
# Public anon key (same value baked into the web apps); not a secret.
: "${CONVIVO_ANON_KEY:=}"
# Station token from Admin → Printing → "Add station" (shown once). Required once, via this
# variable or the portal.
: "${PRINTER_STATION_TOKEN:=}"
: "${STATION_NAME:=Welcome desk}"
: "${BADGE_CHECKIN_ORIGIN:=https://admin.appe.at}"
: "${BROTHER_MODEL:=QL-1110NWB}"
: "${BROTHER_LABEL:=auto}"
: "${PRINTER_IDENTIFIER:=usb://0x04f9:0x20a8}"
# 1 = print "ready" / "no network" labels on the printer (costs media).
: "${STATUS_LABELS:=0}"
: "${PI_HOSTNAME:=convivo-printer}"
: "${TIMEZONE:=Europe/Vienna}"
# comitup replaces <nnnn> with a persistent per-device number.
: "${AP_NAME:=Convivo-Printer-<nnnn>}"
: "${IMAGE_TAR:=$(dirname "$(readlink -f "$0")")/convivo-printer-image.tar.gz}"
: "${INSTALL_DIR:=/opt/convivo-printer}"
# Stages: 1 = run, 0 = skip.
: "${STAGE_WIFI_PORTAL:=1}"
: "${STAGE_APP:=1}"
: "${REBOOT:=0}"
log() { printf '\n\033[1;34m==> %s\033[0m\n' "$*"; }
warn() { printf '\033[1;33m[warn] %s\033[0m\n' "$*" >&2; }
die() { printf '\033[1;31m[fail] %s\033[0m\n' "$*" >&2; exit 1; }
export DEBIAN_FRONTEND=noninteractive
APT_OPTS=(-y -o Dpkg::Options::=--force-confdef -o Dpkg::Options::=--force-confold)
preflight() {
log "Preflight"
[[ $EUID -eq 0 ]] || die "run as root: sudo bash $0"
[[ "$(uname -m)" == "aarch64" ]] || die "expected aarch64, got $(uname -m)"
. /etc/os-release
[[ "${ID:-}" == "debian" || "${ID_LIKE:-}" == *debian* ]] || die "unsupported OS: ${PRETTY_NAME:-unknown}"
echo "OS: $PRETTY_NAME · $(tr -d '\0' </proc/device-tree/model 2>/dev/null || true)"
curl -fsS --max-time 10 -o /dev/null https://deb.debian.org/ || die "no internet access (needed for apt/Docker)"
}
base_system() {
log "Base system"
if [[ "$(hostname)" != "$PI_HOSTNAME" ]]; then
hostnamectl set-hostname "$PI_HOSTNAME"
sed -i "s/^127\.0\.1\.1.*/127.0.1.1\t$PI_HOSTNAME/" /etc/hosts
grep -q "^127\.0\.1\.1" /etc/hosts || printf '127.0.1.1\t%s\n' "$PI_HOSTNAME" >>/etc/hosts
fi
timedatectl set-timezone "$TIMEZONE"
# No RTC on the Pi 4: TLS to Convivo fails until the clock is synced.
systemctl enable --now systemd-timesyncd
apt-get update
apt-get "${APT_OPTS[@]}" full-upgrade
apt-get "${APT_OPTS[@]}" install ca-certificates curl gnupg jq usbutils iw
}
reliability() {
log "Reliability (watchdog, Wi-Fi power save, logs, no auto-upgrades)"
install -d /etc/systemd/system.conf.d /etc/systemd/journald.conf.d /etc/NetworkManager/conf.d
local before
before=$(sha256sum /etc/systemd/system.conf.d/convivo-watchdog.conf 2>/dev/null || true)
cat >/etc/systemd/system.conf.d/convivo-watchdog.conf <<'EOF'
[Manager]
RuntimeWatchdogSec=15s
RebootWatchdogSec=2min
EOF
cat >/etc/systemd/journald.conf.d/convivo.conf <<'EOF'
[Journal]
SystemMaxUse=100M
EOF
cat >/etc/NetworkManager/conf.d/convivo-wifi-powersave.conf <<'EOF'
[connection]
wifi.powersave = 2
EOF
if [[ "$before" != "$(sha256sum /etc/systemd/system.conf.d/convivo-watchdog.conf)" ]]; then
systemctl daemon-reexec
# comitup keeps a D-Bus proxy to the old systemd; without a restart its hotspot never starts DHCP.
systemctl try-restart comitup.service
fi
systemctl restart systemd-journald
# Avoid surprise upgrades/reboots during an event; re-run this script to update.
systemctl disable --now apt-daily.timer apt-daily-upgrade.timer 2>/dev/null || true
}
install_docker() {
log "Docker Engine"
if ! command -v docker >/dev/null || ! docker compose version >/dev/null 2>&1; then
install -m 0755 -d /etc/apt/keyrings
curl -fsSL https://download.docker.com/linux/debian/gpg -o /etc/apt/keyrings/docker.asc
chmod a+r /etc/apt/keyrings/docker.asc
. /etc/os-release
cat >/etc/apt/sources.list.d/docker.sources <<EOF
Types: deb
URIs: https://download.docker.com/linux/debian
Suites: ${VERSION_CODENAME}
Components: stable
Architectures: $(dpkg --print-architecture)
Signed-By: /etc/apt/keyrings/docker.asc
EOF
apt-get update
apt-get "${APT_OPTS[@]}" install docker-ce docker-ce-cli containerd.io docker-buildx-plugin docker-compose-plugin
fi
install -d /etc/docker
local before
before=$(sha256sum /etc/docker/daemon.json 2>/dev/null || true)
cat >/etc/docker/daemon.json <<'EOF'
{
"log-driver": "json-file",
"log-opts": { "max-size": "10m", "max-file": "3" }
}
EOF
systemctl enable docker
# A restart bounces the running worker on its old image, so only restart on config changes.
if [[ "$before" != "$(sha256sum /etc/docker/daemon.json)" ]]; then
systemctl restart docker
fi
if [[ -n "${SUDO_USER:-}" && "$SUDO_USER" != root ]]; then
usermod -aG docker "$SUDO_USER"
fi
}
usb_printer() {
log "USB printer access"
# brother_ql talks raw USB via libusb; the kernel printer driver would hold the device.
echo "blacklist usblp" >/etc/modprobe.d/convivo-no-usblp.conf
modprobe -r usblp 2>/dev/null || true
# ipp-usb claims the QL-1110NWB's IPP-over-USB interfaces (libusb then fails with "Resource busy").
if systemctl list-unit-files ipp-usb.service >/dev/null 2>&1; then
systemctl disable --now ipp-usb.service 2>/dev/null || true
systemctl mask ipp-usb.service
fi
if lsusb -d 04f9: >/dev/null; then
lsusb -d 04f9:
else
warn "no Brother printer detected on USB (connect it and power it on)"
fi
}
env_get() { # env_get KEY FILE -> value of the last KEY=... line (empty if absent)
awk -F= -v k="$1" '$1 == k { sub(/^[^=]*=/, ""); v=$0 } END { print v }' "$2" 2>/dev/null || true
}
install_app() {
log "Convivo printer worker"
if [[ $STAGE_APP != 1 ]]; then echo "skipped (STAGE_APP=0)"; return; fi
install -d -m 0755 "$INSTALL_DIR"
local envf="$INSTALL_DIR/.env" old=""
[[ -f $envf ]] && old=$(cat "$envf")
# Empty setup variables keep what the portal (or an earlier run) already stored.
if [[ -n $old ]]; then
[[ -n $CONVIVO_URL ]] || CONVIVO_URL=$(env_get CONVIVO_URL "$envf")
[[ -n $CONVIVO_ANON_KEY ]] || CONVIVO_ANON_KEY=$(env_get CONVIVO_ANON_KEY "$envf")
[[ -n $PRINTER_STATION_TOKEN ]] || PRINTER_STATION_TOKEN=$(env_get PRINTER_STATION_TOKEN "$envf")
fi
: "${CONVIVO_URL:=https://convivo.appe.at}"
if [[ -f $IMAGE_TAR && -z $PRINTER_STATION_TOKEN ]]; then
die "PRINTER_STATION_TOKEN is empty (create a station in Admin → Printing, or use the portal)"
fi
[[ -n $CONVIVO_ANON_KEY ]] || warn "CONVIVO_ANON_KEY is empty — set it in the portal or the worker cannot reach Convivo"
umask 077
cat >"$envf" <<EOF
CONVIVO_URL=$CONVIVO_URL
CONVIVO_ANON_KEY=$CONVIVO_ANON_KEY
PRINTER_STATION_TOKEN=$PRINTER_STATION_TOKEN
STATION_NAME=$STATION_NAME
BADGE_CHECKIN_ORIGIN=$BADGE_CHECKIN_ORIGIN
BROTHER_MODEL=$BROTHER_MODEL
BROTHER_LABEL=$BROTHER_LABEL
PRINTER_IDENTIFIER=$PRINTER_IDENTIFIER
STATUS_LABELS=$STATUS_LABELS
EOF
umask 022
cat >"$INSTALL_DIR/docker-compose.yml" <<'EOF'
services:
printer:
image: convivo-printer:latest
container_name: convivo-printer
restart: always
network_mode: host
env_file: .env
devices:
- /dev/bus/usb:/dev/bus/usb
# USB char devices (major 189) so a replugged printer is reachable without a restart.
device_cgroup_rules:
- "c 189:* rmw"
volumes:
- /run/convivo:/run/convivo:ro
tmpfs:
- /tmp
shm_size: 256m
logging:
driver: json-file
options: { max-size: "10m", max-file: "3" }
EOF
local loaded
if [[ -f $IMAGE_TAR ]]; then
loaded=$(docker load -i "$IMAGE_TAR" | awk -F': ' '/Loaded image/ {print $2; exit}')
[[ -n $loaded ]] || die "docker load did not report an image"
[[ $loaded == convivo-printer:latest ]] || docker tag "$loaded" convivo-printer:latest
docker image prune -f >/dev/null
elif docker image inspect convivo-printer:latest >/dev/null 2>&1; then
warn "image tar not found — reusing convivo-printer:latest already on the Pi"
else
warn "no image tar and no convivo-printer:latest on the Pi — wrote .env/compose, worker not started"
return
fi
# Compose recreates the container only when the config or image actually changed.
docker compose -f "$INSTALL_DIR/docker-compose.yml" up -d --remove-orphans
}
wifi_portal() {
log "Wi-Fi provisioning portal (comitup)"
if [[ $STAGE_WIFI_PORTAL != 1 ]]; then echo "skipped (STAGE_WIFI_PORTAL=0)"; return; fi
apt-get "${APT_OPTS[@]}" install comitup
install -d /usr/local/lib/convivo
cat >/usr/local/lib/convivo/netstate.sh <<'EOF'
#!/bin/sh
# comitup external_callback ($1 = HOTSPOT|CONNECTING|CONNECTED): publish network state for the worker.
state="${1:-UNKNOWN}"
dir=/run/convivo
mkdir -p "$dir"
ssid=$(iw dev wlan0 info 2>/dev/null | awk '/ssid/ {sub(/^[ \t]*ssid /, ""); print; exit}')
ip=$(ip -4 -o addr show scope global 2>/dev/null | awk '{split($4, a, "/"); print a[1]; exit}')
jq -n --arg state "$state" --arg ssid "$ssid" --arg ip "$ip" --arg at "$(date -Is)" \
'{state: $state, ssid: $ssid, ip: $ip, updated_at: $at}' >"$dir/network.json.tmp"
mv "$dir/network.json.tmp" "$dir/network.json"
EOF
chmod 0755 /usr/local/lib/convivo/netstate.sh
echo "d /run/convivo 0755 root root -" >/etc/tmpfiles.d/convivo.conf
systemd-tmpfiles --create /etc/tmpfiles.d/convivo.conf
# The Convivo portal replaces comitup-web while in HOTSPOT. install.sh copies portal.py
# next to this script; fail clearly if it is missing rather than leave the stock portal.
local portal_src="$(dirname "$(readlink -f "$0")")/portal.py"
[[ -f $portal_src ]] || die "portal.py not found next to setup.sh ($portal_src) — copy it alongside (install.sh does this)"
install -m 0755 -o root -g root "$portal_src" /usr/local/lib/convivo/portal.py
local dropin_dir=/etc/systemd/system/comitup-web.service.d dropin="$dropin_dir/convivo.conf"
install -d -m 0755 "$dropin_dir"
local dropin_before
dropin_before=$(sha256sum "$dropin" 2>/dev/null || true)
cat >"$dropin" <<'EOF'
# Managed by Convivo setup.sh — serve the Convivo portal instead of comitup-web.
[Service]
ExecStart=
ExecStart=/usr/bin/python3 /usr/local/lib/convivo/portal.py
EOF
# A changed drop-in only needs a reload; comitup-web is started on demand in HOTSPOT.
[[ "$dropin_before" != "$(sha256sum "$dropin")" ]] && systemctl daemon-reload
local before
before=$(sha256sum /etc/comitup.conf 2>/dev/null || true)
cat >/etc/comitup.conf <<EOF
# Managed by Convivo setup.sh
ap_name: $AP_NAME
external_callback: /usr/local/lib/convivo/netstate.sh
enable_appliance_mode: false
verbose: 0
EOF
systemctl enable comitup
# A restart re-negotiates Wi-Fi, so only do it when something changed.
if [[ "$before" != "$(sha256sum /etc/comitup.conf)" ]] || ! systemctl is-active --quiet comitup; then
systemctl restart comitup
fi
}
verify() {
log "Verification"
docker --version
docker compose version
systemctl is-active systemd-timesyncd docker >/dev/null && echo "timesyncd + docker active"
[[ -e /dev/watchdog ]] && echo "hardware watchdog present"
lsusb -d 04f9: || warn "Brother printer not detected"
[[ $STAGE_WIFI_PORTAL == 1 ]] && { systemctl is-active comitup || warn "comitup not active"; }
if docker ps --format '{{.Names}}' | grep -qx convivo-printer; then
docker ps --filter name=convivo-printer --format 'worker: {{.Status}}'
fi
if [[ -f /var/run/reboot-required ]]; then
warn "reboot required (kernel/firmware update)"
[[ $REBOOT == 1 ]] && { log "Rebooting"; systemctl reboot; }
fi
}
preflight
base_system
reliability
install_docker
usb_printer
install_app
wifi_portal
verify
log "Done"