File size: 3,489 Bytes
fb6a24f | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 | #!/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
|