#!/usr/bin/env bash # Copy setup (+ worker image when present) to the Pi and run it as root. # Usage: apps/printer/pi/install.sh [ssh-target] (default: convivo-pi) # Env: PI_SUDO_PASSWORD (prompted if unset), STAGE_APP / STAGE_WIFI_PORTAL / REBOOT forwarded to setup.sh. # SKIP_BUILD=1 reuses an existing image tar instead of rebuilding it. # SKIP_IMAGE=1 does not upload the image tar (reuse the image already on the Pi). set -euo pipefail TARGET="${1:-convivo-pi}" DIR="$(cd "$(dirname "$0")" && pwd)" ROOT="$(cd "$DIR/../../.." && pwd)" SETUP="$DIR/setup.local.sh" [[ -f $SETUP ]] || SETUP="$DIR/setup.sh" PORTAL="$ROOT/apps/printer/portal/portal.py" IMAGE="$DIR/convivo-printer-image.tar.gz" # /tmp is a small RAM tmpfs on the Pi; the image tarball needs disk. REMOTE=/var/tmp/convivo-setup if [[ "${SKIP_BUILD:-0}" != 1 && "${SKIP_IMAGE:-0}" != 1 ]]; then # SKIP_IMAGE reuses the Pi's image, so a local build would only be discarded. # Cross-build the arm64 image from the repo root (default docker builder). if ! docker buildx inspect default 2>/dev/null | grep -q linux/arm64; then echo "linux/arm64 not in buildx default builder — installing QEMU binfmt" >&2 docker run --privileged --rm tonistiigi/binfmt --install arm64 fi echo "Building convivo-printer image (arm64) → $IMAGE" ( cd "$ROOT" docker buildx build --platform linux/arm64 \ -f apps/printer/Dockerfile \ --build-arg "APP_VERSION=$(git rev-parse --short HEAD)-$(date +%Y%m%d%H%M)" \ -t convivo-printer:latest \ --output "type=docker,dest=-" . | gzip > "$IMAGE" ) fi ssh "$TARGET" "mkdir -p $REMOTE" scp -q "$SETUP" "$TARGET:$REMOTE/setup.sh" scp -q "$PORTAL" "$TARGET:$REMOTE/portal.py" if [[ "${SKIP_IMAGE:-0}" == 1 ]]; then # Reuse the image already loaded on the Pi; drop any stale tar so setup.sh does not load it. ssh "$TARGET" "rm -f $REMOTE/convivo-printer-image.tar.gz" else [[ -f $IMAGE ]] && scp -q "$IMAGE" "$TARGET:$REMOTE/convivo-printer-image.tar.gz" fi if [[ -z "${PI_SUDO_PASSWORD:-}" ]]; then read -rsp "sudo password for $TARGET: " PI_SUDO_PASSWORD echo fi STAGE_ENV=() for v in STAGE_APP STAGE_WIFI_PORTAL REBOOT; do [[ -n "${!v:-}" ]] && STAGE_ENV+=("$v=${!v}") done printf '%s\n' "$PI_SUDO_PASSWORD" | ssh "$TARGET" "sudo -S -p '' env ${STAGE_ENV[*]:-} bash $REMOTE/setup.sh"