-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsetup.sh
More file actions
executable file
·258 lines (221 loc) · 9.71 KB
/
Copy pathsetup.sh
File metadata and controls
executable file
·258 lines (221 loc) · 9.71 KB
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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
#!/usr/bin/env bash
#
# Photobook — one-time setup.
#
# Creates virtual environments, installs dependencies, provisions the
# database and writes starter configuration. Safe to re-run: every step
# checks for existing state first.
#
# ./setup.sh full setup
# ./setup.sh --no-gpu skip SAM and face model services
# ./setup.sh --check report prerequisites and exit
#
set -euo pipefail
ROOT="$(cd "$(dirname "$0")" && pwd)"
BACKEND="$ROOT/backend"
SERVICES="$ROOT/services"
FRONTEND="$ROOT/frontend"
PG_DB=${PG_DB:-photobook}
PG_USER=${PG_USER:-photobook}
PG_PASSWORD=${PG_PASSWORD:-photobook}
RED=$'\033[0;31m'; GREEN=$'\033[0;32m'; YELLOW=$'\033[1;33m'
BLUE=$'\033[0;34m'; CYAN=$'\033[0;36m'; NC=$'\033[0m'
info() { echo "${BLUE}[info]${NC} $1"; }
ok() { echo "${GREEN}[ ok ]${NC} $1"; }
warn() { echo "${YELLOW}[warn]${NC} $1"; }
fail() { echo "${RED}[fail]${NC} $1"; }
head_() { echo; echo "${CYAN}══ $1${NC}"; }
WITH_GPU=true
CHECK_ONLY=false
for arg in "$@"; do
case "$arg" in
--no-gpu) WITH_GPU=false ;;
--check) CHECK_ONLY=true ;;
-h|--help) sed -n '2,12p' "$0" | sed 's/^# \{0,1\}//'; exit 0 ;;
*) fail "unknown option: $arg"; exit 1 ;;
esac
done
# ── Prerequisites ───────────────────────────────────────────────────────────
missing=0
check_cmd() {
local cmd=$1 what=$2 how=$3
if command -v "$cmd" >/dev/null 2>&1; then
ok "$what — $("$cmd" --version 2>&1 | head -1)"
else
fail "$what not found. Install with: $how"
missing=$((missing + 1))
fi
}
head_ "Prerequisites"
check_cmd python3 "Python 3.11+" "brew install python@3.13 | apt install python3"
check_cmd node "Node 18+" "brew install node | apt install nodejs npm"
check_cmd psql "PostgreSQL 14+" "brew install postgresql@16 | apt install postgresql"
check_cmd redis-server "Redis" "brew install redis | apt install redis-server"
PY_OK=$(python3 -c 'import sys; print(1 if sys.version_info >= (3,11) else 0)' 2>/dev/null || echo 0)
[ "$PY_OK" = "1" ] || { fail "Python 3.11 or newer is required"; missing=$((missing+1)); }
if ! pg_isready -q 2>/dev/null; then
warn "PostgreSQL is installed but not accepting connections"
warn " start it with: brew services start postgresql@16 (or: sudo systemctl start postgresql)"
missing=$((missing + 1))
fi
if [ "$missing" -gt 0 ]; then
echo; fail "$missing prerequisite(s) missing — resolve the above and re-run."
exit 1
fi
ok "all prerequisites satisfied"
$CHECK_ONLY && exit 0
# ── Configuration ───────────────────────────────────────────────────────────
head_ "Configuration"
if [ -f "$BACKEND/.env" ]; then
ok "backend/.env exists (leaving untouched)"
else
cp "$BACKEND/.env.example" "$BACKEND/.env"
SECRET=$(python3 -c "import secrets; print(secrets.token_urlsafe(48))")
# BSD and GNU sed differ on -i; write through a temp file instead.
python3 - "$BACKEND/.env" "$SECRET" "$PG_USER" "$PG_PASSWORD" "$PG_DB" <<'PYEOF'
import sys, pathlib
path, secret, user, password, db = sys.argv[1:6]
p = pathlib.Path(path)
p.write_text(
p.read_text()
.replace("SECRET_KEY=", f"SECRET_KEY={secret}", 1)
.replace(
"postgresql+asyncpg://photobook:photobook@localhost:5432/photobook",
f"postgresql+asyncpg://{user}:{password}@localhost:5432/{db}",
)
)
PYEOF
ok "backend/.env created with a generated SECRET_KEY"
fi
for svc in geo-service sam-service face-service vlm-service; do
if [ -f "$SERVICES/$svc/.env" ]; then
ok "services/$svc/.env exists"
elif [ -f "$SERVICES/$svc/.env.example" ]; then
cp "$SERVICES/$svc/.env.example" "$SERVICES/$svc/.env"
ok "services/$svc/.env created from example"
fi
done
# ── Database ────────────────────────────────────────────────────────────────
head_ "Database"
if psql -lqt 2>/dev/null | cut -d\| -f1 | grep -qw "$PG_DB"; then
ok "database '$PG_DB' exists"
else
info "creating role and database '$PG_DB'"
psql -d postgres -v ON_ERROR_STOP=0 -q <<SQL || true
DO \$\$ BEGIN
IF NOT EXISTS (SELECT FROM pg_roles WHERE rolname = '$PG_USER') THEN
CREATE ROLE $PG_USER LOGIN PASSWORD '$PG_PASSWORD';
END IF;
END \$\$;
SQL
createdb -O "$PG_USER" "$PG_DB" 2>/dev/null || psql -d postgres -qc "CREATE DATABASE $PG_DB OWNER $PG_USER" || true
ok "database created"
fi
# pgvector backs face-similarity search; the migrations assume it exists.
if psql -d "$PG_DB" -tAc "SELECT 1 FROM pg_available_extensions WHERE name='vector'" 2>/dev/null | grep -q 1; then
psql -d "$PG_DB" -qc "CREATE EXTENSION IF NOT EXISTS vector" 2>/dev/null || true
ok "pgvector available"
else
fail "pgvector is not installed — face matching will not work"
fail " install with: brew install pgvector (or: apt install postgresql-16-pgvector)"
exit 1
fi
# ── Python environments ─────────────────────────────────────────────────────
make_venv() {
local dir=$1 label=$2 venv_name=${3:-.venv}
local venv="$dir/$venv_name"
[ -f "$dir/requirements.txt" ] || { warn "$label: no requirements.txt, skipping"; return; }
if [ -d "$venv" ]; then
ok "$label: environment exists"
else
info "$label: creating environment"
python3 -m venv "$venv"
fi
info "$label: installing dependencies (this can take a few minutes)"
"$venv/bin/pip" install --quiet --upgrade pip
"$venv/bin/pip" install --quiet -r "$dir/requirements.txt"
ok "$label: dependencies installed"
}
head_ "Backend"
make_venv "$BACKEND" "backend"
head_ "Services"
make_venv "$SERVICES/geo-service" "geo-service"
make_venv "$SERVICES/vlm-service" "vlm-service"
if $WITH_GPU; then
make_venv "$SERVICES/face-service" "face-service" "venv"
make_venv "$SERVICES/sam-service" "sam-service" "venv"
else
warn "skipping face-service and sam-service (--no-gpu)"
fi
# ── Migrations ──────────────────────────────────────────────────────────────
head_ "Schema"
# Two paths, depending on whether this database already has a schema.
#
# Fresh database: build it from the ORM models with create_all, apply the
# DDL that models cannot express (db/schema_extras.sql), then stamp alembic
# at head so future migrations apply cleanly. The migration chain cannot
# build a schema from nothing — its earliest revisions predate the current
# models — so replaying it here would fail.
#
# Existing database: run the migrations, which is what they are for.
HAS_SCHEMA=$(psql -d "$PG_DB" -tAc \
"SELECT to_regclass('public.kg_nodes') IS NOT NULL" 2>/dev/null || echo f)
if [ "$HAS_SCHEMA" = "t" ]; then
info "existing schema found — applying migrations"
if ( cd "$BACKEND" && ./.venv/bin/alembic upgrade head ); then
ok "schema at latest revision"
else
fail "migrations failed — schema is not usable"
fail " check DATABASE_URL in backend/.env and that PostgreSQL is reachable"
exit 1
fi
else
info "empty database — creating schema from models"
( cd "$BACKEND" && ./.venv/bin/python -c "
import asyncio, sys
sys.path.insert(0, '.')
from db.session import init_db
asyncio.run(init_db())
" ) || { fail "schema creation failed"; exit 1; }
ok "tables created"
psql -d "$PG_DB" -q -v ON_ERROR_STOP=1 -f "$BACKEND/db/schema_extras.sql" \
|| { fail "could not apply indexes and triggers"; exit 1; }
ok "indexes and triggers applied"
( cd "$BACKEND" && ./.venv/bin/alembic stamp head >/dev/null 2>&1 ) \
&& ok "alembic stamped at head"
fi
# ── Frontend ────────────────────────────────────────────────────────────────
head_ "Frontend"
if [ -d "$FRONTEND/node_modules" ]; then
ok "node_modules exists"
else
info "installing frontend dependencies"
( cd "$FRONTEND" && npm install --silent )
ok "frontend dependencies installed"
fi
# ── Models ──────────────────────────────────────────────────────────────────
head_ "Model weights"
if $WITH_GPU; then
if [ -f "$SERVICES/sam-service/sam3.pt" ]; then
ok "SAM3 checkpoint present"
else
warn "SAM3 checkpoint missing — segmentation will be unavailable"
warn " see docs/INSTALL.md § Model weights"
fi
ok "face model (buffalo_l) downloads automatically on first use"
warn "the VLM is NOT bundled — point VLM_SERVICE_URL at your own endpoint"
warn " see docs/INSTALL.md § Vision-language model"
else
warn "model setup skipped (--no-gpu)"
fi
# ── Done ────────────────────────────────────────────────────────────────────
head_ "Setup complete"
cat <<DONE
Next:
./run.sh start everything
backend/.venv/bin/python -m scripts.seed_demo_user
create a login to sign in with
The stack listens on:
frontend http://localhost:5173
backend http://localhost:8020/api/v1/docs
DONE