archive-db / tools /predeploy.sh
tuka1's picture
Add build/deploy tooling under tools/
fb6a24f verified
Raw History Blame Contribute Delete
3.49 kB
#!/usr/bin/env bash
# predeploy.sh β€” gate that runs before every push to the Space.
#
# Catches the classes of bugs we've actually hit in production:
# 1. Syntax / import errors in server.py
# 2. SQLite "attempt to write a readonly database" β€” PRAGMA writes,
# WAL lock file creation, anything that needs a writable mount
# 3. 500s on common endpoints (top_hated + year filter was our worst)
#
# How: chmod the local posts.db to 0444 (true read-only), spin up uvicorn
# on a private test port (8799), run tests/smoke.py, then restore perms.
# trap ensures perms are restored even if the test suite errors out.
#
# Usage:
# ./predeploy.sh # run gates, exit 0 on success
# ./predeploy.sh --skip-perms # skip the chmod step (DB already RO)
#
# deploy.sh push calls this automatically.
set -uo pipefail
cd "$(dirname "$0")"
DB="$(pwd)/posts.db"
PORT=8799
TEST_BASE="http://127.0.0.1:${PORT}"
SERVER_LOG="/tmp/predeploy_server.log"
SERVER_PID=""
ORIG_PERMS=""
red() { printf '\033[31m%s\033[0m\n' "$*"; }
green() { printf '\033[32m%s\033[0m\n' "$*"; }
yellow() { printf '\033[33m%s\033[0m\n' "$*"; }
cleanup() {
local status=$?
if [ -n "$SERVER_PID" ] && kill -0 "$SERVER_PID" 2>/dev/null; then
kill "$SERVER_PID" 2>/dev/null || true
wait "$SERVER_PID" 2>/dev/null || true
fi
if [ -n "$ORIG_PERMS" ] && [ -f "$DB" ]; then
chmod "$ORIG_PERMS" "$DB"
fi
exit $status
}
trap cleanup EXIT INT TERM
# ---- gate 1: syntax / import ----
echo "β–Ά syntax check"
if ! python3 -m py_compile server.py 2>&1; then
red "βœ— server.py syntax check FAILED"
exit 1
fi
green "βœ“ server.py compiles"
echo "β–Ά import check (catches top-level errors)"
if ! (cd .. && python3 -c "import search.server" 2>&1); then
red "βœ— server.py import FAILED"
exit 1
fi
green "βœ“ server.py imports cleanly"
# ---- gate 1b: parser unit tests (fast, no server needed) ----
echo "β–Ά parser unit tests"
if ! python3 tests/test_parsers.py 2>&1 | tail -5; then
red "βœ— parser unit tests FAILED"
exit 1
fi
green "βœ“ parser unit tests pass"
# ---- gate 2: smoke tests against RO DB ----
if [ ! -f "$DB" ]; then
yellow "⚠ posts.db not present, skipping runtime smoke tests"
exit 0
fi
if [ "${1:-}" != "--skip-perms" ]; then
ORIG_PERMS=$(stat -f "%Lp" "$DB" 2>/dev/null || stat -c "%a" "$DB")
echo "β–Ά chmod 0444 posts.db (orig perms: $ORIG_PERMS β€” will restore on exit)"
chmod 0444 "$DB"
fi
echo "β–Ά booting test server on port $PORT"
# Use a fresh process group so cleanup can kill the whole tree if needed.
(cd .. && python3 -m uvicorn search.server:app --host 127.0.0.1 --port "$PORT" --log-level warning) \
> "$SERVER_LOG" 2>&1 &
SERVER_PID=$!
# Wait up to 15s for the server to come up.
for i in $(seq 1 30); do
if curl -s -o /dev/null -m 1 "$TEST_BASE/api/stats"; then
break
fi
if ! kill -0 "$SERVER_PID" 2>/dev/null; then
red "βœ— test server died during startup"
echo "--- server log:"
tail -30 "$SERVER_LOG"
exit 1
fi
sleep 0.5
done
if ! curl -s -o /dev/null -m 2 "$TEST_BASE/api/stats"; then
red "βœ— test server never came up"
echo "--- server log:"
tail -30 "$SERVER_LOG"
exit 1
fi
green "βœ“ test server up"
echo "β–Ά running smoke tests"
if ! TEST_BASE="$TEST_BASE" python3 tests/smoke.py; then
red "βœ— smoke tests FAILED"
echo "--- server log:"
tail -50 "$SERVER_LOG"
exit 1
fi
green "βœ“ all smoke tests passed"
echo
green "PREDEPLOY βœ“ safe to push"
exit 0