-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathinstall.sh
More file actions
executable file
·396 lines (348 loc) · 13.4 KB
/
install.sh
File metadata and controls
executable file
·396 lines (348 loc) · 13.4 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
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
#!/bin/bash
set -euo pipefail
# PRDforge — one-command install
# Usage:
# ./install.sh # Interactive (auto-detect client)
# ./install.sh --claude-code # Claude Code (HTTP transport)
# ./install.sh --claude-desktop # Claude Desktop (stdio transport)
# ./install.sh --uninstall # Remove MCP config + optionally stop services
SCRIPT_DIR="$(cd "$(dirname "$0")" && pwd)"
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
CYAN='\033[0;36m'
NC='\033[0m'
info() { echo -e "${CYAN}▸${NC} $1"; }
ok() { echo -e "${GREEN}✓${NC} $1"; }
warn() { echo -e "${YELLOW}!${NC} $1"; }
err() { echo -e "${RED}✗${NC} $1" >&2; }
die() { err "$1"; exit 1; }
port_available() {
local port="$1"
python3 - "$port" <<'PY'
import socket, sys
port = int(sys.argv[1])
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
try:
s.bind(("127.0.0.1", port))
except OSError:
sys.exit(1)
finally:
s.close()
sys.exit(0)
PY
}
find_available_port() {
local start_port="$1"
local end_port="$2"
python3 - "$start_port" "$end_port" <<'PY'
import socket, sys
start_port = int(sys.argv[1])
end_port = int(sys.argv[2])
for port in range(start_port, end_port + 1):
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
try:
s.bind(("127.0.0.1", port))
print(port)
sys.exit(0)
except OSError:
continue
finally:
s.close()
sys.exit(1)
PY
}
CLAUDE_CODE_CONFIG="$HOME/.claude/mcp.json"
CLAUDE_DESKTOP_CONFIG="$HOME/Library/Application Support/Claude/claude_desktop_config.json"
MCP_SERVER_NAME="prd-forge"
GHCR_PREFIX="ghcr.io/tommass/prdforge"
# ─── Argument parsing ────────────────────────────────────────────────
MODE=""
FORCE=false
BUILD_LOCAL=false
for arg in "$@"; do
case "$arg" in
--claude-code) MODE="code" ;;
--claude-desktop) MODE="desktop" ;;
--uninstall) MODE="uninstall" ;;
--force) FORCE=true ;;
--build) BUILD_LOCAL=true ;;
-h|--help)
echo "Usage: ./install.sh [--claude-code|--claude-desktop|--uninstall] [--force] [--build]"
echo ""
echo " --claude-code Configure for Claude Code (HTTP transport)"
echo " --claude-desktop Configure for Claude Desktop (stdio transport)"
echo " --uninstall Remove MCP config + optionally stop services"
echo " --force Overwrite existing config without asking"
echo " --build Build images locally instead of pulling from ghcr.io"
exit 0 ;;
*) die "Unknown option: $arg" ;;
esac
done
# ─── JSON merge helper (uses python3, no jq dependency) ──────────────
json_set_mcp() {
local config_file="$1"
local entry_json="$2"
python3 -c "
import json, sys, os
path = sys.argv[1]
entry = json.loads(sys.argv[2])
data = {}
if os.path.exists(path):
with open(path) as f:
data = json.load(f)
data.setdefault('mcpServers', {}).update(entry)
os.makedirs(os.path.dirname(path), exist_ok=True)
with open(path, 'w') as f:
json.dump(data, f, indent=2)
f.write('\n')
" "$config_file" "$entry_json"
}
json_remove_mcp() {
local config_file="$1"
python3 -c "
import json, sys, os
path = sys.argv[1]
key = sys.argv[2]
if not os.path.exists(path):
sys.exit(0)
with open(path) as f:
data = json.load(f)
servers = data.get('mcpServers', {})
if key in servers:
del servers[key]
with open(path, 'w') as f:
json.dump(data, f, indent=2)
f.write('\n')
print(f'Removed {key}')
else:
print(f'{key} not found in config')
" "$config_file" "$MCP_SERVER_NAME"
}
# ─── Uninstall ────────────────────────────────────────────────────────
if [ "$MODE" = "uninstall" ]; then
info "Removing $MCP_SERVER_NAME from MCP configs..."
for cfg in "$CLAUDE_CODE_CONFIG" "$CLAUDE_DESKTOP_CONFIG"; do
if [ -f "$cfg" ]; then
result=$(json_remove_mcp "$cfg")
ok "$result ($cfg)"
fi
done
echo ""
read -rp "Also stop Docker services and remove data? (y/N) " answer
if [[ "$answer" =~ ^[Yy]$ ]]; then
info "Stopping services..."
docker compose -f "$SCRIPT_DIR/docker-compose.yml" down -v
ok "Services stopped and volumes removed"
fi
echo ""
ok "PRDforge uninstalled"
exit 0
fi
# ─── Prerequisites ───────────────────────────────────────────────────
info "Checking prerequisites..."
command -v docker >/dev/null 2>&1 || die "docker not found. Install Docker Desktop: https://docs.docker.com/desktop/"
docker info >/dev/null 2>&1 || die "Docker daemon not running. Start Docker Desktop first."
command -v python3 >/dev/null 2>&1 || die "python3 not found"
DEFAULT_POSTGRES_PORT=5432
if [ -n "${POSTGRES_PORT:-}" ]; then
if ! port_available "$POSTGRES_PORT"; then
die "POSTGRES_PORT=$POSTGRES_PORT is already in use. Set POSTGRES_PORT to a free port and retry."
fi
else
POSTGRES_PORT="$DEFAULT_POSTGRES_PORT"
if ! port_available "$POSTGRES_PORT"; then
fallback_port=$(find_available_port 5433 5500 || true)
[ -n "${fallback_port:-}" ] || die "Ports 5432-5500 are unavailable. Free a port or set POSTGRES_PORT manually."
warn "Port 5432 is already in use; using POSTGRES_PORT=$fallback_port"
POSTGRES_PORT="$fallback_port"
fi
fi
export POSTGRES_PORT
# Persist to .env so `docker compose up -d` works without install.sh
ENV_FILE="$SCRIPT_DIR/.env"
if [ -f "$ENV_FILE" ]; then
# Remove old POSTGRES_PORT line if present, then append
grep -v '^POSTGRES_PORT=' "$ENV_FILE" > "$ENV_FILE.tmp" || true
mv "$ENV_FILE.tmp" "$ENV_FILE"
fi
echo "POSTGRES_PORT=$POSTGRES_PORT" >> "$ENV_FILE"
ok "Prerequisites OK"
info "Using PostgreSQL host port: $POSTGRES_PORT"
# ─── Auto-detect client if not specified ─────────────────────────────
if [ -z "$MODE" ]; then
echo ""
info "Select Claude client to configure:"
echo " 1) Claude Code (HTTP transport — recommended)"
echo " 2) Claude Desktop (stdio transport)"
echo ""
read -rp "Choice [1]: " choice
case "${choice:-1}" in
1) MODE="code" ;;
2) MODE="desktop" ;;
*) die "Invalid choice" ;;
esac
fi
# ─── Check for existing config ───────────────────────────────────────
if [ "$MODE" = "code" ]; then
CONFIG_FILE="$CLAUDE_CODE_CONFIG"
elif [ "$MODE" = "desktop" ]; then
CONFIG_FILE="$CLAUDE_DESKTOP_CONFIG"
fi
if [ -f "$CONFIG_FILE" ] && [ "$FORCE" = false ]; then
if python3 -c "
import json, sys
with open(sys.argv[1]) as f:
data = json.load(f)
sys.exit(0 if '$MCP_SERVER_NAME' in data.get('mcpServers', {}) else 1)
" "$CONFIG_FILE" 2>/dev/null; then
warn "$MCP_SERVER_NAME already configured in $CONFIG_FILE"
read -rp "Overwrite? (y/N) " answer
if [[ ! "$answer" =~ ^[Yy]$ ]]; then
info "Skipping config. Use --force to overwrite."
fi
fi
fi
# ─── Start Docker services ──────────────────────────────────────────
echo ""
COMPOSE_FILE="$SCRIPT_DIR/docker-compose.yml"
if [ "$BUILD_LOCAL" = true ]; then
info "Building and starting Docker services locally..."
docker compose -f "$COMPOSE_FILE" up -d --build 2>&1 | tail -5
else
# Try pre-built images from ghcr.io, fall back to local build
PROD_COMPOSE="$SCRIPT_DIR/docker-compose.prod.yml"
if docker pull "$GHCR_PREFIX-mcp-server:latest" >/dev/null 2>&1 && \
docker pull "$GHCR_PREFIX-api:latest" >/dev/null 2>&1 && \
docker pull "$GHCR_PREFIX-frontend:latest" >/dev/null 2>&1; then
ok "Pulled pre-built images from ghcr.io"
COMPOSE_FILE="$PROD_COMPOSE"
info "Starting Docker services (pre-built)..."
docker compose -f "$COMPOSE_FILE" up -d 2>&1 | tail -5
else
warn "Pre-built images not available, building locally..."
docker compose -f "$COMPOSE_FILE" up -d --build 2>&1 | tail -5
fi
fi
info "Waiting for services to be healthy..."
attempts=0
max_attempts=30
while [ $attempts -lt $max_attempts ]; do
if docker compose -f "$COMPOSE_FILE" ps --format json 2>/dev/null | python3 -c "
import sys, json
lines = sys.stdin.read().strip().split('\n')
services = [json.loads(l) for l in lines if l.strip()]
healthy = all(s.get('Health','') == 'healthy' or s.get('State','') == 'running' for s in services)
sys.exit(0 if healthy and len(services) >= 5 else 1)
" 2>/dev/null; then
break
fi
sleep 2
attempts=$((attempts + 1))
printf "."
done
echo ""
if [ $attempts -ge $max_attempts ]; then
warn "Services took too long to start. Check: docker compose logs"
else
ok "Docker services running"
fi
# ─── Create admin account (first-time only) ─────────────────────────
FRONTEND_URL="http://localhost:3000"
SETUP_URL="$FRONTEND_URL/api/auth/setup"
# Check if bootstrap was already done (query DB directly, no side-effects)
setup_needed=true
bootstrap_done=$(docker compose -f "$COMPOSE_FILE" exec -T postgres \
psql -U "${POSTGRES_USER:-prdforge}" -d "${POSTGRES_DB:-prdforge}" -tAc \
"SELECT EXISTS(SELECT 1 FROM prdforge_bootstrap WHERE setup_type='first_user' AND completed=true)" 2>/dev/null || echo "f")
if [ "$bootstrap_done" = "t" ]; then
ok "Admin account already exists — skipping"
setup_needed=false
fi
if [ "$setup_needed" = true ]; then
echo ""
info "Create your admin account:"
read -rp " Name: " ADMIN_NAME
read -rp " Email: " ADMIN_EMAIL
while true; do
read -rsp " Password: " ADMIN_PASSWORD
echo ""
read -rsp " Confirm password: " ADMIN_PASSWORD2
echo ""
if [ "$ADMIN_PASSWORD" = "$ADMIN_PASSWORD2" ]; then
break
fi
warn "Passwords don't match. Try again."
done
# Escape JSON values
SETUP_JSON=$(python3 -c "import json,sys; print(json.dumps({'name':sys.argv[1],'email':sys.argv[2],'password':sys.argv[3]}))" "$ADMIN_NAME" "$ADMIN_EMAIL" "$ADMIN_PASSWORD")
response=$(curl -sf -X POST "$SETUP_URL" \
-H "Content-Type: application/json" \
-d "$SETUP_JSON" 2>&1) || true
if echo "$response" | python3 -c "import sys,json; d=json.load(sys.stdin); sys.exit(0 if d.get('success') else 1)" 2>/dev/null; then
ok "Admin account created ($ADMIN_EMAIL)"
else
error_msg=$(echo "$response" | python3 -c "import sys,json; print(json.load(sys.stdin).get('error','unknown error'))" 2>/dev/null || echo "$response")
warn "Could not create admin account: $error_msg"
warn "You can create it manually at $FRONTEND_URL"
fi
fi
# ─── Configure MCP ──────────────────────────────────────────────────
echo ""
if [ "$MODE" = "code" ]; then
info "Configuring Claude Code ($CONFIG_FILE)..."
# Claude Code connects to MCP server via HTTP; the MCP server runs in Docker
# and reaches PostgreSQL via the internal Docker network (postgres:5432),
# so POSTGRES_PORT (host mapping) is not needed here.
MCP_ENTRY="{\"$MCP_SERVER_NAME\": {\"type\": \"http\", \"url\": \"http://localhost:8080/mcp/\"}}"
json_set_mcp "$CONFIG_FILE" "$MCP_ENTRY"
ok "Claude Code MCP config written"
elif [ "$MODE" = "desktop" ]; then
info "Configuring Claude Desktop ($CONFIG_FILE)..."
# Create venv for stdio transport
VENV_DIR="$SCRIPT_DIR/mcp_server/.venv"
if [ ! -d "$VENV_DIR" ]; then
info "Creating Python venv..."
python3 -m venv "$VENV_DIR"
fi
info "Installing MCP server dependencies..."
"$VENV_DIR/bin/pip" install -q -r "$SCRIPT_DIR/mcp_server/requirements.txt"
ok "Python venv ready"
PYTHON_PATH="$VENV_DIR/bin/python"
SERVER_PATH="$SCRIPT_DIR/mcp_server/server.py"
MCP_ENTRY="{\"$MCP_SERVER_NAME\": {\"command\": \"$PYTHON_PATH\", \"args\": [\"$SERVER_PATH\"], \"env\": {\"DATABASE_URL\": \"postgresql://prdforge:prdforge@localhost:${POSTGRES_PORT}/prdforge\"}}}"
json_set_mcp "$CONFIG_FILE" "$MCP_ENTRY"
ok "Claude Desktop MCP config written"
fi
# ─── Validate ────────────────────────────────────────────────────────
echo ""
info "Validating..."
if curl -sf http://localhost:8080/mcp/ -o /dev/null 2>/dev/null; then
ok "MCP server responding (http://localhost:8080/mcp/)"
else
warn "MCP server not responding yet — may still be starting"
fi
if curl -sf http://localhost:8088/health -o /dev/null 2>/dev/null; then
ok "Python API responding (http://localhost:8088)"
else
warn "Python API not responding yet — may still be starting"
fi
if curl -sf http://localhost:3000/ -o /dev/null 2>/dev/null; then
ok "Frontend responding (http://localhost:3000)"
else
warn "Frontend not responding yet — may still be starting"
fi
# ─── Done ────────────────────────────────────────────────────────────
echo ""
echo -e "${GREEN}PRDforge installed successfully!${NC}"
echo ""
echo " Frontend: http://localhost:3000"
echo " Python API: http://localhost:8088"
echo " MCP Server: http://localhost:8080/mcp/"
echo ""
if [ "$MODE" = "code" ]; then
echo " → Restart Claude Code to connect"
elif [ "$MODE" = "desktop" ]; then
echo " → Restart Claude Desktop (Cmd+Q, reopen)"
fi
echo ""