Files
os/tools/pkg/packages.sh
Tamas Csikel d5be90443d Package-based updates: sf-update, signed shell and app packages
- sf-update (root): lists sf-shell / sf-app-* in the Gitea generic registry,
  downloads, checks the Ed25519 signature and unpacks to /var/lib/sf-pkg,
  switches via current/previous/staged symlinks; the factory copies on the
  read-only root stay as fallback. A new shell is on trial and rolls back
  after 3 failed starts.
- Launcher: apps from factory + installed packages (newer version wins),
  Settings > System > Updates (check, install now), shell trial confirm.
- Image: sf-update units (boot apply, 6-hourly download, launcher requests),
  sf-shell-exec wrapper for sf-remote, factory versions, registry token.
- CI: .gitea/workflows/packages.yml builds, signs and uploads packages whose
  version isn't published yet (tools/pkg/packages.sh).
- docs/updates.md: protocol and layout.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-24 09:27:59 +02:00

123 lines
4.3 KiB
Bash

#!/usr/bin/env bash
# Builds, signs and uploads SmartFox packages (protocol: docs/updates.md).
#
# tools/pkg/packages.sh [--upload] [shell] [app:<id>...] [apps]
#
# With no package names it does the shell and every app. A package whose
# version is already in the registry is skipped (bump the version to publish):
# shell version = [workspace.package] version in Cargo.toml
# app:<id> version = "version" in apps/<id>/app.toml
#
# Environment:
# SF_SIGNING_KEY Ed25519 private key (PEM), or SF_SIGNING_KEY_FILE
# SF_PACKAGE_TOKEN "user:token", or a token of SF_OWNER, with write:package (upload only)
# SF_GITEA default https://gitea.bluefox.support
# SF_OWNER default bluefox
# SF_OUT output folder, default target/packages
set -euo pipefail
ROOT="$(cd "$(dirname "$0")/../.." && pwd)"
GITEA="${SF_GITEA:-https://gitea.bluefox.support}"
OWNER="${SF_OWNER:-bluefox}"
OUT="${SF_OUT:-$ROOT/target/packages}"
# Binaries in the shell package (built with cargo --release).
SHELL_BINS=(sf-launcher sf-remote)
UPLOAD=0
WANT=()
for a in "$@"; do
case "$a" in
--upload) UPLOAD=1 ;;
shell | apps | app:*) WANT+=("$a") ;;
*) echo "unknown argument: $a" >&2; exit 2 ;;
esac
done
[ ${#WANT[@]} -gt 0 ] || WANT=(shell apps)
KEY_FILE="${SF_SIGNING_KEY_FILE:-}"
TMP=$(mktemp -d)
trap 'rm -rf "$TMP"' EXIT
if [ -z "$KEY_FILE" ]; then
[ -n "${SF_SIGNING_KEY:-}" ] || { echo "SF_SIGNING_KEY or SF_SIGNING_KEY_FILE is needed" >&2; exit 2; }
KEY_FILE="$TMP/key.pem"
(umask 077; printf '%s\n' "$SF_SIGNING_KEY" >"$KEY_FILE")
fi
auth() {
case "${SF_PACKAGE_TOKEN:-}" in
*:*) printf -- '-u\n%s\n' "$SF_PACKAGE_TOKEN" ;;
?*) printf -- '-u\n%s:%s\n' "$OWNER" "$SF_PACKAGE_TOKEN" ;;
esac
}
mapfile -t AUTH < <(auth)
url() { echo "$GITEA/api/packages/$OWNER/generic/$1/$2/$1-$2.tar.zst"; }
published() {
[ "$UPLOAD" = 1 ] || return 1
local code
code=$(curl -s -o /dev/null -w '%{http_code}' -I "${AUTH[@]}" "$(url "$1" "$2")")
[ "$code" = 200 ]
}
# pack <name> <version> <dir>: tar.zst + .sig in $OUT, uploaded with --upload.
pack() {
local name=$1 version=$2 dir=$3 file
printf 'name = "%s"\nversion = "%s"\n' "$name" "$version" >"$dir/pkg.toml"
mkdir -p "$OUT"
file="$OUT/$name-$version.tar.zst"
tar --sort=name --owner=0 --group=0 --numeric-owner --mtime=@0 -C "$dir" -cf - . | zstd -q -19 -T0 -f -o "$file"
openssl pkeyutl -sign -inkey "$KEY_FILE" -rawin -in "$file" -out "$file.sig"
echo "built $(basename "$file") ($(du -h "$file" | cut -f1))"
if [ "$UPLOAD" = 1 ]; then
for f in "$file" "$file.sig"; do
curl -fsS "${AUTH[@]}" --upload-file "$f" "$GITEA/api/packages/$OWNER/generic/$name/$version/$(basename "$f")" >/dev/null
done
echo "uploaded $name $version"
fi
}
toml_version() { sed -n 's/^version *= *"\(.*\)".*/\1/p' "$1" | head -1; }
do_shell() {
local version
version=$(sed -n '/^\[workspace.package\]/,/^\[/ s/^version *= *"\(.*\)"/\1/p' "$ROOT/Cargo.toml")
if published sf-shell "$version"; then
echo "sf-shell $version: already published"
return
fi
local args=()
for b in "${SHELL_BINS[@]}"; do args+=(-p "$b"); done
(cd "$ROOT" && cargo build --release --locked "${args[@]}")
local dir="$TMP/sf-shell"
rm -rf "$dir"
mkdir -p "$dir/bin"
for b in "${SHELL_BINS[@]}"; do install -m755 "$ROOT/target/release/$b" "$dir/bin/$b"; done
install -m755 "$ROOT/tools/siri-remote/fastpair.py" "$dir/bin/sf-pair"
pack sf-shell "$version" "$dir"
}
do_app() {
local id=$1 src="$ROOT/apps/$1" version
[ -f "$src/app.toml" ] || { echo "no app $id" >&2; return 1; }
version=$(toml_version "$src/app.toml")
[ -n "$version" ] || { echo "apps/$id/app.toml has no version, skipped"; return; }
if published "sf-app-$id" "$version"; then
echo "sf-app-$id $version: already published"
return
fi
local dir="$TMP/sf-app-$id"
rm -rf "$dir"
cp -r "$src" "$dir"
chmod -R u=rwX,go=rX "$dir"
pack "sf-app-$id" "$version" "$dir"
}
for w in "${WANT[@]}"; do
case "$w" in
shell) do_shell ;;
apps) for d in "$ROOT"/apps/*/; do do_app "$(basename "$d")"; done ;;
app:*) do_app "${w#app:}" ;;
esac
done