feat: session-first workspace activity and installable daemon
Workspaces page: - list the newest sessions across workspaces in the activity column; the daemon exposes GET /api/v1/sessions/recent as a disk projection and the browser enriches live rows from the per-workspace session relay - put recent activity first below 1100px and fix the mobile page-header flex-basis gap Smoke: - pin PI2_HOST=127.0.0.1 in both smoke scripts and make the startup-line assertion follow the configured host; the daemon default stays 0.0.0.0 Install: - standalone binary build/install scripts, packaging templates, CI/release hooks, CLI-first install docs and the supporting config/doctor changes
This commit is contained in:
1 parent
fc51c3be37
commit
d2e20d428a
50 files changed
+5104
-188
No files matched your search
Executable
+195
@@ -0,0 +1,195 @@
|
||||
#!/usr/bin/env bash
|
||||
#
|
||||
# Build a standalone binary distribution of pi2d.
|
||||
#
|
||||
# Produces:
|
||||
# <output-dir>/bin/pi2
|
||||
# <output-dir>/share/pi2d/web/dist/...
|
||||
# <output-dir>/LICENSE
|
||||
# <output-dir>/pi2d-<version>-<os>-<arch>.tar.gz
|
||||
#
|
||||
# Usage:
|
||||
# bash scripts/build-binary.sh [flags]
|
||||
#
|
||||
# Flags:
|
||||
# --target <bun-target> Cross-compile target (e.g. bun-linux-x64, bun-darwin-arm64)
|
||||
# --version <v> Release version (default: version from package.json)
|
||||
# --output-dir <dir> Output directory (default: dist-bin)
|
||||
# --skip-build Reuse existing dist/ and web/dist/
|
||||
# --no-archive Do not produce the .tar.gz archive
|
||||
set -euo pipefail
|
||||
|
||||
ORIG_CWD="$(pwd)"
|
||||
ROOT="$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd)"
|
||||
|
||||
TARGET=""
|
||||
VERSION=""
|
||||
OUTPUT_DIR=""
|
||||
SKIP_BUILD=0
|
||||
NO_ARCHIVE=0
|
||||
|
||||
while [[ $# -gt 0 ]]; do
|
||||
case "$1" in
|
||||
--target)
|
||||
TARGET="$2"
|
||||
shift 2
|
||||
;;
|
||||
--target=*)
|
||||
TARGET="${1#*=}"
|
||||
shift
|
||||
;;
|
||||
--version)
|
||||
VERSION="$2"
|
||||
shift 2
|
||||
;;
|
||||
--version=*)
|
||||
VERSION="${1#*=}"
|
||||
shift
|
||||
;;
|
||||
--output-dir)
|
||||
OUTPUT_DIR="$2"
|
||||
shift 2
|
||||
;;
|
||||
--output-dir=*)
|
||||
OUTPUT_DIR="${1#*=}"
|
||||
shift
|
||||
;;
|
||||
--skip-build)
|
||||
SKIP_BUILD=1
|
||||
shift
|
||||
;;
|
||||
--no-archive)
|
||||
NO_ARCHIVE=1
|
||||
shift
|
||||
;;
|
||||
-h|--help)
|
||||
cat <<EOF
|
||||
Usage: $0 [options]
|
||||
|
||||
Options:
|
||||
--target <target> Bun compilation target (default: host target)
|
||||
Supported: linux-x64, linux-arm64, linux-x64-musl,
|
||||
linux-arm64-musl, darwin-x64, darwin-arm64
|
||||
--version <version> Version string (default: package.json version)
|
||||
--output-dir <dir> Output directory (default: dist-bin)
|
||||
--skip-build Skip tsc and web build if dist/ and web/dist/ exist
|
||||
--no-archive Skip packaging the .tar.gz archive
|
||||
-h, --help Show this help message
|
||||
EOF
|
||||
exit 0
|
||||
;;
|
||||
*)
|
||||
printf 'Error: unknown option: %s\n' "$1" >&2
|
||||
exit 1
|
||||
;;
|
||||
esac
|
||||
done
|
||||
|
||||
cd "$ROOT"
|
||||
|
||||
if [[ -z "$OUTPUT_DIR" ]]; then
|
||||
OUTPUT_DIR="$ROOT/dist-bin"
|
||||
elif [[ "$OUTPUT_DIR" != /* ]]; then
|
||||
OUTPUT_DIR="$ORIG_CWD/$OUTPUT_DIR"
|
||||
fi
|
||||
|
||||
if [[ -z "$VERSION" ]]; then
|
||||
VERSION="$(node -e "console.log(JSON.parse(require('fs').readFileSync('package.json', 'utf8')).version)")"
|
||||
fi
|
||||
|
||||
detect_host_target() {
|
||||
local os arch
|
||||
case "$(uname -s)" in
|
||||
Linux) os="linux" ;;
|
||||
Darwin) os="darwin" ;;
|
||||
*) os="$(uname -s | tr '[:upper:]' '[:lower:]')" ;;
|
||||
esac
|
||||
|
||||
case "$(uname -m)" in
|
||||
x86_64|amd64) arch="x64" ;;
|
||||
aarch64|arm64) arch="arm64" ;;
|
||||
*) arch="$(uname -m)" ;;
|
||||
esac
|
||||
|
||||
if [[ "$os" == "linux" ]]; then
|
||||
if ! getconf GNU_LIBC_VERSION >/dev/null 2>&1; then
|
||||
arch="${arch}-musl"
|
||||
fi
|
||||
fi
|
||||
|
||||
printf '%s-%s\n' "$os" "$arch"
|
||||
}
|
||||
|
||||
if [[ -n "$TARGET" ]]; then
|
||||
TARGET_NAME="${TARGET#bun-}"
|
||||
case "$TARGET_NAME" in
|
||||
linux-x64|linux-arm64|linux-x64-musl|linux-arm64-musl|darwin-x64|darwin-arm64)
|
||||
OS_ARCH="$TARGET_NAME"
|
||||
BUN_TARGET="bun-$TARGET_NAME"
|
||||
;;
|
||||
*)
|
||||
printf 'Error: unsupported target "%s". Supported: linux-x64, linux-arm64, linux-x64-musl, linux-arm64-musl, darwin-x64, darwin-arm64\n' "$TARGET" >&2
|
||||
exit 1
|
||||
;;
|
||||
esac
|
||||
else
|
||||
BUN_TARGET=""
|
||||
OS_ARCH="$(detect_host_target)"
|
||||
fi
|
||||
|
||||
if [[ "$SKIP_BUILD" -eq 1 && -f "dist/cli/pi2.js" && -d "web/dist" ]]; then
|
||||
printf '==> reusing existing dist/ and web/dist/\n'
|
||||
else
|
||||
printf '==> building TypeScript and web UI\n'
|
||||
pnpm build
|
||||
pnpm build:web
|
||||
fi
|
||||
|
||||
[[ -f "dist/cli/pi2.js" ]] || { printf 'Error: dist/cli/pi2.js not found\n' >&2; exit 1; }
|
||||
[[ -d "web/dist" ]] || { printf 'Error: web/dist directory not found\n' >&2; exit 1; }
|
||||
[[ -f "LICENSE" ]] || { printf 'Error: LICENSE file not found\n' >&2; exit 1; }
|
||||
|
||||
ARCHIVE_NAME="pi2d-${VERSION}-${OS_ARCH}.tar.gz"
|
||||
ARCHIVE_PATH="$OUTPUT_DIR/$ARCHIVE_NAME"
|
||||
|
||||
# Clean stale outputs for idempotency
|
||||
rm -rf "${OUTPUT_DIR:?}/bin" "${OUTPUT_DIR:?}/share" "${OUTPUT_DIR:?}/LICENSE"
|
||||
rm -f "$ARCHIVE_PATH"
|
||||
|
||||
mkdir -p "$OUTPUT_DIR/bin"
|
||||
mkdir -p "$OUTPUT_DIR/share/pi2d/web"
|
||||
|
||||
printf '==> compiling binary with bun\n'
|
||||
BUN_CMD=(
|
||||
bun build
|
||||
dist/cli/pi2.js
|
||||
--compile
|
||||
--outfile "$OUTPUT_DIR/bin/pi2"
|
||||
--define "PI2_BUILD_VERSION=\"$VERSION\""
|
||||
)
|
||||
if [[ -n "$BUN_TARGET" ]]; then
|
||||
BUN_CMD+=("--target=$BUN_TARGET")
|
||||
fi
|
||||
|
||||
"${BUN_CMD[@]}"
|
||||
chmod +x "$OUTPUT_DIR/bin/pi2"
|
||||
|
||||
printf '==> copying assets\n'
|
||||
cp -R web/dist "$OUTPUT_DIR/share/pi2d/web/dist"
|
||||
cp LICENSE "$OUTPUT_DIR/LICENSE"
|
||||
|
||||
if [[ "$NO_ARCHIVE" -eq 0 ]]; then
|
||||
printf '==> packaging tarball\n'
|
||||
tar -czf "$ARCHIVE_PATH" -C "$OUTPUT_DIR" bin share LICENSE
|
||||
fi
|
||||
|
||||
BIN_PATH="$OUTPUT_DIR/bin/pi2"
|
||||
BIN_SIZE="$(du -h "$BIN_PATH" | cut -f1)"
|
||||
|
||||
if [[ "$NO_ARCHIVE" -eq 1 ]]; then
|
||||
printf 'Artifact: %s (%s)\n' "$BIN_PATH" "$BIN_SIZE"
|
||||
else
|
||||
ARCHIVE_SIZE="$(du -h "$ARCHIVE_PATH" | cut -f1)"
|
||||
printf 'Binary: %s (%s)\n' "$BIN_PATH" "$BIN_SIZE"
|
||||
printf 'Artifact: %s (%s)\n' "$ARCHIVE_PATH" "$ARCHIVE_SIZE"
|
||||
fi
|
||||
Executable
+543
@@ -0,0 +1,543 @@
|
||||
#!/bin/sh
|
||||
#
|
||||
# pi2 standalone installer
|
||||
#
|
||||
# Installs the precompiled pi2 binary and web console assets with no Node,
|
||||
# no npm, and no root privileges required.
|
||||
#
|
||||
# Quick install:
|
||||
# curl -fsSL https://raw.githubusercontent.com/zgs225/pi2d/main/scripts/install.sh | sh
|
||||
#
|
||||
# Uninstall:
|
||||
# curl -fsSL https://raw.githubusercontent.com/zgs225/pi2d/main/scripts/install.sh | sh -s -- --uninstall
|
||||
#
|
||||
set -eu
|
||||
|
||||
# -----------------------------------------------------------------------------
|
||||
# Configuration & Knobs
|
||||
# -----------------------------------------------------------------------------
|
||||
|
||||
PI2_INSTALL_DIR="${PI2_INSTALL_DIR:-$HOME/.local/lib/pi2d}"
|
||||
PI2_BIN_DIR="${PI2_BIN_DIR:-$HOME/.local/bin}"
|
||||
PI2_SKIP_CHECKSUM="${PI2_SKIP_CHECKSUM:-0}"
|
||||
|
||||
# Strip trailing slashes for clean path manipulation
|
||||
PI2_INSTALL_DIR="${PI2_INSTALL_DIR%/}"
|
||||
PI2_BIN_DIR="${PI2_BIN_DIR%/}"
|
||||
|
||||
staging=""
|
||||
tmp_dir=""
|
||||
|
||||
cleanup() {
|
||||
exit_code=$?
|
||||
trap - EXIT INT TERM
|
||||
if [ -n "$staging" ] && [ -d "$staging" ]; then
|
||||
rm -rf "$staging"
|
||||
fi
|
||||
if [ -n "$tmp_dir" ] && [ -d "$tmp_dir" ]; then
|
||||
rm -rf "$tmp_dir"
|
||||
fi
|
||||
exit "$exit_code"
|
||||
}
|
||||
trap cleanup EXIT INT TERM
|
||||
|
||||
show_help() {
|
||||
cat << 'EOF'
|
||||
Usage: install.sh [OPTIONS]
|
||||
|
||||
Installs the compiled pi2 binary and web UI with no Node, no npm, and no root.
|
||||
|
||||
Options:
|
||||
--uninstall, -u Uninstall pi2 and remove binary symlink
|
||||
--help, -h Show this help message
|
||||
|
||||
Environment variables:
|
||||
PI2_VERSION Version to install (default: latest release tag from GitHub)
|
||||
PI2_INSTALL_DIR Installation directory (default: $HOME/.local/lib/pi2d)
|
||||
PI2_BIN_DIR Directory for executable symlink (default: $HOME/.local/bin)
|
||||
PI2_BASE_URL Base URL for release assets (default: GitHub releases URL)
|
||||
PI2_ARCHIVE Path to a local tarball (skips network download)
|
||||
PI2_SKIP_CHECKSUM Set to 1 to bypass SHA256 checksum verification
|
||||
PI2_CHECKSUM_FILE Explicit path to SHA256SUMS file
|
||||
EOF
|
||||
}
|
||||
|
||||
print_supported_platforms() {
|
||||
printf 'Supported platforms:\n' >&2
|
||||
printf ' - linux-x64\n' >&2
|
||||
printf ' - linux-arm64\n' >&2
|
||||
printf ' - linux-x64-musl\n' >&2
|
||||
printf ' - linux-arm64-musl\n' >&2
|
||||
printf ' - darwin-x64\n' >&2
|
||||
printf ' - darwin-arm64\n' >&2
|
||||
}
|
||||
|
||||
# -----------------------------------------------------------------------------
|
||||
# Platform Detection
|
||||
# -----------------------------------------------------------------------------
|
||||
|
||||
detect_platform() {
|
||||
raw_os="$(uname -s)"
|
||||
case "$raw_os" in
|
||||
Linux) os="linux" ;;
|
||||
Darwin) os="darwin" ;;
|
||||
*)
|
||||
printf 'Error: Unsupported operating system: %s\n' "$raw_os" >&2
|
||||
print_supported_platforms
|
||||
exit 1
|
||||
;;
|
||||
esac
|
||||
|
||||
raw_arch="$(uname -m)"
|
||||
case "$raw_arch" in
|
||||
x86_64|amd64) arch="x64" ;;
|
||||
aarch64|arm64) arch="arm64" ;;
|
||||
*)
|
||||
printf 'Error: Unsupported architecture: %s\n' "$raw_arch" >&2
|
||||
print_supported_platforms
|
||||
exit 1
|
||||
;;
|
||||
esac
|
||||
|
||||
is_musl=0
|
||||
if [ "$os" = "linux" ]; then
|
||||
if [ -f /etc/alpine-release ]; then
|
||||
is_musl=1
|
||||
elif command -v ldd >/dev/null 2>&1 && ldd --version 2>&1 | grep -i musl >/dev/null 2>&1; then
|
||||
is_musl=1
|
||||
elif ls /lib/ld-musl-*.so* >/dev/null 2>&1 || ls /usr/lib/ld-musl-*.so* >/dev/null 2>&1; then
|
||||
is_musl=1
|
||||
fi
|
||||
fi
|
||||
}
|
||||
|
||||
# -----------------------------------------------------------------------------
|
||||
# HTTP / Download Helpers
|
||||
# -----------------------------------------------------------------------------
|
||||
|
||||
fetch_text() {
|
||||
url="$1"
|
||||
if command -v curl >/dev/null 2>&1; then
|
||||
curl -fsSL -H "Accept: application/vnd.github+json" "$url"
|
||||
elif command -v wget >/dev/null 2>&1; then
|
||||
wget -qO- --header="Accept: application/vnd.github+json" "$url"
|
||||
else
|
||||
printf 'Error: Neither curl nor wget was found. Please install curl or wget.\n' >&2
|
||||
exit 1
|
||||
fi
|
||||
}
|
||||
|
||||
download_file() {
|
||||
url="$1"
|
||||
dest="$2"
|
||||
if command -v curl >/dev/null 2>&1; then
|
||||
curl -fsSL "$url" -o "$dest"
|
||||
elif command -v wget >/dev/null 2>&1; then
|
||||
wget -qO "$dest" "$url"
|
||||
else
|
||||
printf 'Error: Neither curl nor wget was found. Please install curl or wget.\n' >&2
|
||||
exit 1
|
||||
fi
|
||||
}
|
||||
|
||||
# -----------------------------------------------------------------------------
|
||||
# Checksum Verification
|
||||
# -----------------------------------------------------------------------------
|
||||
|
||||
compute_sha256() {
|
||||
target_file="$1"
|
||||
if command -v sha256sum >/dev/null 2>&1; then
|
||||
sha256sum "$target_file" | awk '{print $1}'
|
||||
elif command -v shasum >/dev/null 2>&1; then
|
||||
shasum -a 256 "$target_file" | awk '{print $1}'
|
||||
elif command -v openssl >/dev/null 2>&1; then
|
||||
openssl dgst -sha256 "$target_file" | awk '{print $NF}'
|
||||
else
|
||||
printf 'Error: Neither sha256sum, shasum, nor openssl is available.\n' >&2
|
||||
printf 'Install sha256sum/shasum or set PI2_SKIP_CHECKSUM=1 to bypass.\n' >&2
|
||||
exit 1
|
||||
fi
|
||||
}
|
||||
|
||||
verify_checksum() {
|
||||
file_to_check="$1"
|
||||
checksum_file="$2"
|
||||
require_entry="${3:-1}"
|
||||
file_name="$(basename "$file_to_check")"
|
||||
|
||||
expected="$(awk -v fname="$file_name" -v fpath="$file_to_check" '{
|
||||
if (NF == 1 && length($1) == 64) { print $1; exit }
|
||||
name = $2;
|
||||
sub(/^\*/, "", name);
|
||||
bname = name;
|
||||
sub(/.*\//, "", bname);
|
||||
if (name == fname || name == fpath || bname == fname) { print $1; exit }
|
||||
}' "$checksum_file")"
|
||||
|
||||
if [ -z "$expected" ]; then
|
||||
if [ "$require_entry" -eq 1 ]; then
|
||||
printf 'Error: No SHA256 checksum entry found for %s in %s\n' "$file_name" "$checksum_file" >&2
|
||||
exit 1
|
||||
else
|
||||
printf 'info: no checksum entry for %s found in %s; skipping checksum verification\n' "$file_name" "$checksum_file"
|
||||
return 0
|
||||
fi
|
||||
fi
|
||||
|
||||
actual="$(compute_sha256 "$file_to_check")"
|
||||
|
||||
expected="$(printf '%s' "$expected" | tr '[:upper:]' '[:lower:]')"
|
||||
actual="$(printf '%s' "$actual" | tr '[:upper:]' '[:lower:]')"
|
||||
|
||||
if [ "$actual" != "$expected" ]; then
|
||||
printf 'Error: SHA256 checksum verification failed for %s\n' "$file_to_check" >&2
|
||||
printf ' Expected: %s\n' "$expected" >&2
|
||||
printf ' Actual: %s\n' "$actual" >&2
|
||||
exit 1
|
||||
fi
|
||||
|
||||
printf 'Checksum verified: %s\n' "$actual"
|
||||
}
|
||||
|
||||
# -----------------------------------------------------------------------------
|
||||
# Parse Arguments
|
||||
# -----------------------------------------------------------------------------
|
||||
|
||||
uninstall_mode=0
|
||||
for arg in "$@"; do
|
||||
case "$arg" in
|
||||
--uninstall|-u|uninstall)
|
||||
uninstall_mode=1
|
||||
;;
|
||||
--help|-h|help)
|
||||
show_help
|
||||
exit 0
|
||||
;;
|
||||
*)
|
||||
printf 'Error: Unknown argument: %s\n\n' "$arg" >&2
|
||||
show_help
|
||||
exit 1
|
||||
;;
|
||||
esac
|
||||
done
|
||||
|
||||
detect_platform
|
||||
|
||||
# -----------------------------------------------------------------------------
|
||||
# Uninstall Mode
|
||||
# -----------------------------------------------------------------------------
|
||||
|
||||
if [ "$uninstall_mode" -eq 1 ]; then
|
||||
printf 'Uninstalling pi2...\n'
|
||||
removed_any=0
|
||||
|
||||
if [ -d "$PI2_INSTALL_DIR" ]; then
|
||||
rm -rf "$PI2_INSTALL_DIR"
|
||||
printf ' - Removed install directory: %s\n' "$PI2_INSTALL_DIR"
|
||||
removed_any=1
|
||||
fi
|
||||
|
||||
if [ -e "$PI2_BIN_DIR/pi2" ] || [ -L "$PI2_BIN_DIR/pi2" ]; then
|
||||
rm -f "$PI2_BIN_DIR/pi2"
|
||||
printf ' - Removed binary symlink: %s\n' "$PI2_BIN_DIR/pi2"
|
||||
removed_any=1
|
||||
fi
|
||||
|
||||
if [ "$removed_any" -eq 0 ]; then
|
||||
printf 'No existing installation found at %s or %s/pi2.\n' "$PI2_INSTALL_DIR" "$PI2_BIN_DIR"
|
||||
else
|
||||
printf 'pi2 uninstalled successfully.\n'
|
||||
fi
|
||||
|
||||
# Report untouched directories
|
||||
if [ -n "${PI2_DATA_DIR:-}" ]; then
|
||||
disp_data="$PI2_DATA_DIR"
|
||||
elif [ "$os" = "darwin" ]; then
|
||||
disp_data="$HOME/Library/Application Support/pi2"
|
||||
else
|
||||
disp_data="${XDG_DATA_HOME:-$HOME/.local/share}/pi2"
|
||||
fi
|
||||
|
||||
if [ -n "${PI2_RUNTIME_DIR:-}" ]; then
|
||||
disp_runtime="$PI2_RUNTIME_DIR"
|
||||
elif [ "$os" = "darwin" ]; then
|
||||
disp_runtime="$HOME/Library/Caches/pi2/run"
|
||||
else
|
||||
uid="$(id -u 2>/dev/null || echo 0)"
|
||||
disp_runtime="${XDG_RUNTIME_DIR:-${TMPDIR:-/tmp}/pi2-${uid}}/pi2"
|
||||
fi
|
||||
|
||||
if [ -n "${PI2_STATE_DIR:-}" ]; then
|
||||
disp_state="$PI2_STATE_DIR"
|
||||
elif [ "$os" = "darwin" ]; then
|
||||
disp_state="$HOME/Library/Logs/pi2"
|
||||
else
|
||||
disp_state="${XDG_STATE_HOME:-$HOME/.local/state}/pi2"
|
||||
fi
|
||||
|
||||
printf '\nThe following runtime, data, and state directories were left untouched:\n'
|
||||
printf ' - Runtime: %s\n' "$disp_runtime"
|
||||
printf ' - Data: %s\n' "$disp_data"
|
||||
printf ' - State: %s\n' "$disp_state"
|
||||
exit 0
|
||||
fi
|
||||
|
||||
# -----------------------------------------------------------------------------
|
||||
# Install Mode: Resolve Artifact
|
||||
# -----------------------------------------------------------------------------
|
||||
|
||||
archive_path=""
|
||||
target=""
|
||||
version=""
|
||||
|
||||
if [ -n "${PI2_ARCHIVE:-}" ]; then
|
||||
# Offline install path
|
||||
if [ ! -f "$PI2_ARCHIVE" ]; then
|
||||
printf 'Error: Local archive not found: %s\n' "$PI2_ARCHIVE" >&2
|
||||
exit 1
|
||||
fi
|
||||
archive_path="$PI2_ARCHIVE"
|
||||
archive_name="$(basename "$archive_path")"
|
||||
|
||||
if [ -n "${PI2_VERSION:-}" ]; then
|
||||
version="${PI2_VERSION#v}"
|
||||
else
|
||||
case "$archive_name" in
|
||||
pi2d-*-*)
|
||||
v="${archive_name#pi2d-}"
|
||||
version="${v%%-*}"
|
||||
;;
|
||||
*)
|
||||
version="custom"
|
||||
;;
|
||||
esac
|
||||
fi
|
||||
|
||||
case "$archive_name" in
|
||||
pi2d-*-*.tar.gz)
|
||||
t="${archive_name#pi2d-}"
|
||||
t="${t#*-}"
|
||||
target="${t%.tar.gz}"
|
||||
;;
|
||||
*)
|
||||
target="${os}-${arch}"
|
||||
;;
|
||||
esac
|
||||
|
||||
if [ "$PI2_SKIP_CHECKSUM" = "1" ]; then
|
||||
printf 'info: skipping checksum verification (PI2_SKIP_CHECKSUM=1)\n'
|
||||
else
|
||||
sums_file=""
|
||||
require_entry=1
|
||||
if [ -n "${PI2_CHECKSUM_FILE:-}" ] && [ -f "$PI2_CHECKSUM_FILE" ]; then
|
||||
sums_file="$PI2_CHECKSUM_FILE"
|
||||
require_entry=1
|
||||
elif [ -f "${archive_path}.sha256" ]; then
|
||||
sums_file="${archive_path}.sha256"
|
||||
require_entry=1
|
||||
elif [ -f "$(dirname "$archive_path")/SHA256SUMS" ]; then
|
||||
sums_file="$(dirname "$archive_path")/SHA256SUMS"
|
||||
require_entry=0
|
||||
fi
|
||||
|
||||
if [ -n "$sums_file" ]; then
|
||||
verify_checksum "$archive_path" "$sums_file" "$require_entry"
|
||||
else
|
||||
printf 'info: no SHA256SUMS found next to archive; skipping checksum verification\n'
|
||||
fi
|
||||
fi
|
||||
else
|
||||
# Online install path
|
||||
tmp_dir="$(mktemp -d "${TMPDIR:-/tmp}/pi2d-install.XXXXXX")"
|
||||
|
||||
if [ -n "${PI2_VERSION:-}" ]; then
|
||||
version="${PI2_VERSION#v}"
|
||||
else
|
||||
printf 'Querying latest release from GitHub...\n'
|
||||
latest_api_url="https://api.github.com/repos/zgs225/pi2d/releases/latest"
|
||||
release_json=""
|
||||
if ! release_json="$(fetch_text "$latest_api_url" 2>/dev/null)"; then
|
||||
printf 'Error: Failed to fetch latest release from %s\n' "$latest_api_url" >&2
|
||||
printf 'The GitHub API may be unreachable or rate-limited.\n' >&2
|
||||
printf 'Specify a version explicitly: PI2_VERSION=<version> %s\n' "$0" >&2
|
||||
exit 1
|
||||
fi
|
||||
|
||||
tag=""
|
||||
if command -v jq >/dev/null 2>&1; then
|
||||
tag="$(printf '%s' "$release_json" | jq -r .tag_name 2>/dev/null || true)"
|
||||
fi
|
||||
if [ -z "$tag" ] || [ "$tag" = "null" ]; then
|
||||
tag="$(printf '%s\n' "$release_json" | sed -n 's/.*"tag_name"[[:space:]]*:[[:space:]]*"\([^"]*\)".*/\1/p' | head -n 1)"
|
||||
fi
|
||||
|
||||
if [ -z "$tag" ] || [ "$tag" = "null" ]; then
|
||||
printf 'Error: Could not parse release tag from GitHub API response.\n' >&2
|
||||
printf 'Specify a version explicitly: PI2_VERSION=<version> %s\n' "$0" >&2
|
||||
exit 1
|
||||
fi
|
||||
version="${tag#v}"
|
||||
fi
|
||||
|
||||
base_url="${PI2_BASE_URL:-https://github.com/zgs225/pi2d/releases/download/v${version}}"
|
||||
base_url="${base_url%/}"
|
||||
|
||||
# Fetch SHA256SUMS if checksums are enabled
|
||||
sums_file=""
|
||||
if [ "$PI2_SKIP_CHECKSUM" != "1" ]; then
|
||||
sums_url="${base_url}/SHA256SUMS"
|
||||
sums_file="$tmp_dir/SHA256SUMS"
|
||||
printf 'Downloading SHA256SUMS from %s...\n' "$sums_url"
|
||||
if ! download_file "$sums_url" "$sums_file"; then
|
||||
printf 'Error: Failed to download SHA256SUMS from %s\n' "$sums_url" >&2
|
||||
printf 'To bypass checksum verification, set PI2_SKIP_CHECKSUM=1\n' >&2
|
||||
exit 1
|
||||
fi
|
||||
fi
|
||||
|
||||
# Determine target architecture/libc
|
||||
if [ "$os" = "darwin" ]; then
|
||||
target="darwin-${arch}"
|
||||
archive_name="pi2d-${version}-${target}.tar.gz"
|
||||
elif [ "$os" = "linux" ]; then
|
||||
if [ "$is_musl" -eq 1 ]; then
|
||||
musl_target="linux-${arch}-musl"
|
||||
musl_archive="pi2d-${version}-${musl_target}.tar.gz"
|
||||
glibc_target="linux-${arch}"
|
||||
glibc_archive="pi2d-${version}-${glibc_target}.tar.gz"
|
||||
|
||||
if [ -n "$sums_file" ] && [ -f "$sums_file" ]; then
|
||||
if grep -F "$musl_archive" "$sums_file" >/dev/null 2>&1; then
|
||||
target="$musl_target"
|
||||
archive_name="$musl_archive"
|
||||
elif grep -F "$glibc_archive" "$sums_file" >/dev/null 2>&1; then
|
||||
printf 'info: musl asset not found in SHA256SUMS, falling back to glibc (%s)\n' "$glibc_target"
|
||||
target="$glibc_target"
|
||||
archive_name="$glibc_archive"
|
||||
else
|
||||
printf 'Error: Neither %s nor %s found in release SHA256SUMS\n' "$musl_archive" "$glibc_archive" >&2
|
||||
print_supported_platforms
|
||||
exit 1
|
||||
fi
|
||||
else
|
||||
target="$musl_target"
|
||||
archive_name="$musl_archive"
|
||||
fi
|
||||
else
|
||||
target="linux-${arch}"
|
||||
archive_name="pi2d-${version}-${target}.tar.gz"
|
||||
fi
|
||||
fi
|
||||
|
||||
archive_url="${base_url}/${archive_name}"
|
||||
archive_path="$tmp_dir/${archive_name}"
|
||||
printf 'Downloading %s...\n' "$archive_url"
|
||||
|
||||
if ! download_file "$archive_url" "$archive_path"; then
|
||||
if [ "$os" = "linux" ] && [ "$target" = "linux-${arch}-musl" ]; then
|
||||
printf 'info: musl archive download failed, falling back to glibc asset\n'
|
||||
target="linux-${arch}"
|
||||
archive_name="pi2d-${version}-${target}.tar.gz"
|
||||
archive_url="${base_url}/${archive_name}"
|
||||
archive_path="$tmp_dir/${archive_name}"
|
||||
if ! download_file "$archive_url" "$archive_path"; then
|
||||
printf 'Error: Failed to download release archive %s from %s\n' "$archive_name" "$base_url" >&2
|
||||
print_supported_platforms
|
||||
exit 1
|
||||
fi
|
||||
else
|
||||
printf 'Error: Failed to download release archive %s from %s\n' "$archive_name" "$base_url" >&2
|
||||
print_supported_platforms
|
||||
exit 1
|
||||
fi
|
||||
fi
|
||||
|
||||
if [ "$PI2_SKIP_CHECKSUM" = "1" ]; then
|
||||
printf 'info: skipping checksum verification (PI2_SKIP_CHECKSUM=1)\n'
|
||||
else
|
||||
verify_checksum "$archive_path" "$sums_file" 1
|
||||
fi
|
||||
fi
|
||||
|
||||
# -----------------------------------------------------------------------------
|
||||
# Unpack & Atomic Install
|
||||
# -----------------------------------------------------------------------------
|
||||
|
||||
printf 'Installing pi2 to %s...\n' "$PI2_INSTALL_DIR"
|
||||
mkdir -p "$PI2_INSTALL_DIR"
|
||||
staging="$PI2_INSTALL_DIR/.staging.$$"
|
||||
rm -rf "$staging"
|
||||
mkdir -p "$staging"
|
||||
|
||||
if ! tar -xzf "$archive_path" -C "$staging"; then
|
||||
rm -rf "$staging"
|
||||
staging=""
|
||||
printf 'Error: Failed to extract archive %s\n' "$archive_path" >&2
|
||||
exit 1
|
||||
fi
|
||||
|
||||
if [ ! -f "$staging/bin/pi2" ]; then
|
||||
rm -rf "$staging"
|
||||
staging=""
|
||||
printf 'Error: Archive does not contain bin/pi2\n' >&2
|
||||
exit 1
|
||||
fi
|
||||
|
||||
chmod +x "$staging/bin/pi2"
|
||||
if [ ! -x "$staging/bin/pi2" ]; then
|
||||
rm -rf "$staging"
|
||||
staging=""
|
||||
printf 'Error: %s/bin/pi2 is not executable\n' "$staging" >&2
|
||||
exit 1
|
||||
fi
|
||||
|
||||
# Atomic swap: move existing directories aside before moving staged directories
|
||||
rm -rf "$PI2_INSTALL_DIR/bin.old.$$" "$PI2_INSTALL_DIR/share.old.$$" "$PI2_INSTALL_DIR/LICENSE.old.$$"
|
||||
[ -d "$PI2_INSTALL_DIR/bin" ] && mv "$PI2_INSTALL_DIR/bin" "$PI2_INSTALL_DIR/bin.old.$$"
|
||||
[ -d "$PI2_INSTALL_DIR/share" ] && mv "$PI2_INSTALL_DIR/share" "$PI2_INSTALL_DIR/share.old.$$"
|
||||
[ -f "$PI2_INSTALL_DIR/LICENSE" ] && mv "$PI2_INSTALL_DIR/LICENSE" "$PI2_INSTALL_DIR/LICENSE.old.$$"
|
||||
|
||||
mv "$staging/bin" "$PI2_INSTALL_DIR/bin"
|
||||
if [ -d "$staging/share" ]; then
|
||||
mv "$staging/share" "$PI2_INSTALL_DIR/share"
|
||||
fi
|
||||
if [ -f "$staging/LICENSE" ]; then
|
||||
mv "$staging/LICENSE" "$PI2_INSTALL_DIR/LICENSE"
|
||||
fi
|
||||
|
||||
rm -rf "$PI2_INSTALL_DIR/bin.old.$$" "$PI2_INSTALL_DIR/share.old.$$" "$PI2_INSTALL_DIR/LICENSE.old.$$" "$staging"
|
||||
staging=""
|
||||
|
||||
# Symlink executable into PI2_BIN_DIR
|
||||
mkdir -p "$PI2_BIN_DIR"
|
||||
symlink="$PI2_BIN_DIR/pi2"
|
||||
binary_target="$PI2_INSTALL_DIR/bin/pi2"
|
||||
|
||||
rm -f "$symlink"
|
||||
ln -s "$binary_target" "$symlink"
|
||||
|
||||
# -----------------------------------------------------------------------------
|
||||
# Final Output & Next Steps
|
||||
# -----------------------------------------------------------------------------
|
||||
|
||||
printf '\npi2 %s (%s) installed successfully!\n\n' "$version" "$target"
|
||||
printf 'Install location:\n'
|
||||
printf ' - Directory: %s\n' "$PI2_INSTALL_DIR"
|
||||
printf ' - Executable: %s -> %s\n\n' "$symlink" "$binary_target"
|
||||
|
||||
printf 'Next steps:\n'
|
||||
printf ' - Run in foreground:\n'
|
||||
printf ' pi2 daemon\n'
|
||||
printf ' - Run as background service (systemd user unit or macOS LaunchAgent):\n'
|
||||
printf ' pi2 install-service --write\n'
|
||||
|
||||
in_path=0
|
||||
case ":$PATH:" in
|
||||
*":$PI2_BIN_DIR:"*) in_path=1 ;;
|
||||
esac
|
||||
|
||||
if [ "$in_path" -eq 0 ]; then
|
||||
printf '\nWarning: %s is not in your PATH.\n' "$PI2_BIN_DIR"
|
||||
printf 'To run pi2 directly, add it to your shell configuration (e.g. ~/.bashrc or ~/.zshrc):\n'
|
||||
printf " export PATH=\"%s:\$PATH\"\n" "$PI2_BIN_DIR"
|
||||
fi
|
||||
Executable
+303
@@ -0,0 +1,303 @@
|
||||
#!/usr/bin/env bash
|
||||
#
|
||||
# Smoke test for the compiled standalone binary distribution of pi2d.
|
||||
#
|
||||
# Asserts the dist-bin/bin/pi2 binary against:
|
||||
# 1. Daemon startup: writes exactly ONE stdout line matching 'listening on http://'
|
||||
# 2. GET /api/v1/info: 200, v == 1, version == package.json version
|
||||
# 3. GET /: 200 and HTML body (served from share/pi2d/web/dist)
|
||||
# 4. GET /api/v1/health: static item ok
|
||||
# 5. pi2 doctor: node/runtime check ok, no FAIL for node, exit code 0 or 1
|
||||
# 6. pi2 install-service --dry-run: ExecStart first word is binary path, no 'pi2.js' or 'node '
|
||||
# 7. SIGTERM: clean exit 0, daemon.json and daemon.lock removed
|
||||
#
|
||||
# Usage:
|
||||
# bash scripts/smoke-binary.sh
|
||||
set -euo pipefail
|
||||
|
||||
ROOT="$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd)"
|
||||
cd "$ROOT"
|
||||
|
||||
TMP="$(mktemp -d "${TMPDIR:-/tmp}/pi2-smoke-binary.XXXXXX")"
|
||||
PI2_RUNTIME_DIR="$TMP/run"
|
||||
PI2_DATA_DIR="$TMP/data"
|
||||
PI2_STATE_DIR="$TMP/state"
|
||||
export PI2_RUNTIME_DIR PI2_DATA_DIR PI2_STATE_DIR
|
||||
export PI2_PORT=0
|
||||
# Hermetic: bind loopback even though the daemon defaults to 0.0.0.0 (the
|
||||
# unit/env tests cover the wildcard default); never inherit an ambient PI2_HOST.
|
||||
export PI2_HOST="${PI2_HOST:-127.0.0.1}"
|
||||
export PI2_LOG_LEVEL="${PI2_LOG_LEVEL:-warn}"
|
||||
|
||||
LOG="$TMP/pi2-daemon.log"
|
||||
DAEMON_JSON="$PI2_RUNTIME_DIR/daemon.json"
|
||||
LOCK="$PI2_RUNTIME_DIR/daemon.lock"
|
||||
BODY="$TMP/body"
|
||||
DAEMON_PID=""
|
||||
FAILED=0
|
||||
|
||||
cleanup() {
|
||||
if [[ -n "$DAEMON_PID" ]] && kill -0 "$DAEMON_PID" 2>/dev/null; then
|
||||
kill -TERM "$DAEMON_PID" 2>/dev/null || true
|
||||
for _ in $(seq 1 50); do
|
||||
kill -0 "$DAEMON_PID" 2>/dev/null || break
|
||||
sleep 0.1
|
||||
done
|
||||
kill -KILL "$DAEMON_PID" 2>/dev/null || true
|
||||
fi
|
||||
[[ "${PI2_SMOKE_KEEP:-0}" == "1" ]] || rm -rf "$TMP"
|
||||
}
|
||||
trap cleanup EXIT
|
||||
|
||||
pass() { printf 'PASS %s\n' "$*"; }
|
||||
fail() {
|
||||
printf 'FAIL %s\n' "$*" >&2
|
||||
FAILED=$((FAILED + 1))
|
||||
}
|
||||
|
||||
require() {
|
||||
command -v "$1" >/dev/null 2>&1 || {
|
||||
printf 'FAIL missing required command: %s\n' "$1" >&2
|
||||
exit 1
|
||||
}
|
||||
}
|
||||
|
||||
require curl
|
||||
require node
|
||||
|
||||
# request <method> <path> [curl args...] -> writes body to $BODY, echoes status
|
||||
request() {
|
||||
local method="$1"
|
||||
local path="$2"
|
||||
shift 2
|
||||
curl -sS --max-time 5 -o "$BODY" -w '%{http_code}' -X "$method" "$@" "$BASE_URL$path" 2>/dev/null || echo "000"
|
||||
}
|
||||
|
||||
expect_status() {
|
||||
local expected="$1"
|
||||
local method="$2"
|
||||
local path="$3"
|
||||
shift 3
|
||||
local actual
|
||||
actual="$(request "$method" "$path" "$@")"
|
||||
if [[ "$actual" != "$expected" ]]; then
|
||||
fail "expected HTTP $expected for $method $path, got $actual ($(head -c 200 "$BODY" 2>/dev/null || true))"
|
||||
return 1
|
||||
fi
|
||||
return 0
|
||||
}
|
||||
|
||||
expect_json() {
|
||||
local expression="$1"
|
||||
local what="$2"
|
||||
local file="${3:-$BODY}"
|
||||
if ! node --input-type=module -e "
|
||||
import { readFileSync } from 'node:fs';
|
||||
const body = JSON.parse(readFileSync(process.argv[1], 'utf8'));
|
||||
if (!($expression)) {
|
||||
process.exit(1);
|
||||
}
|
||||
" "$file" 2>/dev/null; then
|
||||
fail "$what"
|
||||
return 1
|
||||
fi
|
||||
return 0
|
||||
}
|
||||
|
||||
BINARY="$ROOT/dist-bin/bin/pi2"
|
||||
|
||||
if [[ "${PI2_SKIP_BUILD:-0}" == "1" && -x "$BINARY" ]]; then
|
||||
printf '==> reusing existing binary at %s (PI2_SKIP_BUILD=1)\n' "$BINARY"
|
||||
else
|
||||
printf '==> building binary with scripts/build-binary.sh\n'
|
||||
bash scripts/build-binary.sh --no-archive >"$TMP/build.log" 2>&1 || {
|
||||
cat "$TMP/build.log" >&2
|
||||
printf 'FAIL scripts/build-binary.sh failed\n' >&2
|
||||
exit 1
|
||||
}
|
||||
fi
|
||||
|
||||
[[ -x "$BINARY" ]] || {
|
||||
printf 'FAIL compiled binary missing or not executable at %s\n' "$BINARY" >&2
|
||||
exit 1
|
||||
}
|
||||
|
||||
PKG_VERSION="$(node -e "console.log(JSON.parse(require('fs').readFileSync('package.json', 'utf8')).version)")"
|
||||
|
||||
# --- Assertion 1: bin/pi2 daemon --port 0 writes exactly ONE stdout line matching 'listening on http://' ---
|
||||
printf '==> starting pi2 daemon via compiled binary (port 0)\n'
|
||||
"$BINARY" daemon --port 0 >"$TMP/stdout.log" 2>"$LOG" &
|
||||
DAEMON_PID=$!
|
||||
|
||||
for _ in $(seq 1 200); do
|
||||
[[ -f "$DAEMON_JSON" ]] && break
|
||||
kill -0 "$DAEMON_PID" 2>/dev/null || break
|
||||
sleep 0.05
|
||||
done
|
||||
|
||||
if [[ ! -f "$DAEMON_JSON" ]]; then
|
||||
fail "daemon.json was not written by compiled binary"
|
||||
BASE_URL=""
|
||||
else
|
||||
PORT="$(node --input-type=module -e "
|
||||
import { readFileSync } from 'node:fs';
|
||||
const info = JSON.parse(readFileSync(process.argv[1], 'utf8'));
|
||||
process.stdout.write(String(info.port));
|
||||
" "$DAEMON_JSON")"
|
||||
BASE_URL="http://127.0.0.1:$PORT"
|
||||
|
||||
MATCH_COUNT="$(grep -c 'listening on http://' "$TMP/stdout.log" || true)"
|
||||
if [[ "$MATCH_COUNT" -eq 1 ]]; then
|
||||
pass "1. daemon --port 0 wrote exactly ONE line matching 'listening on http://'"
|
||||
else
|
||||
fail "1. daemon --port 0 expected exactly ONE matching stdout line, got $MATCH_COUNT ($(cat "$TMP/stdout.log"))"
|
||||
fi
|
||||
fi
|
||||
|
||||
# --- Assertion 2: GET /api/v1/info -> 200, v == 1, version == package.json version ---
|
||||
if [[ -n "$BASE_URL" ]]; then
|
||||
if expect_status 200 GET /api/v1/info; then
|
||||
if expect_json "body.v === 1 && body.version === '$PKG_VERSION'" "2. GET /api/v1/info reports v=1 and version=$PKG_VERSION"; then
|
||||
pass "2. GET /api/v1/info -> 200, v=1, version=$PKG_VERSION"
|
||||
fi
|
||||
else
|
||||
fail "2. GET /api/v1/info did not return 200"
|
||||
fi
|
||||
else
|
||||
fail "2. GET /api/v1/info skipped (daemon not running)"
|
||||
fi
|
||||
|
||||
# --- Assertion 3: GET / -> 200 and an HTML body (served from share/pi2d/web/dist) ---
|
||||
if [[ -n "$BASE_URL" ]]; then
|
||||
if expect_status 200 GET /; then
|
||||
if ! grep -qi 'text/html' <(curl -sS -D - -o /dev/null --max-time 5 "$BASE_URL/"); then
|
||||
fail "3. GET / did not return text/html"
|
||||
elif ! grep -q 'id="root"\|id=app' "$BODY"; then
|
||||
fail "3. GET / body is not the web UI shell"
|
||||
elif ! grep -q '<title>' "$BODY"; then
|
||||
fail "3. GET / body has no <title>"
|
||||
else
|
||||
pass "3. GET / -> 200 html from share/pi2d/web/dist"
|
||||
fi
|
||||
else
|
||||
fail "3. GET / returned non-200"
|
||||
fi
|
||||
else
|
||||
fail "3. GET / skipped (daemon not running)"
|
||||
fi
|
||||
|
||||
# --- Assertion 4: GET /api/v1/health -> static item ok ---
|
||||
if [[ -n "$BASE_URL" ]]; then
|
||||
if expect_status 200 GET /api/v1/health; then
|
||||
if expect_json "Array.isArray(body.items) && body.items.some(item => item.name === 'static' && item.ok === true)" "4. GET /api/v1/health static item ok"; then
|
||||
pass "4. GET /api/v1/health -> static item ok"
|
||||
fi
|
||||
else
|
||||
fail "4. GET /api/v1/health did not return 200"
|
||||
fi
|
||||
else
|
||||
fail "4. GET /api/v1/health skipped (daemon not running)"
|
||||
fi
|
||||
|
||||
# --- Assertion 5: bin/pi2 doctor reports node/runtime check as ok and not failure ---
|
||||
set +e
|
||||
"$BINARY" doctor >"$TMP/doctor.log" 2>&1
|
||||
DOCTOR_EXIT=$?
|
||||
set -e
|
||||
|
||||
DOCTOR_OK=1
|
||||
if [[ "$DOCTOR_EXIT" -ne 0 && "$DOCTOR_EXIT" -ne 1 ]]; then
|
||||
fail "5. pi2 doctor exited with unexpected code $DOCTOR_EXIT (expected 0 or 1)"
|
||||
DOCTOR_OK=0
|
||||
fi
|
||||
if grep -E '^[[:space:]]*FAIL[[:space:]]+node\b' "$TMP/doctor.log" >/dev/null 2>&1; then
|
||||
fail "5. pi2 doctor reported FAIL line for node"
|
||||
DOCTOR_OK=0
|
||||
fi
|
||||
if ! grep -E '^[[:space:]]*ok[[:space:]]+(node|runtime)\b' "$TMP/doctor.log" >/dev/null 2>&1; then
|
||||
fail "5. pi2 doctor did not report node/runtime check as ok"
|
||||
DOCTOR_OK=0
|
||||
fi
|
||||
if [[ "$DOCTOR_OK" -eq 1 ]]; then
|
||||
pass "5. pi2 doctor reports node/runtime check as ok (exit code $DOCTOR_EXIT)"
|
||||
fi
|
||||
|
||||
# --- Assertion 6: bin/pi2 install-service --dry-run renders clean ExecStart ---
|
||||
set +e
|
||||
"$BINARY" install-service --dry-run >"$TMP/service.log" 2>&1
|
||||
SERVICE_EXIT=$?
|
||||
set -e
|
||||
|
||||
SERVICE_OK=1
|
||||
if [[ "$SERVICE_EXIT" -ne 0 ]]; then
|
||||
fail "6. pi2 install-service --dry-run failed with exit code $SERVICE_EXIT"
|
||||
SERVICE_OK=0
|
||||
fi
|
||||
|
||||
EXEC_START="$(grep '^ExecStart=' "$TMP/service.log" || true)"
|
||||
if [[ -z "$EXEC_START" ]]; then
|
||||
fail "6. pi2 install-service --dry-run did not output ExecStart="
|
||||
SERVICE_OK=0
|
||||
else
|
||||
FIRST_WORD="$(echo "${EXEC_START#ExecStart=}" | awk '{print $1}' | tr -d '"')"
|
||||
REAL_BINARY="$(realpath "$BINARY")"
|
||||
if [[ "$FIRST_WORD" != "$REAL_BINARY" && "$FIRST_WORD" != "$BINARY" ]]; then
|
||||
fail "6. ExecStart first word is '$FIRST_WORD', expected '$REAL_BINARY'"
|
||||
SERVICE_OK=0
|
||||
fi
|
||||
if echo "$EXEC_START" | grep -q 'pi2\.js'; then
|
||||
fail "6. ExecStart contains 'pi2.js': $EXEC_START"
|
||||
SERVICE_OK=0
|
||||
fi
|
||||
if echo "$EXEC_START" | grep -q 'node '; then
|
||||
fail "6. ExecStart contains 'node ': $EXEC_START"
|
||||
SERVICE_OK=0
|
||||
fi
|
||||
fi
|
||||
|
||||
if [[ "$SERVICE_OK" -eq 1 ]]; then
|
||||
pass "6. pi2 install-service --dry-run renders clean ExecStart without pi2.js or node"
|
||||
fi
|
||||
|
||||
# --- Assertion 7: SIGTERM -> clean exit 0, daemon.json + daemon.lock removed ---
|
||||
if [[ -n "$DAEMON_PID" ]] && kill -0 "$DAEMON_PID" 2>/dev/null; then
|
||||
printf '==> stopping daemon via SIGTERM\n'
|
||||
kill -TERM "$DAEMON_PID"
|
||||
set +e
|
||||
wait "$DAEMON_PID"
|
||||
DAEMON_EXIT=$?
|
||||
set -e
|
||||
DAEMON_PID=""
|
||||
|
||||
SIGTERM_OK=1
|
||||
if [[ "$DAEMON_EXIT" -ne 0 ]]; then
|
||||
fail "7. daemon exited with code $DAEMON_EXIT on SIGTERM (expected 0)"
|
||||
SIGTERM_OK=0
|
||||
fi
|
||||
if [[ -f "$LOCK" ]]; then
|
||||
fail "7. daemon.lock was not removed on SIGTERM"
|
||||
SIGTERM_OK=0
|
||||
fi
|
||||
if [[ -f "$DAEMON_JSON" ]]; then
|
||||
fail "7. daemon.json was not removed on SIGTERM"
|
||||
SIGTERM_OK=0
|
||||
fi
|
||||
|
||||
if [[ "$SIGTERM_OK" -eq 1 ]]; then
|
||||
pass "7. SIGTERM -> clean exit 0, daemon.json and daemon.lock removed"
|
||||
fi
|
||||
else
|
||||
fail "7. SIGTERM skipped (daemon was not running)"
|
||||
fi
|
||||
|
||||
if [[ "$FAILED" -gt 0 ]]; then
|
||||
printf '\nSMOKE FAILED (%d assertion(s) failed, tmp dir: %s)\n' "$FAILED" "$TMP" >&2
|
||||
if [[ -f "$LOG" ]]; then
|
||||
printf '\n--- pi2 daemon log ---\n' >&2
|
||||
tail -n 40 "$LOG" >&2 || true
|
||||
fi
|
||||
PI2_SMOKE_KEEP=1
|
||||
exit 1
|
||||
fi
|
||||
|
||||
printf '\nSMOKE OK (%s)\n' "$TMP"
|
||||
+6
-4
@@ -18,6 +18,9 @@ PI2_DATA_DIR="$TMP/data"
|
||||
PI2_STATE_DIR="$TMP/state"
|
||||
export PI2_RUNTIME_DIR PI2_DATA_DIR PI2_STATE_DIR
|
||||
export PI2_PORT=0
|
||||
# Hermetic: bind loopback (the daemon defaults to 0.0.0.0, which the unit/env
|
||||
# tests cover); an ambient PI2_HOST from the developer's shell must not leak in.
|
||||
export PI2_HOST="${PI2_HOST:-127.0.0.1}"
|
||||
export PI2_LOG_LEVEL="${PI2_LOG_LEVEL:-warn}"
|
||||
|
||||
LOG="$TMP/pi2-daemon.log"
|
||||
@@ -25,7 +28,6 @@ DAEMON_JSON="$PI2_RUNTIME_DIR/daemon.json"
|
||||
LOCK="$PI2_RUNTIME_DIR/daemon.lock"
|
||||
TOKEN_FILE="$PI2_RUNTIME_DIR/cli.token"
|
||||
BODY="$TMP/body"
|
||||
STARTED=0
|
||||
DAEMON_PID=""
|
||||
|
||||
cleanup() {
|
||||
@@ -133,9 +135,9 @@ pass "daemon.lock holds pid $DAEMON_PID"
|
||||
|
||||
expect_json "body.protocolVersion === 1" "daemon.json protocolVersion === 1" "$DAEMON_JSON"
|
||||
|
||||
grep -q "^pi2 daemon listening on http://127.0.0.1:$PORT$" "$TMP/stdout.log" ||
|
||||
fail "stdout line missing: expected 'pi2 daemon listening on http://127.0.0.1:$PORT' (got: $(cat "$TMP/stdout.log"))"
|
||||
pass "stdout announced http://127.0.0.1:$PORT"
|
||||
grep -q "^pi2 daemon listening on http://$PI2_HOST:$PORT$" "$TMP/stdout.log" ||
|
||||
fail "stdout line missing: expected 'pi2 daemon listening on http://$PI2_HOST:$PORT' (got: $(cat "$TMP/stdout.log"))"
|
||||
pass "stdout announced http://$PI2_HOST:$PORT"
|
||||
|
||||
[[ -f "$TOKEN_FILE" ]] || fail "cli.token missing"
|
||||
TOKEN="$(tr -d '\n' <"$TOKEN_FILE")"
|
||||
|
||||
@@ -337,6 +337,27 @@ async function handleApi(req, res, url) {
|
||||
if (pathname === "/api/v1/workspaces" && method === "GET") {
|
||||
return sendJson(res, 200, FIXTURES.workspaces);
|
||||
}
|
||||
// Disk-projected recent sessions (see docs/protocol.md §3.1). The mock has
|
||||
// no live/disk split, so it projects the fixture rows and lets the browser
|
||||
// merge the `/sessions` relay over them, exactly like the real console.
|
||||
if (pathname === "/api/v1/sessions/recent" && method === "GET") {
|
||||
const raw = Number(url.searchParams.get("limit"));
|
||||
const limit = Number.isFinite(raw) && raw > 0 ? Math.min(200, Math.floor(raw)) : 20;
|
||||
const nameById = new Map(FIXTURES.workspaces.map((workspace) => [workspace.workspaceId, workspace.name]));
|
||||
const rows = FIXTURES.sessions
|
||||
.map((session) => ({
|
||||
sessionId: session.sessionId,
|
||||
workspaceId: session.workspaceId,
|
||||
workspaceName: nameById.get(session.workspaceId) ?? session.workspaceId,
|
||||
cwd: session.cwd ?? "",
|
||||
sessionFile: `/sessions/${session.sessionId}.jsonl`,
|
||||
timestamp: session.createdAt ?? session.lastActiveAt ?? 0,
|
||||
modified: session.lastActiveAt ?? session.modified ?? 0,
|
||||
}))
|
||||
.sort((a, b) => b.modified - a.modified || a.sessionId.localeCompare(b.sessionId))
|
||||
.slice(0, limit);
|
||||
return sendJson(res, 200, { v: 1, sessions: rows });
|
||||
}
|
||||
let match = /^\/api\/v1\/workspaces\/([^/]+)\/sessions$/.exec(pathname);
|
||||
if (match && method === "GET") {
|
||||
const workspaceId = decodeURIComponent(match[1]);
|
||||
|
||||
Executable
+302
@@ -0,0 +1,302 @@
|
||||
#!/usr/bin/env bash
|
||||
set -euo pipefail
|
||||
|
||||
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
|
||||
REPO_ROOT="$(cd "${SCRIPT_DIR}/.." && pwd)"
|
||||
|
||||
VERSION=""
|
||||
CHECKSUMS_INPUT=""
|
||||
OUT_DIR="${REPO_ROOT}/build"
|
||||
NO_DOWNLOAD=0
|
||||
|
||||
KNOWN_TARGETS=(
|
||||
"linux-x64"
|
||||
"linux-arm64"
|
||||
"linux-x64-musl"
|
||||
"linux-arm64-musl"
|
||||
"darwin-x64"
|
||||
"darwin-arm64"
|
||||
)
|
||||
|
||||
usage() {
|
||||
cat <<EOF
|
||||
Usage: $(basename "$0") [options]
|
||||
|
||||
Generates packaging artifacts (Homebrew formula, Arch PKGBUILD, .SRCINFO)
|
||||
for system-packaged channels with deterministic checksum substitution.
|
||||
|
||||
Options:
|
||||
--version <v> Release version (e.g. 0.1.6). Defaults to package.json version.
|
||||
--checksums <path> Path to SHA256SUMS file or directory containing release assets.
|
||||
--out <dir> Output directory (default: build/)
|
||||
--no-download Do not download SHA256SUMS from GitHub; require --checksums.
|
||||
-h, --help Show this help message.
|
||||
EOF
|
||||
}
|
||||
|
||||
# Parse command line flags
|
||||
while [[ $# -gt 0 ]]; do
|
||||
case "$1" in
|
||||
--version)
|
||||
[[ $# -ge 2 ]] || { echo "Error: --version requires an argument" >&2; exit 1; }
|
||||
VERSION="$2"
|
||||
shift 2
|
||||
;;
|
||||
--checksums)
|
||||
[[ $# -ge 2 ]] || { echo "Error: --checksums requires an argument" >&2; exit 1; }
|
||||
CHECKSUMS_INPUT="$2"
|
||||
shift 2
|
||||
;;
|
||||
--out)
|
||||
[[ $# -ge 2 ]] || { echo "Error: --out requires an argument" >&2; exit 1; }
|
||||
OUT_DIR="$2"
|
||||
shift 2
|
||||
;;
|
||||
--no-download)
|
||||
NO_DOWNLOAD=1
|
||||
shift
|
||||
;;
|
||||
-h|--help)
|
||||
usage
|
||||
exit 0
|
||||
;;
|
||||
*)
|
||||
echo "Error: Unknown argument: $1" >&2
|
||||
usage >&2
|
||||
exit 1
|
||||
;;
|
||||
esac
|
||||
done
|
||||
|
||||
# Derive version if not provided
|
||||
if [[ -z "${VERSION}" ]]; then
|
||||
if [[ -f "${REPO_ROOT}/package.json" ]]; then
|
||||
VERSION=$(node -e 'console.log(JSON.parse(require("fs").readFileSync("'"${REPO_ROOT}"'/package.json")).version)' 2>/dev/null || grep -m1 '"version":' "${REPO_ROOT}/package.json" | sed -E 's/.*"version": *"([^"]+)".*/\1/')
|
||||
fi
|
||||
fi
|
||||
|
||||
if [[ -z "${VERSION}" ]]; then
|
||||
echo "Error: Release version not specified and could not be determined from package.json" >&2
|
||||
exit 1
|
||||
fi
|
||||
|
||||
# Normalize version (Arch PKGBUILD forbids leading 'v')
|
||||
VERSION="${VERSION#v}"
|
||||
|
||||
is_known_target() {
|
||||
local t="$1"
|
||||
for k in "${KNOWN_TARGETS[@]}"; do
|
||||
if [[ "$k" == "$t" ]]; then
|
||||
return 0
|
||||
fi
|
||||
done
|
||||
return 1
|
||||
}
|
||||
|
||||
extract_target() {
|
||||
local fname="$1"
|
||||
local base
|
||||
base=$(basename "$fname")
|
||||
local target=""
|
||||
|
||||
if [[ "$base" == pi2d-"${VERSION}"-*.tar.gz ]]; then
|
||||
target="${base#pi2d-"${VERSION}"-}"
|
||||
target="${target%.tar.gz}"
|
||||
elif [[ "$base" =~ ^pi2d-([0-9][^-]*)-(.*)\.tar\.gz$ ]]; then
|
||||
local other_ver="${BASH_REMATCH[1]}"
|
||||
echo "Error: Version mismatch in checksums filename '${base}': expected '${VERSION}', got '${other_ver}'" >&2
|
||||
exit 1
|
||||
elif [[ "$base" == pi2d-*.tar.gz ]]; then
|
||||
target="${base#pi2d-}"
|
||||
target="${target%.tar.gz}"
|
||||
elif [[ "$base" == *.tar.gz ]]; then
|
||||
target="${base%.tar.gz}"
|
||||
else
|
||||
target="$base"
|
||||
fi
|
||||
|
||||
if ! is_known_target "$target"; then
|
||||
echo "Error: Unknown target in checksums: '${target}' (from filename: '${fname}')" >&2
|
||||
exit 1
|
||||
fi
|
||||
|
||||
echo "$target"
|
||||
}
|
||||
|
||||
declare -A CHECKSUMS
|
||||
|
||||
process_checksum_line() {
|
||||
local line="$1"
|
||||
# Skip empty lines or comments
|
||||
[[ -z "${line// }" ]] && return 0
|
||||
[[ "${line}" =~ ^[[:space:]]*# ]] && return 0
|
||||
|
||||
local hash=""
|
||||
local filename=""
|
||||
read -r hash filename <<< "${line}"
|
||||
|
||||
# Strip any leading * from binary mode sha256sum
|
||||
filename="${filename#\*}"
|
||||
# Clean whitespace and carriage returns
|
||||
hash="${hash//[[:space:]]/}"
|
||||
filename="${filename//$'\r'/}"
|
||||
filename="${filename#"${filename%%[![:space:]]*}"}"
|
||||
filename="${filename%"${filename##*[![:space:]]}"}"
|
||||
|
||||
if ! [[ "${hash}" =~ ^[0-9a-fA-F]{64}$ ]]; then
|
||||
echo "Error: Invalid sha256 checksum format: '${hash}' for file '${filename}'" >&2
|
||||
exit 1
|
||||
fi
|
||||
|
||||
local target
|
||||
target="$(extract_target "${filename}")"
|
||||
CHECKSUMS["${target}"]="${hash}"
|
||||
}
|
||||
|
||||
# Collect checksums
|
||||
if [[ -n "${CHECKSUMS_INPUT}" ]]; then
|
||||
if [[ -f "${CHECKSUMS_INPUT}" ]]; then
|
||||
while IFS= read -r line || [[ -n "$line" ]]; do
|
||||
process_checksum_line "$line"
|
||||
done < "${CHECKSUMS_INPUT}"
|
||||
elif [[ -d "${CHECKSUMS_INPUT}" ]]; then
|
||||
if [[ -f "${CHECKSUMS_INPUT}/SHA256SUMS" ]]; then
|
||||
while IFS= read -r line || [[ -n "$line" ]]; do
|
||||
process_checksum_line "$line"
|
||||
done < "${CHECKSUMS_INPUT}/SHA256SUMS"
|
||||
else
|
||||
local_found=0
|
||||
for f in "${CHECKSUMS_INPUT}"/*.tar.gz; do
|
||||
[[ -f "$f" ]] || continue
|
||||
local_found=1
|
||||
hash=$(sha256sum "$f" | awk '{print $1}')
|
||||
target=$(extract_target "$f")
|
||||
CHECKSUMS["${target}"]="${hash}"
|
||||
done
|
||||
if [[ $local_found -eq 0 ]]; then
|
||||
echo "Error: Directory '${CHECKSUMS_INPUT}' contains neither SHA256SUMS nor .tar.gz assets" >&2
|
||||
exit 1
|
||||
fi
|
||||
fi
|
||||
else
|
||||
echo "Error: Checksums path does not exist: ${CHECKSUMS_INPUT}" >&2
|
||||
exit 1
|
||||
fi
|
||||
else
|
||||
if [[ $NO_DOWNLOAD -eq 1 ]]; then
|
||||
echo "Error: --no-download specified but --checksums was not provided." >&2
|
||||
exit 1
|
||||
fi
|
||||
|
||||
SUMS_URL="https://github.com/zgs225/pi2d/releases/download/v${VERSION}/SHA256SUMS"
|
||||
echo "Downloading checksums from ${SUMS_URL}..."
|
||||
TMP_SUMS=$(mktemp)
|
||||
trap 'rm -f "${TMP_SUMS}"' EXIT
|
||||
if ! curl -fsSL "${SUMS_URL}" -o "${TMP_SUMS}"; then
|
||||
echo "Error: Failed to download SHA256SUMS from ${SUMS_URL}" >&2
|
||||
exit 1
|
||||
fi
|
||||
while IFS= read -r line || [[ -n "$line" ]]; do
|
||||
process_checksum_line "$line"
|
||||
done < "${TMP_SUMS}"
|
||||
rm -f "${TMP_SUMS}"
|
||||
trap - EXIT
|
||||
fi
|
||||
|
||||
# Ensure all 6 required targets have checksums
|
||||
for target in "${KNOWN_TARGETS[@]}"; do
|
||||
if [[ -z "${CHECKSUMS[${target}]:-}" ]]; then
|
||||
echo "Error: Missing checksum for required target: ${target}" >&2
|
||||
exit 1
|
||||
fi
|
||||
done
|
||||
|
||||
SHA256_LINUX_X64="${CHECKSUMS[linux-x64]}"
|
||||
SHA256_LINUX_ARM64="${CHECKSUMS[linux-arm64]}"
|
||||
SHA256_LINUX_X64_MUSL="${CHECKSUMS[linux-x64-musl]}"
|
||||
SHA256_LINUX_ARM64_MUSL="${CHECKSUMS[linux-arm64-musl]}"
|
||||
SHA256_DARWIN_X64="${CHECKSUMS[darwin-x64]}"
|
||||
SHA256_DARWIN_ARM64="${CHECKSUMS[darwin-arm64]}"
|
||||
|
||||
BREW_TEMPLATE="${REPO_ROOT}/packaging/homebrew/pi2d.rb.in"
|
||||
AUR_TEMPLATE="${REPO_ROOT}/packaging/aur/PKGBUILD.in"
|
||||
|
||||
if [[ ! -f "${BREW_TEMPLATE}" ]]; then
|
||||
echo "Error: Homebrew template not found at ${BREW_TEMPLATE}" >&2
|
||||
exit 1
|
||||
fi
|
||||
|
||||
if [[ ! -f "${AUR_TEMPLATE}" ]]; then
|
||||
echo "Error: AUR template not found at ${AUR_TEMPLATE}" >&2
|
||||
exit 1
|
||||
fi
|
||||
|
||||
mkdir -p "${OUT_DIR}"
|
||||
|
||||
render_template() {
|
||||
local src="$1"
|
||||
local dst="$2"
|
||||
sed \
|
||||
-e "s|@@VERSION@@|${VERSION}|g" \
|
||||
-e "s|@@SHA256_LINUX_X64@@|${SHA256_LINUX_X64}|g" \
|
||||
-e "s|@@SHA256_LINUX_ARM64@@|${SHA256_LINUX_ARM64}|g" \
|
||||
-e "s|@@SHA256_LINUX_X64_MUSL@@|${SHA256_LINUX_X64_MUSL}|g" \
|
||||
-e "s|@@SHA256_LINUX_ARM64_MUSL@@|${SHA256_LINUX_ARM64_MUSL}|g" \
|
||||
-e "s|@@SHA256_DARWIN_X64@@|${SHA256_DARWIN_X64}|g" \
|
||||
-e "s|@@SHA256_DARWIN_ARM64@@|${SHA256_DARWIN_ARM64}|g" \
|
||||
"${src}" > "${dst}"
|
||||
}
|
||||
|
||||
render_template "${BREW_TEMPLATE}" "${OUT_DIR}/pi2d.rb"
|
||||
render_template "${AUR_TEMPLATE}" "${OUT_DIR}/PKGBUILD"
|
||||
|
||||
TAB=$'\t'
|
||||
if command -v makepkg >/dev/null 2>&1; then
|
||||
( cd "${OUT_DIR}" && makepkg --printsrcinfo ) > "${OUT_DIR}/SRCINFO"
|
||||
else
|
||||
cat <<EOF > "${OUT_DIR}/SRCINFO"
|
||||
pkgbase = pi2d-bin
|
||||
${TAB}pkgdesc = Web console daemon for pi2.nvim
|
||||
${TAB}pkgver = ${VERSION}
|
||||
${TAB}pkgrel = 1
|
||||
${TAB}url = https://github.com/zgs225/pi2d
|
||||
${TAB}arch = x86_64
|
||||
${TAB}arch = aarch64
|
||||
${TAB}license = MIT
|
||||
${TAB}provides = pi2d
|
||||
${TAB}conflicts = pi2d
|
||||
${TAB}options = !strip
|
||||
${TAB}source_x86_64 = https://github.com/zgs225/pi2d/releases/download/v${VERSION}/pi2d-${VERSION}-linux-x64.tar.gz
|
||||
${TAB}sha256sums_x86_64 = ${SHA256_LINUX_X64}
|
||||
${TAB}source_aarch64 = https://github.com/zgs225/pi2d/releases/download/v${VERSION}/pi2d-${VERSION}-linux-arm64.tar.gz
|
||||
${TAB}sha256sums_aarch64 = ${SHA256_LINUX_ARM64}
|
||||
|
||||
pkgname = pi2d-bin
|
||||
EOF
|
||||
fi
|
||||
cp "${OUT_DIR}/SRCINFO" "${OUT_DIR}/.SRCINFO"
|
||||
|
||||
# Verify no placeholders survived
|
||||
for file in "${OUT_DIR}/pi2d.rb" "${OUT_DIR}/PKGBUILD" "${OUT_DIR}/SRCINFO" "${OUT_DIR}/.SRCINFO"; do
|
||||
if grep -q "@@" "${file}"; then
|
||||
echo "Error: Unsubstituted placeholder found in ${file}:" >&2
|
||||
grep -n "@@" "${file}" >&2
|
||||
exit 1
|
||||
fi
|
||||
done
|
||||
|
||||
echo "Packaging update completed successfully for pi2d v${VERSION}."
|
||||
echo ""
|
||||
echo "Target SHA256 checksums used:"
|
||||
echo " darwin-arm64: ${SHA256_DARWIN_ARM64}"
|
||||
echo " darwin-x64: ${SHA256_DARWIN_X64}"
|
||||
echo " linux-arm64: ${SHA256_LINUX_ARM64}"
|
||||
echo " linux-arm64-musl: ${SHA256_LINUX_ARM64_MUSL}"
|
||||
echo " linux-x64: ${SHA256_LINUX_X64}"
|
||||
echo " linux-x64-musl: ${SHA256_LINUX_X64_MUSL}"
|
||||
echo ""
|
||||
echo "Rendered files:"
|
||||
echo " ${OUT_DIR}/pi2d.rb"
|
||||
echo " ${OUT_DIR}/PKGBUILD"
|
||||
echo " ${OUT_DIR}/SRCINFO"
|
||||
echo " ${OUT_DIR}/.SRCINFO"
|
||||
Reference in new issue
Block a user